-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add BUILD_USER macro which returns the user who caused the build (
#145)
- Loading branch information
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/main/java/org/jenkinsci/plugins/tokenmacro/impl/BuildUserMacro.java
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.jenkinsci.plugins.tokenmacro.impl; | ||
|
||
import com.google.common.collect.ListMultimap; | ||
import hudson.Extension; | ||
import hudson.FilePath; | ||
import hudson.model.AbstractBuild; | ||
import hudson.model.Cause; | ||
import hudson.model.Run; | ||
import hudson.model.TaskListener; | ||
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException; | ||
import org.jenkinsci.plugins.tokenmacro.TokenMacro; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Extension | ||
public class BuildUserMacro extends TokenMacro { | ||
public static final String MACRO_NAME = "BUILD_USER"; | ||
|
||
@Override | ||
public boolean acceptsMacroName(String macroName) { | ||
return macroName.equals(MACRO_NAME); | ||
} | ||
|
||
@Override | ||
public List<String> getAcceptedMacroNames() { | ||
return Collections.singletonList(MACRO_NAME); | ||
} | ||
|
||
@Override | ||
public String evaluate(AbstractBuild<?, ?> context, TaskListener listener, String macroName, Map<String, String> arguments, ListMultimap<String, String> argumentMultimap) throws MacroEvaluationException, IOException, InterruptedException { | ||
return evaluate(context,null,listener,macroName,arguments,argumentMultimap); | ||
} | ||
|
||
@Override | ||
public String evaluate(Run<?, ?> run, FilePath workspace, TaskListener listener, String macroName, Map<String, String> arguments, ListMultimap<String, String> argumentMultimap) throws MacroEvaluationException, IOException, InterruptedException { | ||
Cause.UserIdCause userIdCause = run.getCause(Cause.UserIdCause.class); | ||
if(userIdCause != null) { | ||
return userIdCause.getUserId(); | ||
} | ||
return Messages.TokenMacro_Unknown(); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/main/resources/org/jenkinsci/plugins/tokenmacro/impl/BuildUserMacro/help.groovy
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
dt("\${BUILD_USER}") | ||
dd(_("Displays the user who caused the current build.")) |
1 change: 1 addition & 0 deletions
1
src/main/resources/org/jenkinsci/plugins/tokenmacro/impl/Messages.properties
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
TokenMacro.Unknown=<Unknown> |
42 changes: 42 additions & 0 deletions
42
src/test/java/org/jenkinsci/plugins/tokenmacro/impl/BuildUserMacroTest.java
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.jenkinsci.plugins.tokenmacro.impl; | ||
|
||
import hudson.model.AbstractBuild; | ||
import hudson.model.Cause; | ||
import hudson.model.Result; | ||
import hudson.util.StreamTaskListener; | ||
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; | ||
import org.jenkinsci.plugins.workflow.job.WorkflowJob; | ||
import org.jenkinsci.plugins.workflow.job.WorkflowRun; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.Issue; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
import org.jvnet.hudson.test.WithoutJenkins; | ||
|
||
import java.io.StringWriter; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
@SuppressWarnings({"unchecked"}) | ||
public class BuildUserMacroTest { | ||
|
||
@Rule | ||
public JenkinsRule j = new JenkinsRule(); | ||
|
||
@Test | ||
@WithoutJenkins | ||
public void testGetContent_BuildUser() | ||
throws Exception { | ||
AbstractBuild build = mock(AbstractBuild.class); | ||
Cause.UserIdCause cause = mock(Cause.UserIdCause.class); | ||
when(cause.getUserId()).thenReturn("johndoe"); | ||
when(build.getCause(Cause.UserIdCause.class)).thenReturn(cause); | ||
|
||
String content = new BuildUserMacro().evaluate(build, StreamTaskListener.fromStdout(), BuildUserMacro.MACRO_NAME, null, null); | ||
|
||
assertEquals("johndoe", content); | ||
} | ||
} |