Skip to content

Commit 12c2b6d

Browse files
author
TheSnoozer
committed
#17: Avoid incomplete branch names on AzureDevOps
1 parent ad7cf6e commit 12c2b6d

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

src/main/java/pl/project13/core/cibuild/AzureDevOpsBuildServerData.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class AzureDevOpsBuildServerData extends BuildServerDataProvider {
3636
* @see <a href="https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables">Azure DevOps - Build variables</a>
3737
*/
3838
public static boolean isActiveServer(@Nonnull Map<String, String> env) {
39-
return env.containsKey("AZURE_HTTP_USER_AGENT");
39+
return env.containsKey("AZURE_HTTP_USER_AGENT") || env.containsKey("TF_BUILD");
4040
}
4141

4242
@Override
@@ -48,8 +48,37 @@ void loadBuildNumber(@Nonnull Properties properties) {
4848

4949
@Override
5050
public String getBuildBranch() {
51-
String environmentBasedBuildSourceBranchName = env.get("BUILD_SOURCEBRANCHNAME");
52-
log.info(String.format("Using environment variable based branch name. BUILD_SOURCEBRANCHNAME = %s", environmentBasedBuildSourceBranchName));
53-
return environmentBasedBuildSourceBranchName;
51+
/**
52+
* Build.SourceBranch
53+
* The branch of the triggering repo the build was queued for. Some examples:
54+
* - Git repo branch: refs/heads/main
55+
* - Git repo pull request: refs/pull/1/merge
56+
* - TFVC repo branch: $/teamproject/main
57+
* - TFVC repo gated check-in: Gated_2016-06-06_05.20.51.4369;username@live.com
58+
* - TFVC repo shelveset build: myshelveset;username@live.com
59+
* - When your pipeline is triggered by a tag: refs/tags/your-tag-name
60+
*/
61+
String environmentBasedBuildSourceBranch = env.get("BUILD_SOURCEBRANCH");
62+
if (environmentBasedBuildSourceBranch != null && !environmentBasedBuildSourceBranch.isEmpty()) {
63+
if (environmentBasedBuildSourceBranch.startsWith(BRANCH_REF_PREFIX)) {
64+
String branchName = environmentBasedBuildSourceBranch.substring(BRANCH_REF_PREFIX.length());
65+
log.info(String.format("Using environment variable based branch name. BUILD_SOURCEBRANCH = %s (branch = %s)",
66+
environmentBasedBuildSourceBranch, branchName));
67+
return branchName;
68+
}
69+
if (environmentBasedBuildSourceBranch.startsWith(PULL_REQUEST_REF_PREFIX)) {
70+
String branchName = environmentBasedBuildSourceBranch.substring(PULL_REQUEST_REF_PREFIX.length());
71+
log.info(String.format("Using environment variable based branch name. BUILD_SOURCEBRANCH = %s (branch = %s)",
72+
environmentBasedBuildSourceBranch, branchName));
73+
return branchName;
74+
}
75+
if (environmentBasedBuildSourceBranch.startsWith(TAG_REF_PREFIX)) {
76+
String branchName = environmentBasedBuildSourceBranch.substring(TAG_REF_PREFIX.length());
77+
log.info(String.format("Using environment variable based branch name. BUILD_SOURCEBRANCH = %s (branch = %s)",
78+
environmentBasedBuildSourceBranch, branchName));
79+
return branchName;
80+
}
81+
}
82+
return "";
5483
}
5584
}

src/main/java/pl/project13/core/cibuild/BuildServerDataProvider.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public abstract class BuildServerDataProvider {
3939
private List<String> excludeProperties = null;
4040
private List<String> includeOnlyProperties = null;
4141
private Map<String, Supplier<String>> additionalProperties = new HashMap<>();
42+
protected static final String BRANCH_REF_PREFIX = "refs/heads/";
43+
protected static final String PULL_REQUEST_REF_PREFIX = "refs/pull/";
44+
protected static final String TAG_REF_PREFIX = "refs/tags/";
4245

4346
BuildServerDataProvider(@Nonnull LogInterface log, @Nonnull Map<String, String> env) {
4447
this.log = log;

src/main/java/pl/project13/core/cibuild/GitHubBuildServerData.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@
2424
import java.util.Properties;
2525

2626
public class GitHubBuildServerData extends BuildServerDataProvider {
27-
28-
private static final String BRANCH_REF_PREFIX = "refs/heads/";
29-
private static final String PULL_REQUEST_REF_PREFIX = "refs/pull/";
30-
3127
GitHubBuildServerData(LogInterface log, @Nonnull Map<String, String> env) {
3228
super(log, env);
3329
}

0 commit comments

Comments
 (0)