Skip to content

Commit

Permalink
feat: Add BUILD_USER macro which returns the user who caused the build (
Browse files Browse the repository at this point in the history
  • Loading branch information
slide authored Nov 17, 2022
1 parent 93c7f7f commit d7cc1f2
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
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();
}
}
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."))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TokenMacro.Unknown=<Unknown>
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);
}
}

0 comments on commit d7cc1f2

Please sign in to comment.