Uh oh!
There was an error while loading. Please reload this page.
Add failed-pipeline trigger and enrich pipeline event metadata - #1919
Add failed-pipeline trigger and enrich pipeline event metadata#1919damjanek wants to merge 7 commits into
Conversation
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.
cae825c to
101ea27Comparekrisstern
commented
Aug 17, 2026
Thanks @damjanek for the PR! Let me gfive it a review over the next few days |
There was a problem hiding this comment.
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
CauseDatabuild 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
| File | Description |
|---|---|
| src/test/java/com/dabsquared/gitlabjenkins/util/GitLabClientStub.java | Adds stub method for commit→merge-requests lookup. |
| src/test/java/com/dabsquared/gitlabjenkins/trigger/handler/pipeline/PipelineHookTriggerHandlerImplTest.java | Adds tests for triggering/ignoring failed pipeline events. |
| src/test/java/com/dabsquared/gitlabjenkins/service/GitLabClientStub.java | Adds stub method for commit→merge-requests lookup (returns empty list). |
| src/main/resources/com/dabsquared/gitlabjenkins/GitLabPushTrigger/config.jelly | Adds UI checkbox for “Build on failed pipeline events”. |
| src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/pipeline/PipelineHookTriggerHandlerImpl.java | Adds commit→MR resolution and enriches cause/build metadata for pipeline events. |
| src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/pipeline/PipelineHookTriggerHandlerFactory.java | Extends factory to support allowed states derived from success/failed flags independently. |
| src/main/java/com/dabsquared/gitlabjenkins/GitLabPushTrigger.java | Persists and wires the new “failed pipeline” trigger option into handler initialization. |
| src/main/java/com/dabsquared/gitlabjenkins/gitlab/hook/model/Project.java | Adds git-specific HTTP/SSH URL fields to the hook model. |
| src/main/java/com/dabsquared/gitlabjenkins/gitlab/hook/model/PipelineHook.java | Adds commit and mergeRequest fields to the pipeline hook model. |
| src/main/java/com/dabsquared/gitlabjenkins/gitlab/hook/model/PipelineEventObjectAttributes.java | Adds pipeline iid/source/url fields to the hook model. |
| src/main/java/com/dabsquared/gitlabjenkins/gitlab/hook/model/Commit.java | Adds commit title field to the hook model. |
| src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/V4GitLabApiProxy.java | Adds v4 API endpoint for commit→merge-requests lookup. |
| src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/V3GitLabApiProxy.java | Adds v3 API endpoint for commit→merge-requests lookup. |
| src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/ResteasyGitLabClient.java | Exposes commit→merge-requests method through the client. |
| src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/GitLabApiProxy.java | Adds commit→merge-requests method to the proxy interface. |
| src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/AutodetectingGitLabClient.java | Adds autodetected delegation for commit→merge-requests call. |
| src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/GitLabClient.java | Adds commit→merge-requests method to the public client interface. |
| src/main/java/com/dabsquared/gitlabjenkins/cause/CauseData.java | Adds 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
resolvedMergeRequestis 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.
| // 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(); | ||
| } |
| private final List<String> allowedStates; | ||
| private MergeRequest resolvedMergeRequest; | ||
| private String id; | ||
| private String message; | ||
| private String title; | ||
| private Date timestamp; | ||
| private String url; |
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.
What this does
Makes GitLab pipeline (CI) event handling useful beyond "build only on success":
no longer go unnoticed. The trigger now derives its allowed pipeline states
from the success and failed flags independently.
getCommitMergeRequests(projectId, sha)(GitLab v3/v4
/repository/commits/{sha}/merge_requests) so a pipeline eventis associated with its MR; used to resolve MR title/iid/id and the target
branch (MR target → project default branch → ref).
message/title/author name/email/url, and project web URL / path-with-namespace.
Testing
Automated — added to
PipelineHookTriggerHandlerImplTest:pipeline_build_on_failed_event— afailedevent triggers when enabledpipeline_build_ignores_failed_when_not_configured—failedignored when disabledResult:
Tests run: 4, Failures: 0, Errors: 0(Maven 3.9.9 / JDK 21);hpipackages cleanly.Manual — deployed the built
.hpiagainst a live GitLab: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