Skip to content

Commit 995635b

Browse files
GODRIVER-3008 Add mongodb-drivers-comment-bot support (#361)
* Add a github app folder * handle dir * bootstrap node * fix path handling * use mjs * debug * debug * use sub dir * fix nvm dir * update gitignore * cleanup * make sha an input * cleanup * undo debug caat * Update .evergreen/github_app/create_or_modify_comment.mjs Co-authored-by: Bailey Pearson <[email protected]> * Update .evergreen/github_app/create_or_modify_comment.mjs Co-authored-by: Bailey Pearson <[email protected]> * Update .evergreen/github_app/create_or_modify_comment.mjs Co-authored-by: Bailey Pearson <[email protected]> * Add node bootstrap scripts * cleanup for local usage * fix handling of repo * source the file * enable debug print * more debug * fix path handling --------- Co-authored-by: Bailey Pearson <[email protected]>
1 parent 9df8705 commit 995635b

File tree

10 files changed

+930
-0
lines changed

10 files changed

+930
-0
lines changed

.evergreen/auth_aws/setup_secrets.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env bash
22
# setup_secrets
3+
# See https://wiki.corp.mongodb.com/display/DRIVERS/Using+AWS+Secrets+Manager+to+Store+Testing+Secrets
4+
# for details on usage.
35
set -eu
46

57
HERE=$(dirname $0)

.evergreen/github_app/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Supporting code for GitHub Apps
2+
3+
## mongodb-drivers-comment-bot
4+
5+
Use mongodb-drivers-comment-bot to create/update comments on PRs
6+
with the results of an EVG task. We offer a convenience script to install node,
7+
fetch secrets and run the app:
8+
9+
```bash
10+
bash create_or_modifiy_comment.sh -o "<repo-owner>" -n "<repo-name>" -h "<head-commit-sha>" -m "<comment-match>" -c "<path-to-comment-file>"
11+
```
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* Create or modify a GitHub comment using the mongodb-drivers-comment-bot.
3+
*/
4+
import * as fs from "fs";
5+
import * as process from "process";
6+
import { program } from 'commander';
7+
import { App } from "octokit";
8+
9+
const appId = process.env.GITHUB_APP_ID;
10+
const privateKey = process.env.GITHUB_SECRET_KEY.replace(/\\n/g, '\n');
11+
if (appId == '' || privateKey == '') {
12+
console.error("Missing GitHub App auth information");
13+
process.exit(1)
14+
}
15+
16+
// Handle cli.
17+
program
18+
.version('1.0.0', '-v, --version')
19+
.usage('[OPTIONS]...')
20+
.requiredOption('-o, --repo-owner <owner>', 'The owner of the repo (e.g. mongodb).')
21+
.requiredOption('-n, --repo-name <name>', 'The name of the repo (e.g. mongo-go-driver).')
22+
.requiredOption('-m, --body-match <string>', 'The comment body to match')
23+
.requiredOption('-c, --comment-path <path>', 'The path to the comment body file')
24+
.requiredOption('-h, --head-sha <sha>', 'The sha of the head commit')
25+
.parse(process.argv);
26+
27+
const options = program.opts();
28+
const {
29+
repoOwner: owner,
30+
repoName: repo,
31+
bodyMatch,
32+
commentPath,
33+
headSha: targetSha
34+
} = options;
35+
const bodyText = fs.readFileSync(commentPath, { encoding: 'utf8' });
36+
37+
// Set up the app.
38+
const installId = process.env['GITHUB_APP_INSTALL_ID_' + owner.toUpperCase()];
39+
if (installId == '') {
40+
console.error(`Missing install id for ${owner}`)
41+
process.exit(1)
42+
}
43+
const app = new App({ appId, privateKey });
44+
const octokit = await app.getInstallationOctokit(installId);
45+
const headers = {
46+
"x-github-api-version": "2022-11-28",
47+
};
48+
49+
// Find the matching pull request.
50+
let resp = await octokit.request("GET /repos/{owner}/{repo}/pulls?state=open&per_page=100", {
51+
owner,
52+
repo,
53+
headers
54+
});
55+
const issue = resp.data.find(pr => pr.head.sha === targetSha);
56+
if (issue == null) {
57+
console.error(`ERROR: Could not find matching pull request for sha ${targetSha}`)
58+
process.exit(1)
59+
}
60+
const { number: issueNumber } = issue
61+
62+
// Find a matching comment if it exists, and update it.
63+
resp = await octokit.request("GET /repos/{owner}/{repo}/issues/{issue_number}/comments", {
64+
owner,
65+
repo,
66+
issue_number: issueNumber,
67+
headers
68+
});
69+
const comment = resp.data.find(comment => comment.body.includes(bodyMatch));
70+
if (!comment) {
71+
// create comment.
72+
await octokit.request("POST /repos/{owner}/{repo}/issues/{issue_number}/comments", {
73+
owner,
74+
repo,
75+
issue_number: issueNumber,
76+
body: bodyText,
77+
headers
78+
});
79+
} else {
80+
// update comment.
81+
await octokit.request("PATCH /repos/{owner}/{repo}/issues/comments/{comment_id}", {
82+
owner,
83+
repo,
84+
body: bodyText,
85+
comment_id: comment.id,
86+
headers
87+
});
88+
89+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
set -o errexit # Exit the script with error if any of the commands fail
3+
set -x
4+
5+
DIR="$(dirname "${BASH_SOURCE[0]}")"
6+
pushd $DIR
7+
8+
# Bootstrap the secrets.
9+
bash $DIR/../auth_aws/setup_secrets.sh drivers/comment-bot
10+
source secrets-export.sh
11+
12+
# Install node and activate it.
13+
bash $DIR/../install-node.sh
14+
source $DIR/../init-node-and-npm-env.sh
15+
16+
# Install and run the app.
17+
set -x
18+
npm install
19+
node create_or_modify_comment.mjs "$@"
20+
popd

.evergreen/github_app/index.js

Whitespace-only changes.

0 commit comments

Comments
 (0)