Skip to content

Commit

Permalink
Fixed an issue with the BuildURL value in the Build Information (#141)
Browse files Browse the repository at this point in the history
* update errorprone to fix compille issue

* Revert to getting the build server's Url from the server, rather than the agent's config

* added gitatttributes files to make sure spotless does the right thing with line endings on Windows
  • Loading branch information
slewis74 authored Mar 27, 2022
1 parent 1d2cf27 commit c194001
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text eol=lf
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
id 'com.github.ben-manes.versions' version '0.36.0'
id 'com.github.hierynomus.license' version '0.16.1'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'net.ltgt.errorprone' version '1.3.0'
id 'net.ltgt.errorprone' version '2.0.0'
id 'maven-publish'
id 'java'
id 'distribution'
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=6.1.11
version=6.1.12
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,23 @@ protected OctopusCommandBuilder createCommand() {
teamCityServerUrl, build.getAccessUser(), build.getAccessCode());
final Build restfulBuild = teamCityServer.build(new BuildId(buildIdString));

final String buildNumber = restfulBuild.getBuildNumber();
String buildUrlString = sharedConfigParameters.get("externalBuildUrl");
if (buildUrlString == null) {
// if the Global settings don't have a Server URL then fall back to using the agent's
// configuration for the server's URL
buildUrlString = teamCityServerUrl + "/viewLog.html?buildId=" + buildNumber;
}

final OctopusBuildInformation buildInformation =
builder.build(
sharedConfigParameters.get("octopus_vcstype"),
sharedConfigParameters.get("vcsroot.url"),
sharedConfigParameters.get("build.vcs.number"),
restfulBuild.getBranch().getName(),
createJsonCommitHistory(restfulBuild),
teamCityServerUrl,
buildIdString,
build.getBuildNumber());
buildUrlString,
buildNumber);

if (verboseLogging) {
buildLogger.message("Creating " + dataFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public OctopusBuildInformation build(
final String vcsCommitNumber,
final String branch,
final String commitsJson,
final String serverUrl,
final String buildId,
final String externalBuildUrl,
final String buildNumber) {

final OctopusBuildInformation buildInformation = new OctopusBuildInformation();
Expand All @@ -27,7 +26,7 @@ public OctopusBuildInformation build(
gson.fromJson(commitsJson, new TypeToken<List<Commit>>() {}.getType());
buildInformation.Branch = branch;
buildInformation.BuildNumber = buildNumber;
buildInformation.BuildUrl = serverUrl + "/viewLog.html?buildId=" + buildId;
buildInformation.BuildUrl = externalBuildUrl;
buildInformation.VcsType = vcsType;
buildInformation.VcsRoot = vcsRoot;
buildInformation.VcsCommitNumber = vcsCommitNumber;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,16 @@ public List<BuildInformationUploaderContext> collateParameters() throws RunBuild
final Map<String, String> sharedConfigParameters = runningBuild.getSharedConfigParameters();

final BuildInfoUserData buildInfoUserData = new BuildInfoUserData(parameters);
final String buildId = Long.toString(runningBuild.getBuildId());

final URL buildUrl = constructBuildUrl(runningBuild, buildId);
String buildUrlString = sharedConfigParameters.get("externalBuildUrl");
if (buildUrlString == null) {
// if the Global settings don't have a Server URL then fall back to using the agent's
// configuration for the server's URL
final String buildId = Long.toString(runningBuild.getBuildId());
buildUrlString =
runningBuild.getAgentConfiguration().getServerUrl() + "/viewLog.html?buildId=" + buildId;
}
final URL buildUrl = constructBuildUrl(buildUrlString);

final BuildInformationUploaderContextBuilder buildInfoBuilder =
new BuildInformationUploaderContextBuilder()
Expand All @@ -75,15 +82,12 @@ public List<BuildInformationUploaderContext> collateParameters() throws RunBuild
.collect(Collectors.toList());
}

private URL constructBuildUrl(final AgentRunningBuild runningBuild, final String buildId)
throws RunBuildException {
private URL constructBuildUrl(final String externalBuildUrl) throws RunBuildException {

final String buildUrlString =
runningBuild.getAgentConfiguration().getServerUrl() + "/viewLog.html?buildId=" + buildId;
try {
return new URL(buildUrlString);
return new URL(externalBuildUrl);
} catch (final MalformedURLException e) {
throw new RunBuildException("Failed to construct a build URL from " + buildUrlString, e);
throw new RunBuildException("Failed to construct a build URL from " + externalBuildUrl, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public void pushBuildInfoToOctopusServer() throws IOException, RunBuildException
sharedConfigParameters.put("octopus_vcstype", "git");
sharedConfigParameters.put("vcsroot.url", "git://git.git/git.git");
sharedConfigParameters.put("build.vcs.number", "COMMIT_HASH");
sharedConfigParameters.put(
"externalBuildUrl",
"http://teamcityServer.com/viewLog.html?tab=buildLog&buildId=BuildNumber");

when(mockBuild.getBuildNumber()).thenReturn("BuildNumber");
when(mockBuild.getSharedConfigParameters()).thenReturn(sharedConfigParameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
import jetbrains.buildServer.serverSide.BuildStartContext;
import jetbrains.buildServer.serverSide.BuildStartContextProcessor;
import jetbrains.buildServer.serverSide.SRunningBuild;
import jetbrains.buildServer.serverSide.WebLinks;
import jetbrains.buildServer.vcs.VcsRootInstanceEntry;
import octopus.teamcity.common.OctopusConstants;

public class OctopusBuildInformationBuildStartProcessor implements BuildStartContextProcessor {

private final Logger logger = Loggers.SERVER;
private final WebLinks webLinks;

public OctopusBuildInformationBuildStartProcessor(final ExtensionHolder extensionHolder) {
public OctopusBuildInformationBuildStartProcessor(
final ExtensionHolder extensionHolder, final WebLinks webLinks) {
extensionHolder.registerExtension(
BuildStartContextProcessor.class, this.getClass().getName(), this);
this.webLinks = webLinks;
}

@Override
Expand All @@ -40,6 +44,8 @@ public void updateParameters(final BuildStartContext buildStartContext) {
}
buildStartContext.addSharedParameter("octopus_vcstype", vcsType);
}
final String buildUrl = webLinks.getViewLogUrl(buildStartContext.getBuild());
buildStartContext.addSharedParameter("externalBuildUrl", buildUrl);
}
} catch (final Throwable t) {
logger.error("Failed to write VCS type into the buildstartContext's shared parameters", t);
Expand Down

0 comments on commit c194001

Please sign in to comment.