Skip to content

Commit

Permalink
feat: update
Browse files Browse the repository at this point in the history
  • Loading branch information
rikumi committed Jul 13, 2024
1 parent 7186066 commit 8011a92
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/server/routes/github-webhook.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { IncomingMessage, ServerResponse } from 'http';
import { getStreamContent } from 'src/utils/stream';
import { getCurrentBranchName, unsafeUpdateBot } from 'src/utils/update';

export const ROUTE = '/github-webhook';

const githubWebhookHandler = async (req: IncomingMessage, res: ServerResponse) => {
if (req.headers['X-GitHub-Event'] !== 'push') {
return;
}
// do not trust the content until we begin to check secret here
const body = JSON.parse((await getStreamContent(req)).toString('utf-8'));
console.log('/github-webhook', JSON.stringify(body, null, 2));
// TODO

const { ref, head_commit: headCommit } = body;
const currentBranch = await getCurrentBranchName();
if (ref === `refs/heads/${currentBranch}` && !headCommit?.message?.includes('skip ci')) {
unsafeUpdateBot();
}
res.writeHead(200);
res.end();
};
Expand Down
5 changes: 5 additions & 0 deletions src/utils/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const exec = async (command: string, options: cp.ExecOptions) => {
});
};

export const getCurrentBranchName = async () => {
const cwd = path.resolve(__dirname, '..');
return (await exec('git branch --show-current', { cwd })).trim();
};

export const unsafeUpdateBot = async (
branch?: string,
onPullFinished?: (pullResult: string) => Promise<void>,
Expand Down

0 comments on commit 8011a92

Please sign in to comment.