Skip to content

Commit

Permalink
feat(gitlab,azure): try approving before auto-merge
Browse files Browse the repository at this point in the history
  • Loading branch information
felipecrs committed Dec 1, 2024
1 parent 7877331 commit 56bd600
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 26 deletions.
55 changes: 30 additions & 25 deletions lib/modules/platform/azure/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,18 +513,7 @@ export async function createPr({
);
}
if (platformPrOptions?.autoApprove) {
await azureApiGit.createPullRequestReviewer(
{
reviewerUrl: pr.createdBy!.url,
vote: AzurePrVote.Approved,
isFlagged: false,
isRequired: false,
},
config.repoId,
// TODO #22198
pr.pullRequestId!,
pr.createdBy!.id!,
);
await approvePr(pr);
}
await Promise.all(
labels!.map((label) =>
Expand Down Expand Up @@ -581,19 +570,7 @@ export async function updatePr({
objToUpdate.status = PullRequestStatus.Abandoned;
}
if (platformPrOptions?.autoApprove) {
const pr = await azureApiGit.getPullRequestById(prNo, config.project);
await azureApiGit.createPullRequestReviewer(
{
reviewerUrl: pr.createdBy!.url,
vote: AzurePrVote.Approved,
isFlagged: false,
isRequired: false,
},
config.repoId,
// TODO #22198
pr.pullRequestId!,
pr.createdBy!.id!,
);
await approvePr(prNo);
}

const updatedPr = await azureApiGit.updatePullRequest(
Expand Down Expand Up @@ -1015,3 +992,31 @@ export async function deleteLabel(
const azureApiGit = await azureApi.gitApi();
await azureApiGit.deletePullRequestLabels(config.repoId, prNumber, label);
}

export async function approvePr(
prNumberOrPr: number | GitPullRequest,
): Promise<void> {
const azureApiGit = await azureApi.gitApi();
const pr =
typeof prNumberOrPr === 'number'
? await azureApiGit.getPullRequestById(prNumberOrPr, config.project)

Check failure on line 1002 in lib/modules/platform/azure/index.ts

View workflow job for this annotation

GitHub Actions / test (1/3)

modules/platform/azure/index › updatePr(prNo

TypeError: Cannot read properties of undefined (reading 'getPullRequestById') at getPullRequestById (lib/modules/platform/azure/index.ts:1002:27) at Object.updatePr (lib/modules/platform/azure/index.ts:573:5) at Object.<anonymous> (lib/modules/platform/azure/index.spec.ts:1307:18)
: prNumberOrPr;
if (
pr.reviewers?.some((reviewer) => reviewer.vote === AzurePrVote.Approved)
) {
logger.debug('PR is already approved');
return;
}
await azureApiGit.createPullRequestReviewer(

Check failure on line 1010 in lib/modules/platform/azure/index.ts

View workflow job for this annotation

GitHub Actions / test (1/3)

modules/platform/azure/index › createPr() › should create and return an approved PR object

TypeError: Cannot read properties of undefined (reading 'createPullRequestReviewer') at createPullRequestReviewer (lib/modules/platform/azure/index.ts:1010:21) at Object.createPr (lib/modules/platform/azure/index.ts:516:5) at Object.<anonymous> (lib/modules/platform/azure/index.spec.ts:1160:18)
{
reviewerUrl: pr.createdBy!.url,
vote: AzurePrVote.Approved,
isFlagged: false,
isRequired: false,
},
config.repoId,
// TODO #22198
pr.pullRequestId!,
pr.createdBy!.id!,
);
}
2 changes: 1 addition & 1 deletion lib/modules/platform/gerrit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export async function createPr(prConfig: CreatePRConfig): Promise<Pr | null> {
TAG_PULL_REQUEST_BODY,
);
if (prConfig.platformPrOptions?.autoApprove) {
await client.approveChange(pr._number);
await approvePr(pr._number);
}
return getPr(pr._number);
}
Expand Down
5 changes: 5 additions & 0 deletions lib/modules/platform/gitlab/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export async function getUserID(username: string): Promise<number> {
return userInfo[0].id;
}

export async function getSelfUserID(): Promise<number> {
const userInfo = (await gitlabApi.getJson<{ id: number }>(`user`)).body;
return userInfo.id;
}

async function getMembers(group: string): Promise<GitLabUser[]> {
const groupEncoded = encodeURIComponent(group);
return (
Expand Down
19 changes: 19 additions & 0 deletions lib/modules/platform/gitlab/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import { smartTruncate } from '../utils/pr-body';
import {
getMemberUserIDs,
getMemberUsernames,
getSelfUserID,
getUserID,
gitlabApi,
isUserBusy,
Expand All @@ -65,6 +66,7 @@ import { getMR, updateMR } from './merge-request';
import { LastPipelineId } from './schema';
import type {
GitLabMergeRequest,
GitLabMergeRequestApprovals,
GitlabComment,
GitlabIssue,
GitlabPr,
Expand Down Expand Up @@ -711,6 +713,23 @@ async function tryPrAutomerge(
}

async function approvePr(pr: number): Promise<void> {
try {
const { body: approvals } =
await gitlabApi.getJson<GitLabMergeRequestApprovals>(
`projects/${config.repository}/merge_requests/${pr}/approvals`,
);
const selfUserId = await getSelfUserID();
if (
approvals.approved_by?.some(
(approval) => approval.user?.id === selfUserId,
)
) {
logger.debug('MR already approved');
return;
}
} catch (err) {
logger.warn({ err }, 'GitLab: Error checking if merge request is approved');
}
try {
await gitlabApi.postJson(
`projects/${config.repository}/merge_requests/${pr}/approve`,
Expand Down
4 changes: 4 additions & 0 deletions lib/modules/platform/gitlab/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ export interface GitlabUserStatus {
emoji?: string;
availability: 'not_set' | 'busy';
}

export interface GitLabMergeRequestApprovals {
approved_by?: { user?: GitLabUser }[];
}

0 comments on commit 56bd600

Please sign in to comment.