Skip to content

Commit

Permalink
Support build promotion with projects (#613)
Browse files Browse the repository at this point in the history
  • Loading branch information
yahavi authored Feb 6, 2022
1 parent 69a21cc commit 0d31bb7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
import org.jfrog.build.extractor.ci.BuildInfo;
import org.jfrog.build.extractor.ci.BuildRetention;
import org.jfrog.build.api.dependency.BuildPatternArtifacts;
import org.jfrog.build.api.dependency.BuildPatternArtifactsRequest;
import org.jfrog.build.api.dependency.PatternResultFileSet;
Expand All @@ -19,42 +17,11 @@
import org.jfrog.build.client.DownloadResponse;
import org.jfrog.build.client.ItemLastModified;
import org.jfrog.build.client.artifactoryXrayResponse.ArtifactoryXrayResponse;
import org.jfrog.build.extractor.ci.BuildInfo;
import org.jfrog.build.extractor.ci.BuildRetention;
import org.jfrog.build.extractor.clientConfiguration.client.ManagerBase;
import org.jfrog.build.extractor.clientConfiguration.client.RepositoryType;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.CheckRepositoryType;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.CreateRepository;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.DeleteBuilds;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.DeleteProperties;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.DeleteRepository;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.DeleteRepositoryContent;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.DistributeBuild;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.Download;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.DownloadHeaders;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.DownloadToFile;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.ExecuteUserPlugin;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.GetAllBuildNumbers;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.GetBuildInfo;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.GetItemLastModified;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.GetNpmAuth;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.GetRepositoriesKeys;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.GetStagingStrategy;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.GetUserPluginInfo;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.IsRepositoryExist;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.PromotionUserPlugin;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.PublishBuildInfo;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.ReportUsage;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.RetrievePatternArtifacts;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.ScanBuild;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.SearchArtifactsByAql;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.SearchArtifactsByPattern;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.SearchArtifactsByProperties;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.SendBuildRetention;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.SendModuleInfo;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.SetProperties;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.StageBuild;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.UpdateFileProperty;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.Upload;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.Version;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.*;
import org.jfrog.build.extractor.clientConfiguration.client.response.GetAllBuildNumbersResponse;
import org.jfrog.build.extractor.clientConfiguration.deploy.DeployDetails;
import org.jfrog.build.extractor.usageReport.UsageReporter;
Expand Down Expand Up @@ -272,8 +239,8 @@ public void sendBuildRetention(BuildRetention buildRetention, String buildName,
sendBuildRetentionService.execute(jfrogHttpClient);
}

public void stageBuild(String buildName, String buildNumber, Promotion promotion) throws IOException {
StageBuild stageBuildService = new StageBuild(buildName, buildNumber, promotion, log);
public void stageBuild(String buildName, String buildNumber, String project, Promotion promotion) throws IOException {
StageBuild stageBuildService = new StageBuild(buildName, buildNumber, project, promotion, log);
stageBuildService.execute(jfrogHttpClient);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,36 @@

import java.io.IOException;

import static org.jfrog.build.extractor.clientConfiguration.client.artifactory.services.PublishBuildInfo.getProjectQueryParam;
import static org.jfrog.build.extractor.clientConfiguration.util.JsonUtils.toJsonString;

public class StageBuild extends VoidJFrogService {
private static final String BUILD_STAGING_STRATEGY_ENDPOINT = "api/build/promote/";
private final String buildName;
private final String buildNumber;
private final String project;
private final Promotion promotion;

public StageBuild(String buildName, String buildNumber, Promotion promotion, Log logger) {
public StageBuild(String buildName, String buildNumber, String project, Promotion promotion, Log logger) {
super(logger);
this.buildName = buildName;
this.buildNumber = buildNumber;
this.project = project;
this.promotion = promotion;
}

@Override
public HttpRequestBase createRequest() throws IOException {
HttpPost request = new HttpPost(BUILD_STAGING_STRATEGY_ENDPOINT + encodeUrl(buildName) + "/" + encodeUrl(buildNumber));
String url = String.format("%s%s/%s%s", BUILD_STAGING_STRATEGY_ENDPOINT, encodeUrl(buildName), encodeUrl(buildNumber), getProjectQueryParam(project));
HttpPost request = new HttpPost(url);
StringEntity stringEntity = new StringEntity(toJsonString(promotion));
stringEntity.setContentType("application/vnd.org.jfrog.artifactory.build.PromotionRequest+json");
request.setEntity(stringEntity);
log.info("Promotion build " + buildName + ", #" + buildNumber);
String logMsg = String.format("Promotion build %s, #%s", buildName, buildNumber);
if (StringUtils.isNotBlank(project)) {
logMsg += ", in project " + project;
}
log.info(logMsg);
return request;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@

import org.apache.commons.io.FileUtils;
import org.jfrog.build.IntegrationTestsBase;
import org.jfrog.build.extractor.builder.BuildInfoBuilder;
import org.jfrog.build.extractor.ci.Agent;
import org.jfrog.build.extractor.ci.BuildAgent;
import org.jfrog.build.extractor.ci.BuildInfo;
import org.jfrog.build.extractor.ci.BuildRetention;
import org.jfrog.build.extractor.ci.Issues;
import org.jfrog.build.extractor.ci.MatrixParameter;
import org.jfrog.build.extractor.ci.Module;
import org.jfrog.build.extractor.ci.Vcs;
import org.jfrog.build.api.builder.PromotionBuilder;
import org.jfrog.build.api.release.Promotion;
import org.jfrog.build.api.release.PromotionStatus;
import org.jfrog.build.extractor.builder.BuildInfoBuilder;
import org.jfrog.build.extractor.ci.*;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
Expand Down Expand Up @@ -51,18 +46,49 @@ protected void cleanup() throws IOException {
* Send build info to artifactory, receive it and compare.
*/
@Test
public void sendBuildInfoAndBuildRetentioTest() throws IOException {
doSendBuildInfoTest(null);
sendBuildRetention("");
public void sendBuildInfoAndBuildRetentionTest() throws IOException {
try {
publishAndCheckBuildInfo(null);
sendBuildRetention(null);
} finally {
cleanTestBuilds(BUILD_NAME, BUILD_NUMBER, null);
}
}

@Test
public void sendBuildInfoWithProjectTest() throws IOException {
doSendBuildInfoTest("btests");
sendBuildRetention("btests");
String projectKey = "btests";
try {
publishAndCheckBuildInfo(projectKey);
sendBuildRetention(projectKey);
} finally {
cleanTestBuilds(BUILD_NAME, BUILD_NUMBER, projectKey);
}
}

@Test
public void buildStagingTest() throws IOException {
try {
publishAndCheckBuildInfo(null);
stageBuild(null);
} finally {
cleanTestBuilds(BUILD_NAME, BUILD_NUMBER, null);
}
}

@Test
public void buildStagingWithProjectTest() throws IOException {
String projectKey = "btests";
try {
publishAndCheckBuildInfo(projectKey);
stageBuild(projectKey);
} finally {
cleanTestBuilds(BUILD_NAME, BUILD_NUMBER, projectKey);
}
}

private void doSendBuildInfoTest(String project) throws IOException {

private void publishAndCheckBuildInfo(String project) throws IOException {
final Date STARTED = new Date();
final List<Vcs> VCS = Arrays.asList(new Vcs("foo", "1"),
new Vcs("bar", "2"),
Expand Down Expand Up @@ -102,9 +128,6 @@ private void doSendBuildInfoTest(String project) throws IOException {

// Compare
Assert.assertEquals(toJsonString(buildInfoToSend), toJsonString(receivedBuildInfo));

// Cleanup
cleanTestBuilds(BUILD_NAME, BUILD_NUMBER, project);
}

private void sendBuildRetention(String project) throws IOException {
Expand All @@ -113,4 +136,9 @@ private void sendBuildRetention(String project) throws IOException {
buildRetention.setDeleteBuildArtifacts(false);
artifactoryManager.sendBuildRetention(buildRetention, BUILD_NAME, project, false);
}

private void stageBuild(String project) throws IOException {
Promotion promotion = new PromotionBuilder().build();
artifactoryManager.stageBuild(BUILD_NAME, BUILD_NUMBER, project, promotion);
}
}

0 comments on commit 0d31bb7

Please sign in to comment.