Skip to content

Add failed-pipeline trigger and enrich pipeline event metadata - #1919

Open
damjanek wants to merge 7 commits into
jenkinsci:masterfrom
damjanek:feature/pipeline-event-enrichment
Open

Add failed-pipeline trigger and enrich pipeline event metadata#1919
damjanek wants to merge 7 commits into
jenkinsci:masterfrom
damjanek:feature/pipeline-event-enrichment

Conversation

@damjanek

Copy link
Copy Markdown

What this does

Makes GitLab pipeline (CI) event handling useful beyond "build only on success":

  • Trigger: new "Build on failed pipeline events" option, so failed pipelines
    no longer go unnoticed. The trigger now derives its allowed pipeline states
    from the success and failed flags independently.
  • Merge request resolution: new getCommitMergeRequests(projectId, sha)
    (GitLab v3/v4 /repository/commits/{sha}/merge_requests) so a pipeline event
    is associated with its MR; used to resolve MR title/iid/id and the target
    branch (MR target → project default branch → ref).
  • Env vars: richer build env exposing pipeline id/iid/source/url, commit
    message/title/author name/email/url, and project web URL / path-with-namespace.

Testing

Automated — added to PipelineHookTriggerHandlerImplTest:

  • pipeline_build_on_failed_event — a failed event triggers when enabled
  • pipeline_build_ignores_failed_when_not_configuredfailed ignored when disabled

Result: Tests run: 4, Failures: 0, Errors: 0 (Maven 3.9.9 / JDK 21); hpi packages cleanly.

Manual — deployed the built .hpi against a live GitLab:

  • Failed pipeline event triggers a build with the option on; only successful with it off.
  • MR-linked pipeline resolves the MR and builds the MR target branch.
  • Confirmed gitlabPipeline*, gitlabCommit*, gitlabProject* env vars populated.

The model/API additions have no dedicated unit tests as they are plain
payload/proxy mappings exercised by the above; let me know if you'd like the env-var mapping unit-tested.

Submitter checklist

  • Opening from a topic/feature/bugfix branch (not master)
  • PR title represents the desired changelog entry
  • Described what I did
  • Link to relevant issues
  • Link to relevant PRs
  • Provided tests demonstrating the feature

@damjanek
damjanek requested a review from a team as a code ownerJune 28, 2026 22:55
@github-actionsgithub-actionsBot added the tests This PR adds/removes/updates test cases label Jun 28, 2026
Make pipeline (CI) event handling useful beyond the previous
"build only on successful pipelines" behaviour.
Trigger:
- Add a "Build on failed pipeline events" option
(triggerOnFailedPipelineEvent) alongside the existing successful-pipeline
option, so failed pipelines no longer go unnoticed.
PipelineHookTriggerHandlerFactory now derives the set of allowed pipeline
states from both flags.
Merge request resolution:
- Add GitLabClient#getCommitMergeRequests(projectId, sha), backed by the
GitLab v3/v4 "/repository/commits/{sha}/merge_requests" endpoint, so a
pipeline event can be associated with its merge request. The handler uses
this to resolve the MR title/iid/id and to derive the correct target
branch (MR target branch, else project default branch, else the ref).
Build cause / environment variables:
- Expose richer pipeline metadata as build environment variables: pipeline
id/iid/source/url, commit message/title/author name/email/url, and
project web URL / path-with-namespace.
- Populate the pipeline CauseData from the project and commit payloads and
use the source branch for branch/sourceBranch.
Webhook model:
- Add the payload fields required by the above: PipelineHook.commit and
mergeRequest, Commit.title, Project.gitSshUrl / gitHttpUrl, and
PipelineEventObjectAttributes.iid / source / url.
Tests:
- Cover triggering on failed pipeline events and that failed events are
ignored when the option is disabled; add getCommitMergeRequests stubs.
CI (ci.jenkins.io) failed on the spotless:check goal because PipelineHookTriggerHandlerImpl was not formatted per palantir-java-format; reformat it to satisfy spotless.
Also guard a possible null dereference of objectAttributes (SpotBugs NP_NULL_ON_SOME_PATH) by checking it is non-null before reading its status.
@damjanek
damjanekforce-pushed the feature/pipeline-event-enrichment branch from cae825c to 101ea27CompareJuly 23, 2026 09:04
@krisstern

Copy link
Copy Markdown
Member

Thanks @damjanek for the PR! Let me gfive it a review over the next few days

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR expands GitLab pipeline (CI) webhook support in the Jenkins GitLab plugin by allowing jobs to trigger on failed pipeline events and by enriching pipeline-triggered builds with additional metadata (including pipeline/commit/project details and merge request association).

Changes:

  • Add a new trigger option to build on failed pipeline events, and update the pipeline handler factory/trigger wiring accordingly.
  • Enrich pipeline hook processing to resolve associated merge requests (via commits → MRs API) and expose additional pipeline/commit/project fields through CauseData build variables.
  • Extend unit tests to cover triggering on failed pipeline events and ignoring failed events when not configured.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 3 comments.

Show a summary per file
FileDescription
src/test/java/com/dabsquared/gitlabjenkins/util/GitLabClientStub.javaAdds stub method for commit→merge-requests lookup.
src/test/java/com/dabsquared/gitlabjenkins/trigger/handler/pipeline/PipelineHookTriggerHandlerImplTest.javaAdds tests for triggering/ignoring failed pipeline events.
src/test/java/com/dabsquared/gitlabjenkins/service/GitLabClientStub.javaAdds stub method for commit→merge-requests lookup (returns empty list).
src/main/resources/com/dabsquared/gitlabjenkins/GitLabPushTrigger/config.jellyAdds UI checkbox for “Build on failed pipeline events”.
src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/pipeline/PipelineHookTriggerHandlerImpl.javaAdds commit→MR resolution and enriches cause/build metadata for pipeline events.
src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/pipeline/PipelineHookTriggerHandlerFactory.javaExtends factory to support allowed states derived from success/failed flags independently.
src/main/java/com/dabsquared/gitlabjenkins/GitLabPushTrigger.javaPersists and wires the new “failed pipeline” trigger option into handler initialization.
src/main/java/com/dabsquared/gitlabjenkins/gitlab/hook/model/Project.javaAdds git-specific HTTP/SSH URL fields to the hook model.
src/main/java/com/dabsquared/gitlabjenkins/gitlab/hook/model/PipelineHook.javaAdds commit and mergeRequest fields to the pipeline hook model.
src/main/java/com/dabsquared/gitlabjenkins/gitlab/hook/model/PipelineEventObjectAttributes.javaAdds pipeline iid/source/url fields to the hook model.
src/main/java/com/dabsquared/gitlabjenkins/gitlab/hook/model/Commit.javaAdds commit title field to the hook model.
src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/V4GitLabApiProxy.javaAdds v4 API endpoint for commit→merge-requests lookup.
src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/V3GitLabApiProxy.javaAdds v3 API endpoint for commit→merge-requests lookup.
src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/ResteasyGitLabClient.javaExposes commit→merge-requests method through the client.
src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/GitLabApiProxy.javaAdds commit→merge-requests method to the proxy interface.
src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/AutodetectingGitLabClient.javaAdds autodetected delegation for commit→merge-requests call.
src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/GitLabClient.javaAdds commit→merge-requests method to the public client interface.
src/main/java/com/dabsquared/gitlabjenkins/cause/CauseData.javaAdds new pipeline/commit/project fields and exports them as build variables.
Suppressed comments (1)

src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/pipeline/PipelineHookTriggerHandlerImpl.java:53

  • resolvedMergeRequest is an instance field but isn’t reset per webhook. If the current event can’t resolve an MR (empty/failed API call), the previous event’s MR data can leak into this build’s cause/env vars.
 PipelineEventObjectAttributes objectAttributes = hook.getObjectAttributes();
try {
GitLabConnectionProperty property = job.getProperty(GitLabConnectionProperty.class);
if (property != null && property.getClient() != null) {
GitLabClient client = property.getClient();

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +136 to +143
// If the pipeline is associated with a merge request, use the MR target branch
if (hook.getMergeRequest() != null && hook.getMergeRequest().getTargetBranch() != null) {
return hook.getMergeRequest().getTargetBranch();
}
// Otherwise fall back to the project's default branch
if (hook.getProject() != null && hook.getProject().getDefaultBranch() != null) {
return hook.getProject().getDefaultBranch();
}
Comment on lines 35 to 37
private final List<String> allowedStates;
private MergeRequest resolvedMergeRequest;

Comment on lines 16 to 20
private String id;
private String message;
private String title;
private Date timestamp;
private String url;
krissternand others added 3 commits August 23, 2026 21:17
Store the commit-resolved merge request on the per-request PipelineHook instead of a handler field. PipelineHookTriggerHandlerImpl is reused across webhook deliveries via GitLabPushTrigger, so the mutable field could be overwritten by concurrent pipeline events and leak merge request metadata into the wrong build.
As the resolved merge request now lives on the hook, getTargetBranch() also picks it up, so pipelines associated with a merge request build against the merge request target branch as intended. The lookup is skipped when the payload already carries a merge request, which leaves a single source of truth and simplifies retrieveCauseData().
Include Commit.title in equals(), hashCode() and toString() so commits that differ only by title no longer compare equal.
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testsThis PR adds/removes/updates test cases

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@damjanek@krisstern