Commit eec81e02 authored by Lennard Sprong's avatar Lennard Sprong
Browse files

fix: Select latest pipeline between branch and MR

parent 165e3e75
Loading
Loading
Loading
Loading
+30 −3
Original line number Diff line number Diff line
@@ -19,16 +19,43 @@ describe('getPipelineAndMrForBranch', () => {
          {
            request: getPipelinesForMr(mr),
            response: [
              { ...pipeline, iid: 1 },
              { ...pipeline, iid: 2 },
              { ...pipeline, iid: 4 },
              { ...pipeline, iid: 5 },
            ],
          },
          {
            request: getPipelinesForRef(project, branchName),
            response: [{ ...pipeline, iid: 1 }],
          },
        ),
      );

      const { pipeline: p } = await getPipelineAndMrForBranch(fakeService, project, branchName);

      expect(p?.iid).toBe(5);
    });

    it('returns branch pipeline if MR pipeline is outdated', async () => {
      const fakeService = new GitLabService({ instanceUrl: '', token: '' });
      const branchName = 'branch-name';

      jest.spyOn(fakeService, 'fetchFromApi').mockImplementation(
        createFakeFetchFromApi(
          { request: getMergeRequestsForBranch(project, branchName), response: [mr] },
          {
            request: getPipelinesForMr(mr),
            response: [{ ...pipeline, iid: 2 }],
          },
          {
            request: getPipelinesForRef(project, branchName),
            response: [{ ...pipeline, iid: 3 }],
          },
        ),
      );

      const { pipeline: p } = await getPipelineAndMrForBranch(fakeService, project, branchName);

      expect(p?.iid).toBe(2);
      expect(p?.iid).toBe(3);
    });
  });

+8 −4
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ export const getPipelineAndMrForBranch = async (
}> => {
  // Use centralized MR lookup helper (handles normalization, selection, error handling)
  let mr: RestMr | undefined;
  let mrPipeline: RestPipeline | undefined;
  try {
    mr =
      (await findOpenMrForCurrentBranch(gitlabService, project, trackingBranchName)) || undefined;
@@ -39,8 +40,7 @@ export const getPipelineAndMrForBranch = async (
    try {
      const pipelines = await gitlabService.fetchFromApi(getPipelinesForMr(mr));
      if (pipelines && pipelines.length > 0) {
        const pipeline = sort(pipelines, (p1, p2) => p2.iid - p1.iid)[0];
        return { mr, pipeline };
        [mrPipeline] = sort(pipelines, (p1, p2) => p2.iid - p1.iid);
      }
    } catch (e) {
      handleApiError(e, `Failed to fetch pipelines for MR !${mr.iid}`);
@@ -52,9 +52,13 @@ export const getPipelineAndMrForBranch = async (
    const pipelines = await gitlabService.fetchFromApi(
      getPipelinesForRef(project, trackingBranchName),
    );
    return { mr, pipeline: pipelines?.[0] };
    const branchPipeline = pipelines?.[0];
    const pipeline =
      (mrPipeline?.iid ?? 0) > (branchPipeline?.iid ?? 0) ? mrPipeline : branchPipeline;

    return { mr, pipeline };
  } catch (e) {
    handleApiError(e, `Failed to fetch pipelines for ref "${trackingBranchName}"`);
    return { mr };
    return { mr, pipeline: mrPipeline };
  }
};