All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.marvelution.jira.plugins.jenkins.rest.BuildResource Maven / Gradle / Ivy

There is a newer version: 2.3.1
Show newest version
/*
 * Copyright (c) 2012-present Marvelution B.V.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.marvelution.jira.plugins.jenkins.rest;

import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import org.marvelution.jira.plugins.jenkins.model.Build;
import org.marvelution.jira.plugins.jenkins.model.Job;
import org.marvelution.jira.plugins.jenkins.rest.exception.NotFoundException;
import org.marvelution.jira.plugins.jenkins.services.BuildService;
import org.marvelution.jira.plugins.jenkins.services.JenkinsPluginConfiguration;
import org.marvelution.jira.plugins.jenkins.services.JobService;

import com.atlassian.plugins.rest.common.security.AnonymousAllowed;

/**
 * REST resource for the Jenkins plugin, with endpoints to trigger the synchronization of new builds and get issue link information.
 *
 * @author Mark Rekveld
 * @since 1.6.0
 */
@AnonymousAllowed
@Path("build")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class BuildResource {

	private final JenkinsPluginConfiguration pluginConfiguration;
	private final JobService jobService;
	private final BuildService buildService;

	public BuildResource(JenkinsPluginConfiguration pluginConfiguration, JobService jobService, BuildService buildService) {
		this.pluginConfiguration = pluginConfiguration;
		this.jobService = jobService;
		this.buildService = buildService;
	}

	/**
	 * Trigger the synchronization of a specific Job
	 *
	 * @param jobId the Id of the Job to synchronize
	 */
	@PUT
	@Path("{jobId}")
	public void syncJob(@PathParam("jobId") int jobId) {
		Job job = jobService.get(jobId);
		if (job != null) {
			jobService.sync(jobId);
		} else {
			throw new NotFoundException();
		}
	}

	/**
	 * Returns a list of issues linked to the specific build
	 *
	 * @param jobId       the Id of the Job
	 * @param buildNumber the build number of the job
	 * @return 200 OK with a map of issue keys to issue urls, 404 NOT FOUND in case either the job of build is not found
	 */
	@GET
	@Path("{jobId}/{buildNumber}/links")
	public Map getBuildLinks(@PathParam("jobId") int jobId, @PathParam("buildNumber") int buildNumber) {
		Job job = jobService.get(jobId);
		if (job != null) {
			Build build = buildService.get(job, buildNumber);
			if (build != null) {
				Map links = new HashMap<>();
				for (String issueKey : buildService.getRelatedIssueKeys(build)) {
					links.put(issueKey, UriBuilder.fromUri(pluginConfiguration.getJIRABaseUrl()).path("browse").path(issueKey).build()
					                              .toASCIIString());
				}
				return links;
			}
		}
		throw new NotFoundException();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy