Docs Menu
Docs Home
/ /
Atlas App Services
/ /

GitHub Snippets [Deprecated]

On this page

  • Overview
  • Log All Commits in MongoDB
  • Automatically Comment On New Pull Requests

Important

Third Party Services & Push Notifications Deprecation

Third party services and push notifications in App Services have been deprecated in favor of creating HTTP endpoints that use external dependencies in functions.

Webhooks have been renamed to HTTPS Endpoints with no change in behavior. You should migrate existing Webhooks.

Existing services will continue to work until September 30, 2025.

Because third party services and push notifications are now deprecated, they have been removed by default from the App Services UI. If you need to manage an existing third party service or push notification, you can add the configurations back to the UI by doing the following:

  • In the left navigation, under the Manage section, click App Settings.

  • Enable the toggle switch next to Temporarily Re-Enable 3rd Party Services, and then save your changes.

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.

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 = commits.map(commit => {
return {
commit: commit,
pushed_by: pusher,
repo: {
name: repository.name,
url: repository.url
}
}
})
// Get a client for the `GitHubRepo.logs` collection in MongoDB
const mongodb = context.services.get("mongodb-atlas");
const logs = mongodb.db("GitHubRepo").collection("commit-logs");
// Insert the log documents in MongoDB
try {
const insertResult = await logs.insertMany(commitLogs)
console.log(insertResult.insertedIds);
} catch(err) {
console.error(err)
}
};

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: "api.github.com",
path: `/repos/${repository.owner.login}/${repository.name}/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 `https://api.github.com`.
const http = context.services.get("<HTTP Service Name>");
// Send the Request to GitHub
const request = { ...github_basic_auth, ...pr_comments_url, headers, body };
const result = await http.post(request);
// 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);
}
};

Back

AWS S3 Service Snippets