DEV Community

James
James

Posted on

2 2

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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay