The blog post shows some useful snippets for interacting with Github API.
Jenkins pipelines regularly interacts with Github (public or Enterprise) API to perform some query/posting, for example, regarding the current pull request.
For that reason, some of the following snippets are either in Groovy or curl commands embedded in Groovy-based Jenkinsfile code with some Jenkinsfile DSLs.
stage("Merge PR"){steps{withCredentials([usernamePassword(credentialsId:'credential-value',usernameVariable:'ACCESS_TOKEN_USERNAME',passwordVariable:'ACCESS_TOKEN_PASSWORD',)]){defGITHUB='https://github.ibm.com/api/v3/repos'sh"curl -X PUT -d '{\"commit_title\": \"Merge pull request\"}' ${GITHUB}/org-name/repo-name/pulls/${env.CHANGE_ID}/merge?access_token=${env.ACCESS_TOKEN_PASSWORD}"}}}
The Jenkins-provided environment variable $CHANGE_ID, in the case of a pull request, is the pull request number.
Working with Branches
Getting email of branch maintainer
At the end of a Jenkins build for a feature branch (NOT develop/master), you may want to email some developer of its status, as opposed to blasting a whole distribution list.
Note that in Git, there is no such metadata for branch creator, as discussed here.
Instead, it makes more sense to notify the latest/active committer which is likely the owner of the branch.
defgetBranchCreator(StringgithubUsername,StringgithubToken,Stringrepo,Stringbranch){StringGITHUB_API='https://git.enterprise.com/api/v3/repos'Stringurl="${GITHUB_API}/${githubUsername}/${repo}/branches/${branch}"println"Querying ${url}"deftext=url.toURL().getText(requestProperties:['Authorization':"token ${githubToken}"])defjson=newJsonSlurper().parseText(text)// Get last committer.defcreator=json.commit.commit.committer.email// TRICKY: json.commit.commit.committer.email is not valid if someone commits from Github web interface.// In the case, committer name is 'GitHub Enterprise'.if(json.commit.commit.committer.name=='GitHub Enterprise'){// Use author's email insteadcreator=json.commit.commit.author.email}// TRICKY: the following can return inconsistent data, including "null".// def author = json.authorreturncreator}// Calling from JenkinsfilewithCredentials([[$class:'UsernamePasswordMultiBinding',credentialsId:'my-credentials',passwordVariable:'GITHUB_PASSWORD',usernameVariable:'GITHUB_USERNAME']]){if(env.BRANCH_NAME==~/PR-\d+/){// If it is a PR build, use some distribution listemail='someemail@enterprise.com'}else{// NOTE: Replace env.GITHUB_USERNAME with the correct Github org name.email=getBranchCreator(env.GITHUB_USERNAME,env.GITHUB_PASSWORD,'my_repo',env.BRANCH_NAME)}}
Deleting a branch
Searching how to delete a branch in Github API’s Branches reference does not return anything.
In fact, to delete a branch, we have to delete its HEAD reference as shown here.
While the two fields are usually the same in normal commits (with same associated email and timestamp), they have different meanings.
In summary, the author is the one who created the content, and the committer is the one who committed it.
The two fields can be different in some common Github workflows:
Commit a change from Github web interface: The author is the logged-in user (e.g., tdongsi) but the “committer” field usually has the Github default name and email, e.g., “Github Enterprise” and “no-reply@github.com”.
Make and/or merge a pull request from Github: For example, Alice submitted a pull request which was accepted and then merged by Betty (the repository owner). In that case, the author is Alice and the committer is Betty.
Due to that subtle difference in committer and author in different scenarios, one has to be careful when using data sent by Github API in a Jenkins pipeline.
For example, you want to send email to the repository owner (committer) at the end of a Pull Request build, but what if someone adds a commit via Github web interface (commiter email would be “no-reply@github.com” which is not helpful).
2) There is an API rate limit for the free public Github API (note “X-RateLimit-Limit” and “X-RateLimit-Remaining” in output below).
You are likely to hit this rate limit quickly if you are polling the repos for updates.
Instead of polling from your CI (e.g., Jenkins) system, it is recommended to use Github webhooks.