Skip to content

Instantly share code, notes, and snippets.

@smarr
Created August 11, 2020 19:37
Show Gist options
  • Select an option

  • Save smarr/317e83149bb564e54dc2b662a312ae03 to your computer and use it in GitHub Desktop.

Select an option

Save smarr/317e83149bb564e54dc2b662a312ae03 to your computer and use it in GitHub Desktop.

Revisions

  1. smarr revised this gist Aug 11, 2020. No changes.
  2. smarr created this gist Aug 11, 2020.
    50 changes: 50 additions & 0 deletions comment-on-github.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    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) });