Skip to content

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

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

Add failed-pipeline trigger and enrich pipeline event metadata#1919
damjanek wants to merge 8 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 owner June 28, 2026 22:55
@github-actions github-actions Bot added the tests This PR adds/removes/updates test cases label Jun 28, 2026
damjanek added 2 commits July 23, 2026 09:23
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
damjanek force-pushed the feature/pipeline-event-enrichment branch from cae825c to 101ea27 Compare July 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

Copilot AI 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
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

  • 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.

krisstern and others added 4 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.

Copilot AI 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.

🟡 Changes recommended

The pipeline MR-resolution call path should prefer a safe project ID over pathWithNamespace, and the new env-var outputs are currently unverified by unit tests.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details
  • Files reviewed: 18/18 changed files
  • Comments generated: 2
  • Review effort level: Lite

Comment on lines +72 to +76
String projectPath = hook.getProject() != null
? hook.getProject().getPathWithNamespace()
: (hook.getProjectId() != null
? hook.getProjectId().toString()
: null);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks valid

Comment thread src/main/java/com/dabsquared/gitlabjenkins/cause/CauseData.java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tests This PR adds/removes/updates test cases

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants