-
Notifications
You must be signed in to change notification settings - Fork 38
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
Try resolve commit from run first #27
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7,6 +7,7 @@ | |||||
import jenkins.plugins.git.AbstractGitSCMSource; | ||||||
import jenkins.scm.api.SCMHead; | ||||||
import jenkins.scm.api.SCMRevision; | ||||||
import org.apache.commons.lang3.StringUtils; | ||||||
import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider; | ||||||
import org.jenkinsci.plugins.github_branch_source.GitHubAppCredentials; | ||||||
import org.jenkinsci.plugins.github_branch_source.GitHubSCMSource; | ||||||
|
@@ -75,7 +76,13 @@ class GitHubChecksContext { | |||||
* @return the commit sha of the run or null | ||||||
*/ | ||||||
public String getHeadSha() { | ||||||
return resolveHeadSha(); | ||||||
String commit = resolveCommit(); | ||||||
if (StringUtils.isEmpty(commit)) { | ||||||
return resolveHeadSha(); | ||||||
} | ||||||
else { | ||||||
return commit; | ||||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
|
@@ -150,14 +157,32 @@ private String resolveHeadSha() { | |||||
getRepository(), head.get().getName())); | ||||||
} | ||||||
|
||||||
if (revision.get() instanceof AbstractGitSCMSource.SCMRevisionImpl) { | ||||||
return ((AbstractGitSCMSource.SCMRevisionImpl) revision.get()).getHash(); | ||||||
return resolveCommit(revision.get()); | ||||||
} | ||||||
|
||||||
private String resolveCommit() { | ||||||
if (run == null) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my refactored version I'm not sure if I did understand the GitHub REST API correctly: is it valid to call it without a SHA? If not then we should not create a ChecksPublisher. This is the way I did it with Freestyle jobs and pipelines. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason we introduce the nullable run here is that when the job is still in the queue, the run is not available, so we can't get the url of the run (see #13 (comment)) and will return the url of the job view instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't find a better way to avoid nullable run, but maybe we can add another boolean field There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I saw your refactoring on this problem, simple but great! Ignore the previous two comments :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The SHA is required for a check, without it, the check cannot be created successfully. |
||||||
return StringUtils.EMPTY; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. returning an empty string is usually a codesmell, just return null if you can't find it, imo
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then we need to guard all methods that invoke the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is only called internally from one place? |
||||||
} | ||||||
|
||||||
Optional<SCMRevision> revision = scmFacade.findRevision(resolveSource(), run); | ||||||
if (revision.isPresent()) { | ||||||
return resolveCommit(revision.get()); | ||||||
} | ||||||
else { | ||||||
return StringUtils.EMPTY; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
} | ||||||
|
||||||
private String resolveCommit(final SCMRevision revision) { | ||||||
if (revision instanceof AbstractGitSCMSource.SCMRevisionImpl) { | ||||||
return ((AbstractGitSCMSource.SCMRevisionImpl) revision).getHash(); | ||||||
} | ||||||
else if (revision.get() instanceof PullRequestSCMRevision) { | ||||||
return ((PullRequestSCMRevision) revision.get()).getPullHash(); | ||||||
else if (revision instanceof PullRequestSCMRevision) { | ||||||
return ((PullRequestSCMRevision) revision).getPullHash(); | ||||||
} | ||||||
else { | ||||||
throw new IllegalStateException("Unsupported revision type: " + revision.get().getClass().getName()); | ||||||
throw new IllegalStateException("Unsupported revision type: " + revision.getClass().getName()); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we catch that error before we create the publisher and return then a |
||||||
} | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
null
is allowed (which I think is a bad choice) then we must mark the method withNullable