How to close a pull request in GitHub using Jenkins Jobs - github

I have a problem, how to close a pull request using Jenkins jobs.
my goal is, I can get a branch, using this branch name to find, if under this branch, a pull request existed.
if yes, then I close this pull request through Github API.
Question, How to get pull request ID through Branch name?
2: if I get pull request ID, how to close this pull request.
Any Solutions?
I finde

Related

Getting PR information in the jenkins pipeline

I am using bitbucket and jenkins as my CI/CD solution.
I am running a sanity job everytime Pull request is raised. I want to send email notification on the success or failure of the job to the owner of the pull requuest. The challenge I am facing is I am not able to get the user information for the PR in jenkins pipeline.
Anybody has solved this porblem ?
Here's a different approach to this: instead of directly emailing the author of the PR, use the Bitbucket API to write a comment on the PR (doc). Since you would already have the PR ID, it's just a matter of sending a POST request.
By default, any comments written on a Bitbucket PR automatically trigger an email to the PR author + PR reviewers so they will be getting notified via email as well.

Azure pipelines variable for PR number of a merged pull request

Azure dev-ops pipelines have predefined varialbles related to github pull requests. I can use SYSTEM_PULLREQUEST_PULLREQUESTNUMBER for getting PR number that triggered my pipeline. However I get no value from SYSTEM_PULLREQUEST_PULLREQUESTNUMBER when my pipeline is triggered again as a result of merging this PR in the main repo.
My use case is to identify the list of files that were changed in the original PR.
I looked into Azure user predefined variable document but could not see if there is any variable available to get this information.
When the pipeline is ran the second time when it is merged, it is considered to have the trigger type CI not Pull Request. Therefore the PR number is unavailable in this context because there was no PR.
You could also try to save the pull request number to a variable group in the previous run triggered by a pull request.
https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/cli/pipeline-variable-group-secret-nonsecret-variables?view=azure-devops
You could try and steal the pull request number from the commit message. If your PR merge type is set to 'squash commit' you can write some regex to pull the PR number out of the commit message.
https://learn.microsoft.com/en-us/azure/devops/repos/git/merging-with-squash?view=azure-devops#squash-merge

Jenkins Github Pull Request Merger does not respect statuses

I'm trying to set up an automated CI process GitHub and Jenkins. The goal is to have developers create feature branches and generate pull requests that are automatically merged (if they pass build, of course) using Jenkins Github Pull Request Merger.
It is a further goal to require that pull requests be against an open GitHub issue. For us, that means that either the pull request title or at least one of the pull request commit messages must contain a substring like "fixes #NN" where #NN must reference an open GitHub issue. This 'issue_opened' check is also automated - our 'issue_opened' GitHub App queries the GitHub issues and examines commit messages and the PR title, then it POSTs the pull request with a status (for testing purposes I'm always posting 'failure').
The process envisioned is as follows:
1. Feature branch pushes are automatically built by Jenkins.
2. When a feature branch is ready and passes Jenkins testing, a developer will generate a pull request; this automatically triggers steps 3 & 4, each running independently:
3. Our 'issue_opened' GitHub App very quickly POSTs a status to the pull request.
4. Jenkins performs the build - it takes much longer than step 3. If the build passes, Jenkins applies that status. If all statuses are 'success', the pull request is automatically merged.
What I observe:
Currently, my pull requests are merging feature branches to master. Master is protected (GitHub master branch: Settings>Branches>Protect this branch>Require status checks to pass before merging and the 'issue_opened' status check is set to Required.) Everything works as planned, except that the Github Pull Request Merger breaks GitHub convention and only respects its own status, not the other statuses.
So the PR merge depends only on Jenkins:
After step 3 POSTs a 'failure' status but before step 4 completes, GitHub reports that "Required statuses must pass before merging" and indicates that the 'issue_opened' status is 'failure'. But when the Jenkins build succeeds, the merge takes place anyway.
FWIW, the merge also happens if the feature branch already has a bad status at the time the pull request is created.
Any way I can get this to do what I want?
After more desperate fiddling I tried enabling the GH master branch protection setting Settings>Branches>Protected Branches>master>Protect this branch>Include administrators and 'voila': it pretty much works for me, more or less:
Jenkins Github Pull Request Merger still tries to do the merge, but GitHub returns this:
HTTP response code: 405, message: 'Method Not Allowed'.
As a result of the rc 405, Jenkins generates a java.io.IOException and regurgitates this json message from GH:
{"message":"2 of 2 required status checks have not succeeded: 1 failing and 1 pending.","documentation_url":"https://help.github.com/enterprise/2.10/user/articles/about-protected-branches"}
Jenkins then POSTS a 'failure' status (which one might quibble over because the build itself didn't fail).
This makes sense, since I am an admin for this repo, but I didn't anticipate that the Jenkins Github Pull Request Merger would not check the statuses. But I'm very pleased that this will get the job done for me, though from my point of view it would be cleaner if Jenkins first posted the build status, then POSTed the merge. Even better, if it checked the statuses it could simply skip the attempt to POST, and I wouldn't have had to enable the Include administrators protection. As it stands, I don't see a way to clear the Jenkins-posted failure status on the pull request. So we'll have to close such failed pull requests and create new ones.
Additional Info
Since posting initial answer I have discovered that one must not set/enable the GitHub Branch protections status check that comes from the Jenkins build. If it is not enabled, one can close the failed pull request, correct whatever problems caused other status checks or the Jenkins build to fail, and then start the pull request process over again by opening a new pull request.

How to get pull request id from Jenkins Pipeline

I'm trying to analyse my source code with Sonar using Jenkins pipelines. To ask Sonar to notify Github with the results I need to specify the Pull Request ID.
How can I get this Pull Request ID from Jenkins Pipelines?
We are using GitHub Organization Folder Plugin to build pull requests, not GitHub pull request builder plugin. That's why $ghprbPullId is not working for me. Any ideas how to get the pull request id in a different way?
Jenkins exposes a global variable named CHANGE_ID:
For a multibranch project corresponding to some kind of
change request, this will be set to the change ID, such as a pull
request number.
This variable is only populated for pull request builds, so you have to disable branch builds and enable PR builds in your pipeline's configuration for branch sources:
My pipeline step then looks like this:
def PULL_REQUEST = env.CHANGE_ID
stage('Analysis') {
withCredentials([[$class: 'StringBinding', credentialsId: '***', variable: 'GITHUB_ACCESS_TOKEN']]) {
withSonarQubeEnv('Sonar') {
withMaven(maven: 'M3') {
sh "mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.2:sonar " +
"-Dsonar.analysis.mode=preview " +
"-Dsonar.github.pullRequest=${PULL_REQUEST} " +
"-Dsonar.github.oauth=${GITHUB_ACCESS_TOKEN}"
}
}
}
}
You get the PR number through for example env.BRANCH_NAME.
if (env.BRANCH_NAME.startsWith('PR-')) {
def prNum = env.BRANCH_NAME.replace(/^PR-/, '')
...
}
In the case that Thomas' answer doesn't work or apply to you, you may also (possibly) use the branch name to get the Pull Request number by querying the Github REST API. All you need is an API token and the branch name, lookup the pull requests in order of date updated DESC, and find the first PR that matches your branch name. That will have the Pull Request number.
This only works if you have a unique branch name for each pull request (such as a JIRA issue ticket number).

How do I get the files from a public github repo pull request on my local computer?

How do I get the files from a public repo pull request on my local computer?
A developer submitted a pull request to a public repo (to which I don't have write access).
I want to test his code before I acknowledge the pull request.
How do I get the master of the public repo + his pull request on my local in order to compile and test it?
You can click the 'info' icon to get the git commands you need to run.
You can also get any pull request as a patch by appending .patch to the url. For instance:
https://github.com/github/github-services/pull/146.patch