Skip to content

Commit

Permalink
Use TransientActionFactory instead of `GithubProjectProperty.getJob…
Browse files Browse the repository at this point in the history
…Actions`.

This makes the "GitHub" link also show up for Workflow projects.
  • Loading branch information
arthurschreiber committed Jan 14, 2016
1 parent 2b94ad7 commit fd19ca8
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package com.coravy.hudson.plugins.github;

import java.util.Collection;
import java.util.Collections;

import hudson.Extension;
import hudson.model.Action;
import hudson.model.Job;
import jenkins.model.TransientActionFactory;

/**
* Add the Github Logo/Icon to the sidebar.
Expand Down Expand Up @@ -30,4 +36,23 @@ public String getUrlName() {
return projectProperty.getProjectUrl().baseUrl();
}

@SuppressWarnings("rawtypes")
@Extension
public static class GithubLinkActionFactory extends TransientActionFactory<Job> {
@Override
public Class<Job> type() {
return Job.class;
}

@Override
public Collection<? extends Action> createFor(Job j) {
GithubProjectProperty prop = ((Job<?, ?>) j).getProperty(GithubProjectProperty.class);

if (prop == null) {
return Collections.emptySet();
} else {
return Collections.singleton(new GithubLinkAction(prop));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.cloudbees.jenkins.GitHubPushTrigger;
import hudson.Extension;
import hudson.model.Action;
import hudson.model.Job;
import hudson.model.JobProperty;
import hudson.model.JobPropertyDescriptor;
Expand All @@ -14,8 +13,6 @@

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.Collections;
import java.util.logging.Logger;

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

@Override
public Collection<? extends Action> getJobActions(Job<?, ?> job) {
if (null != projectUrl) {
return Collections.singleton(new GithubLinkAction(this));
}
return Collections.emptyList();
}

/**
* Extracts value of display name from given job, or just returns full name if field or prop is not defined
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.coravy.hudson.plugins.github;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.empty;
import static org.junit.Assert.assertThat;

import java.io.IOException;
import java.util.Collection;

import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import com.coravy.hudson.plugins.github.GithubLinkAction.GithubLinkActionFactory;

import hudson.model.Action;

public class GithubLinkActionFactoryTest {
@Rule
public JenkinsRule j = new JenkinsRule();

public GithubLinkActionFactory factory = new GithubLinkActionFactory();

@Test
public void shouldCreateGithubLinkActionForJobWithGithubProjectProperty() throws IOException {
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.addProperty(new GithubProjectProperty("https://github.com/jenkinsci/github-plugin/"));

Collection<? extends Action> actions = factory.createFor(p);
assertThat(actions.size(), is(equalTo(1)));

GithubLinkAction action = (GithubLinkAction) actions.iterator().next();
assertThat(action.getUrlName(), is(equalTo("https://github.com/jenkinsci/github-plugin/")));
}

@Test
public void shouldNotCreateGithubLinkActionForJobWithoutGithubProjectProperty() throws IOException {
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");

Collection<? extends Action> actions = factory.createFor(p);
assertThat(actions, is(empty()));
}
}

0 comments on commit fd19ca8

Please sign in to comment.