8000 Use `TransientActionFactory` instead of `GithubProjectProperty.getJob… · github-cloud/github-plugin@054719a · GitHub
[go: up one dir, main page]

Skip to content

Commit 054719a

Browse files
Use TransientActionFactory instead of GithubProjectProperty.getJobActions.
This makes the "GitHub" link also show up for Workflow projects.
1 parent 2b94ad7 commit 054719a

File tree

3 files changed

+82
-11
lines changed

3 files changed

+82
-11
lines changed

src/main/java/com/coravy/hudson/plugins/github/GithubLinkAction.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package com.coravy.hudson.plugins.github;
22

3+
import java.util.Collection;
4+
import java.util.Collections;
5+
6+
import hudson.Extension;
37
import hudson.model.Action;
8+
import hudson.model.Job;
9+
import jenkins.model.TransientActionFactory;
410

511
/**
612
* Add the Github Logo/Icon to the sidebar.
@@ -30,4 +36,23 @@ public String getUrlName() {
3036
return projectProperty.getProjectUrl().baseUrl();
3137
}
3238

39+
@SuppressWarnings("rawtypes")
40+
@Extension
41+
public static class GithubLinkActionFactory extends TransientActionFactory<Job> {
42+
@Override
43+
public Class<Job> type() {
44+
return Job.class;
45+
}
46+
47+
@Override
48+
public Collection<? extends Action> createFor(Job j) {
49+
GithubProjectProperty prop = ((Job<?, ?>) j).getProperty(GithubProjectProperty.class);
50+
51+
if (prop == null) {
52+
return Collections.emptySet();
53+
} else {
54+
return Collections.singleton(new GithubLinkAction(prop));
55+
}
56+
}
57+
}
3358
}

src/main/java/com/coravy/hudson/plugins/github/GithubProjectProperty.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.cloudbees.jenkins.GitHubPushTrigger;
44
import hudson.Extension;
5-
import hudson.model.Action;
65
import hudson.model.Job;
76
import hudson.model.JobProperty;
87
import hudson.model.JobPropertyDescriptor;
@@ -14,8 +13,6 @@
1413

1514
import javax.annotation.CheckForNull;
1615
import javax.annotation.Nonnull;
17-
import java.util.Collection;
18-
import java.util.Collections;
1916
import java.util.logging.Logger;
2017

2118
import static org.apache.commons.lang3.StringUtils.isNotBlank;
@@ -83,14 +80,6 @@ public void setDisplayName(String displayName) {
8380
this.displayName = displayName;
8481
}
8582

86-
@Override
87-
public Collection<? extends Action> getJobActions(Job<?, ?> job) {
88-
if (null != projectUrl) {
89-
return Collections.singleton(new GithubLinkAction(this));
90-
}
91-
return Collections.emptyList();
92-
}
93-
9483
/**
9584
* Extracts value of display name from given job, or just returns full name if field or prop is not defined
9685
*
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.coravy.hudson.plugins.github;
2+
3+
import static org.hamcrest.Matchers.is;
4+
import static org.hamcrest.Matchers.instanceOf;
5+
import static org.hamcrest.Matchers.empty;
6+
import static org.junit.Assert.assertThat;
7+
8+
import java.io.IOException;
9+
import java.util.Collection;
10+
11+
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
12+
import org.junit.Rule;
13+
import org.junit.Test;
14+
import org.jvnet.hudson.test.JenkinsRule;
15+
16+
import com.coravy.hudson.plugins.github.GithubLinkAction.GithubLinkActionFactory;
17+
18+
import hudson.model.Action;
19+
20+
public class GithubLinkActionFactoryTest {
21+
@Rule
22+
public final JenkinsRule rule = new JenkinsRule();
23+
24+
private final GithubLinkActionFactory factory = new GithubLinkActionFactory();
25+
26+
private static final String PROJECT_URL = "https://github.com/jenkinsci/github-plugin/";
27+
28+
private WorkflowJob createExampleJob() throws IOException {
29+
return rule.getInstance().createProject(WorkflowJob.class, "example");
30+
}
31+
32+
private GithubProjectProperty createExampleProperty() {
33+
return new GithubProjectProperty(PROJECT_URL);
34+
}
35+
36+
@Test
37+
public void shouldCreateGithubLinkActionForJobWithGithubProjectProperty() throws IOException {
38+
final WorkflowJob job = createExampleJob();
39+
final GithubProjectProperty property = createExampleProperty();
40+
job.addProperty(property);
41+
42+
final Collection<? extends Action> actions = factory.createFor(job);
43+
assertThat("factored actions list", actions.size(), is(1));
44+
45+
final Action action = actions.iterator().next();
46+
assertThat("instance check", action, is(instanceOf(GithubLinkAction.class)));
47+
assertThat("url of action", action.getUrlName(), is(property.getProjectUrlStr()));
48+
}
49+
50+
@Test
51+
public void shouldNotCreateGithubLinkActionForJobWithoutGithubProjectProperty() throws IOException {
52+
final WorkflowJob job = createExampleJob();
53+
54+
final Collection<? extends Action> actions = factory.createFor(job);
55+
assertThat("factored actions list", actions, is(empty()));
56+
}
57+
}

0 commit comments

Comments
 (0)
0