Skip to content

Commit

Permalink
feat: create merge commit need admin role
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain committed Sep 29, 2024
1 parent b241adb commit 7e57f7c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
10 changes: 1 addition & 9 deletions libs/octo-service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ export class GitHubService {
owner: string,
repo: string,
username: string,
requestLevel: string,
requestLevel: 'write' | 'admin',
) {
// Permission levels higher in the array have higher access to the repo.
const perms = ['none', 'read', 'write', 'admin'];
Expand All @@ -794,12 +794,4 @@ export class GitHubService {
const userPermIndex = perms.indexOf(perm);
return userPermIndex >= requestPermIndex;
}

async checkRepoWritePermission(
owner: string,
repo: string,
username: string,
) {
return this.checkRepoPermission(owner, repo, username, 'write');
}
}
23 changes: 12 additions & 11 deletions src/github/commands/pullRequests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import {
} from '@/constants/opensumi';
import { StopError, removeCommandPrefix } from '@opensumi/bot-commander';

import {
CommandContext,
GitHubCommandCenter,
IssueCommentEvent,
} from '../types';
import { CommandContext, GitHubCommandCenter } from '../types';

export function extractTargetBranchNameFromCommand(str: string) {
const text = removeCommandPrefix(str, kBackportKeyword);
Expand Down Expand Up @@ -41,7 +37,10 @@ function constraintRepo(fullname: string, allowedRepo: Set<string>) {
}
}

async function checkIsPullRequestAndUserHasPermission(ctx: CommandContext) {
async function checkIsPullRequestAndUserHasPermission(
ctx: CommandContext,
requestLevel: 'write' | 'admin',
) {
const { app, payload } = ctx;
const owner = payload.repository.owner.login;
const repo = payload.repository.name;
Expand All @@ -54,10 +53,11 @@ async function checkIsPullRequestAndUserHasPermission(ctx: CommandContext) {
}

const user = payload.sender.login;
const userHaveWritePerm = await app.octoService.checkRepoWritePermission(
const userHaveWritePerm = await app.octoService.checkRepoPermission(
owner,
repo,
user,
requestLevel,
);

if (!userHaveWritePerm) {
Expand All @@ -76,7 +76,7 @@ export function registerPullRequestCommand(it: GitHubCommandCenter) {
const { app, payload } = ctx;
const { issue } = payload;
constraintRepo(payload.repository.full_name, backportAllowedRepo);
await checkIsPullRequestAndUserHasPermission(ctx);
await checkIsPullRequestAndUserHasPermission(ctx, 'write');

const result = await app.octoService.getPrByNumber(
payload.repository.owner.login,
Expand Down Expand Up @@ -135,10 +135,11 @@ Please see: <${getActionsUrl(ActionsRepo.BACKPORT_PR_WORKFLOW)}>`,
constraintRepo(fullname, nextAllowedRepo);

const user = payload.sender.login;
let userHaveWritePerm = await app.octoService.checkRepoWritePermission(
let userHaveWritePerm = await app.octoService.checkRepoPermission(
owner,
repo,
user,
'write',
);
if (!userHaveWritePerm) {
if (
Expand Down Expand Up @@ -186,7 +187,7 @@ Please see: <${getActionsUrl(ActionsRepo.BACKPORT_PR_WORKFLOW)}>`,
const { issue } = payload;
const fullname = payload.repository.full_name;
constraintRepo(fullname, updateLockfileAllowedRepo);
await checkIsPullRequestAndUserHasPermission(ctx);
await checkIsPullRequestAndUserHasPermission(ctx, 'write');

await app.opensumiOctoService.updateLockfileForPr({
pull_number: issue.number,
Expand All @@ -198,7 +199,7 @@ Please see: <${getActionsUrl(ActionsRepo.BACKPORT_PR_WORKFLOW)}>`,
const { issue } = payload;
const fullname = payload.repository.full_name;
constraintRepo(fullname, updateLockfileAllowedRepo);
await checkIsPullRequestAndUserHasPermission(ctx);
await checkIsPullRequestAndUserHasPermission(ctx, 'admin');

await app.opensumiOctoService.createMergeCommitForPr({
pull_number: issue.number,
Expand Down

0 comments on commit 7e57f7c

Please sign in to comment.