Skip to content

Commit

Permalink
[fix] Env var precedence (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
kb-kerem authored Apr 25, 2024
1 parent c39512c commit 57fe0c1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
8 changes: 4 additions & 4 deletions visual-dotnet/SauceLabs.Visual/BuildFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ private static async Task<VisualBuild> Create(VisualApi api, CreateBuildOptions
options.CustomId ??= EnvVars.CustomId;
var result = (await api.CreateBuild(new CreateBuildIn
{
Name = StringUtils.ValueOrDefault(EnvVars.BuildName, options.Name),
Project = StringUtils.ValueOrDefault(EnvVars.Project, options.Project),
Branch = StringUtils.ValueOrDefault(EnvVars.Branch, options.Branch),
DefaultBranch = StringUtils.ValueOrDefault(EnvVars.DefaultBranch, options.DefaultBranch),
Name = StringUtils.ValueOrDefault(options.Name, EnvVars.BuildName),
Project = StringUtils.ValueOrDefault(options.Project, EnvVars.Project),
Branch = StringUtils.ValueOrDefault(options.Branch, EnvVars.Branch),
DefaultBranch = StringUtils.ValueOrDefault(options.DefaultBranch, EnvVars.DefaultBranch),
CustomId = options.CustomId,
})).EnsureValidResponse();

Expand Down
24 changes: 7 additions & 17 deletions visual-java/src/main/java/com/saucelabs/visual/VisualApi.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.saucelabs.visual;

import static com.saucelabs.visual.utils.EnvironmentVariables.isNotBlank;
import static com.saucelabs.visual.utils.EnvironmentVariables.valueOrDefault;

import com.saucelabs.visual.exception.VisualApiException;
import com.saucelabs.visual.graphql.*;
Expand Down Expand Up @@ -230,33 +231,22 @@ public String getName() {
if (isNotBlank(EnvironmentVariables.BUILD_NAME_DEPRECATED)) {
log.warn(
"Sauce Labs Visual: Environment variable \"BUILD_NAME\" is deprecated and will be removed in a future version. Please use \"SAUCE_VISUAL_BUILD_NAME\" instead.");
return EnvironmentVariables.BUILD_NAME_DEPRECATED;
}
if (isNotBlank(EnvironmentVariables.BUILD_NAME)) {
return EnvironmentVariables.BUILD_NAME;
}
return name;
return valueOrDefault(
valueOrDefault(name, EnvironmentVariables.BUILD_NAME),
EnvironmentVariables.BUILD_NAME_DEPRECATED);
}

public String getProject() {
if (isNotBlank(EnvironmentVariables.PROJECT_NAME)) {
return EnvironmentVariables.PROJECT_NAME;
}
return project;
return valueOrDefault(project, EnvironmentVariables.PROJECT_NAME);
}

public String getBranch() {
if (isNotBlank(EnvironmentVariables.BRANCH_NAME)) {
return EnvironmentVariables.BRANCH_NAME;
}
return branch;
return valueOrDefault(branch, EnvironmentVariables.BRANCH_NAME);
}

public String getDefaultBranch() {
if (isNotBlank(EnvironmentVariables.DEFAULT_BRANCH_NAME)) {
return EnvironmentVariables.DEFAULT_BRANCH_NAME;
}
return defaultBranch;
return valueOrDefault(defaultBranch, EnvironmentVariables.DEFAULT_BRANCH_NAME);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ private EnvironmentVariables() {}
public static boolean isNotBlank(String str) {
return str != null && !str.trim().isEmpty();
}

public static String valueOrDefault(String str, String defaultValue) {
return isNotBlank(str) ? str : defaultValue;
}
}

0 comments on commit 57fe0c1

Please sign in to comment.