Skip to content

Commit

Permalink
Revert "spotless apply"
Browse files Browse the repository at this point in the history
This reverts commit 62b16fc.
  • Loading branch information
Cjpilbdev committed Jul 1, 2024
1 parent f68c6db commit 8c2f59f
Show file tree
Hide file tree
Showing 34 changed files with 1,106 additions and 1,584 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Copyright (c) 2016-2024 Pinterest, Inc.
*
* Copyright 2016 Pinterest, Inc.
* <p>
* 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
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* 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.
Expand All @@ -16,42 +16,45 @@
package com.pinterest.teletraan.resource;

import com.google.common.base.Optional;
import com.pinterest.deployservice.allowlists.Allowlist;
import com.pinterest.deployservice.bean.*;
import com.pinterest.deployservice.buildtags.BuildTagsManager;
import com.pinterest.deployservice.buildtags.BuildTagsManagerImpl;
import com.pinterest.deployservice.common.CommonUtils;
import com.pinterest.deployservice.dao.BuildDAO;
import com.pinterest.deployservice.dao.DeployDAO;
import com.pinterest.deployservice.dao.TagDAO;
import com.pinterest.deployservice.events.BuildEventPublisher;
import com.pinterest.deployservice.scm.SourceControlManagerProxy;
import com.pinterest.deployservice.allowlists.Allowlist;
import com.pinterest.teletraan.TeletraanServiceContext;
import com.pinterest.deployservice.events.BuildEventPublisher;
import com.pinterest.teletraan.universal.security.ResourceAuthZInfo;
import com.pinterest.teletraan.universal.security.bean.AuthZResource;

import io.swagger.annotations.*;
import java.net.URI;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.security.RolesAllowed;
import javax.validation.Valid;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URI;
import java.util.List;

@RolesAllowed(TeletraanPrincipalRole.Names.READ)
@Path("/v1/builds")
@Api(tags = "Builds")
@SwaggerDefinition(
tags = {
@Tag(name = "Builds", description = "BUILD information APIs"),
})
@Tag(name = "Builds", description = "BUILD information APIs"),
}
)
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class Builds {
private static final Logger LOG = LoggerFactory.getLogger(Builds.class);
private static final int DEFAULT_SIZE = 100;
private final static int DEFAULT_SIZE = 100;
private final BuildDAO buildDAO;
private final DeployDAO deployDAO;
private final TagDAO tagDAO;
Expand All @@ -70,11 +73,8 @@ public Builds(@Context TeletraanServiceContext context) {

@GET
@Path("/names")
public List<String> getBuildNames(
@QueryParam("filter") Optional<String> nameFilter,
@QueryParam("start") Optional<Integer> start,
@QueryParam("size") Optional<Integer> size)
throws Exception {
public List<String> getBuildNames(@QueryParam("filter") Optional<String> nameFilter,
@QueryParam("start") Optional<Integer> start, @QueryParam("size") Optional<Integer> size) throws Exception {
return buildDAO.getBuildNames(nameFilter.orNull(), start.or(1), size.or(DEFAULT_SIZE));
}

Expand All @@ -83,11 +83,9 @@ public List<String> getBuildNames(
@ApiOperation(
value = "Get branches",
notes = "Returns a list of the repository branches associated with a given build name",
response = String.class,
responseContainer = "List")
response = String.class, responseContainer = "List")
public List<String> getBranches(
@ApiParam(value = "BUILD name", required = true) @PathParam("name") String buildName)
throws Exception {
@ApiParam(value = "BUILD name", required = true)@PathParam("name") String buildName) throws Exception {
return buildDAO.getBranches(buildName);
}

Expand All @@ -97,29 +95,26 @@ public List<String> getBranches(
value = "Get build info",
notes = "Returns a build object given a build id",
response = BuildBean.class)
public BuildBean get(@ApiParam(value = "BUILD id", required = true) @PathParam("id") String id)
throws Exception {
public BuildBean get(
@ApiParam(value = "BUILD id", required = true)@PathParam("id") String id) throws Exception {
BuildBean buildBean = buildDAO.getById(id);
if (buildBean == null) {
throw new WebApplicationException(
String.format("BUILD %s does not exist.", id), Response.Status.NOT_FOUND);
throw new WebApplicationException(String.format("BUILD %s does not exist.", id), Response.Status.NOT_FOUND);
}
return buildBean;
}

@GET
@Path("/{id : [a-zA-Z0-9\\-_]+}/tags")
@ApiOperation(
value = "Get build info with its tags",
notes = "Returns a build object given a build id",
response = BuildTagBean.class)
value = "Get build info with its tags",
notes = "Returns a build object given a build id",
response = BuildTagBean.class)
public BuildTagBean getWithTag(
@ApiParam(value = "BUILD id", required = true) @PathParam("id") String id)
throws Exception {
@ApiParam(value = "BUILD id", required = true)@PathParam("id") String id) throws Exception {
BuildBean buildBean = buildDAO.getById(id);
if (buildBean == null) {
throw new WebApplicationException(
String.format("BUILD %s does not exist.", id), Response.Status.NOT_FOUND);
throw new WebApplicationException(String.format("BUILD %s does not exist.", id), Response.Status.NOT_FOUND);
}

BuildTagsManager manager = new BuildTagsManagerImpl(this.tagDAO);
Expand All @@ -128,19 +123,15 @@ public BuildTagBean getWithTag(
}

@GET
public List<BuildBean> get(
@QueryParam("commit") String scmCommit,
@QueryParam("name") String buildName,
@QueryParam("branch") String scmBranch,
@QueryParam("pageIndex") Optional<Integer> pageIndex,
@QueryParam("pageSize") Optional<Integer> pageSize,
@QueryParam("before") Long before,
@QueryParam("after") Long after)
throws Exception {
public List<BuildBean> get(@QueryParam("commit") String scmCommit,
@QueryParam("name") String buildName, @QueryParam("branch") String scmBranch,
@QueryParam("pageIndex") Optional<Integer> pageIndex,
@QueryParam("pageSize") Optional<Integer> pageSize, @QueryParam("before") Long before,
@QueryParam("after") Long after) throws Exception {


if (StringUtils.isEmpty(scmCommit) && StringUtils.isEmpty(buildName)) {
throw new WebApplicationException(
"Require either commit id or build name in the request.",
throw new WebApplicationException("Require either commit id or build name in the request.",
Response.Status.BAD_REQUEST);
}

Expand All @@ -149,39 +140,33 @@ public List<BuildBean> get(

@GET
@Path("/current")
public List<BuildBean> getCurrentBuildsWithGroupName(@QueryParam("group") String groupName)
throws Exception {
public List<BuildBean> getCurrentBuildsWithGroupName(@QueryParam("group") String groupName) throws Exception {
if (StringUtils.isEmpty(groupName)) {
throw new WebApplicationException(
"Require group name in the request.", Response.Status.BAD_REQUEST);
throw new WebApplicationException("Require group name in the request.", Response.Status.BAD_REQUEST);
}
return buildDAO.getCurrentBuildsByGroupName(groupName);
}

@GET
@Path("/tags")
@ApiOperation(
value = "Get build info along with the build tag info for a given build name",
notes = "Return a bean object containing the build and the build tag",
response = BuildTagBean.class)
public List<BuildTagBean> getBuildsWithTags(
@QueryParam("commit") String scmCommit,
@QueryParam("name") String buildName,
@QueryParam("branch") String scmBranch,
@QueryParam("pageIndex") Optional<Integer> pageIndex,
@QueryParam("pageSize") Optional<Integer> pageSize,
@QueryParam("before") Long before,
@QueryParam("after") Long after)
throws Exception {
value = "Get build info along with the build tag info for a given build name",
notes = "Return a bean object containing the build and the build tag",
response = BuildTagBean.class
)
public List<BuildTagBean> getBuildsWithTags(@QueryParam("commit") String scmCommit,
@QueryParam("name") String buildName, @QueryParam("branch") String scmBranch,
@QueryParam("pageIndex") Optional<Integer> pageIndex,
@QueryParam("pageSize") Optional<Integer> pageSize, @QueryParam("before") Long before,
@QueryParam("after") Long after) throws Exception {

if (StringUtils.isEmpty(buildName) && StringUtils.isEmpty(scmCommit)) {
throw new WebApplicationException(
"Require either commit or build name in the request.",
throw new WebApplicationException("Require either commit or build name in the request.",
Response.Status.BAD_REQUEST);
}

List<BuildBean> builds =
buildDAO.get(scmCommit, buildName, scmBranch, pageIndex, pageSize, before, after);
buildDAO.get(scmCommit, buildName, scmBranch, pageIndex, pageSize, before, after);

BuildTagsManager manager = new BuildTagsManagerImpl(this.tagDAO);
return manager.getEffectiveTagsWithBuilds(builds);
Expand All @@ -193,14 +178,11 @@ public List<BuildTagBean> getBuildsWithTags(
notes = "Publish a build given a build object",
response = Response.class)
@RolesAllowed(TeletraanPrincipalRole.Names.PUBLISHER)
@ResourceAuthZInfo(
type = AuthZResource.Type.BUILD,
idLocation = ResourceAuthZInfo.Location.BODY)
@ResourceAuthZInfo(type = AuthZResource.Type.BUILD, idLocation = ResourceAuthZInfo.Location.BODY)
public Response publish(
@Context SecurityContext sc,
@Context UriInfo uriInfo,
@ApiParam(value = "BUILD object", required = true) @Valid BuildBean buildBean)
throws Exception {
@ApiParam(value = "BUILD object", required = true)@Valid BuildBean buildBean) throws Exception {
if (StringUtils.isEmpty(buildBean.getScm())) {
buildBean.setScm(sourceControlManagerProxy.getDefaultTypeName());
}
Expand All @@ -210,11 +192,7 @@ public Response publish(
}

if (StringUtils.isEmpty(buildBean.getScm_info())) {
buildBean.setScm_info(
sourceControlManagerProxy.generateCommitLink(
buildBean.getScm(),
buildBean.getScm_repo(),
buildBean.getScm_commit()));
buildBean.setScm_info(sourceControlManagerProxy.generateCommitLink(buildBean.getScm(), buildBean.getScm_repo(), buildBean.getScm_commit()));
}

if (StringUtils.isEmpty(buildBean.getPublish_info())) {
Expand All @@ -234,17 +212,14 @@ public Response publish(

// Check if build is approved via our allow list of URLs
if (!Boolean.TRUE.equals(buildAllowlist.approved(buildBean.getArtifact_url()))) {
throw new WebApplicationException(
"Artifact URL points to unapproved location.", Response.Status.BAD_REQUEST);
throw new WebApplicationException("Artifact URL points to unapproved location.",
Response.Status.BAD_REQUEST);
}

// Check if build SCM is approved via allow list of SCMs
if (!Boolean.TRUE.equals(sourceControlManagerProxy.hasSCMType(buildBean.getScm()))) {
throw new WebApplicationException(
String.format(
"Unsupported SCM type. %s not in list %s.",
buildBean.getScm(), sourceControlManagerProxy.getSCMs()),
Response.Status.BAD_REQUEST);
throw new WebApplicationException(String.format("Unsupported SCM type. %s not in list %s.",
buildBean.getScm(), sourceControlManagerProxy.getSCMs()), Response.Status.BAD_REQUEST);
}

// We append commit SHA after build id to make build directory name human friendly
Expand All @@ -253,8 +228,7 @@ public Response publish(
buildBean.setBuild_id(buildId);

buildDAO.insert(buildBean);
LOG.info(
"Successfully published build {} by {}.", buildId, sc.getUserPrincipal().getName());
LOG.info("Successfully published build {} by {}.", buildId, sc.getUserPrincipal().getName());

// publish event
if (buildEventPublisher != null) {
Expand All @@ -267,32 +241,28 @@ public Response publish(
return Response.created(buildUri).entity(buildBean).build();
}


@DELETE
@Path("/{id : [a-zA-Z0-9\\-_]+}")
@ApiOperation(value = "Delete a build", notes = "Deletes a build given a build id")
@ApiOperation(
value = "Delete a build",
notes = "Deletes a build given a build id")
@RolesAllowed(TeletraanPrincipalRole.Names.DELETE)
@ResourceAuthZInfo(
type = AuthZResource.Type.BUILD,
idLocation = ResourceAuthZInfo.Location.PATH)
@ResourceAuthZInfo(type = AuthZResource.Type.BUILD, idLocation = ResourceAuthZInfo.Location.PATH)
public void delete(
@Context SecurityContext sc,
@ApiParam(value = "BUILD id", required = true) @PathParam("id") String id)
throws Exception {
@Context SecurityContext sc,
@ApiParam(value = "BUILD id", required = true)@PathParam("id") String id) throws Exception {
BuildBean buildBean = buildDAO.getById(id);
if (buildBean == null) {
throw new WebApplicationException(
String.format("BUILD %s does not exist.", id), Response.Status.NOT_FOUND);
throw new WebApplicationException(String.format("BUILD %s does not exist.", id), Response.Status.NOT_FOUND);
}

if (deployDAO.isThereADeployWithBuildId(buildBean.getBuild_id())) {
// When a build has been deployed (associated with a deployment), it should not be
// deleted.
// When a build has been deployed (associated with a deployment), it should not be deleted.
// This keeps a record for what was deployed. Also, this helps avoid problem when
// the build is currently deployed (or being actively deployed).
throw new WebApplicationException(
String.format(
"Build %s is currently associated with a deployment and cannot be deleted",
id),
String.format("Build %s is currently associated with a deployment and cannot be deleted", id),
Response.Status.BAD_REQUEST);
}

Expand Down
Loading

0 comments on commit 8c2f59f

Please sign in to comment.