Skip to content

Commit

Permalink
#17: Avoid incomplete branch names on AzureDevOps
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSnoozer committed May 4, 2023
1 parent ad7cf6e commit 12c2b6d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class AzureDevOpsBuildServerData extends BuildServerDataProvider {
* @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>
*/
public static boolean isActiveServer(@Nonnull Map<String, String> env) {
return env.containsKey("AZURE_HTTP_USER_AGENT");
return env.containsKey("AZURE_HTTP_USER_AGENT") || env.containsKey("TF_BUILD");
}

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

@Override
public String getBuildBranch() {
String environmentBasedBuildSourceBranchName = env.get("BUILD_SOURCEBRANCHNAME");
log.info(String.format("Using environment variable based branch name. BUILD_SOURCEBRANCHNAME = %s", environmentBasedBuildSourceBranchName));
return environmentBasedBuildSourceBranchName;
/**
* Build.SourceBranch
* The branch of the triggering repo the build was queued for. Some examples:
* - Git repo branch: refs/heads/main
* - Git repo pull request: refs/pull/1/merge
* - TFVC repo branch: $/teamproject/main
* - TFVC repo gated check-in: Gated_2016-06-06_05.20.51.4369;[email protected]
* - TFVC repo shelveset build: myshelveset;[email protected]
* - When your pipeline is triggered by a tag: refs/tags/your-tag-name
*/
String environmentBasedBuildSourceBranch = env.get("BUILD_SOURCEBRANCH");
if (environmentBasedBuildSourceBranch != null && !environmentBasedBuildSourceBranch.isEmpty()) {
if (environmentBasedBuildSourceBranch.startsWith(BRANCH_REF_PREFIX)) {
String branchName = environmentBasedBuildSourceBranch.substring(BRANCH_REF_PREFIX.length());
log.info(String.format("Using environment variable based branch name. BUILD_SOURCEBRANCH = %s (branch = %s)",
environmentBasedBuildSourceBranch, branchName));
return branchName;
}
if (environmentBasedBuildSourceBranch.startsWith(PULL_REQUEST_REF_PREFIX)) {
String branchName = environmentBasedBuildSourceBranch.substring(PULL_REQUEST_REF_PREFIX.length());
log.info(String.format("Using environment variable based branch name. BUILD_SOURCEBRANCH = %s (branch = %s)",
environmentBasedBuildSourceBranch, branchName));
return branchName;
}
if (environmentBasedBuildSourceBranch.startsWith(TAG_REF_PREFIX)) {
String branchName = environmentBasedBuildSourceBranch.substring(TAG_REF_PREFIX.length());
log.info(String.format("Using environment variable based branch name. BUILD_SOURCEBRANCH = %s (branch = %s)",
environmentBasedBuildSourceBranch, branchName));
return branchName;
}
}
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public abstract class BuildServerDataProvider {
private List<String> excludeProperties = null;
private List<String> includeOnlyProperties = null;
private Map<String, Supplier<String>> additionalProperties = new HashMap<>();
protected static final String BRANCH_REF_PREFIX = "refs/heads/";
protected static final String PULL_REQUEST_REF_PREFIX = "refs/pull/";
protected static final String TAG_REF_PREFIX = "refs/tags/";

BuildServerDataProvider(@Nonnull LogInterface log, @Nonnull Map<String, String> env) {
this.log = log;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@
import java.util.Properties;

public class GitHubBuildServerData extends BuildServerDataProvider {

private static final String BRANCH_REF_PREFIX = "refs/heads/";
private static final String PULL_REQUEST_REF_PREFIX = "refs/pull/";

GitHubBuildServerData(LogInterface log, @Nonnull Map<String, String> env) {
super(log, env);
}
Expand Down

0 comments on commit 12c2b6d

Please sign in to comment.