Skip to content

Instantly share code, notes, and snippets.

@rajivreddy
Forked from StevenACoffman/github.sh
Created May 4, 2021 02:31
Show Gist options
  • Save rajivreddy/7a850cb6de9a9d62bd73bb336d39b4ca to your computer and use it in GitHub Desktop.
Save rajivreddy/7a850cb6de9a9d62bd73bb336d39b4ca to your computer and use it in GitHub Desktop.
Jenkins Scripts For Build Status to Github Pull Request
#!/bin/bash
# Run these functions like this:
# BUILD_URL="http://www.google.com";gh_post_success_comment "mylists-service" "48"
function gh_post_success_comment() {
#Expects that the GITHUB_AUTH_TOKEN is provided already in the environment
#Expects that BUILD_URL is provided already in the environment
# 1st required argument is the repository name like "jstor" or "mylists-service"
# 2nd required argument is the pull request number
# 3rd optional argument is the message itself
REPO="${1:-please-specify-repo}"
PULL_REQUEST_NUMBER="${2:-please-specify-pr-number}"
COMMENT_DEFAULT=":white_check_mark: [All Checks Passed](${BUILD_URL})"
COMMENT_MARKDOWN="${3:-$COMMENT_DEFAULT}"
curl -v -H "Content-Type: application/json" \
-H "Authorization: token ${GITHUB_AUTH_TOKEN}" \
-X POST \
-d "{\"body\":\"${COMMENT_MARKDOWN}\"}" \
"https://api.github.com/repos/ithaka/${REPO}/issues/${PULL_REQUEST_NUMBER}/comments"
}
function gh_post_failure_comment() {
#Expects that the GITHUB_AUTH_TOKEN is provided already in the environment
#Expects that BUILD_URL is provided already in the environment
# 1st required argument is the repository name like "jstor" or "mylists-service"
# 2nd required argument is the pull request number
# 3rd optional argument is the message itself
REPO="${1:-please-specify-repo}"
PULL_REQUEST_NUMBER="${2:-please-specify-pr-number}"
COMMENT_DEFAULT=":x: [Failure](${BUILD_URL})"
COMMENT_MARKDOWN="${3:-$COMMENT_DEFAULT}"
curl -v -H "Content-Type: application/json" \
-H "Authorization: token ${GITHUB_AUTH_TOKEN}" \
-X POST \
-d "{\"body\":\"${COMMENT_MARKDOWN}\"}" \
"https://api.github.com/repos/ithaka/${REPO}/issues/${PULL_REQUEST_NUMBER}/comments"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment