-
Notifications
You must be signed in to change notification settings - Fork 49
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e24ab0d
Support user supplied git environment variables
drodriguezhdez d0d782a
User provided Git environment variables
drodriguezhdez 5b2e518
Refactor resolve git functions
drodriguezhdez 1404db7
Remove unnecesary constant
drodriguezhdez 43be389
Add comments
drodriguezhdez a98fbdb
Use correct function
drodriguezhdez 3014a9f
Apply PR feedback
drodriguezhdez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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 | ||
|
@@ -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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is |
||
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")); | ||
} | ||
|
@@ -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)){ | ||
|
@@ -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; | ||
} | ||
|
||
/** | ||
|
@@ -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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)