Skip to content

Commit

Permalink
fix(nexus): add nexus name endpoint and artifact location (#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
claymccoy authored and Jammy Louie committed Sep 12, 2019
1 parent e40a090 commit 1f60f14
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ gitlab:
The following CI services are supported:

- Artifactory
- Nexus
- Concourse
- Gitlab CI
- Google Cloud Build (GCB)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@

public enum ArtifactServiceProvider {
CUSTOM,
ARTIFACTORY
ARTIFACTORY,
NEXUS
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@
import com.netflix.spinnaker.igor.config.NexusProperties;
import com.netflix.spinnaker.igor.history.EchoService;
import com.netflix.spinnaker.igor.nexus.model.NexusAssetWebhookPayload;
import com.netflix.spinnaker.igor.nexus.model.NexusRepo;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -49,6 +53,11 @@ public NexusController(
missedNotificationId = registry.createId("webhook.missedEchoNotification");
}

@GetMapping("/names")
List<String> getNexusNames() {
return nexusProperties.getRepos().stream().map(NexusRepo::getName).collect(Collectors.toList());
}

@PostMapping(path = "/webhook", consumes = "application/json")
public void webhook(@RequestBody NexusAssetWebhookPayload payload) {
if (!echoService.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,50 @@ public void postEvent(NexusAssetWebhookPayload payload) {
final String version = nameList.get(nameList.size() - 2);
final String artifactId = nameList.get(nameList.size() - 3);
final String group = Strings.join(nameList.subList(0, nameList.size() - 3), '.');
final Artifact artifact =
Artifact.builder()
.type("maven/file")
.reference(group + ":" + artifactId + ":" + version)
.name(group + ":" + artifactId)
.version(version)
.provenance(payload.getRepositoryName())
.build();
final String path = Strings.join(nameList.subList(0, nameList.size() - 3), '/');
final String specificArtifactName =
nameList.subList(nameList.size() - 1, nameList.size()).get(0);
final String specificArtifactVersion =
specificArtifactName.substring(
specificArtifactName.indexOf('-') + 1, specificArtifactName.lastIndexOf(".pom"));
final Optional<NexusRepo> oRepo = findNexusRepo(payload);
oRepo.ifPresent(
repo -> {
AuthenticatedRequest.allowAnonymous(
() ->
echoService.postEvent(
new NexusAssetEvent(
new NexusAssetEvent.Content(repo.getName(), artifact))));
});
repo ->
AuthenticatedRequest.allowAnonymous(
() -> {
String location = null;
if (repo.getBaseUrl() != null) {
String baseUrl =
repo.getBaseUrl()
.replace("/repository", "/service/rest/repository/browse");
if (!baseUrl.endsWith("/")) {
baseUrl = baseUrl + "/";
}
location =
baseUrl
+ repo.getRepo()
+ "/"
+ path
+ "/"
+ artifactId
+ "/"
+ version
+ "/"
+ specificArtifactVersion
+ "/";
}
final Artifact artifact =
Artifact.builder()
.type("maven/file")
.reference(group + ":" + artifactId + ":" + version)
.name(group + ":" + artifactId)
.version(version)
.provenance(payload.getRepositoryName())
.location(location)
.build();
return echoService.postEvent(
new NexusAssetEvent(new NexusAssetEvent.Content(repo.getName(), artifact)));
}));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class NexusEventPosterTest {
final NexusRepo nexusRepo = new NexusRepo();
nexusRepo.setName("nexus-snapshots");
nexusRepo.setRepo("maven-snapshots");
nexusRepo.setBaseUrl("http://localhost:8082/repository/");
nexusRepo.setNodeId("123");
nexusProperties.setRepos(Collections.singletonList(nexusRepo));
}
Expand Down Expand Up @@ -65,6 +66,8 @@ void postArtifact() {
.name("com.example" + ":" + "demo")
.version("0.0.1-SNAPSHOT")
.provenance("maven-snapshots")
.location(
"http://localhost:8082/service/rest/repository/browse/maven-snapshots/com/example/demo/0.0.1-SNAPSHOT/0.0.1-20190828.022502-3/")
.build();
verify(echoService)
.postEvent(
Expand All @@ -86,6 +89,8 @@ void postUpdatedArtifact() {
.name("com.example" + ":" + "demo")
.version("0.0.1-SNAPSHOT")
.provenance("maven-snapshots")
.location(
"http://localhost:8082/service/rest/repository/browse/maven-snapshots/com/example/demo/0.0.1-SNAPSHOT/0.0.1-20190828.022502-3/")
.build();
verify(echoService)
.postEvent(
Expand Down Expand Up @@ -138,6 +143,8 @@ void postNonMatchingRepoArtifactWithRepoId() {
.name("com.example" + ":" + "demo")
.version("0.0.1-SNAPSHOT")
.provenance("DNE")
.location(
"http://localhost:8082/service/rest/repository/browse/maven-snapshots/com/example/demo/0.0.1-SNAPSHOT/0.0.1-20190828.022502-3/")
.build();
verify(echoService)
.postEvent(
Expand Down

0 comments on commit 1f60f14

Please sign in to comment.