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

Support WorkflowJob (Pipeline) jobs #149

Merged
merged 3 commits into from
Feb 17, 2017
Merged
Changes from 1 commit
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
47 changes: 40 additions & 7 deletions tfs/src/main/java/hudson/plugins/tfs/TeamBuildEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import hudson.Extension;
import hudson.model.BuildableItem;
import hudson.model.BuildAuthorizationToken;
import hudson.model.BuildableItem;
import hudson.model.Item;
import hudson.model.Job;
import hudson.model.UnprotectedRootAction;
import hudson.plugins.tfs.model.AbstractCommand;
Expand Down Expand Up @@ -33,6 +34,7 @@
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
Expand All @@ -54,6 +56,7 @@ public class TeamBuildEndpoint implements UnprotectedRootAction {
private static final Map<String, AbstractCommand.Factory> COMMAND_FACTORIES_BY_NAME;
public static final String URL_NAME = "team-build";
public static final String PARAMETER = "parameter";
public static final String BUILD_SOURCE_BRANCH = "Build.SourceBranch";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know if Build.SourceBranch is available in VSTS Release Management scenarios? If not, can we handle its absence gracefully?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great call. Made changes to get the branch from:

  1. jobName input.
  2. Build.SourceBranch variable

Throw if none of them are available.

static final String URL_PREFIX = "/" + URL_NAME + "/";

static {
Expand Down Expand Up @@ -187,6 +190,41 @@ void dispatch(final StaplerRequest req, final StaplerResponse rsp, final TimeDur
}
}

private String getBranch(final StaplerRequest req) {
final String json = req.getParameter("json");
final JSONObject formData = JSONObject.fromObject(json);
final TeamBuildPayload payload = EndpointHelper.MAPPER.convertValue(formData, TeamBuildPayload.class);

final String sourceBranch = payload.BuildVariables.get(BUILD_SOURCE_BRANCH);

return sourceBranch.replace("refs/heads/", "");
}

private Job getJob(final String jobName, final StaplerRequest req) {
final Jenkins jenkins = Jenkins.getInstance();

Job job = jenkins.getItemByFullName(jobName, Job.class);

if (job == null) {
final Item item = jenkins.getItemByFullName(jobName);
final Collection<? extends Job> allJobs = item.getAllJobs();
final String sourceBranch = getBranch(req);

for (final Job j : allJobs) {
if (j.getName().equals(sourceBranch)) {
job = j;
break;
}
}
}

if (job == null) {
throw new IllegalArgumentException("Job not found");
}

return job;
}

private JSONObject innerDispatch(final StaplerRequest req, final StaplerResponse rsp, final TimeDuration delay) throws IOException, ServletException {
commandName = null;
jobName = null;
Expand All @@ -204,12 +242,7 @@ private JSONObject innerDispatch(final StaplerRequest req, final StaplerResponse
throw new IllegalArgumentException("Command not implemented");
}

final Jenkins jenkins = Jenkins.getInstance();

final Job job = jenkins.getItemByFullName(jobName, Job.class);
if (job == null) {
throw new IllegalArgumentException("Job not found");
}
final Job job = getJob(jobName, req);

final ParameterizedJobMixIn.ParameterizedJob jobMixin = (ParameterizedJobMixIn.ParameterizedJob) job;

Expand Down