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

Why not to expose commit hash from SCM as an environment variable? #166

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions src/main/java/jenkins/branch/BranchNameContributor.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.Date;
import jenkins.scm.api.SCMHeadOrigin;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.metadata.ContributorMetadataAction;
import jenkins.scm.api.metadata.ObjectMetadataAction;
import jenkins.scm.api.mixin.ChangeRequestSCMHead;
Expand All @@ -57,10 +58,14 @@ public void buildEnvironmentFor(Job j, EnvVars envs, TaskListener listener) thro
BranchProjectFactory projectFactory = ((MultiBranchProject) parent).getProjectFactory();
if (projectFactory.isProject(j)) {
Branch branch = projectFactory.getBranch(j);
SCMRevision rev = projectFactory.getRevision(j);
SCMHead head = branch.getHead();
// Note: not using Branch.name, since in the future that could be something different
// than SCMHead.name, which is what we really want here.
envs.put("BRANCH_NAME", head.getName());
if (rev != null) {
envs.put("CHANGE_REVISION", rev.toString());
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure that rev.toString() is the right thing here, as it is intended to be
"human-readable". It may work in some of implementations but it, I'm not sure.

It looks like SCMRevision doesn't have a method for what you're trying to do. There probably needs to be a getId() method added to that in order for this to work reliably. Note, we'll also need to handle ChangeRequestSCMRevision differently since it has several possible revisions that users would be interested in.

It might be better if each SCM source implemented their own environment contributor.

}
if (head instanceof ChangeRequestSCMHead) {
envs.putIfNotNull("CHANGE_ID", ((ChangeRequestSCMHead) head).getId());
SCMHead target = ((ChangeRequestSCMHead) head).getTarget();
Expand All @@ -83,13 +88,15 @@ public void buildEnvironmentFor(Job j, EnvVars envs, TaskListener listener) thro
envs.putIfNotNull("CHANGE_AUTHOR_DISPLAY_NAME", cma.getContributorDisplayName());
envs.putIfNotNull("CHANGE_AUTHOR_EMAIL", cma.getContributorEmail());
}

}
if (head instanceof TagSCMHead) {
envs.put("TAG_NAME", head.getName());
envs.putIfNotNull("TAG_TIMESTAMP", Long.toString(((TagSCMHead) head).getTimestamp()));
envs.putIfNotNull("TAG_UNIXTIME", Long.toString(((TagSCMHead) head).getTimestamp()/1000L));
envs.putIfNotNull("TAG_DATE", new Date(((TagSCMHead) head).getTimestamp()).toString());
}

}
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/test/java/jenkins/branch/BranchNameContributorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public void buildEnvironmentFor() throws Exception {
c.createRepository("foo", MockRepositoryFlags.FORKABLE);
Integer cr1Num = c.openChangeRequest("foo", "master");
Integer cr2Num = c.openChangeRequest("foo", "master", MockChangeRequestFlags.FORK);
String rev = c.getRevision("foo", "master");
c.createTag("foo", "master", "v1.0");
BasicMultiBranchProject prj = r.jenkins.createProject(BasicMultiBranchProject.class, "foo");
prj.setCriteria(null);
Expand All @@ -105,19 +106,25 @@ public void buildEnvironmentFor() throws Exception {
FreeStyleProject cr2 = prj.getItem("CR-" + cr2Num);
FreeStyleProject tag = prj.getItem("v1.0");
assertThat("We now have the master branch", master, notNullValue());
assertThat("We now have the revision", rev, notNullValue());
assertThat("We now have the origin CR branch", cr1, notNullValue());
assertThat("We now have the form CR branch", cr2, notNullValue());
assertThat("We now have the tag branch", tag, notNullValue());
EnvVars env = new EnvVars();
instance.buildEnvironmentFor(master, env, new LogTaskListener(LOGGER, Level.FINE));
assertThat(env.keySet(), contains(is("BRANCH_NAME")));
assertThat(env.keySet(), containsInAnyOrder(
is("BRANCH_NAME"),
is("CHANGE_REVISION")
));
assertThat(env.get("BRANCH_NAME"), is("master"));
assertThat(env.get("CHANGE_REVISION"), is(rev));

env = new EnvVars();
instance.buildEnvironmentFor(cr1, env, new LogTaskListener(LOGGER, Level.FINE));
assertThat(env.keySet(), containsInAnyOrder(
is("BRANCH_NAME"),
is("CHANGE_ID"),
is("CHANGE_REVISION"),
is("CHANGE_TARGET"),
is("CHANGE_TITLE"),
is("CHANGE_URL"),
Expand All @@ -142,6 +149,7 @@ public void buildEnvironmentFor() throws Exception {
is("BRANCH_NAME"),
is("CHANGE_ID"),
is("CHANGE_TARGET"),
is("CHANGE_REVISION"),
is("CHANGE_TITLE"),
is("CHANGE_URL"),
is("CHANGE_BRANCH"),
Expand Down