DEV Community

James
James

Posted on

Creating a Github comment from Netlify

Using octokit its possible to interact with Github from CI pipelines. The example below adds a comment to a PR.

const {
  env: { OWNER, ACCESS_TOKEN, REPOSITORY_URL, REVIEW_ID, PULL_REQUEST },
} = require('process');

function init() {
  const { Octokit } = require('@octokit/rest');

  const octokit = new Octokit({
    auth: ACCESS_TOKEN,
  });

  return {
    createComment: async (comment) => {
      const config = {
        owner: OWNER,
        repo: REPOSITORY_URL.split('/').pop(),
        issue_number: REVIEW_ID,
      };

      await octokit.issues.createComment({
        ...config,
        body: comment,
      });
    },
  };
}
Enter fullscreen mode Exit fullscreen mode

And here's how you might use that in a Netlify plugin.

module.exports = {
  onSuccess: async () => {
    const { createComment } = initialiseGithub();
    await createComment('The Netlify build has succeeded');
  },
  onError: async () => {
    const { createComment } = initialiseGithub();
    await createComment('The Netlify build has failed');
  },
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)