-
Notifications
You must be signed in to change notification settings - Fork 47
Support GitHubAppCredentials owner inference when specified on a pipeline using GitHub SCM #396
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
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 |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
| import org.apache.commons.lang3.StringUtils; | ||
|
|
||
| import edu.hm.hafner.util.VisibleForTesting; | ||
| import edu.umd.cs.findbugs.annotations.Nullable; | ||
|
|
||
| import org.kohsuke.github.GHCheckRun; | ||
| import org.kohsuke.github.GHCheckRunBuilder; | ||
|
|
@@ -35,6 +36,9 @@ public class GitHubChecksPublisher extends ChecksPublisher { | |
| private final PluginLogger buildLogger; | ||
| private final String gitHubUrl; | ||
|
|
||
| @Nullable | ||
| private StandardUsernameCredentials credentials; | ||
|
|
||
| /** | ||
| * Creates a new instance of GitHubChecksPublisher. | ||
| * | ||
|
|
@@ -63,9 +67,8 @@ public GitHubChecksPublisher(final GitHubChecksContext context, final PluginLogg | |
| @Override | ||
| public void publish(final ChecksDetails details) { | ||
| try { | ||
| StandardUsernameCredentials credentials = context.getCredentials(); | ||
| // Prevent publication with unsupported credential types | ||
| switch (credentials.getClass().getSimpleName()) { | ||
| switch (getCredentials().getClass().getSimpleName()) { | ||
| case "GitHubAppCredentials": | ||
| case "VaultUsernamePasswordCredentialImpl": | ||
| break; | ||
|
|
@@ -74,12 +77,12 @@ public void publish(final ChecksDetails details) { | |
| } | ||
|
|
||
| String apiUri = null; | ||
| if (credentials instanceof GitHubAppCredentials) { | ||
| apiUri = ((GitHubAppCredentials) credentials).getApiUri(); | ||
| if (getCredentials() instanceof GitHubAppCredentials) { | ||
| apiUri = ((GitHubAppCredentials) getCredentials()).getApiUri(); | ||
| } | ||
|
|
||
| GitHub gitHub = Connector.connect(StringUtils.defaultIfBlank(apiUri, gitHubUrl), | ||
| credentials); | ||
| getCredentials()); | ||
|
|
||
| GitHubChecksDetails gitHubDetails = new GitHubChecksDetails(details); | ||
|
|
||
|
|
@@ -124,12 +127,27 @@ GHCheckRunBuilder getUpdater(final GitHub github, final GitHubChecksDetails deta | |
| @VisibleForTesting | ||
| GHCheckRunBuilder getCreator(final GitHub gitHub, final GitHubChecksDetails details) throws IOException { | ||
| GHCheckRunBuilder builder = gitHub.getRepository(context.getRepository()) | ||
| .createCheckRun(details.getName(), context.getHeadSha()) | ||
| .withStartedAt(details.getStartedAt().orElse(Date.from(Instant.now()))); | ||
| .createCheckRun(details.getName(), context.getHeadSha()) | ||
| .withStartedAt(details.getStartedAt().orElse(Date.from(Instant.now()))); | ||
|
|
||
| return applyDetails(builder, details); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| StandardUsernameCredentials getCredentials() { | ||
| if (credentials == null) { | ||
| credentials = context.getCredentials(); | ||
| if (credentials instanceof GitHubAppCredentials) { | ||
| final var gitHubAppCredentials = (GitHubAppCredentials) credentials; | ||
| if (context instanceof GitHubSCMSourceChecksContext) { | ||
| final var gitHubSCMSourceChecksContext = (GitHubSCMSourceChecksContext) context; | ||
| credentials = gitHubAppCredentials.withOwner(gitHubSCMSourceChecksContext.getOwner()); | ||
|
Member
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. FWIW, I think it is undesirable to have the
Member
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. Sounds fine if someone wants to work on it.
Contributor
Author
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'll create a follow-up PR ASAP. |
||
| } | ||
| } | ||
| } | ||
| return credentials; | ||
| } | ||
|
|
||
| private GHCheckRunBuilder applyDetails(final GHCheckRunBuilder builder, final GitHubChecksDetails details) { | ||
| builder | ||
| .withStatus(details.getStatus()) | ||
|
|
||
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.
credentialswhere moved from method variable to class field. This helpsGitHubChecksPublisherITestto check that owner is propagated from context to credentials.