Skip to content

Commit

Permalink
Merge pull request #814 from DependencyTrack/port-add-project-tags-pa…
Browse files Browse the repository at this point in the history
…rameter-for-bom-endpoints

Port : add project tags parameter for bom endpoints
  • Loading branch information
nscuro authored Jul 31, 2024
2 parents 4013284 + c374692 commit 11d21a2
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 29 deletions.
7 changes: 7 additions & 0 deletions src/main/java/org/dependencytrack/model/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ public class Tag implements Serializable {

private static final long serialVersionUID = -7798359808664731988L;

public Tag() {
}

public Tag(final String name) {
this.name = name;
}

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.NATIVE)
@JsonIgnore
Expand Down
28 changes: 18 additions & 10 deletions src/main/java/org/dependencytrack/resources/v1/BomResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.security.Principal;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;

import static java.util.function.Predicate.not;
import static org.dependencytrack.model.ConfigPropertyConstants.BOM_VALIDATION_ENABLED;

/**
Expand Down Expand Up @@ -295,7 +297,7 @@ public Response uploadBom(@Parameter(required = true) BomSubmitRequest request)
}
}

project = qm.createProject(StringUtils.trimToNull(request.getProjectName()), null, StringUtils.trimToNull(request.getProjectVersion()), null, parent, null, true, true);
project = qm.createProject(StringUtils.trimToNull(request.getProjectName()), null, StringUtils.trimToNull(request.getProjectVersion()), request.getProjectTags(), parent, null, true, true);
Principal principal = getPrincipal();
qm.updateNewProjectACL(project, principal);
} else {
Expand Down Expand Up @@ -342,14 +344,17 @@ public Response uploadBom(@Parameter(required = true) BomSubmitRequest request)
@ApiResponse(responseCode = "404", description = "The project could not be found")
})
@PermissionRequired(Permissions.Constants.BOM_UPLOAD)
public Response uploadBom(@FormDataParam("project") String projectUuid,
@DefaultValue("false") @FormDataParam("autoCreate") boolean autoCreate,
@FormDataParam("projectName") String projectName,
@FormDataParam("projectVersion") String projectVersion,
@FormDataParam("parentName") String parentName,
@FormDataParam("parentVersion") String parentVersion,
@FormDataParam("parentUUID") String parentUUID,
@Parameter(schema = @Schema(type = "string")) @FormDataParam("bom") final List<FormDataBodyPart> artifactParts) {
public Response uploadBom(
@FormDataParam("project") String projectUuid,
@DefaultValue("false") @FormDataParam("autoCreate") boolean autoCreate,
@FormDataParam("projectName") String projectName,
@FormDataParam("projectVersion") String projectVersion,
@FormDataParam("projectTags") String projectTags,
@FormDataParam("parentName") String parentName,
@FormDataParam("parentVersion") String parentVersion,
@FormDataParam("parentUUID") String parentUUID,
@Parameter(schema = @Schema(type = "string")) @FormDataParam("bom") final List<FormDataBodyPart> artifactParts
) {
if (projectUuid != null) { // behavior in v3.0.0
try (QueryManager qm = new QueryManager()) {
final Project project = qm.getObjectByUuid(Project.class, projectUuid);
Expand Down Expand Up @@ -379,7 +384,10 @@ public Response uploadBom(@FormDataParam("project") String projectUuid,
return Response.status(Response.Status.FORBIDDEN).entity("Access to the specified parent project is forbidden").build();
}
}
project = qm.createProject(trimmedProjectName, null, trimmedProjectVersion, null, parent, null, true, true);
final List<org.dependencytrack.model.Tag> tags = (projectTags != null && !projectTags.isBlank())
? Arrays.stream(projectTags.split(",")).map(String::trim).filter(not(String::isEmpty)).map(org.dependencytrack.model.Tag::new).toList()
: null;
project = qm.createProject(trimmedProjectName, null, trimmedProjectVersion, tags, parent, null, true, true);
Principal principal = getPrincipal();
qm.updateNewProjectACL(project, principal);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import org.dependencytrack.model.Tag;

import java.util.List;

/**
* Defines a custom request object used when uploading bill-of-material (bom) documents.
Expand All @@ -51,6 +54,8 @@ public final class BomSubmitRequest {
@Pattern(regexp = RegexSequence.Definition.PRINTABLE_CHARS, message = "The project version may only contain printable characters")
private final String projectVersion;

private final List<Tag> projectTags;

@Pattern(regexp = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", message = "The parent UUID must be a valid 36 character UUID")
private final String parentUUID;

Expand All @@ -71,15 +76,17 @@ public final class BomSubmitRequest {
public BomSubmitRequest(String project,
String projectName,
String projectVersion,
List<Tag> projectTags,
boolean autoCreate,
String bom) {
this(project, projectName, projectVersion, autoCreate, null, null, null, bom);
this(project, projectName, projectVersion, projectTags, autoCreate, null, null, null, bom);
}

@JsonCreator
public BomSubmitRequest(@JsonProperty(value = "project") String project,
@JsonProperty(value = "projectName") String projectName,
@JsonProperty(value = "projectVersion") String projectVersion,
@JsonProperty(value = "projectTags") List<Tag> projectTags,
@JsonProperty(value = "autoCreate") boolean autoCreate,
@JsonProperty(value = "parentUUID") String parentUUID,
@JsonProperty(value = "parentName") String parentName,
Expand All @@ -88,6 +95,7 @@ public BomSubmitRequest(@JsonProperty(value = "project") String project,
this.project = project;
this.projectName = projectName;
this.projectVersion = projectVersion;
this.projectTags = projectTags;
this.autoCreate = autoCreate;
this.parentUUID = parentUUID;
this.parentName = parentName;
Expand All @@ -110,6 +118,11 @@ public String getProjectVersion() {
return projectVersion;
}

@Schema(example = "tag1, tag2")
public List<Tag> getProjectTags() {
return projectTags;
}

@Schema(example = "5341f53c-611b-4388-9d9c-731026dc5eec")
public String getParentUUID() {
return parentUUID;
Expand Down
Loading

0 comments on commit 11d21a2

Please sign in to comment.