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

Skip to content

Commit d6a2a70

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

File tree

3 files changed

+76
-11
lines changed

3 files changed

+76
-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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 JenkinsRule rule = new JenkinsRule();
23+
24+
private GithubLinkActionFactory factory = new GithubLinkActionFactory();
25+
26+
private WorkflowJob createExampleJob() throws IOException {
27+
return rule.jenkins.createProject(WorkflowJob.class, "example");
28+
}
29+
30+
@Test
31+
public void shouldCreateGithubLinkActionForJobWithGithubProjectProperty() throws IOException {
32+
WorkflowJob job = createExampleJob();
33+
String url = "https://github.com/jenkinsci/github-plugin/";
34+
job.addProperty(new GithubProjectProperty(url));
35+
36+
Collection<? extends Action> actions = factory.createFor(job);
37+
assertThat("Only one Action should have been created", actions.size(), is(1));
38+
39+
Action action = actions.iterator().next();
40+
assertThat("Created Action should be instance of GithubLinkAction", action, is(instanceOf(GithubLinkAction.class)));
41+
assertThat("Action URL should equal url set in GithubProjectProperty", action.getUrlName(), is(url));
42+
}
43+
44+
@Test
45+
public void shouldNotCreateGithubLinkActionForJobWithoutGithubProjectProperty() throws IOException {
46+
WorkflowJob job = createExampleJob();
47+
48+
Collection<? extends Action> actions = factory.createFor(job);
49+
assertThat("No Action should have been created", actions, is(empty()));
50+
}
51+
}

0 commit comments

Comments
 (0)
0