Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Identify pull request merge builds #1

Closed
adamrbennett opened this issue Oct 9, 2017 · 4 comments
Closed

Identify pull request merge builds #1

adamrbennett opened this issue Oct 9, 2017 · 4 comments

Comments

@adamrbennett
Copy link

I have been watching this plugin and was excited to see the v1 release, thanks! I have high hopes for this plugin and its usefulness.

I'm using the GitHub Organization plugin with automatic job creation and webhooks.

Here's my workflow:

  1. Open a PR -- PR build job created and run automatically
  2. Merge PR into master -- master build job run automatically

When my PR build executes, I am able to access the pullRequest.merged property, and it is false as I would expect. However, when I merge the PR, and the master build runs, I am not able to access the pullRequest global variable (presumably because the build is executing against master, and not a PR).

I was hoping I would be able to check the pullRequest.merged property in the master build to determine that a PR has just been merged, and execute some additional logic. Is there another way to achieve this, should the approach above work, or is this just not supported at the moment?

@aaronjwhiteside
Copy link

You're right, the pullRequest global variable is only available in Pull Requests.

I'm afraid it wouldn't make much sense to make it available for ordinary branches.

What I think you're probably looking for is some kind of trigger for when a PR is closed/merged. Technically GitHub supports this t(https://developer.github.com/v3/activity/events/types/#pullrequestevent) so it should be possible to do. And I think the plumbing is already in place in the github-api/github plugin, we would just need to write a subscriber org.jenkinsci.plugins.github.extension.GHEventsSubscriber to listen for it.

If you feel up to it you could submit a PR, otherwise I'll add it to my todo list!

To answer your last question, this isn't currently supported by the Pipeline Github Plugin, but I think it would be a welcome addition.

As for is there a possible workaround, I think you might be able to check the branch history/log and determine if the latest commit was a merge commit and perhaps check the commit message, to determine the originating branch/pr.. I haven't tried to investigate this approach myself but a quick google found these:

I hope this gives you a starting point, otherwise I would really appreciate a PR.

@adamrbennett
Copy link
Author

Thanks for the ideas. In the end, I just decided to use the commit hash and query the GitHub API:

def res = checkout scm
def commitHash = res['GIT_COMMIT']
withCredentials([usernameColonPassword(credentialsId: 'github-credentials', variable: 'AUTH')]) {
  pullRequestNumber = sh script: "curl -s -u $AUTH 'https://api.github.com/repos/<org>/<repo>/pulls?state=closed' | jq -j '.[] | select(.merge_commit_sha == \"$commitHash\") | .number'", returnStdout: true
}

I might still have a use for the PR trigger, though, and I started the initial work. If I get an opportunity to finish it, I'll submit a PR. Thanks for the help!

@l3ender
Copy link

l3ender commented Aug 27, 2024

Hello, we have this same desire (run job on a branch and be able to retrieve PR details for a merge to that branch). I am interested to contribute to the library but want to be sure the feature hasn't been added since this issue and that PRs would be accepted!

@aaronwalker - tagging you because I see you previously merged changes into this repo. Can you give your advice/thoughts?

@l3ender
Copy link

l3ender commented Aug 27, 2024

Also wondering if/how this PR relates to #91 - not sure if that is what is referenced by #1 (comment)?

What I think you're probably looking for is some kind of trigger for when a PR is closed/merged. Technically GitHub supports this t(https://developer.github.com/v3/activity/events/types/#pullrequestevent) so it should be possible to do. And I think the plumbing is already in place in the github-api/github plugin, we would just need to write a subscriber org.jenkinsci.plugins.github.extension.GHEventsSubscriber to listen for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants