Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting Git information via environment variables #252

Merged
merged 7 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ of this software and associated documentation files (the "Software"), to deal

import hudson.EnvVars;
import hudson.ExtensionList;
import hudson.FilePath;
import hudson.ProxyConfiguration;
import hudson.XmlFile;
import hudson.console.AnnotatedLargeText;
import hudson.model.*;
import hudson.model.Computer;
import hudson.model.Item;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.User;
import hudson.model.labels.LabelAtom;
import jenkins.model.Jenkins;
import org.apache.commons.lang.ObjectUtils.Null;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.datadog.jenkins.plugins.datadog.model.CIGlobalTagsAction;
import org.datadog.jenkins.plugins.datadog.model.GitCommitAction;
import org.datadog.jenkins.plugins.datadog.model.GitRepositoryAction;
Expand All @@ -53,22 +53,24 @@ of this software and associated documentation files (the "Software"), to deal
import org.jenkinsci.plugins.pipeline.StageStatus;
import org.jenkinsci.plugins.workflow.actions.ErrorAction;
import org.jenkinsci.plugins.workflow.actions.LabelAction;
import org.jenkinsci.plugins.workflow.actions.LogAction;
import org.jenkinsci.plugins.workflow.actions.NotExecutedNodeAction;
import org.jenkinsci.plugins.workflow.actions.QueueItemAction;
import org.jenkinsci.plugins.workflow.actions.StageAction;
import org.jenkinsci.plugins.workflow.actions.ThreadNameAction;
import org.jenkinsci.plugins.workflow.actions.TimingAction;
import org.jenkinsci.plugins.workflow.actions.WarningAction;
import org.jenkinsci.plugins.workflow.flow.FlowExecution;
import org.jenkinsci.plugins.workflow.graph.BlockEndNode;
import org.jenkinsci.plugins.workflow.graph.BlockStartNode;
import org.jenkinsci.plugins.workflow.graph.FlowEndNode;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.steps.StepContext;

import javax.annotation.Nonnull;
import java.io.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.Inet4Address;
import java.net.MalformedURLException;
Expand All @@ -77,7 +79,16 @@ of this software and associated documentation files (the "Software"), to deal
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -893,34 +904,6 @@ public static boolean isPipeline(final Run<?, ?> run) {
return run != null && run.getAction(IsPipelineAction.class) != null;
}

/**
* Returns the Git repository URL based on the EnvVars.
*
* Sometimes multiple repositories can be involved (e.g. forks).
* Jenkins can use the pattern GIT_URL_N with N {1,2,…}.
* By convention, GIT_URL_1 should be the same as GIT_URL
*
* @param envVars
* @return git repository URL
*/
public static String getGitRepositoryUrl(final EnvVars envVars) {
return getGitRepositoryUrl(envVars.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to GitUtils class with the logic to select the correct env var (either user supplied env var or jenkins env var)

/**
* Returns the Git repository URL based on the EnvVars.
*
* Sometimes multiple repositories can be involved (e.g. forks).
* Jenkins can use the pattern GIT_URL_N with N {1,2,…}.
* By convention, GIT_URL_1 should be the same as GIT_URL
*
* @param envVars
* @return git repository URL
*/
public static String getGitRepositoryUrl(final Map<String, String> envVars) {
return StringUtils.isNotEmpty(envVars.get("GIT_URL")) ? envVars.get("GIT_URL") : envVars.get("GIT_URL_1");
}

/**
* Returns an HTTP url connection given a url object. Supports jenkins configured proxy.
*
Expand Down Expand Up @@ -971,6 +954,4 @@ public static HttpURLConnection getHttpURLConnection(final URL url, final int ti
public static URL buildHttpURL(final String hostname, final Integer port, final String path) throws MalformedURLException {
return new URL(String.format("http://%s:%d"+path, hostname, port));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,21 @@ of this software and associated documentation files (the "Software"), to deal

package org.datadog.jenkins.plugins.datadog.model;

import static org.datadog.jenkins.plugins.datadog.util.git.GitConstants.DD_GIT_BRANCH;
import static org.datadog.jenkins.plugins.datadog.util.git.GitConstants.DD_GIT_COMMIT_AUTHOR_DATE;
import static org.datadog.jenkins.plugins.datadog.util.git.GitConstants.DD_GIT_COMMIT_AUTHOR_EMAIL;
import static org.datadog.jenkins.plugins.datadog.util.git.GitConstants.DD_GIT_COMMIT_AUTHOR_NAME;
import static org.datadog.jenkins.plugins.datadog.util.git.GitConstants.DD_GIT_COMMIT_COMMITTER_DATE;
import static org.datadog.jenkins.plugins.datadog.util.git.GitConstants.DD_GIT_COMMIT_COMMITTER_EMAIL;
import static org.datadog.jenkins.plugins.datadog.util.git.GitConstants.DD_GIT_COMMIT_COMMITTER_NAME;
import static org.datadog.jenkins.plugins.datadog.util.git.GitConstants.DD_GIT_COMMIT_MESSAGE;
import static org.datadog.jenkins.plugins.datadog.util.git.GitConstants.DD_GIT_COMMIT_SHA;
import static org.datadog.jenkins.plugins.datadog.util.git.GitConstants.DD_GIT_REPOSITORY_URL;
import static org.datadog.jenkins.plugins.datadog.util.git.GitConstants.DD_GIT_TAG;
import static org.datadog.jenkins.plugins.datadog.util.git.GitConstants.GIT_BRANCH;
import static org.datadog.jenkins.plugins.datadog.util.git.GitUtils.isCommitInfoAlreadyCreated;
import static org.datadog.jenkins.plugins.datadog.util.git.GitUtils.isRepositoryInfoAlreadyCreated;
import static org.datadog.jenkins.plugins.datadog.util.git.GitUtils.isUserSuppliedGit;
import static org.datadog.jenkins.plugins.datadog.util.git.GitUtils.isValidCommit;
import static org.datadog.jenkins.plugins.datadog.util.git.GitUtils.isValidRepositoryURL;

Expand Down Expand Up @@ -93,6 +106,7 @@ public class BuildData implements Serializable {
private String gitCommitterEmail;
private String gitCommitterDate;
private String gitDefaultBranch;
private String gitTag;

// Environment variable from the promoted build plugin
// - See https://plugins.jenkins.io/promoted-builds
Expand Down Expand Up @@ -242,10 +256,23 @@ private void populateEnvVariables(EnvVars envVars){
setExecutorNumber(envVars.get("EXECUTOR_NUMBER"));
setJavaHome(envVars.get("JAVA_HOME"));
setWorkspace(envVars.get("WORKSPACE"));
if (envVars.get("GIT_BRANCH") != null) {
setBranch(envVars.get("GIT_BRANCH"));
setGitUrl(DatadogUtilities.getGitRepositoryUrl(envVars));
setGitCommit(envVars.get("GIT_COMMIT"));
if (isGit(envVars)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is isGit only called in BuildData but not in DatadogTracePipelineLogic?

setBranch(GitUtils.resolveGitBranch(envVars, null));
setGitUrl(GitUtils.resolveGitRepositoryUrl(envVars, null));
setGitCommit(GitUtils.resolveGitCommit(envVars, null));
setGitTag(GitUtils.resolveGitTag(envVars, null));

// Git data supplied by the user has prevalence. We set them first.
// Only the data that has not been set will be updated later.
// If any value is not provided, we maintained the original value if any.
setGitMessage(envVars.get(DD_GIT_COMMIT_MESSAGE, this.gitMessage));
setGitAuthorName(envVars.get(DD_GIT_COMMIT_AUTHOR_NAME, this.gitAuthorName));
setGitAuthorEmail(envVars.get(DD_GIT_COMMIT_AUTHOR_EMAIL, this.gitAuthorEmail));
setGitAuthorDate(envVars.get(DD_GIT_COMMIT_AUTHOR_DATE, this.gitAuthorDate));
setGitCommitterName(envVars.get(DD_GIT_COMMIT_COMMITTER_NAME, this.gitCommitterName));
setGitCommitterEmail(envVars.get(DD_GIT_COMMIT_COMMITTER_EMAIL, this.gitCommitterEmail));
setGitCommitterDate(envVars.get(DD_GIT_COMMIT_COMMITTER_DATE, this.gitCommitterDate));

} else if (envVars.get("CVS_BRANCH") != null) {
setBranch(envVars.get("CVS_BRANCH"));
}
Expand Down Expand Up @@ -300,7 +327,7 @@ private void populateGitVariables(Run<?,?> run, TaskListener listener, EnvVars e

final GitClient gitClient = GitUtils.newGitClient(run, listener, envVars, this.nodeName, this.workspace);
if(isValidCommit(this.gitCommit)){
populateCommitInfo(GitUtils.buildGitCommitAction(run, gitClient ,this.gitCommit));
populateCommitInfo(GitUtils.buildGitCommitAction(run, gitClient, this.gitCommit));
}

if(isValidRepositoryURL(this.gitUrl)){
Expand All @@ -313,31 +340,58 @@ private void populateGitVariables(Run<?,?> run, TaskListener listener, EnvVars e

/**
* Populate the information related to the commit (message, author and committer) based on the GitCommitAction
* only if the user has not set the value manually.
* @param gitCommitAction
*/
private void populateCommitInfo(GitCommitAction gitCommitAction) {
if(gitCommitAction != null) {
this.gitMessage = gitCommitAction.getMessage();
this.gitAuthorName = gitCommitAction.getAuthorName();
this.gitAuthorEmail = gitCommitAction.getAuthorEmail();
this.gitAuthorDate = gitCommitAction.getAuthorDate();
this.gitCommitterName = gitCommitAction.getCommitterName();
this.gitCommitterEmail = gitCommitAction.getCommitterEmail();
this.gitCommitterDate = gitCommitAction.getCommitterDate();
// If any value is not empty, it means that
// the user supplied the value manually
// via environment variables.

if(getGitMessage("").isEmpty()){
setGitMessage(gitCommitAction.getMessage());
}

if(getGitAuthorName("").isEmpty()){
setGitAuthorName(gitCommitAction.getAuthorName());
}

if(getGitAuthorEmail("").isEmpty()) {
setGitAuthorEmail(gitCommitAction.getAuthorEmail());
}

if(getGitAuthorDate("").isEmpty()){
setGitAuthorDate(gitCommitAction.getAuthorDate());
}

if(getGitCommitterName("").isEmpty()){
setGitCommitterName(gitCommitAction.getCommitterName());
}

if(getGitCommitterEmail("").isEmpty()){
setGitCommitterEmail(gitCommitAction.getCommitterEmail());
}

if(getGitCommitterDate("").isEmpty()){
setGitCommitterDate(gitCommitAction.getCommitterDate());
}
}
}

/**
* Return if the Run is based on Git repository checking
* the GIT_BRANCH environment variable.
* the GIT_BRANCH environment variable or the user supplied
* environment variables.
* @param envVars
* @return true if GIT_BRANCH is set.
* @return true if GIT_BRANCH is set or the user supplied GIT information via env vars.
*/
private boolean isGit(EnvVars envVars) {
if(envVars == null){
return false;
}
return envVars.get("GIT_BRANCH") != null;

return isUserSuppliedGit(envVars) || envVars.get(GIT_BRANCH) != null;
}

/**
Expand Down Expand Up @@ -652,6 +706,14 @@ public void setGitDefaultBranch(String gitDefaultBranch) {
this.gitDefaultBranch = gitDefaultBranch;
}

public String getGitTag(String value) {
return defaultIfNull(gitTag, value);
}

public void setGitTag(String gitTag) {
this.gitTag = gitTag;
}

public String getPromotedUrl(String value) {
return defaultIfNull(promotedUrl, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
Expand Down Expand Up @@ -208,7 +209,11 @@ public void finishBuildTrace(final BuildData buildData, final Run<?,?> run) {
buildSpan.putMeta(CITags.GIT_BRANCH, gitBranch);
}

final String gitTag = normalizeTag(rawGitBranch);
// Check if the user set manually the DD_GIT_TAG environment variable.
// Otherwise, Jenkins reports the tag in the Git branch information. (e.g. origin/tags/0.1.0)
final String gitTag = Optional.of(buildData.getGitTag("").isEmpty() ? updatedBuildData.getGitTag("") : buildData.getGitTag(""))
.filter(tag -> !tag.isEmpty())
.orElse(normalizeTag(rawGitBranch));
if(StringUtils.isNotEmpty(gitTag)) {
buildSpan.putMeta(CITags.GIT_TAG, gitTag);
}
Expand Down
Loading