GitHub Snippets

The code snippets on this page cover demonstrate how you can respond to events in a GitHub repository through the GitHub Service. All of the snippets require a GitHub Service interface with rules that allow the service actions used in the snippet.

If your app does not have a GitHub Service interface, create one before using these snippets.

Log All Commits in MongoDB

This GitHub incoming webhook function records all commits pushed to a repo in MongoDB based on a PushEvent payload from GitHub.

exports = async function(pushEvent) {
  // Parse the list of commits from the PushEvent payload.
  // Also grab the user that pushed the commits and the repo information.
  const { commits, pusher, repository } = pushEvent;

  // Create a new array of log documents, one for each commit
  const commitLogs = => {
    return {
      commit: commit,
      pushed_by: pusher,
      repo: {
        url: repository.url

  // Get a client for the `GitHubRepo.logs` collection in MongoDB
  const mongodb ="mongodb-atlas");
  const logs = mongodb.db("GitHubRepo").collection("commit-logs");

  // Insert the log documents in MongoDB
  try {
    const insertResult = await logs.insertMany(commitLogs)
  } catch(err) {

Automatically Comment On New Pull Requests

This GitHub incoming webhook function adds a comment to new pull requests that thanks users for submitting. The webhook accepts a PullRequestEvent payload from GitHub and uses an HTTP service client to create a comment through the GitHub API.

exports = async function(pullRequest) {
  // Get information about the PR from the PullRequestEvent
  const { action, repository, pull_request: pr } = pullRequest;

  // Only run if this is a new PR
  if (action !== "opened") { return }

  // Construct the GitHub API URL for this PR's Comments
  const pr_comments_url = {
    scheme: "https",
    host: "",
    path: `/repos/${repository.owner.login}/${}/issues/${pr.number.$numberInt}/comments`,
  // Specify GitHub API Basic Authentication Fields and Headers
  const github_basic_auth = {
    username: context.values.get("github-credentials").username,
    password: context.values.get("github-credentials").password,
  const headers = {
    // OPTIONAL: Include this header if your security settings require a 2fa code
    "X-GitHub-OTP": ["<2fa Code>"]
  // Specify the comment text
  const body = EJSON.stringify({
    body: `Thank you for submitting a pull request, ${pr.user.login}!`

  try {
    // Get an HTTP service client. The service rules should allow you
    // to send POST requests to ``.
    const http ="<HTTP Service Name>");

    // Send the Request to GitHub
    const request = { ...github_basic_auth, ...pr_comments_url, headers, body };
    const result = await;

    // Check for a Successful Result
    if (result.statusCode == 201) {
      return "Successfully commented on the pull request!";
    } else {
      throw new Error(`Received a bad result status from GitHub: ${result.body.text()}`);
  } catch (err) {
    console.error("Something went wrong while posting the comment.", err);