mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(codegurureviewer): repository association lifecycle with tagging (#2741)
* feat(codegurureviewer): repository association lifecycle with tagging AssociateRepository, DescribeRepositoryAssociation, DisassociateRepository and ListRepositoryAssociations over CodeGuru Reviewer's REST JSON protocol, with TagResource/UntagResource/ListTagsForResource served on the shared /tags path. The Repository union is validated as AWS validates it (exactly one provider, Owner/ConnectionArn for third-party providers, KMSKeyId required for CUSTOMER_MANAGED_CMK), duplicate associations report ConflictException, and an association is Associated from the first read so provider waiters complete immediately. * fix(codegurureviewer): paginate ListRepositoryAssociations, register the model for reflection, generate the action table ListRepositoryAssociations now honours MaxResults and NextToken with real pagination (the filter query keys stay singular - ProviderType, State, Name, Owner - matching the wire mapping the API reference documents for the plural request fields). RepositoryAssociation carries @RegisterForReflection so native-image persistence survives a restart. The docs page now uses the generated action-table markers, has its service-matrix row, and maps the hyphenated codeguru-reviewer signing name to its doc slug. * fix(codegurureviewer): reject a malformed MaxResults with ValidationException, not 404 A JAX-RS Integer binding fails during parameter conversion, so MaxResults=abc surfaced as 404 before the handler ran. The parameter is now bound as a String and parsed in the controller, routing bad values through the modeled 400 ValidationException.
This commit is contained in:
1 parent
eaecb36425
commit
0b46d804c0
13 files changed
+1115
-1
No files matched your search
@@ -702,6 +702,7 @@ public interface EmulatorConfig {
|
||||
|
||||
LakeFormationServiceConfig lakeformation();
|
||||
EfsServiceConfig efs();
|
||||
CodeGuruReviewerServiceConfig codegurureviewer();
|
||||
}
|
||||
|
||||
interface ConnectServiceConfig {
|
||||
@@ -719,6 +720,11 @@ public interface EmulatorConfig {
|
||||
boolean enabled();
|
||||
}
|
||||
|
||||
interface CodeGuruReviewerServiceConfig {
|
||||
@WithDefault("true")
|
||||
boolean enabled();
|
||||
}
|
||||
|
||||
interface IotServiceConfig {
|
||||
@WithDefault("true")
|
||||
boolean enabled();
|
||||
|
||||
@@ -571,7 +571,13 @@ public class ResolvedServiceCatalog {
|
||||
storageMode(config.storage().services().efs().mode(), config.storage().mode()),
|
||||
config.storage().services().efs().flushIntervalMs(), null, ServiceProtocol.REST_JSON,
|
||||
protocols(ServiceProtocol.REST_JSON),
|
||||
Set.of(), Set.of("elasticfilesystem"), Set.of(), Set.of(EfsController.class))
|
||||
Set.of(), Set.of("elasticfilesystem"), Set.of(), Set.of(EfsController.class)),
|
||||
descriptor("codeguru-reviewer", "codegurureviewer",
|
||||
config.services().codegurureviewer().enabled(), true,
|
||||
"codegurureviewer", config.storage().mode(), 5000L, null, ServiceProtocol.REST_JSON,
|
||||
protocols(ServiceProtocol.REST_JSON),
|
||||
Set.of(), Set.of("codeguru-reviewer"), Set.of(),
|
||||
Set.of(io.github.hectorvent.floci.services.codegurureviewer.CodeGuruReviewerController.class))
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
+203
@@ -0,0 +1,203 @@
|
||||
package io.github.hectorvent.floci.services.codegurureviewer;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.JsonErrorResponseUtils;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.services.codegurureviewer.model.RepositoryAssociation;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.Consumes;
|
||||
import jakarta.ws.rs.DELETE;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.POST;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.PathParam;
|
||||
import jakarta.ws.rs.Produces;
|
||||
import jakarta.ws.rs.QueryParam;
|
||||
import jakarta.ws.rs.WebApplicationException;
|
||||
import jakarta.ws.rs.core.Context;
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Amazon CodeGuru Reviewer REST-JSON controller.
|
||||
*
|
||||
* <p>Tagging goes through the shared {@code /tags/{resourceArn}} path, which
|
||||
* {@link CodeGuruReviewerService} serves as a {@code TagHandler}.
|
||||
*
|
||||
* <p>Only the operations declared below are served; anything else falls through to the
|
||||
* emulator's not-found handling rather than a stub success.
|
||||
*/
|
||||
@Path("/")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public class CodeGuruReviewerController {
|
||||
|
||||
private final CodeGuruReviewerService codeGuruReviewerService;
|
||||
private final RegionResolver regionResolver;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
@Inject
|
||||
public CodeGuruReviewerController(CodeGuruReviewerService codeGuruReviewerService,
|
||||
RegionResolver regionResolver, ObjectMapper objectMapper) {
|
||||
this.codeGuruReviewerService = codeGuruReviewerService;
|
||||
this.regionResolver = regionResolver;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/associations")
|
||||
public Response associateRepository(@Context HttpHeaders headers, String body) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
JsonNode request = readTree(body);
|
||||
RepositoryAssociation association = codeGuruReviewerService.associateRepository(
|
||||
request.get("Repository"), request.get("KMSKeyDetails"),
|
||||
parseTags(request.get("Tags")), region);
|
||||
return Response.ok(associationResponse(association)).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/associations/{associationArn}")
|
||||
public Response describeRepositoryAssociation(@PathParam("associationArn") String associationArn,
|
||||
@Context HttpHeaders headers) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
return Response.ok(associationResponse(
|
||||
codeGuruReviewerService.describeRepositoryAssociation(associationArn, region))).build();
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/associations/{associationArn}")
|
||||
public Response disassociateRepository(@PathParam("associationArn") String associationArn,
|
||||
@Context HttpHeaders headers) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
return Response.ok(associationResponse(
|
||||
codeGuruReviewerService.disassociateRepository(associationArn, region))).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* The filter query keys are singular ({@code ProviderType=...&State=...}) because that is
|
||||
* the wire mapping the API reference documents for the plural request fields
|
||||
* ({@code ProviderTypes}, {@code States}, ...): the SDK serializes each list member as a
|
||||
* repeated singular query parameter.
|
||||
*/
|
||||
@GET
|
||||
@Path("/associations")
|
||||
public Response listRepositoryAssociations(@QueryParam("ProviderType") List<String> providerTypes,
|
||||
@QueryParam("State") List<String> states,
|
||||
@QueryParam("Name") List<String> names,
|
||||
@QueryParam("Owner") List<String> owners,
|
||||
@QueryParam("MaxResults") String maxResults,
|
||||
@QueryParam("NextToken") String nextToken,
|
||||
@Context HttpHeaders headers) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
PaginatedResult<RepositoryAssociation> page = codeGuruReviewerService.listRepositoryAssociations(
|
||||
providerTypes, states, names, owners, parseMaxResults(maxResults), nextToken, region);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode summaries = response.putArray("RepositoryAssociationSummaries");
|
||||
for (RepositoryAssociation association : page.items()) {
|
||||
ObjectNode summary = summaries.addObject();
|
||||
summary.put("AssociationArn", association.getAssociationArn());
|
||||
if (association.getConnectionArn() != null) {
|
||||
summary.put("ConnectionArn", association.getConnectionArn());
|
||||
}
|
||||
summary.put("LastUpdatedTimeStamp", epochSeconds(association.getLastUpdatedTimeStamp()));
|
||||
summary.put("AssociationId", association.getAssociationId());
|
||||
summary.put("Name", association.getName());
|
||||
summary.put("Owner", association.getOwner());
|
||||
summary.put("ProviderType", association.getProviderType());
|
||||
summary.put("State", association.getState());
|
||||
}
|
||||
if (page.nextToken() != null) {
|
||||
response.put("NextToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private ObjectNode associationResponse(RepositoryAssociation association) {
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.set("RepositoryAssociation", associationNode(association));
|
||||
response.set("Tags", objectMapper.valueToTree(
|
||||
association.getTags() != null ? association.getTags() : Map.of()));
|
||||
return response;
|
||||
}
|
||||
|
||||
private ObjectNode associationNode(RepositoryAssociation association) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("AssociationId", association.getAssociationId());
|
||||
node.put("AssociationArn", association.getAssociationArn());
|
||||
if (association.getConnectionArn() != null) {
|
||||
node.put("ConnectionArn", association.getConnectionArn());
|
||||
}
|
||||
node.put("Name", association.getName());
|
||||
node.put("Owner", association.getOwner());
|
||||
node.put("ProviderType", association.getProviderType());
|
||||
node.put("State", association.getState());
|
||||
node.put("StateReason", association.getStateReason());
|
||||
node.put("CreatedTimeStamp", epochSeconds(association.getCreatedTimeStamp()));
|
||||
node.put("LastUpdatedTimeStamp", epochSeconds(association.getLastUpdatedTimeStamp()));
|
||||
|
||||
ObjectNode kmsKeyDetails = node.putObject("KMSKeyDetails");
|
||||
kmsKeyDetails.put("EncryptionOption", association.getEncryptionOption());
|
||||
if (association.getKmsKeyId() != null) {
|
||||
kmsKeyDetails.put("KMSKeyId", association.getKmsKeyId());
|
||||
}
|
||||
|
||||
if (association.getS3BucketName() != null) {
|
||||
ObjectNode s3Details = node.putObject("S3RepositoryDetails");
|
||||
s3Details.put("BucketName", association.getS3BucketName());
|
||||
if (association.getSourceCodeArtifactsObjectKey() != null) {
|
||||
ObjectNode codeArtifacts = s3Details.putObject("CodeArtifacts");
|
||||
codeArtifacts.put("SourceCodeArtifactsObjectKey", association.getSourceCodeArtifactsObjectKey());
|
||||
if (association.getBuildArtifactsObjectKey() != null) {
|
||||
codeArtifacts.put("BuildArtifactsObjectKey", association.getBuildArtifactsObjectKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
private long epochSeconds(Instant instant) {
|
||||
return instant != null ? instant.getEpochSecond() : 0L;
|
||||
}
|
||||
|
||||
// Bound as String so a malformed value reaches this method: a JAX-RS Integer binding
|
||||
// fails during parameter conversion and surfaces as 404, where AWS models 400.
|
||||
private Integer parseMaxResults(String maxResults) {
|
||||
if (maxResults == null || maxResults.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return Integer.valueOf(maxResults);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new AwsException("ValidationException",
|
||||
"1 validation error detected: Value '" + maxResults
|
||||
+ "' at 'maxResults' failed to satisfy constraint: Member must be an integer.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private JsonNode readTree(String body) {
|
||||
try {
|
||||
return objectMapper.readTree(body == null || body.isBlank() ? "{}" : body);
|
||||
} catch (Exception e) {
|
||||
throw new WebApplicationException(JsonErrorResponseUtils.createSerializationErrorResponse());
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> parseTags(JsonNode tagsNode) {
|
||||
Map<String, String> tags = new LinkedHashMap<>();
|
||||
if (tagsNode != null && tagsNode.isObject()) {
|
||||
tagsNode.fields().forEachRemaining(entry -> tags.put(entry.getKey(), entry.getValue().asText()));
|
||||
}
|
||||
return tags;
|
||||
}
|
||||
}
|
||||
+312
@@ -0,0 +1,312 @@
|
||||
package io.github.hectorvent.floci.services.codegurureviewer;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.Pagination;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.TagHandler;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.codegurureviewer.model.RepositoryAssociation;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Amazon CodeGuru Reviewer repository associations.
|
||||
*
|
||||
* <p>An association reaches the terminal {@code Associated} state as soon as
|
||||
* {@code AssociateRepository} returns, so the provider waiter that polls
|
||||
* {@code DescribeRepositoryAssociation} completes on its first attempt instead of spinning
|
||||
* through an {@code Associating} state the emulator would never leave.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class CodeGuruReviewerService implements TagHandler {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(CodeGuruReviewerService.class);
|
||||
|
||||
private static final Pattern NAME = Pattern.compile("\\S[\\w.-]*");
|
||||
private static final Pattern OWNER = Pattern.compile("\\S(.*\\S)?");
|
||||
private static final Set<String> ENCRYPTION_OPTIONS = Set.of("AWS_OWNED_CMK", "CUSTOMER_MANAGED_CMK");
|
||||
private static final String ASSOCIATED = "Associated";
|
||||
private static final String DISASSOCIATED = "Disassociated";
|
||||
private static final String ARN_RESOURCE_PREFIX = "association:";
|
||||
|
||||
private final StorageBackend<String, RepositoryAssociation> associations;
|
||||
private final RegionResolver regionResolver;
|
||||
|
||||
@Inject
|
||||
public CodeGuruReviewerService(StorageFactory storageFactory, RegionResolver regionResolver) {
|
||||
this.associations = storageFactory.create("codegurureviewer", "codegurureviewer-associations.json",
|
||||
new TypeReference<Map<String, RepositoryAssociation>>() {});
|
||||
this.regionResolver = regionResolver;
|
||||
}
|
||||
|
||||
// ──────────────────────── Repository associations ────────────────────────
|
||||
|
||||
/**
|
||||
* {@code Repository} is a union: exactly one of {@code CodeCommit}, {@code Bitbucket},
|
||||
* {@code GitHubEnterpriseServer} or {@code S3Bucket} may be set, and the association's
|
||||
* {@code Name}, {@code Owner} and {@code ProviderType} follow from whichever it is.
|
||||
*/
|
||||
public RepositoryAssociation associateRepository(JsonNode repository, JsonNode kmsKeyDetails,
|
||||
Map<String, String> tags, String region) {
|
||||
if (repository == null || !repository.isObject()) {
|
||||
throw new AwsException("ValidationException", "Repository is required.", 400);
|
||||
}
|
||||
|
||||
RepositoryAssociation association = new RepositoryAssociation();
|
||||
String accountId = regionResolver.getAccountId();
|
||||
int members = 0;
|
||||
|
||||
JsonNode codeCommit = repository.get("CodeCommit");
|
||||
if (codeCommit != null && codeCommit.isObject()) {
|
||||
members++;
|
||||
association.setProviderType("CodeCommit");
|
||||
association.setName(requireName(codeCommit, "Repository.CodeCommit.Name"));
|
||||
association.setOwner(accountId);
|
||||
}
|
||||
JsonNode bitbucket = repository.get("Bitbucket");
|
||||
if (bitbucket != null && bitbucket.isObject()) {
|
||||
members++;
|
||||
applyThirdParty(association, bitbucket, "Bitbucket");
|
||||
}
|
||||
JsonNode gitHubEnterprise = repository.get("GitHubEnterpriseServer");
|
||||
if (gitHubEnterprise != null && gitHubEnterprise.isObject()) {
|
||||
members++;
|
||||
applyThirdParty(association, gitHubEnterprise, "GitHubEnterpriseServer");
|
||||
}
|
||||
JsonNode s3Bucket = repository.get("S3Bucket");
|
||||
if (s3Bucket != null && s3Bucket.isObject()) {
|
||||
members++;
|
||||
association.setProviderType("S3Bucket");
|
||||
association.setName(requireName(s3Bucket, "Repository.S3Bucket.Name"));
|
||||
association.setOwner(accountId);
|
||||
String bucketName = text(s3Bucket, "BucketName");
|
||||
if (bucketName == null || bucketName.isBlank()) {
|
||||
throw new AwsException("ValidationException", "Repository.S3Bucket.BucketName is required.", 400);
|
||||
}
|
||||
association.setS3BucketName(bucketName);
|
||||
}
|
||||
|
||||
if (members != 1) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Repository must set exactly one of CodeCommit, Bitbucket, GitHubEnterpriseServer, S3Bucket.",
|
||||
400);
|
||||
}
|
||||
if (findByRepository(association.getProviderType(), association.getOwner(), association.getName(), region)
|
||||
!= null) {
|
||||
throw new AwsException("ConflictException",
|
||||
"Repository " + association.getName() + " is already associated.", 409);
|
||||
}
|
||||
|
||||
applyKmsKeyDetails(association, kmsKeyDetails);
|
||||
|
||||
String associationId = UUID.randomUUID().toString();
|
||||
Instant now = Instant.now();
|
||||
association.setAssociationId(associationId);
|
||||
association.setAssociationArn(regionResolver.buildArn("codeguru-reviewer", region,
|
||||
ARN_RESOURCE_PREFIX + associationId));
|
||||
association.setState(ASSOCIATED);
|
||||
association.setStateReason("Pull Request Notification configuration successful");
|
||||
association.setCreatedTimeStamp(now);
|
||||
association.setLastUpdatedTimeStamp(now);
|
||||
association.setTags(tags != null ? new LinkedHashMap<>(tags) : new LinkedHashMap<>());
|
||||
|
||||
associations.put(storageKey(region, associationId), association);
|
||||
LOG.infov("Associated CodeGuru Reviewer repository: {0} ({1})",
|
||||
association.getName(), association.getProviderType());
|
||||
return association;
|
||||
}
|
||||
|
||||
public RepositoryAssociation describeRepositoryAssociation(String associationArn, String region) {
|
||||
return associations.get(lookupKey(associationArn, region))
|
||||
.orElseThrow(() -> associationNotFound(associationArn));
|
||||
}
|
||||
|
||||
public RepositoryAssociation disassociateRepository(String associationArn, String region) {
|
||||
RepositoryAssociation association = describeRepositoryAssociation(associationArn, region);
|
||||
associations.delete(storageKey(region, association.getAssociationId()));
|
||||
association.setState(DISASSOCIATED);
|
||||
association.setLastUpdatedTimeStamp(Instant.now());
|
||||
LOG.infov("Disassociated CodeGuru Reviewer repository: {0}", association.getName());
|
||||
return association;
|
||||
}
|
||||
|
||||
public PaginatedResult<RepositoryAssociation> listRepositoryAssociations(
|
||||
List<String> providerTypes, List<String> states, List<String> names, List<String> owners,
|
||||
Integer maxResults, String nextToken, String region) {
|
||||
String regionPrefix = region + "::";
|
||||
List<RepositoryAssociation> matching = associations.scan(key -> key.startsWith(regionPrefix)).stream()
|
||||
.filter(association -> matches(providerTypes, association.getProviderType()))
|
||||
.filter(association -> matches(states, association.getState()))
|
||||
.filter(association -> matches(names, association.getName()))
|
||||
.filter(association -> matches(owners, association.getOwner()))
|
||||
.sorted(Comparator.comparing(RepositoryAssociation::getName)
|
||||
.thenComparing(RepositoryAssociation::getAssociationId))
|
||||
.toList();
|
||||
return Pagination.paginate(matching, RepositoryAssociation::getAssociationId,
|
||||
maxResults, nextToken, 100, "ValidationException");
|
||||
}
|
||||
|
||||
// ──────────────────────────── Tags ────────────────────────────
|
||||
|
||||
@Override
|
||||
public String serviceKey() {
|
||||
return "codeguru-reviewer";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String tagsBodyKey() {
|
||||
return "Tags";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> listTags(String region, String arn) {
|
||||
Map<String, String> tags = forTagging(arn, region).getTags();
|
||||
return tags != null ? tags : Map.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tagResource(String region, String arn, Map<String, String> tags) {
|
||||
RepositoryAssociation association = forTagging(arn, region);
|
||||
if (association.getTags() == null) {
|
||||
association.setTags(new HashMap<>());
|
||||
}
|
||||
association.getTags().putAll(tags);
|
||||
associations.put(storageKey(region, association.getAssociationId()), association);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void untagResource(String region, String arn, List<String> tagKeys) {
|
||||
RepositoryAssociation association = forTagging(arn, region);
|
||||
if (association.getTags() != null && tagKeys != null) {
|
||||
tagKeys.forEach(association.getTags()::remove);
|
||||
}
|
||||
associations.put(storageKey(region, association.getAssociationId()), association);
|
||||
}
|
||||
|
||||
/**
|
||||
* The tag operations declare {@code ResourceNotFoundException} where the association
|
||||
* operations declare {@code NotFoundException}, so the lookup is repeated here with the
|
||||
* error code the tag path's model actually names.
|
||||
*/
|
||||
private RepositoryAssociation forTagging(String arn, String region) {
|
||||
return associations.get(lookupKey(arn, region))
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
"Repository association " + arn + " does not exist.", 404));
|
||||
}
|
||||
|
||||
// ──────────────────────────── Helpers ────────────────────────────
|
||||
|
||||
private void applyThirdParty(RepositoryAssociation association, JsonNode repository, String providerType) {
|
||||
association.setProviderType(providerType);
|
||||
association.setName(requireName(repository, "Repository." + providerType + ".Name"));
|
||||
|
||||
String owner = text(repository, "Owner");
|
||||
if (owner == null || !OWNER.matcher(owner).matches() || owner.length() > 100) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Repository." + providerType + ".Owner is required and must be 1-100 characters.", 400);
|
||||
}
|
||||
association.setOwner(owner);
|
||||
|
||||
String connectionArn = text(repository, "ConnectionArn");
|
||||
if (connectionArn == null || connectionArn.isBlank()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Repository." + providerType + ".ConnectionArn is required.", 400);
|
||||
}
|
||||
association.setConnectionArn(connectionArn);
|
||||
}
|
||||
|
||||
private void applyKmsKeyDetails(RepositoryAssociation association, JsonNode kmsKeyDetails) {
|
||||
String encryptionOption = "AWS_OWNED_CMK";
|
||||
String kmsKeyId = null;
|
||||
if (kmsKeyDetails != null && kmsKeyDetails.isObject()) {
|
||||
String requested = text(kmsKeyDetails, "EncryptionOption");
|
||||
if (requested != null) {
|
||||
if (!ENCRYPTION_OPTIONS.contains(requested)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"KMSKeyDetails.EncryptionOption must be one of AWS_OWNED_CMK, CUSTOMER_MANAGED_CMK.",
|
||||
400);
|
||||
}
|
||||
encryptionOption = requested;
|
||||
}
|
||||
kmsKeyId = text(kmsKeyDetails, "KMSKeyId");
|
||||
}
|
||||
if ("CUSTOMER_MANAGED_CMK".equals(encryptionOption) && (kmsKeyId == null || kmsKeyId.isBlank())) {
|
||||
throw new AwsException("ValidationException",
|
||||
"KMSKeyDetails.KMSKeyId is required when EncryptionOption is CUSTOMER_MANAGED_CMK.", 400);
|
||||
}
|
||||
association.setEncryptionOption(encryptionOption);
|
||||
association.setKmsKeyId(kmsKeyId);
|
||||
}
|
||||
|
||||
private RepositoryAssociation findByRepository(String providerType, String owner, String name, String region) {
|
||||
String regionPrefix = region + "::";
|
||||
return associations.scan(key -> key.startsWith(regionPrefix)).stream()
|
||||
.filter(association -> providerType.equals(association.getProviderType())
|
||||
&& owner.equals(association.getOwner())
|
||||
&& name.equals(association.getName()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
private String lookupKey(String associationArn, String region) {
|
||||
AwsArnUtils.Arn arn;
|
||||
try {
|
||||
arn = AwsArnUtils.parse(associationArn);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new AwsException("ValidationException",
|
||||
"AssociationArn is not a valid CodeGuru Reviewer association ARN: " + associationArn, 400);
|
||||
}
|
||||
if (!"codeguru-reviewer".equals(arn.service()) || !arn.resource().startsWith(ARN_RESOURCE_PREFIX)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"AssociationArn is not a valid CodeGuru Reviewer association ARN: " + associationArn, 400);
|
||||
}
|
||||
String arnRegion = arn.region().isEmpty() ? region : arn.region();
|
||||
return storageKey(arnRegion, arn.resource().substring(ARN_RESOURCE_PREFIX.length()));
|
||||
}
|
||||
|
||||
private String requireName(JsonNode repository, String field) {
|
||||
String name = text(repository, "Name");
|
||||
if (name == null || !NAME.matcher(name).matches() || name.length() > 100) {
|
||||
throw new AwsException("ValidationException",
|
||||
field + " is required and must be 1-100 characters matching ^\\S[\\w.-]*$.", 400);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
private boolean matches(List<String> filter, String value) {
|
||||
return filter == null || filter.isEmpty() || filter.contains(value);
|
||||
}
|
||||
|
||||
private String text(JsonNode node, String field) {
|
||||
JsonNode value = node.get(field);
|
||||
if (value == null || value.isNull()) {
|
||||
return null;
|
||||
}
|
||||
return value.asText();
|
||||
}
|
||||
|
||||
private AwsException associationNotFound(String associationArn) {
|
||||
return new AwsException("NotFoundException",
|
||||
"Repository association " + associationArn + " does not exist.", 404);
|
||||
}
|
||||
|
||||
private String storageKey(String region, String associationId) {
|
||||
return region + "::" + associationId;
|
||||
}
|
||||
}
|
||||
+157
@@ -0,0 +1,157 @@
|
||||
package io.github.hectorvent.floci.services.codegurureviewer.model;
|
||||
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/** An association between CodeGuru Reviewer and a source repository. */
|
||||
@RegisterForReflection
|
||||
public class RepositoryAssociation {
|
||||
|
||||
private String associationId;
|
||||
private String associationArn;
|
||||
private String connectionArn;
|
||||
private String name;
|
||||
private String owner;
|
||||
private String providerType;
|
||||
private String state;
|
||||
private String stateReason;
|
||||
private Instant createdTimeStamp;
|
||||
private Instant lastUpdatedTimeStamp;
|
||||
private String kmsKeyId;
|
||||
private String encryptionOption;
|
||||
private String s3BucketName;
|
||||
private String sourceCodeArtifactsObjectKey;
|
||||
private String buildArtifactsObjectKey;
|
||||
private Map<String, String> tags = new LinkedHashMap<>();
|
||||
|
||||
public String getAssociationId() {
|
||||
return associationId;
|
||||
}
|
||||
|
||||
public void setAssociationId(String associationId) {
|
||||
this.associationId = associationId;
|
||||
}
|
||||
|
||||
public String getAssociationArn() {
|
||||
return associationArn;
|
||||
}
|
||||
|
||||
public void setAssociationArn(String associationArn) {
|
||||
this.associationArn = associationArn;
|
||||
}
|
||||
|
||||
public String getConnectionArn() {
|
||||
return connectionArn;
|
||||
}
|
||||
|
||||
public void setConnectionArn(String connectionArn) {
|
||||
this.connectionArn = connectionArn;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
public void setOwner(String owner) {
|
||||
this.owner = owner;
|
||||
}
|
||||
|
||||
public String getProviderType() {
|
||||
return providerType;
|
||||
}
|
||||
|
||||
public void setProviderType(String providerType) {
|
||||
this.providerType = providerType;
|
||||
}
|
||||
|
||||
public String getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(String state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public String getStateReason() {
|
||||
return stateReason;
|
||||
}
|
||||
|
||||
public void setStateReason(String stateReason) {
|
||||
this.stateReason = stateReason;
|
||||
}
|
||||
|
||||
public Instant getCreatedTimeStamp() {
|
||||
return createdTimeStamp;
|
||||
}
|
||||
|
||||
public void setCreatedTimeStamp(Instant createdTimeStamp) {
|
||||
this.createdTimeStamp = createdTimeStamp;
|
||||
}
|
||||
|
||||
public Instant getLastUpdatedTimeStamp() {
|
||||
return lastUpdatedTimeStamp;
|
||||
}
|
||||
|
||||
public void setLastUpdatedTimeStamp(Instant lastUpdatedTimeStamp) {
|
||||
this.lastUpdatedTimeStamp = lastUpdatedTimeStamp;
|
||||
}
|
||||
|
||||
public String getKmsKeyId() {
|
||||
return kmsKeyId;
|
||||
}
|
||||
|
||||
public void setKmsKeyId(String kmsKeyId) {
|
||||
this.kmsKeyId = kmsKeyId;
|
||||
}
|
||||
|
||||
public String getEncryptionOption() {
|
||||
return encryptionOption;
|
||||
}
|
||||
|
||||
public void setEncryptionOption(String encryptionOption) {
|
||||
this.encryptionOption = encryptionOption;
|
||||
}
|
||||
|
||||
public String getS3BucketName() {
|
||||
return s3BucketName;
|
||||
}
|
||||
|
||||
public void setS3BucketName(String s3BucketName) {
|
||||
this.s3BucketName = s3BucketName;
|
||||
}
|
||||
|
||||
public String getSourceCodeArtifactsObjectKey() {
|
||||
return sourceCodeArtifactsObjectKey;
|
||||
}
|
||||
|
||||
public void setSourceCodeArtifactsObjectKey(String sourceCodeArtifactsObjectKey) {
|
||||
this.sourceCodeArtifactsObjectKey = sourceCodeArtifactsObjectKey;
|
||||
}
|
||||
|
||||
public String getBuildArtifactsObjectKey() {
|
||||
return buildArtifactsObjectKey;
|
||||
}
|
||||
|
||||
public void setBuildArtifactsObjectKey(String buildArtifactsObjectKey) {
|
||||
this.buildArtifactsObjectKey = buildArtifactsObjectKey;
|
||||
}
|
||||
|
||||
public Map<String, String> getTags() {
|
||||
return tags;
|
||||
}
|
||||
|
||||
public void setTags(Map<String, String> tags) {
|
||||
this.tags = tags;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user