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

WIP: provide COMMIT_MESSAGE as an env var #796

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,7 @@ GIT_LOCAL_BRANCH:: Name of branch being built without remote name, as in `master
GIT_COMMIT:: SHA-1 of the commit used in this build
GIT_PREVIOUS_COMMIT:: SHA-1 of the commit used in the preceding build of this project
GIT_PREVIOUS_SUCCESSFUL_COMMIT:: SHA-1 of the commit used in the most recent successful build of this project
GIT_COMMIT_TITLE:: The text up to the first blank line in the commit message used in this build

[#system-configuration-variables]
=== System Configuration Variables
Expand Down
23 changes: 20 additions & 3 deletions src/main/java/hudson/plugins/git/GitSCM.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public class GitSCM extends GitSCMBackwardCompatibility {
public static final String GIT_LOCAL_BRANCH = "GIT_LOCAL_BRANCH";
public static final String GIT_CHECKOUT_DIR = "GIT_CHECKOUT_DIR";
public static final String GIT_COMMIT = "GIT_COMMIT";
public static final String GIT_COMMIT_TITLE = "GIT_COMMIT_TITLE";
public static final String GIT_PREVIOUS_COMMIT = "GIT_PREVIOUS_COMMIT";
public static final String GIT_PREVIOUS_SUCCESSFUL_COMMIT = "GIT_PREVIOUS_SUCCESSFUL_COMMIT";

Expand Down Expand Up @@ -1352,6 +1353,9 @@ public void checkout(Run<?, ?> build, Launcher launcher, FilePath workspace, Tas

// Needs to be after the checkout so that revToBuild is in the workspace
try {
String shortMessage = getCommitMessage(listener, git, revToBuild);
listener.getLogger().println("Commit message: \"" + shortMessage + "\"");
environment.put(GIT_COMMIT_TITLE, shortMessage);
Copy link
Contributor

Choose a reason for hiding this comment

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

@MarkEWaite In this(https://github.com/jenkinsci/git-plugin/pull/1049/files#r583842074) pull request you mentioned with this enviroment variable is set only for the process.
I want to understand what is the scope of adding a variable to EnvVars is? Since it is being added in git plugin would it be available to just the checkout step or it will be available in subsequent steps like build etc. ? From the bug description I believe we want to use this env variable to check if we want to run the build step based on the commit message. So would this env variable be available in the pipeline script in the build step with this ?

printCommitMessageToLog(listener, git, revToBuild);
} catch (IOException | ArithmeticException | GitException ge) {
// JENKINS-45729 reports a git exception when revToBuild cannot be found in the workspace.
Expand Down Expand Up @@ -1380,14 +1384,27 @@ public void checkout(Run<?, ?> build, Launcher launcher, FilePath workspace, Tas
}
}

private void printCommitMessageToLog(TaskListener listener, GitClient git, final Build revToBuild)
throws IOException {
/**
* Get the short message from the last commit
*
* @param listener
* Used for writing to build console
* @param git
* Used for invoking Git
* @param revToBuild
* Points to the revision we'll be building. This includes all the branches we've merged.
*
* @return Short message for the commit
* @throws IOException
*/
private String getCommitMessage(TaskListener listener, GitClient git, final Build revToBuild) throws IOException {
try {
RevCommit commit = git.withRepository(new RevCommitRepositoryCallback(revToBuild));
listener.getLogger().println("Commit message: \"" + commit.getShortMessage() + "\"");
return commit.getShortMessage();
} catch (InterruptedException | MissingObjectException e) {
e.printStackTrace(listener.error("Unable to retrieve commit message"));
}
return "";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package hudson.plugins.git.GitSCM
def l = namespace(lib.JenkinsTagLib)

// TODO handle GitSCMExtension.populateEnvironmentVariables somehow, say by optionally including GitSCMExtension/buildEnv.groovy; though GIT_{COMMITTER,AUTHOR}_{NAME,EMAIL} are only overridden by UserIdentity
['GIT_COMMIT', 'GIT_PREVIOUS_COMMIT', 'GIT_PREVIOUS_SUCCESSFUL_COMMIT', 'GIT_BRANCH', 'GIT_LOCAL_BRANCH', 'GIT_CHECKOUT_DIR', 'GIT_URL', 'GIT_COMMITTER_NAME', 'GIT_AUTHOR_NAME', 'GIT_COMMITTER_EMAIL', 'GIT_AUTHOR_EMAIL'].each {name ->
['GIT_COMMIT', 'GIT_PREVIOUS_COMMIT', 'GIT_PREVIOUS_SUCCESSFUL_COMMIT', 'GIT_BRANCH', 'GIT_LOCAL_BRANCH', 'GIT_CHECKOUT_DIR', 'GIT_URL', 'GIT_COMMITTER_NAME', 'GIT_AUTHOR_NAME', 'GIT_COMMITTER_EMAIL', 'GIT_AUTHOR_EMAIL', 'GIT_COMMIT_TITLE'].each {name ->
l.buildEnvVar(name: name) {
raw(_("${name}.blurb"))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
GIT_COMMIT.blurb=The commit hash being checked out.
GIT_PREVIOUS_COMMIT.blurb=The hash of the commit last built on this branch, if any.
GIT_PREVIOUS_SUCCESSFUL_COMMIT.blurb=The hash of the commit last successfully built on this branch, if any.
GIT_COMMIT_TITLE.blurb=The text up to the first blank line in a commit message is treated as the commit title.
GIT_BRANCH.blurb=The remote branch name, if any.
GIT_LOCAL_BRANCH.blurb=The local branch name being checked out, if applicable.
GIT_CHECKOUT_DIR.blurb=The directory that the repository will be checked out to. This contains the value set in Checkout to a sub-directory, if used.
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/hudson/plugins/git/GitSCMTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3064,6 +3064,24 @@ public void testCommitMessageIsPrintedToLogs() throws Exception {
assertThat(values, hasItem("Commit message: \"test commit\""));
}

@Test
public void testCommitMessageIsEnvVar() throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar to this I was trying to set environment variable for https://issues.jenkins.io/browse/JENKINS-38699. Does this test case run correctly in the current state?

String title = "test commit";
sampleRepo.init();
sampleRepo.write("file", "v1");
sampleRepo.git("commit", "--all", "--message", title);
FreeStyleProject p = setupSimpleProject("master");
Run<?,?> run = rule.buildAndAssertSuccess(p);
TaskListener mockListener = Mockito.mock(TaskListener.class);
Mockito.when(mockListener.getLogger()).thenReturn(Mockito.spy(StreamTaskListener.fromStdout().getLogger()));

p.getScm().checkout(run, new Launcher.LocalLauncher(listener),
new FilePath(run.getRootDir()).child("tmp-" + "master"),
mockListener, null, SCMRevisionState.NONE);

assertEquals("Commit message should be an env var", title, getEnvVars(p).get(GitSCM.GIT_COMMIT_TITLE));
}

/**
* Method performs HTTP get on "notifyCommit" URL, passing it commit by SHA1
* and tests for build data consistency.
Expand Down