Skip to content

Instantly share code, notes, and snippets.

@smarr
Created August 11, 2020 19:37
Show Gist options
  • Save smarr/317e83149bb564e54dc2b662a312ae03 to your computer and use it in GitHub Desktop.
Save smarr/317e83149bb564e54dc2b662a312ae03 to your computer and use it in GitHub Desktop.
Using TypeScript and Octokit/rest.js to Comment on Pull Request or Commit
import { Octokit } from '@octokit/rest';
import { createAppAuth } from '@octokit/auth-app';
import { readFileSync } from 'fs';
// authorize on app level
const app = new Octokit({
authStrategy: createAppAuth,
auth: {
type: "app",
id: 12345,
privateKey: readFileSync('app-private-key.pem')
}
});
async function start() {
// get the installation id
const install = await app.apps.getRepoInstallation({
owner: 'owner',
repo: 'repo'
});
// authorize on repo level
const appInstall = new Octokit({
authStrategy: createAppAuth,
auth: {
type: "install",
id: 12345,
privateKey: readFileSync('app-private-key.pem')
installationId: install.data.id
}
});
// create a comment on a commit, it normally will show on on related PRs
let result = await appInstall.repos.createCommitComment({
// or perhaps comment on an issue/PR
// let result = await appInstall.issues.createComment({
// issue_number: 42,
owner: 'owner',
repo: 'repo',
commit_sha: 'sha',
body:`### Test
This is test with markdown.
Let's see whether it works.`});
console.log('got result: ');
console.log(result);
}
start().then(() => console.log('done')).catch(e => { console.error(e) });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment