Skip to content

Commit

Permalink
GODRIVER-3008 Add mongodb-drivers-comment-bot support (#361)
Browse files Browse the repository at this point in the history
* 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
blink1073 and baileympearson committed Oct 16, 2023
1 parent 9df8705 commit 995635b
Show file tree
Hide file tree
Showing 10 changed files with 930 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .evergreen/auth_aws/setup_secrets.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env bash
# setup_secrets
# See https://wiki.corp.mongodb.com/display/DRIVERS/Using+AWS+Secrets+Manager+to+Store+Testing+Secrets
# for details on usage.
set -eu

HERE=$(dirname $0)
Expand Down
11 changes: 11 additions & 0 deletions .evergreen/github_app/README.md
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>"
```
89 changes: 89 additions & 0 deletions .evergreen/github_app/create_or_modify_comment.mjs
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
});

}
20 changes: 20 additions & 0 deletions .evergreen/github_app/create_or_modify_comment.sh
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 added .evergreen/github_app/index.js
Empty file.
Loading

0 comments on commit 995635b

Please sign in to comment.