Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,33 @@ public Job getJob(
return Jenkins.get().getItemByFullName(jobFullName, Job.class);
}

/**
* A {@link Cause} that indicates a Jenkins build was triggered via MCP call.
* <p>
* This is useful for the end user to understand that the call was trigger through this plugin's code
* and not some manual user intervention.</p>
* <p>And among others, it allows plugins like
* <a href="https://plugins.jenkins.io/buildtriggerbadge/">...</a> to offer custom badges</p>
*
* @see #triggerBuild(String, Map)
*/
public static class MCPCause extends Cause {
private final String userId;

public MCPCause() {
User currentUser = User.current();
this.userId = currentUser != null ? currentUser.getId() : null;
}

@Override
public String getShortDescription() {
if (userId != null) {
return "Triggered via MCP Server by " + userId;
}
return "Triggered via MCP Server";
}
}

@Tool(description = "Trigger a build for a Jenkins job") // keep the default value for destructive (true)
public boolean triggerBuild(
@ToolParam(description = "Full path of the Jenkins job (e.g., 'folder/job-name')") String jobFullName,
Expand All @@ -93,8 +120,7 @@ public boolean triggerBuild(

if (job != null) {
job.checkPermission(Item.BUILD);
Cause.UserIdCause userIdCause = new Cause.UserIdCause();
CauseAction action = new CauseAction(userIdCause);
CauseAction action = new CauseAction(new MCPCause());
Copy link
Member

Choose a reason for hiding this comment

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

could be even more simple :)

            Cause.UserIdCause userIdCause = new Cause.UserIdCause();
            CauseAction action = new CauseAction(userIdCause){
                @Override
                public String getShortDescription() {
                    if (userIdCause.getUserId() != null) {
                        return "Triggered via MCP Server by " + userIdCause.getUserId();
                    }
                    return "Triggered by MCP Server";
                }
            };

Bear in mind getShortDescription is deprecated so if use by other plugins it may change (not used anymore) in the future.

if (job.isParameterized() && job instanceof Job j) {
ParametersDefinitionProperty parametersDefinition =
(ParametersDefinitionProperty) j.getProperty(ParametersDefinitionProperty.class);
Expand Down
Loading