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

When HEAD is detached find GitHub PR by SHA #247

Merged
merged 2 commits into from
Jul 15, 2017

Conversation

drewish
Copy link
Contributor

@drewish drewish commented Jun 16, 2017

I've been trying to use the GithubPullRequestReviewFormatter with a Jenkins pipeline but ran into a problem. Jenkins doesn't expose the PR number and it checks out the commit from the webhook rather than the branch. The best fix I could come up with was modifying pronto to find the PR based off the SHA if the repo has a detached HEAD.

I wasn't sure if this would be an acceptable change so I didn't bother with any specs. If it's something you'd consider merging I'm happy to add tests.

@mmozuras
Copy link
Member

@drewish seems good to me 👍

@mknapik @doomspork @aergonaut what do you think?

@aergonaut
Copy link
Member

Seems alright to me. I don't think it's possible to have more than one PR with the same head SHA, so finding the PR based on that seems fine.

@drewish
Copy link
Contributor Author

drewish commented Jun 19, 2017

Cool I'll throw some specs together. Any suggestions on how to resolve that code climate issue?

@drewish
Copy link
Contributor Author

drewish commented Jun 19, 2017

Actually looking at the existing tests I'm not sure specs for this make a ton of sense. Pronto::Github#pull is private and Pronto::Git::Repository#head_detached? just delegates to the Rugged::Repository. If there's something that makes sense to the maintainers I'm happy to add it though.

@drewish drewish changed the title Find PR by SHA with detached HEAD When HEAD is detached find GitHub PR by SHA Jun 19, 2017
@doomspork
Copy link
Member

@mmozuras sounds like a good idea, I'd run into this issue myself w/ a previous client.

@aergonaut
Copy link
Member

Codeclimate is complaining about the ABC size of that block. I personally think the block is fine as written so I would rather just increase the max value of that metric. @mmozuras what do you think about that?

@mknapik
Copy link
Contributor

mknapik commented Jun 20, 2017

@drewish You might find it useful: https://github.com/u2i/jenkins-pipeline-utils/blob/master/src/me/knapik/GitHub.groovy
https://github.com/u2i/jenkins-pipeline-utils/blob/master/vars/foreachPullRequest.groovy

And an example usage:

        foreachPullRequest(GITHUB_ACCESS_TOKEN_CREDENTIALS_ID) { int pullRequestId, String targetBranchRef ->
            withEnv(["PULL_REQUEST_ID=${pullRequestId}"]) {
                rake "ci:pronto:pr[origin/${targetBranchRef}]"
            }
        }

Also Jenkins Pipeline has two types of jobs for GitHub org: branch job and PR job. I'm pretty sure PR job provide you with PR number in an environment variable.

If you'd like to use my jenkins pipeline shared library I can provide you with more info how to set it up.

Although, having it in Pronto would be so much better :-)

@drewish
Copy link
Contributor Author

drewish commented Jun 20, 2017

@mknapik oh neat yeah i've got some ruby code that does something similar. trying to do it in groovy with the CPS stuff was too much of a frustration for me. i found it easier to just call it via the shell. but when i was looking at the code in pronto i realized it just needed a couple tweaks to let me ditch my custom code.

Updated: If you know the name of an environment variable with the PR number in it please let me know. I'd spend up https://issues.jenkins-ci.org/browse/JENKINS-44948 against the GitHub Branch Source Plugin since it didn't seem like existing functionality.

@mknapik
Copy link
Contributor

mknapik commented Jun 21, 2017

@drewish The name of the job for PR job type is PR-XX.
image
So it is possible to strip PR-. Not as straightforward though.

@drewish
Copy link
Contributor Author

drewish commented Jun 26, 2017

@mknapik ah thanks, I checked and we're doing the branch based PRs so that makes more sense to me now.

@mmozuras I can bump the ABC metric as part of this PR if you think that makes sense or I'm happy to do that in a separate PR if that needs to be a prerequisite for this.

@mmozuras
Copy link
Member

@drewish we're in no hurry, so would prefer if you'd do it as part of this PR 👍

@drewish
Copy link
Contributor Author

drewish commented Jun 28, 2017

I bumped it up to 25 since that was showing ~21. It looks like we've resolved a few other places that were getting flagged for that complexity issue.

It's looking green!

@drewish
Copy link
Contributor Author

drewish commented Jul 3, 2017

@mmozuras let me know if there's anything else this PR needs.

@mmozuras
Copy link
Member

@drewish nope, looks good! 👍

@mmozuras mmozuras merged commit e97cb2b into prontolabs:master Jul 15, 2017
@mmozuras
Copy link
Member

Released as part of v0.9.4.

@drewish drewish deleted the support-detached branch May 14, 2019 19:59
apiology pushed a commit to apiology/pronto that referenced this pull request Dec 27, 2019
apiology pushed a commit to apiology/pronto that referenced this pull request Dec 27, 2019
When HEAD is detached find GitHub PR by SHA
AdamHawkinsa pushed a commit to AdamHawkinsa/pronto that referenced this pull request Nov 20, 2023
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

Successfully merging this pull request may close these issues.

None yet

5 participants