Skip to content

Commit

Permalink
refactor: unify octo service
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain committed Sep 30, 2024
1 parent 4949478 commit fd8883b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 37 deletions.
32 changes: 11 additions & 21 deletions src/github/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { PrivilegeEvent } from '@/constants';
import { ActionsRepo, VERSION_SYNC_KEYWORD } from '@/constants/opensumi';
import Environment from '@/env';
import { AppSetting } from '@/kv/types';
import { GitHubService } from '@opensumi/octo-service';

import { CommandContext, issueCc } from './commands';
import { parseCommandInMarkdownComments } from './commands/parse';
Expand All @@ -15,16 +14,13 @@ import { setupWebhooksTemplate } from './handler';
import { OpenSumiOctoService } from './service/opensumi';

export class App {
ctx: {
setting: AppSetting;
};
octoService: GitHubService;
opensumiOctoService: OpenSumiOctoService;
octoService: OpenSumiOctoService;

octoApp: OctoApp<{ Octokit: typeof Octokit }>;

constructor(setting: AppSetting) {
constructor(public setting: AppSetting) {
const { appSettings, githubSecret } = setting;
this.octoService = new OpenSumiOctoService();

this.octoApp = new OctoApp({
appId: appSettings.appId,
Expand All @@ -34,12 +30,6 @@ export class App {
},
Octokit: Octokit,
});
this.ctx = {
setting,
};

this.octoService = new GitHubService();
this.opensumiOctoService = new OpenSumiOctoService();

this.octoApp.webhooks.on(
'issue_comment.created',
Expand All @@ -60,12 +50,12 @@ export class App {
case 'opensumi/core':
file = ActionsRepo.SYNC_FILE;
name = 'opensumi';
await this.opensumiOctoService.syncOpenSumiVersion(version);
await this.octoService.syncOpenSumiVersion(version);
break;
case 'opensumi/codeblitz':
file = ActionsRepo.SYNC_CODEBLITZ_FILE;
name = 'codeblitz';
await this.opensumiOctoService.syncCodeblitzVersion(version);
await this.octoService.syncCodeblitzVersion(version);
break;
default:
throw new Error('unknown repo');
Expand All @@ -77,7 +67,7 @@ export class App {
text: `[${fullname}] ${tag} has published. [starts synchronizing ${name} packages@${version} to npmmirror](https://github.com/opensumi/actions/actions/workflows/${file})`,
},
PrivilegeEvent,
this.ctx.setting,
this.setting,
);
};

Expand Down Expand Up @@ -179,17 +169,17 @@ export class App {
}

async init() {
const octo = await this.getOcto();
this.octoService.octo = octo;
this.opensumiOctoService.octo = octo;
this.octoService.octo = await this.getOcto();
}

listenWebhooks() {
setupWebhooksTemplate(
this.octoApp.webhooks,
this.ctx,
{
setting: this.setting,
},
async ({ markdown, eventName }) => {
await sendToDing(markdown, eventName, this.ctx.setting);
await sendToDing(markdown, eventName, this.setting);
},
);
}
Expand Down
12 changes: 6 additions & 6 deletions src/github/commands/pullRequests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export function registerPullRequestCommand(it: GitHubCommandCenter) {
return;
}

await app.opensumiOctoService.backportPr({
await app.octoService.backportPr({
pull_number: issue.number,
target_branch: targetBranch,
});
Expand Down Expand Up @@ -143,8 +143,8 @@ Please see: <${getActionsUrl(ActionsRepo.BACKPORT_PR_WORKFLOW)}>`,
);
if (!userHaveWritePerm) {
if (
app.ctx.setting.userWhoCanRelease &&
app.ctx.setting.userWhoCanRelease.includes(user)
app.setting.userWhoCanRelease &&
app.setting.userWhoCanRelease.includes(user)
) {
userHaveWritePerm = true;
}
Expand Down Expand Up @@ -175,7 +175,7 @@ Please see: <${getActionsUrl(ActionsRepo.BACKPORT_PR_WORKFLOW)}>`,
return;
}

await app.opensumiOctoService.prNextRelease({
await app.octoService.prNextRelease({
pull_number: issue.number,
fullname,
});
Expand All @@ -189,7 +189,7 @@ Please see: <${getActionsUrl(ActionsRepo.BACKPORT_PR_WORKFLOW)}>`,
constraintRepo(fullname, updateLockfileAllowedRepo);
await checkIsPullRequestAndUserHasPermission(ctx, 'write');

await app.opensumiOctoService.updateLockfileForPr({
await app.octoService.updateLockfileForPr({
pull_number: issue.number,
});
await app.createReactionForIssueComment(ctx, 'rocket');
Expand All @@ -201,7 +201,7 @@ Please see: <${getActionsUrl(ActionsRepo.BACKPORT_PR_WORKFLOW)}>`,
constraintRepo(fullname, updateLockfileAllowedRepo);
await checkIsPullRequestAndUserHasPermission(ctx, 'admin');

await app.opensumiOctoService.createMergeCommitForPr({
await app.octoService.createMergeCommitForPr({
pull_number: issue.number,
});
await app.createReactionForIssueComment(ctx, 'rocket');
Expand Down
20 changes: 10 additions & 10 deletions src/im/commands/opensumi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function registerOpenSumiCommand(it: IMCommandCenter) {
try {
text =
'\n\n' +
(await app.opensumiOctoService.getLastNCommitsText({
(await app.octoService.getLastNCommitsText({
owner: 'opensumi',
repo: 'github-bot',
}));
Expand All @@ -61,8 +61,8 @@ export function registerOpenSumiCommand(it: IMCommandCenter) {
);
}

await app.opensumiOctoService.deployBot();
await app.opensumiOctoService.deployBotPre();
await app.octoService.deployBot();
await app.octoService.deployBotPre();
await bot.reply(
convertToDingMarkdown(
'开始部署机器人',
Expand All @@ -86,7 +86,7 @@ export function registerOpenSumiCommand(it: IMCommandCenter) {
try {
text =
'\n\n' +
(await app.opensumiOctoService.getLastNCommitsText({
(await app.octoService.getLastNCommitsText({
owner: 'opensumi',
repo: 'github-bot',
}));
Expand All @@ -96,7 +96,7 @@ export function registerOpenSumiCommand(it: IMCommandCenter) {
);
}

await app.opensumiOctoService.deployBotPre(workflowRef);
await app.octoService.deployBotPre(workflowRef);
await bot.reply(
convertToDingMarkdown(
'开始部署预发机器人',
Expand Down Expand Up @@ -148,7 +148,7 @@ export function registerOpenSumiCommand(it: IMCommandCenter) {
params.time = command.args.t || command.args.time;
}

await app.opensumiOctoService.monthlyReport(params);
await app.octoService.monthlyReport(params);
await bot.replyText('Starts generating monthly report.');
},
[],
Expand Down Expand Up @@ -183,7 +183,7 @@ async function publishNextVersion(
`找不到 ref: ${ref}, 错误信息: ${(error as Error).message}`,
);
}
const text = await app.opensumiOctoService.getLastNCommitsText({
const text = await app.octoService.getLastNCommitsText({
owner: 'opensumi',
repo,
ref,
Expand All @@ -199,7 +199,7 @@ async function publishNextVersion(
workflowInfo.ref = workflowRef;
}

await app.opensumiOctoService.releaseNextVersion(workflowInfo, ref);
await app.octoService.releaseNextVersion(workflowInfo, ref);
await bot.reply(
convertToDingMarkdown(
`Releasing a next version of ${name}`,
Expand Down Expand Up @@ -241,7 +241,7 @@ async function syncVersion(
workflow_id: ActionsRepo.SYNC_FILE,
});

await app.opensumiOctoService.syncOpenSumiVersion(version, workflowRef);
await app.octoService.syncOpenSumiVersion(version, workflowRef);
await bot.reply(
convertToDingMarkdown(
'Synchronizing OpenSumi packages',
Expand All @@ -256,7 +256,7 @@ async function syncVersion(
workflow_id: ActionsRepo.SYNC_CODEBLITZ_FILE,
});

await app.opensumiOctoService.syncCodeblitzVersion(version, workflowRef);
await app.octoService.syncCodeblitzVersion(version, workflowRef);
await bot.reply(
convertToDingMarkdown(
'Synchronizing CodeBlitz packages',
Expand Down

0 comments on commit fd8883b

Please sign in to comment.