Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(platform/gitlab): handle assignee id not found #29307

Merged
merged 14 commits into from
Jun 4, 2024
13 changes: 11 additions & 2 deletions lib/modules/platform/gitlab/http.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import is from '@sindresorhus/is';
import { logger } from '../../../logger';
import { GitlabHttp } from '../../../util/http/gitlab';
import type { GitLabUser, GitlabUserStatus } from './types';

export const gitlabApi = new GitlabHttp();

export async function getUserID(username: string): Promise<number> {
return (
const userInfo = (
await gitlabApi.getJson<{ id: number }[]>(`users?username=${username}`)
).body[0].id;
).body;

if (is.emptyArray(userInfo)) {
throw new Error(
`User ID for the username: ${username} could not be found.`,
);
}

return userInfo[0].id;
}

async function getMembers(group: string): Promise<GitLabUser[]> {
Expand Down
33 changes: 33 additions & 0 deletions lib/modules/platform/gitlab/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1316,11 +1316,44 @@ describe('modules/platform/gitlab/index', () => {
httpMock
.scope(gitlabApiHost)
.get('/api/v4/users?username=someuser')
.replyWithError('some error')
.get('/api/v4/users?username=someotheruser')
.reply(200, [{ id: 124 }])
.put('/api/v4/projects/undefined/merge_requests/42?assignee_ids[]=124')
.replyWithError('some error');
await expect(
gitlab.addAssignees(42, ['someuser', 'someotheruser']),
).toResolve();
});

it('should log message for each assignee that could not be found', async () => {
httpMock
.scope(gitlabApiHost)
.get('/api/v4/users?username=someuser')
.reply(304, [])
.get('/api/v4/users?username=someotheruser')
.reply(200, [{ id: 124 }])
.put('/api/v4/projects/undefined/merge_requests/42?assignee_ids[]=124')
.reply(200);
await expect(
gitlab.addAssignees(42, ['someuser', 'someotheruser']),
).toResolve();
expect(logger.warn).toHaveBeenCalledWith(
{
assignee: 'someuser',
},
'Failed to add assignee - could not get ID',
);
expect(logger.debug).toHaveBeenCalledWith(
{
assignee: 'someuser',
err: new Error(
'User ID for the username: someuser could not be found.',
),
},
'getUserID() error',
);
});
});

describe('addReviewers(iid, reviewers)', () => {
Expand Down
8 changes: 7 additions & 1 deletion lib/modules/platform/gitlab/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,13 @@ export async function addAssignees(
logger.debug(`Adding assignees '${assignees.join(', ')}' to #${iid}`);
const assigneeIds: number[] = [];
for (const assignee of assignees) {
assigneeIds.push(await getUserID(assignee));
try {
const userId = await getUserID(assignee);
assigneeIds.push(userId);
} catch (err) {
logger.debug({ assignee, err }, 'getUserID() error');
logger.warn({ assignee }, 'Failed to add assignee - could not get ID');
}
}
const url = `projects/${
config.repository
Expand Down