-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <bailey.pearson@gmail.com> * Update .evergreen/github_app/create_or_modify_comment.mjs Co-authored-by: Bailey Pearson <bailey.pearson@gmail.com> * Update .evergreen/github_app/create_or_modify_comment.mjs Co-authored-by: Bailey Pearson <bailey.pearson@gmail.com> * 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 <bailey.pearson@gmail.com>
- Loading branch information
1 parent
9df8705
commit 995635b
Showing
10 changed files
with
930 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Supporting code for GitHub Apps | ||
|
||
## mongodb-drivers-comment-bot | ||
|
||
Use mongodb-drivers-comment-bot to create/update comments on PRs | ||
with the results of an EVG task. We offer a convenience script to install node, | ||
fetch secrets and run the app: | ||
|
||
```bash | ||
bash create_or_modifiy_comment.sh -o "<repo-owner>" -n "<repo-name>" -h "<head-commit-sha>" -m "<comment-match>" -c "<path-to-comment-file>" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* Create or modify a GitHub comment using the mongodb-drivers-comment-bot. | ||
*/ | ||
import * as fs from "fs"; | ||
import * as process from "process"; | ||
import { program } from 'commander'; | ||
import { App } from "octokit"; | ||
|
||
const appId = process.env.GITHUB_APP_ID; | ||
const privateKey = process.env.GITHUB_SECRET_KEY.replace(/\\n/g, '\n'); | ||
if (appId == '' || privateKey == '') { | ||
console.error("Missing GitHub App auth information"); | ||
process.exit(1) | ||
} | ||
|
||
// Handle cli. | ||
program | ||
.version('1.0.0', '-v, --version') | ||
.usage('[OPTIONS]...') | ||
.requiredOption('-o, --repo-owner <owner>', 'The owner of the repo (e.g. mongodb).') | ||
.requiredOption('-n, --repo-name <name>', 'The name of the repo (e.g. mongo-go-driver).') | ||
.requiredOption('-m, --body-match <string>', 'The comment body to match') | ||
.requiredOption('-c, --comment-path <path>', 'The path to the comment body file') | ||
.requiredOption('-h, --head-sha <sha>', 'The sha of the head commit') | ||
.parse(process.argv); | ||
|
||
const options = program.opts(); | ||
const { | ||
repoOwner: owner, | ||
repoName: repo, | ||
bodyMatch, | ||
commentPath, | ||
headSha: targetSha | ||
} = options; | ||
const bodyText = fs.readFileSync(commentPath, { encoding: 'utf8' }); | ||
|
||
// Set up the app. | ||
const installId = process.env['GITHUB_APP_INSTALL_ID_' + owner.toUpperCase()]; | ||
if (installId == '') { | ||
console.error(`Missing install id for ${owner}`) | ||
process.exit(1) | ||
} | ||
const app = new App({ appId, privateKey }); | ||
const octokit = await app.getInstallationOctokit(installId); | ||
const headers = { | ||
"x-github-api-version": "2022-11-28", | ||
}; | ||
|
||
// Find the matching pull request. | ||
let resp = await octokit.request("GET /repos/{owner}/{repo}/pulls?state=open&per_page=100", { | ||
owner, | ||
repo, | ||
headers | ||
}); | ||
const issue = resp.data.find(pr => pr.head.sha === targetSha); | ||
if (issue == null) { | ||
console.error(`ERROR: Could not find matching pull request for sha ${targetSha}`) | ||
process.exit(1) | ||
} | ||
const { number: issueNumber } = issue | ||
|
||
// Find a matching comment if it exists, and update it. | ||
resp = await octokit.request("GET /repos/{owner}/{repo}/issues/{issue_number}/comments", { | ||
owner, | ||
repo, | ||
issue_number: issueNumber, | ||
headers | ||
}); | ||
const comment = resp.data.find(comment => comment.body.includes(bodyMatch)); | ||
if (!comment) { | ||
// create comment. | ||
await octokit.request("POST /repos/{owner}/{repo}/issues/{issue_number}/comments", { | ||
owner, | ||
repo, | ||
issue_number: issueNumber, | ||
body: bodyText, | ||
headers | ||
}); | ||
} else { | ||
// update comment. | ||
await octokit.request("PATCH /repos/{owner}/{repo}/issues/comments/{comment_id}", { | ||
owner, | ||
repo, | ||
body: bodyText, | ||
comment_id: comment.id, | ||
headers | ||
}); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/bin/bash | ||
set -o errexit # Exit the script with error if any of the commands fail | ||
set -x | ||
|
||
DIR="$(dirname "${BASH_SOURCE[0]}")" | ||
pushd $DIR | ||
|
||
# Bootstrap the secrets. | ||
bash $DIR/../auth_aws/setup_secrets.sh drivers/comment-bot | ||
source secrets-export.sh | ||
|
||
# Install node and activate it. | ||
bash $DIR/../install-node.sh | ||
source $DIR/../init-node-and-npm-env.sh | ||
|
||
# Install and run the app. | ||
set -x | ||
npm install | ||
node create_or_modify_comment.mjs "$@" | ||
popd |
Empty file.
Oops, something went wrong.