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

Add GitHub branch and pull request info to the 'Details' widget #826

Draft
wants to merge 20 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.88</version>
<version>5.5</version>
<relativePath />
</parent>

Expand Down Expand Up @@ -33,7 +33,9 @@
<changelist>999999-SNAPSHOT</changelist>
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
<hpi.compatibleSinceVersion>2.2.0</hpi.compatibleSinceVersion>
<jenkins.version>2.440.3</jenkins.version>
<!-- https://www.jenkins.io/doc/developer/plugin-development/choosing-jenkins-baseline/ -->
<jenkins.baseline>2.479</jenkins.baseline>
<jenkins.version>2.494-rc35907.d09ffe974e75</jenkins.version>
<useBeta>true</useBeta>
<spotless.check.skip>false</spotless.check.skip>
</properties>
Expand All @@ -42,8 +44,8 @@
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.440.x</artifactId>
<version>3234.v5ca_5154341ef</version>
<artifactId>bom-${jenkins.baseline}.x</artifactId>
<version>3893.v213a_42768d35</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package org.jenkinsci.plugins.github_branch_source;

import hudson.Util;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import jenkins.model.Jenkins;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.StaplerRequest2;
import org.kohsuke.stapler.StaplerResponse2;

// TODO replace with corresponding core functionality once Jenkins core has JENKINS-42443

Check warning on line 11 in src/main/java/org/jenkinsci/plugins/github_branch_source/FillErrorResponse.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: replace with corresponding core functionality once Jenkins core has JENKINS-42443
class FillErrorResponse extends IOException implements HttpResponse {

private final boolean clearList;
Expand All @@ -20,8 +19,7 @@
}

@Override
public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object node)
throws IOException, ServletException {
public void generateResponse(StaplerRequest2 req, StaplerResponse2 rsp, Object node) throws IOException {
rsp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
rsp.setContentType("text/html;charset=UTF-8");
rsp.setHeader("X-Jenkins-Select-Error", clearList ? "clear" : "retain");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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.SCMHead;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.SCMRevisionAction;
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() {
SCMRevisionAction scmRevisionAction = getObject().getAction(SCMRevisionAction.class);
SCMRevision revision = scmRevisionAction.getRevision();

if (revision instanceof PullRequestSCMRevision pullRequestSCMRevision) {
PullRequestSCMHead head = (PullRequestSCMHead) pullRequestSCMRevision.getHead();
return head.getSourceBranch();
}

SCMHead head = revision.getHead();
return head.getName();
}

@Override
public String getLink() {
var run = (Run<?, ?>) getObject();
ObjectMetadataAction action = run.getParent().getAction(ObjectMetadataAction.class);
return action.getObjectUrl();
}

@Override
public DetailGroup getGroup() {
return ScmDetailGroup.get();

Check warning on line 48 in src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubBranchDetail.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 15-48 are not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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.SCMRevision;
import jenkins.scm.api.SCMRevisionAction;
import jenkins.scm.api.SCMSource;

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() {
SCMRevisionAction scmRevisionAction = getObject().getAction(SCMRevisionAction.class);
SCMRevision revision = scmRevisionAction.getRevision();

if (revision instanceof AbstractGitSCMSource.SCMRevisionImpl abstractRevision) {
return abstractRevision.getHash().substring(0, 7);
} else if (revision instanceof PullRequestSCMRevision pullRequestSCMRevision) {
return pullRequestSCMRevision.getPullHash().substring(0, 7);
}

return null;
}

@Override
public String getLink() {
SCMRevisionAction scmRevisionAction = getObject().getAction(SCMRevisionAction.class);
SCMRevision revision = scmRevisionAction.getRevision();

if (revision instanceof AbstractGitSCMSource.SCMRevisionImpl abstractRevision) {
GitHubSCMSource src = (GitHubSCMSource) SCMSource.SourceByItem.findSource(((Run) getObject()).getParent());

if (src == null) {
return null;
}

// TODO - ends with .git

Check warning on line 47 in src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubCommitDetail.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: - ends with .git
return src.getRepositoryUrl() + "/commit/" + abstractRevision.getHash();
} else if (revision instanceof PullRequestSCMRevision pullRequestSCMRevision) {
var 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();

Check warning on line 60 in src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubCommitDetail.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 14-60 are not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerRequest2;

@Extension
public class GitHubConfiguration extends GlobalConfiguration {
Expand All @@ -60,7 +60,7 @@ public GitHubConfiguration() {
}

@Override
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
public boolean configure(StaplerRequest2 req, JSONObject json) throws FormException {
req.bindJSON(this, json);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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;

@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) {
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;

Check warning on line 43 in src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubDetailFactory.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 15-43 are not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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.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() {
var run = (Run<?, ?>) getObject();
ObjectMetadataAction action = run.getParent().getAction(ObjectMetadataAction.class);
return action.getObjectDisplayName();
}

@Override
public String getLink() {
var run = (Run<?, ?>) getObject();
GitHubLink repoLink = run.getParent().getAction(GitHubLink.class);
return repoLink.getUrl();
}

@Override
public DetailGroup getGroup() {
return ScmDetailGroup.get();

Check warning on line 38 in src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubPullRequestDetail.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 12-38 are not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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.SCMSource;

public class GitHubRepositoryDetail extends Detail {
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 = (GitHubSCMSource) SCMSource.SourceByItem.findSource(((Run) getObject()).getParent());

if (source == null) {
return null;
}

return source.getRepoOwner() + "/" + source.getRepository();
}

@Override
public String getLink() {
GitHubSCMSource source = (GitHubSCMSource) SCMSource.SourceByItem.findSource(((Run) getObject()).getParent());

if (source == null) {
return null;
}

// TODO - Has .git on the end

Check warning on line 41 in src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubRepositoryDetail.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: - Has .git on the end
return source.getRepositoryUrl();
}

@Override
public DetailGroup getGroup() {
return ScmDetailGroup.get();

Check warning on line 47 in src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubRepositoryDetail.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 12-47 are not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import hudson.util.LogTaskListener;
import jakarta.servlet.http.HttpServletResponse;
import java.io.Closeable;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand Down Expand Up @@ -85,7 +86,6 @@
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletResponse;
import jenkins.model.Jenkins;
import jenkins.plugins.git.AbstractGitSCMSource;
import jenkins.plugins.git.GitTagSCMRevision;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.jenkinsci.plugins.github_branch_source;

import hudson.Extension;
import hudson.ExtensionList;
import jenkins.model.details.DetailGroup;

// TODO - Should be moved to parent plugin

Check warning on line 7 in src/main/java/org/jenkinsci/plugins/github_branch_source/ScmDetailGroup.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: - Should be moved to parent plugin
@Extension(ordinal = -1)
public class ScmDetailGroup extends DetailGroup {

public static ScmDetailGroup get() {
return ExtensionList.lookupSingleton(ScmDetailGroup.class);

Check warning on line 12 in src/main/java/org/jenkinsci/plugins/github_branch_source/ScmDetailGroup.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 9-12 are not covered by tests
}
}
Loading
Loading