Skip to content

Commit

Permalink
fix(platform/gitlab): handle assignee id not found (#29307)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastian Poxhofer <secustor@users.noreply.github.com>
  • Loading branch information
RahulGautamSingh and secustor committed Jun 4, 2024
1 parent 23ffe8b commit cb804b0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
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

0 comments on commit cb804b0

Please sign in to comment.