-
Notifications
You must be signed in to change notification settings - Fork 384
Add GitHub branch and pull request info to the 'Details' widget #826
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
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
22269dd
Init
janfaracik 2972f60
Update pom.xml
janfaracik 70a2ec0
Push
janfaracik 6aab691
Update GitHubDetailFactory.java
janfaracik 1a83038
Simplify
janfaracik e00614a
Update GitHubBranchDetail.java
janfaracik d9e54f5
Update GitHubBranchDetail.java
janfaracik 84f30a8
Semi working
janfaracik c5ea7ac
Update GitHubRepositoryDetail.java
janfaracik bdc8c7c
Update GitHubRepositoryDetail.java
janfaracik 95a0202
Update GitHubPullRequestDetail.java
janfaracik 63816db
Rough null checks...
janfaracik 461177d
Spotless
janfaracik c87badb
Tidy up
janfaracik c755701
Update GitHubBranchDetail.java
janfaracik 8e25978
Fix a few things
janfaracik e66c0e3
Drop isApplicable
janfaracik 2a57ab6
Update GitHubDetailFactory.java
janfaracik dafc5c4
Update GitHubDetailFactory.java
janfaracik d90b1a9
Push
janfaracik 857feaa
Update pom.xml
janfaracik b2212c7
Merge branch 'master' into detail-widget
janfaracik 9f37620
Update GitHubRepositoryDetail.java
janfaracik 7f0e5bf
Merge branch 'master' into detail-widget
janfaracik f3f8e59
Push
janfaracik d01e501
Remove comments
janfaracik 2abc5b2
Update group
janfaracik ab18842
Merge branch 'master' into detail-widget
timja 24e27b7
Update pom.xml
timja 24ab3d1
Refine code, add tests
janfaracik d48c6a0
Spotless
janfaracik 1e546fd
Update GitHubRepositoryDetail.java
janfaracik 43592e6
Merge branch 'master' into detail-widget
janfaracik da8130c
Refine
janfaracik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubBranchDetail.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package org.jenkinsci.plugins.github_branch_source; | ||
|
|
||
| import edu.umd.cs.findbugs.annotations.Nullable; | ||
| import hudson.model.Actionable; | ||
| import hudson.model.Run; | ||
| import jenkins.model.details.Detail; | ||
| import jenkins.model.details.DetailGroup; | ||
| import jenkins.scm.api.SCMDetailGroup; | ||
| import jenkins.scm.api.metadata.ObjectMetadataAction; | ||
|
|
||
| public class GitHubBranchDetail extends Detail { | ||
| public GitHubBranchDetail(Actionable object) { | ||
| super(object); | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public String getIconClassName() { | ||
| return "symbol-git-branch-outline plugin-ionicons-api"; | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public String getDisplayName() { | ||
| return getObjectMetadataAction().getObjectDisplayName(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getLink() { | ||
| return getObjectMetadataAction().getObjectUrl(); | ||
| } | ||
|
|
||
| @Override | ||
| public DetailGroup getGroup() { | ||
| return SCMDetailGroup.get(); | ||
| } | ||
|
|
||
| private ObjectMetadataAction getObjectMetadataAction() { | ||
| Run<?, ?> run = (Run<?, ?>) getObject(); | ||
| return run.getParent().getAction(ObjectMetadataAction.class); | ||
| } | ||
| } | ||
75 changes: 75 additions & 0 deletions
75
src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubCommitDetail.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package org.jenkinsci.plugins.github_branch_source; | ||
|
|
||
| import hudson.model.Actionable; | ||
| import hudson.model.Run; | ||
| import jenkins.model.details.Detail; | ||
| import jenkins.model.details.DetailGroup; | ||
| import jenkins.plugins.git.AbstractGitSCMSource; | ||
| import jenkins.scm.api.SCMDetailGroup; | ||
| import jenkins.scm.api.SCMRevision; | ||
| import jenkins.scm.api.SCMRevisionAction; | ||
|
|
||
| public class GitHubCommitDetail extends Detail { | ||
| public GitHubCommitDetail(Actionable object) { | ||
| super(object); | ||
| } | ||
|
|
||
| public String getIconClassName() { | ||
| return "symbol-git-commit-outline plugin-ionicons-api"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| SCMRevision revision = getRevision(); | ||
|
|
||
| if (revision == null) { | ||
| return null; | ||
| } | ||
|
|
||
| if (revision instanceof AbstractGitSCMSource.SCMRevisionImpl abstractRevision) { | ||
timja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return abstractRevision.getHash().substring(0, 7); | ||
| } | ||
|
|
||
| if (revision instanceof PullRequestSCMRevision pullRequestSCMRevision) { | ||
| return pullRequestSCMRevision.getPullHash().substring(0, 7); | ||
timja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public String getLink() { | ||
| SCMRevision revision = getRevision(); | ||
|
|
||
| if (revision == null) { | ||
| return null; | ||
| } | ||
|
|
||
| if (revision instanceof AbstractGitSCMSource.SCMRevisionImpl abstractRevision) { | ||
| return new GitHubRepositoryDetail(getObject()).getLink() + "/commit/" + abstractRevision.getHash(); | ||
| } | ||
|
|
||
| if (revision instanceof PullRequestSCMRevision pullRequestSCMRevision) { | ||
| Run<?, ?> run = (Run<?, ?>) getObject(); | ||
| GitHubLink repoLink = run.getParent().getAction(GitHubLink.class); | ||
| return repoLink.getUrl() + "/commits/" + pullRequestSCMRevision.getPullHash(); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public DetailGroup getGroup() { | ||
| return SCMDetailGroup.get(); | ||
| } | ||
|
|
||
| private SCMRevision getRevision() { | ||
| SCMRevisionAction scmRevisionAction = getObject().getAction(SCMRevisionAction.class); | ||
|
|
||
| if (scmRevisionAction == null) { | ||
| return null; | ||
| } | ||
|
|
||
| return scmRevisionAction.getRevision(); | ||
| } | ||
| } | ||
53 changes: 53 additions & 0 deletions
53
src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubDetailFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package org.jenkinsci.plugins.github_branch_source; | ||
|
|
||
| import edu.umd.cs.findbugs.annotations.NonNull; | ||
| import hudson.Extension; | ||
| import hudson.model.Run; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import jenkins.model.details.Detail; | ||
| import jenkins.model.details.DetailFactory; | ||
| import jenkins.scm.api.SCMRevision; | ||
| import jenkins.scm.api.SCMRevisionAction; | ||
| import jenkins.scm.api.SCMSource; | ||
|
|
||
| @Extension | ||
| public final class GitHubDetailFactory extends DetailFactory<Run> { | ||
|
|
||
| @Override | ||
| public Class<Run> type() { | ||
| return Run.class; | ||
| } | ||
|
|
||
| @NonNull | ||
| @Override | ||
| public List<? extends Detail> createFor(@NonNull Run target) { | ||
| SCMSource src = SCMSource.SourceByItem.findSource(target.getParent()); | ||
|
|
||
| // Don't add details for non-GitHub SCM sources | ||
| if (!(src instanceof GitHubSCMSource)) { | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| SCMRevisionAction scmRevisionAction = target.getAction(SCMRevisionAction.class); | ||
|
|
||
| if (scmRevisionAction == null) { | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| List<Detail> details = new ArrayList<>(); | ||
| SCMRevision revision = scmRevisionAction.getRevision(); | ||
|
|
||
| if (revision instanceof PullRequestSCMRevision) { | ||
| details.add(new GitHubPullRequestDetail(target)); | ||
| } else { | ||
| details.add(new GitHubBranchDetail(target)); | ||
| } | ||
|
|
||
| details.add(new GitHubCommitDetail(target)); | ||
| details.add(new GitHubRepositoryDetail(target)); | ||
|
|
||
| return details; | ||
| } | ||
| } | ||
42 changes: 42 additions & 0 deletions
42
src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubPullRequestDetail.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package org.jenkinsci.plugins.github_branch_source; | ||
|
|
||
| import edu.umd.cs.findbugs.annotations.Nullable; | ||
| import hudson.model.Actionable; | ||
| import hudson.model.Run; | ||
| import jenkins.model.details.Detail; | ||
| import jenkins.model.details.DetailGroup; | ||
| import jenkins.scm.api.SCMDetailGroup; | ||
| import jenkins.scm.api.metadata.ObjectMetadataAction; | ||
|
|
||
| public class GitHubPullRequestDetail extends Detail { | ||
| public GitHubPullRequestDetail(Actionable object) { | ||
| super(object); | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public String getIconClassName() { | ||
| return "symbol-git-pull-request-outline plugin-ionicons-api"; | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public String getDisplayName() { | ||
| return getObjectMetadataAction().getObjectDisplayName(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getLink() { | ||
| return getObjectMetadataAction().getObjectUrl(); | ||
| } | ||
|
|
||
| @Override | ||
| public DetailGroup getGroup() { | ||
| return SCMDetailGroup.get(); | ||
| } | ||
|
|
||
| private ObjectMetadataAction getObjectMetadataAction() { | ||
| Run<?, ?> run = (Run<?, ?>) getObject(); | ||
| return run.getParent().getAction(ObjectMetadataAction.class); | ||
| } | ||
| } | ||
59 changes: 59 additions & 0 deletions
59
src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubRepositoryDetail.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package org.jenkinsci.plugins.github_branch_source; | ||
|
|
||
| import edu.umd.cs.findbugs.annotations.Nullable; | ||
| import hudson.model.Actionable; | ||
| import hudson.model.Run; | ||
| import jenkins.model.details.Detail; | ||
| import jenkins.model.details.DetailGroup; | ||
| import jenkins.scm.api.SCMDetailGroup; | ||
| import jenkins.scm.api.SCMSource; | ||
|
|
||
| public class GitHubRepositoryDetail extends Detail { | ||
janfaracik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public GitHubRepositoryDetail(Actionable object) { | ||
| super(object); | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public String getIconClassName() { | ||
| return "symbol-logo-github plugin-ionicons-api"; | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public String getDisplayName() { | ||
| GitHubSCMSource source = getSCMSource(); | ||
|
|
||
| if (source == null) { | ||
| return null; | ||
| } | ||
|
|
||
| return source.getRepoOwner() + "/" + source.getRepository(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getLink() { | ||
| GitHubSCMSource source = getSCMSource(); | ||
|
|
||
| if (source == null) { | ||
| return null; | ||
| } | ||
|
|
||
| return source.getRepositoryUrl(); | ||
| } | ||
|
|
||
| @Override | ||
| public DetailGroup getGroup() { | ||
| return SCMDetailGroup.get(); | ||
| } | ||
|
|
||
| private GitHubSCMSource getSCMSource() { | ||
| var source = SCMSource.SourceByItem.findSource(((Run) getObject()).getParent()); | ||
|
|
||
| if (source instanceof GitHubSCMSource githubSource) { | ||
| return githubSource; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
28 changes: 28 additions & 0 deletions
28
src/test/java/org/jenkinsci/plugins/github_branch_source/GitHubBranchDetailTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package org.jenkinsci.plugins.github_branch_source; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| import hudson.model.Job; | ||
| import hudson.model.Run; | ||
| import jenkins.scm.api.metadata.ObjectMetadataAction; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class GitHubBranchDetailTest { | ||
|
|
||
| @Test | ||
| void displaysAsExpected() { | ||
| Run run = mock(Run.class); | ||
| Job job = mock(Job.class); | ||
| when(run.getParent()).thenReturn(job); | ||
| ObjectMetadataAction metadata = mock(ObjectMetadataAction.class); | ||
| when(metadata.getObjectDisplayName()).thenReturn("main"); | ||
| when(metadata.getObjectUrl()).thenReturn("https://github.com/octo/hello-world/tree/main"); | ||
| when(job.getAction(ObjectMetadataAction.class)).thenReturn(metadata); | ||
|
|
||
| GitHubBranchDetail detail = new GitHubBranchDetail(run); | ||
|
|
||
| assertEquals("main", detail.getDisplayName()); | ||
| assertEquals("https://github.com/octo/hello-world/tree/main", detail.getLink()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.