Skip to content

Commit

Permalink
Merge pull request #105 from arthurschreiber/arthur/extend-workflow-s…
Browse files Browse the repository at this point in the history
…upport

Show the "GitHub" link for Workflow projects
  • Loading branch information
lanwen committed Jan 14, 2016
2 parents 2b94ad7 + 054719a commit ad03690
Show file tree
Hide file tree
Showing 3 changed files with 82 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,57 @@
package com.coravy.hudson.plugins.github;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.instanceOf;
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 final JenkinsRule rule = new JenkinsRule();

private final GithubLinkActionFactory factory = new GithubLinkActionFactory();

private static final String PROJECT_URL = "https://github.com/jenkinsci/github-plugin/";

private WorkflowJob createExampleJob() throws IOException {
return rule.getInstance().createProject(WorkflowJob.class, "example");
}

private GithubProjectProperty createExampleProperty() {
return new GithubProjectProperty(PROJECT_URL);
}

@Test
public void shouldCreateGithubLinkActionForJobWithGithubProjectProperty() throws IOException {
final WorkflowJob job = createExampleJob();
final GithubProjectProperty property = createExampleProperty();
job.addProperty(property);

final Collection<? extends Action> actions = factory.createFor(job);
assertThat("factored actions list", actions.size(), is(1));

final Action action = actions.iterator().next();
assertThat("instance check", action, is(instanceOf(GithubLinkAction.class)));
assertThat("url of action", action.getUrlName(), is(property.getProjectUrlStr()));
}

@Test
public void shouldNotCreateGithubLinkActionForJobWithoutGithubProjectProperty() throws IOException {
final WorkflowJob job = createExampleJob();

final Collection<? extends Action> actions = factory.createFor(job);
assertThat("factored actions list", actions, is(empty()));
}
}

0 comments on commit ad03690

Please sign in to comment.