Skip to content

Commit

Permalink
feat: support create merge commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain committed Jun 21, 2024
1 parent 239a7f7 commit bba6f2f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 43 deletions.
7 changes: 7 additions & 0 deletions src/constants/opensumi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
89 changes: 47 additions & 42 deletions src/github/commands/pullRequests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -37,34 +41,42 @@ function constraintRepo(fullname: string, allowedRepo: Set<string>) {
}
}

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,
Expand Down Expand Up @@ -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');
Expand Down
4 changes: 3 additions & 1 deletion src/github/commands/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import type { App } from '../app';

export type IssueCommentHandler = () => Promise<void>;

export type IssueCommentEvent = ExtractPayload<'issue_comment'>;

export interface CommandContext {
app: App;
payload: ExtractPayload<'issue_comment'>;
payload: IssueCommentEvent;
id: string;
name: string;
octokit: Octokit;
Expand Down
10 changes: 10 additions & 0 deletions src/github/service/opensumi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit bba6f2f

Please sign in to comment.