Skip to content

Commit 18a659b

Browse files
feat: unify coalesce behavior
1 parent f5db27a commit 18a659b

File tree

9 files changed

+68
-40
lines changed

9 files changed

+68
-40
lines changed

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/CiVisibilityRepoServices.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ static PullRequestInfo buildPullRequestInfo(
125125
}
126126

127127
// complete with CI vars if user didn't provide all information
128-
PullRequestInfo ciInfo = PullRequestInfo.merge(userInfo, ciProviderInfo.buildPullRequestInfo());
128+
PullRequestInfo ciInfo = PullRequestInfo.coalesce(userInfo, ciProviderInfo.buildPullRequestInfo());
129129
String headSha = ciInfo.getHeadCommit().getSha();
130130
if (Strings.isNotBlank(headSha)) {
131131
// if head sha present try to populate author, committer and message info through git client
132132
try {
133133
CommitInfo commitInfo = gitClient.getCommitInfo(headSha, true);
134-
return PullRequestInfo.merge(
134+
return PullRequestInfo.coalesce(
135135
ciInfo, new PullRequestInfo(null, null, null, commitInfo, null));
136136
} catch (Exception ignored) {
137137
}
@@ -170,7 +170,7 @@ private static PullRequestInfo buildUserPullRequestInfo(
170170
new CommitInfo(environment.get(Constants.DDCI_PULL_REQUEST_SOURCE_SHA)),
171171
null);
172172

173-
return PullRequestInfo.merge(userInfo, ddCiInfo);
173+
return PullRequestInfo.coalesce(userInfo, ddCiInfo);
174174
}
175175

176176
private static String getRepoRoot(CIInfo ciInfo, GitClient.Factory gitClientFactory) {

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/ci/PullRequestInfo.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,19 @@ public boolean isComplete() {
6767
}
6868

6969
/**
70-
* Merges info by completing the empty information fields with the fallback's
70+
* Combine infos by completing the empty information fields in {@code first} with {@code second}'s
7171
*
72-
* @param info Base PR info
73-
* @param fallback Fallback PR info
74-
* @return Completed PR info
72+
* @param first Base PR info
73+
* @param second Fallback PR info
74+
* @return Combined PR info
7575
*/
76-
public static PullRequestInfo merge(PullRequestInfo info, PullRequestInfo fallback) {
76+
public static PullRequestInfo coalesce(final PullRequestInfo first, final PullRequestInfo second) {
7777
return new PullRequestInfo(
78-
Strings.isNotBlank(info.baseBranch) ? info.baseBranch : fallback.baseBranch,
79-
Strings.isNotBlank(info.baseBranchSha) ? info.baseBranchSha : fallback.baseBranchSha,
80-
Strings.isNotBlank(info.baseBranchHeadSha)
81-
? info.baseBranchHeadSha
82-
: fallback.baseBranchHeadSha,
83-
CommitInfo.merge(info.headCommit, fallback.headCommit),
84-
Strings.isNotBlank(info.pullRequestNumber)
85-
? info.pullRequestNumber
86-
: fallback.pullRequestNumber);
78+
Strings.coalesce(first.baseBranch, second.baseBranch),
79+
Strings.coalesce(first.baseBranchSha, second.baseBranchSha),
80+
Strings.coalesce(first.baseBranchHeadSha, second.baseBranchHeadSha),
81+
CommitInfo.coalesce(first.headCommit, second.headCommit),
82+
Strings.coalesce(first.pullRequestNumber, second.pullRequestNumber));
8783
}
8884

8985
@Override

dd-java-agent/agent-ci-visibility/src/test/groovy/datadog/trace/civisibility/ci/PullRequestInfoTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ class PullRequestInfoTest extends Specification {
3838
new PullRequestInfo("branch", "baseSha", SHA_A, COMMIT_A, "42") | true
3939
}
4040

41-
def "test info merge"() {
41+
def "test info coalesce"() {
4242
expect:
43-
PullRequestInfo.merge(infoA, infoB) == result
43+
PullRequestInfo.coalesce(infoA, infoB) == result
4444

4545
where:
4646
infoA | infoB | result

internal-api/src/main/java/datadog/trace/api/git/CommitInfo.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,18 @@ public boolean isComplete() {
5959
}
6060

6161
/**
62-
* Merges info by completing the empty information fields with the fallback's
62+
* Combine infos by completing the empty information fields in {@code first} with {@code second}'s
6363
*
64-
* @param info Base commit info
65-
* @param fallback Fallback commit info
66-
* @return Completed commit info
64+
* @param first Base commit info
65+
* @param second Fallback commit info
66+
* @return Combined commit info
6767
*/
68-
public static CommitInfo merge(CommitInfo info, CommitInfo fallback) {
68+
public static CommitInfo coalesce(final CommitInfo first, final CommitInfo second) {
6969
return new CommitInfo(
70-
Strings.isNotBlank(info.sha) ? info.sha : fallback.sha,
71-
PersonInfo.merge(info.author, fallback.author),
72-
PersonInfo.merge(info.committer, fallback.committer),
73-
Strings.isNotBlank(info.fullMessage) ? info.fullMessage : fallback.fullMessage);
70+
Strings.coalesce(first.sha, second.sha),
71+
PersonInfo.coalesce(first.author, second.author),
72+
PersonInfo.coalesce(first.committer, second.committer),
73+
Strings.coalesce(first.fullMessage, second.fullMessage));
7474
}
7575

7676
@Override

internal-api/src/main/java/datadog/trace/api/git/PersonInfo.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@ public boolean isComplete() {
5757
}
5858

5959
/**
60-
* Merges info by completing the empty information fields with the fallback's
60+
* Combine infos by completing the empty information fields in {@code first} with {@code second}'s
6161
*
62-
* @param info Base person info
63-
* @param fallback Fallback person info
64-
* @return Completed person info
62+
* @param first Base person info
63+
* @param second Fallback person info
64+
* @return Combined person info
6565
*/
66-
public static PersonInfo merge(final PersonInfo info, final PersonInfo fallback) {
66+
public static PersonInfo coalesce(final PersonInfo first, final PersonInfo second) {
6767
return new PersonInfo(
68-
Strings.isNotBlank(info.name) ? info.name : fallback.name,
69-
Strings.isNotBlank(info.email) ? info.email : fallback.email,
70-
Strings.isNotBlank(info.iso8601Date) ? info.iso8601Date : fallback.iso8601Date);
68+
Strings.coalesce(first.name, second.name),
69+
Strings.coalesce(first.email, second.email),
70+
Strings.coalesce(first.iso8601Date, second.iso8601Date));
7171
}
7272

7373
@Override

internal-api/src/main/java/datadog/trace/util/Strings.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.security.NoSuchAlgorithmException;
88
import java.util.concurrent.ThreadLocalRandom;
99
import javax.annotation.Nonnull;
10+
import javax.annotation.Nullable;
1011

1112
public final class Strings {
1213

@@ -240,4 +241,18 @@ public static String[] concat(String[] arr, String... extra) {
240241
System.arraycopy(extra, 0, result, arr.length, extra.length);
241242
return result;
242243
}
244+
245+
/**
246+
* @return first non-blank string out of the two, {@code null} if both are blank
247+
*/
248+
@Nullable
249+
public static String coalesce(@Nullable final String first, @Nullable final String second) {
250+
if (isNotBlank(first)) {
251+
return first;
252+
} else if (isNotBlank(second)) {
253+
return second;
254+
} else {
255+
return null;
256+
}
257+
}
243258
}

internal-api/src/test/groovy/datadog/trace/api/civisibility/git/CommitInfoTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ class CommitInfoTest extends Specification {
3333
new CommitInfo("sha", PERSON_A, PERSON_A, "msg") | true
3434
}
3535

36-
def "test info merge"() {
36+
def 'test info coalesce'() {
3737
expect:
38-
CommitInfo.merge(infoA, infoB) == result
38+
CommitInfo.coalesce(infoA, infoB) == result
3939
where:
4040
infoA | infoB | result
4141
new CommitInfo("shaA", PERSON_A, PERSON_A, "msgA") | new CommitInfo("shaB", PERSON_B, PERSON_B, "msgB") | new CommitInfo("shaA", PERSON_A, PERSON_A, "msgA")

internal-api/src/test/groovy/datadog/trace/api/civisibility/git/PersonInfoTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ class PersonInfoTest extends Specification {
2828
new PersonInfo("name", "email", "date") | true
2929
}
3030

31-
def "test info merge"() {
31+
def 'test info coalesce'() {
3232
expect:
33-
PersonInfo.merge(infoA, infoB) == result
33+
PersonInfo.coalesce(infoA, infoB) == result
3434
where:
3535
infoA | infoB | result
3636
new PersonInfo("nameA", "emailA", "dateA") | new PersonInfo("nameB", "emailB", "dateB") | new PersonInfo("nameA", "emailA", "dateA")

internal-api/src/test/groovy/datadog/trace/util/StringsTest.groovy

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,21 @@ class StringsTest extends DDSpecification {
158158
'' | ''
159159
'[email protected]' | '7A6F757A6F754073616E73676C7574656E2E636F6D'
160160
}
161+
162+
void 'test coalesce: #first - #second'() {
163+
when:
164+
def combined = Strings.coalesce(first, second)
165+
166+
then:
167+
expected == combined
168+
169+
where:
170+
first | second | expected
171+
"a" | "b" | "a"
172+
"a" | null | "a"
173+
null | "b" | "b"
174+
"" | "b" | "b"
175+
null | null | null
176+
"" | "" | null
177+
}
161178
}

0 commit comments

Comments
 (0)