From bba6f2f25bd9eacfc9893d0637e87feb40494ca9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E5=A3=B0?= Date: Fri, 21 Jun 2024 13:16:54 +0800 Subject: [PATCH] feat: support create merge commit --- src/constants/opensumi.ts | 7 ++ src/github/commands/pullRequests/index.ts | 89 ++++++++++++----------- src/github/commands/types.ts | 4 +- src/github/service/opensumi.ts | 10 +++ 4 files changed, 67 insertions(+), 43 deletions(-) diff --git a/src/constants/opensumi.ts b/src/constants/opensumi.ts index feda97e7..062ee905 100644 --- a/src/constants/opensumi.ts +++ b/src/constants/opensumi.ts @@ -76,6 +76,13 @@ export namespace ActionsRepo { workflow_id: UPDATE_LOCKFILE_FILE, ref: 'main', }; + + export const CREATE_MERGE_COMMIT_FILE = 'create-merge-commit.yml'; + export const CREATE_MERGE_COMMIT_WORKFLOW = { + ...info, + workflow_id: CREATE_MERGE_COMMIT_FILE, + ref: 'main', + }; } export function getActionsUrl({ diff --git a/src/github/commands/pullRequests/index.ts b/src/github/commands/pullRequests/index.ts index c2f8d5b3..d4b5192b 100644 --- a/src/github/commands/pullRequests/index.ts +++ b/src/github/commands/pullRequests/index.ts @@ -5,7 +5,11 @@ import { } from '@/constants/opensumi'; import { removeCommandPrefix, StopError } from '@opensumi/bot-commander'; -import { GitHubCommandCenter } from '../types'; +import { + CommandContext, + GitHubCommandCenter, + IssueCommentEvent, +} from '../types'; export function extractTargetBranchNameFromCommand(str: string) { const text = removeCommandPrefix(str, kBackportKeyword); @@ -37,34 +41,42 @@ function constraintRepo(fullname: string, allowedRepo: Set) { } } +async function checkIsPullRequestAndUserHasPermission(ctx: CommandContext) { + const { app, payload } = ctx; + const owner = payload.repository.owner.login; + const repo = payload.repository.name; + const { issue } = payload; + + const pull_request = issue.pull_request; + if (!pull_request) { + // only handle pull request + return; + } + + const user = payload.sender.login; + const userHaveWritePerm = await app.octoService.checkRepoWritePermission( + owner, + repo, + user, + ); + + if (!userHaveWritePerm) { + await app.createReactionForIssueComment(ctx, 'confused'); + throw new StopError( + `User ${user} does not have write permission to the repository`, + ); + } +} + export function registerPullRequestCommand(it: GitHubCommandCenter) { // backport to v2.23 it.on( kBackportKeyword, async (ctx, _command) => { const { app, payload } = ctx; - const owner = payload.repository.owner.login; - const repo = payload.repository.name; - constraintRepo(payload.repository.full_name, backportAllowedRepo); - const { issue } = payload; - - const pull_request = issue.pull_request; - if (!pull_request) { - // only handle pull request - return; - } - - const user = payload.sender.login; - const userHaveWritePerm = await app.octoService.checkRepoWritePermission( - owner, - repo, - user, - ); - if (!userHaveWritePerm) { - await app.createReactionForIssueComment(ctx, 'confused'); - return; - } + constraintRepo(payload.repository.full_name, backportAllowedRepo); + await checkIsPullRequestAndUserHasPermission(ctx); const result = await app.octoService.getPrByNumber( payload.repository.owner.login, @@ -171,31 +183,24 @@ Please see: <${getActionsUrl(ActionsRepo.BACKPORT_PR_WORKFLOW)}>`, it.on('update-lock', async (ctx) => { const { app, payload } = ctx; - const owner = payload.repository.owner.login; - const repo = payload.repository.name; + const { issue } = payload; const fullname = payload.repository.full_name; constraintRepo(fullname, updateLockfileAllowedRepo); + await checkIsPullRequestAndUserHasPermission(ctx); - const user = payload.sender.login; - const userHaveWritePerm = await app.octoService.checkRepoWritePermission( - owner, - repo, - user, - ); - - if (!userHaveWritePerm) { - await app.createReactionForIssueComment(ctx, 'confused'); - return; - } - + await app.opensumiOctoService.updateLockfileForPr({ + pull_number: issue.number, + }); + await app.createReactionForIssueComment(ctx, 'rocket'); + }); + it.on('create-merge-commit', async (ctx) => { + const { app, payload } = ctx; const { issue } = payload; - const pull_request = issue.pull_request; - if (!pull_request) { - // only handle pull request - return; - } + const fullname = payload.repository.full_name; + constraintRepo(fullname, updateLockfileAllowedRepo); + await checkIsPullRequestAndUserHasPermission(ctx); - await app.opensumiOctoService.updateLockfileForPr({ + await app.opensumiOctoService.createMergeCommitForPr({ pull_number: issue.number, }); await app.createReactionForIssueComment(ctx, 'rocket'); diff --git a/src/github/commands/types.ts b/src/github/commands/types.ts index a1b8dcf0..8376a816 100644 --- a/src/github/commands/types.ts +++ b/src/github/commands/types.ts @@ -7,9 +7,11 @@ import type { App } from '../app'; export type IssueCommentHandler = () => Promise; +export type IssueCommentEvent = ExtractPayload<'issue_comment'>; + export interface CommandContext { app: App; - payload: ExtractPayload<'issue_comment'>; + payload: IssueCommentEvent; id: string; name: string; octokit: Octokit; diff --git a/src/github/service/opensumi.ts b/src/github/service/opensumi.ts index a935cbc7..0d5b0c5a 100644 --- a/src/github/service/opensumi.ts +++ b/src/github/service/opensumi.ts @@ -179,4 +179,14 @@ export class OpenSumiOctoService extends GitHubService { }); return workflow; } + + async createMergeCommitForPr({ pull_number }: { pull_number: number }) { + const workflow = await this.octo.actions.createWorkflowDispatch({ + ...ActionsRepo.CREATE_MERGE_COMMIT_WORKFLOW, + inputs: { + pull_number: pull_number.toString(), + }, + }); + return workflow; + } }