Skip to content

Commit

Permalink
When creating a BuildData instance, use Git values from BuildData pop…
Browse files Browse the repository at this point in the history
…ulated during previous pipeline stages as fallback (#356)
  • Loading branch information
nikita-tkachenko-datadog authored Aug 28, 2023
1 parent 32f2ec7 commit 55103b9
Showing 1 changed file with 54 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,6 @@ of this software and associated documentation files (the "Software"), to deal
import hudson.triggers.SCMTrigger;
import hudson.triggers.TimerTrigger;
import hudson.util.LogTaskListener;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.datadog.jenkins.plugins.datadog.DatadogUtilities;
import org.datadog.jenkins.plugins.datadog.traces.BuildSpanManager;
import org.datadog.jenkins.plugins.datadog.traces.message.TraceSpan;
import org.datadog.jenkins.plugins.datadog.util.SuppressFBWarnings;
import org.datadog.jenkins.plugins.datadog.util.TagsUtil;
import org.datadog.jenkins.plugins.datadog.util.git.GitUtils;
import org.jenkinsci.plugins.gitclient.GitClient;

import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.Charset;
Expand All @@ -74,6 +64,16 @@ of this software and associated documentation files (the "Software"), to deal
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.datadog.jenkins.plugins.datadog.DatadogUtilities;
import org.datadog.jenkins.plugins.datadog.traces.BuildSpanAction;
import org.datadog.jenkins.plugins.datadog.traces.BuildSpanManager;
import org.datadog.jenkins.plugins.datadog.traces.message.TraceSpan;
import org.datadog.jenkins.plugins.datadog.util.SuppressFBWarnings;
import org.datadog.jenkins.plugins.datadog.util.TagsUtil;
import org.datadog.jenkins.plugins.datadog.util.git.GitUtils;
import org.jenkinsci.plugins.gitclient.GitClient;

public class BuildData implements Serializable {

Expand Down Expand Up @@ -229,6 +229,50 @@ public BuildData(Run run, TaskListener listener) throws IOException, Interrupted
setTraceId(Long.toUnsignedString(buildSpan.context().getTraceId()));
setSpanId(Long.toUnsignedString(buildSpan.context().getSpanId()));
}

BuildSpanAction buildSpanAction = run.getAction(BuildSpanAction.class);
if (buildSpanAction != null) {
getMissingGitValuesFrom(buildSpanAction.getBuildData());
}
}

private void getMissingGitValuesFrom(BuildData previousData) {
if (branch == null) {
branch = previousData.branch;
}
if (gitUrl == null) {
gitUrl = previousData.gitUrl;
}
if (gitCommit == null) {
gitCommit = previousData.gitCommit;
}
if (gitMessage == null) {
gitMessage = previousData.gitMessage;
}
if (gitAuthorName == null) {
gitAuthorName = previousData.gitAuthorName;
}
if (gitAuthorEmail == null) {
gitAuthorEmail = previousData.gitAuthorEmail;
}
if (gitAuthorDate == null) {
gitAuthorDate = previousData.gitAuthorDate;
}
if (gitCommitterName == null) {
gitCommitterName = previousData.gitCommitterName;
}
if (gitCommitterEmail == null) {
gitCommitterEmail = previousData.gitCommitterEmail;
}
if (gitCommitterDate == null) {
gitCommitterDate = previousData.gitCommitterDate;
}
if (gitDefaultBranch == null) {
gitDefaultBranch = previousData.gitDefaultBranch;
}
if (gitTag == null) {
gitTag = previousData.gitTag;
}
}

private void populateBuildParameters(Run<?,?> run) {
Expand Down

0 comments on commit 55103b9

Please sign in to comment.