From 662ea6e1bae8dc6d9eef4757507992d45f24f056 Mon Sep 17 00:00:00 2001 From: Rahul Gautam Singh Date: Sun, 10 Nov 2024 03:14:57 +0530 Subject: [PATCH 1/5] refactor some types and consolidate duplicate code --- .../gitlab/__snapshots__/index.spec.ts.snap | 78 ++++++++-- lib/modules/platform/gitlab/index.spec.ts | 143 +++++++++++------- lib/modules/platform/gitlab/index.ts | 52 ++----- lib/modules/platform/gitlab/types.ts | 1 + lib/modules/platform/gitlab/utils.ts | 19 +++ 5 files changed, 183 insertions(+), 110 deletions(-) create mode 100644 lib/modules/platform/gitlab/utils.ts diff --git a/lib/modules/platform/gitlab/__snapshots__/index.spec.ts.snap b/lib/modules/platform/gitlab/__snapshots__/index.spec.ts.snap index 7d0bdd4c005485..5638f4538f777f 100644 --- a/lib/modules/platform/gitlab/__snapshots__/index.spec.ts.snap +++ b/lib/modules/platform/gitlab/__snapshots__/index.spec.ts.snap @@ -2,62 +2,116 @@ exports[`modules/platform/gitlab/index createPr(branchName, title, body) raises with squash enabled when repository squash option is always 1`] = ` { - "id": 1, - "iid": 12345, + "bodyStruct": { + "hash": "9a92616496f01e045bfc15bbb549a8627754d2ca4c56ead79a18fb580b8e301f", + }, + "hasAssignees": false, + "headPipelineSha": undefined, + "headPipelineStatus": undefined, + "labels": undefined, "number": 12345, + "reviewers": undefined, + "sha": undefined, "sourceBranch": "some-branch", + "state": undefined, + "targetBranch": "master", "title": "some title", } `; exports[`modules/platform/gitlab/index createPr(branchName, title, body) raises with squash enabled when repository squash option is default_on 1`] = ` { - "id": 1, - "iid": 12345, + "bodyStruct": { + "hash": "9a92616496f01e045bfc15bbb549a8627754d2ca4c56ead79a18fb580b8e301f", + }, + "hasAssignees": false, + "headPipelineSha": undefined, + "headPipelineStatus": undefined, + "labels": undefined, "number": 12345, + "reviewers": undefined, + "sha": undefined, "sourceBranch": "some-branch", + "state": undefined, + "targetBranch": "master", "title": "some title", } `; exports[`modules/platform/gitlab/index createPr(branchName, title, body) returns the PR 1`] = ` { - "id": 1, - "iid": 12345, + "bodyStruct": { + "hash": "9a92616496f01e045bfc15bbb549a8627754d2ca4c56ead79a18fb580b8e301f", + }, + "hasAssignees": false, + "headPipelineSha": undefined, + "headPipelineStatus": undefined, + "labels": undefined, "number": 12345, + "reviewers": undefined, + "sha": undefined, "sourceBranch": "some-branch", + "state": undefined, + "targetBranch": "master", "title": "some title", } `; exports[`modules/platform/gitlab/index createPr(branchName, title, body) supports draftPR on < 13.2 1`] = ` { - "id": 1, - "iid": 12345, + "bodyStruct": { + "hash": "9a92616496f01e045bfc15bbb549a8627754d2ca4c56ead79a18fb580b8e301f", + }, + "hasAssignees": false, + "headPipelineSha": undefined, + "headPipelineStatus": undefined, "isDraft": true, + "labels": undefined, "number": 12345, + "reviewers": undefined, + "sha": undefined, "sourceBranch": "some-branch", + "state": undefined, + "targetBranch": "master", "title": "some title", } `; exports[`modules/platform/gitlab/index createPr(branchName, title, body) supports draftPR on >= 13.2 1`] = ` { - "id": 1, - "iid": 12345, + "bodyStruct": { + "hash": "9a92616496f01e045bfc15bbb549a8627754d2ca4c56ead79a18fb580b8e301f", + }, + "hasAssignees": false, + "headPipelineSha": undefined, + "headPipelineStatus": undefined, "isDraft": true, + "labels": undefined, "number": 12345, + "reviewers": undefined, + "sha": undefined, "sourceBranch": "some-branch", + "state": undefined, + "targetBranch": "master", "title": "some title", } `; exports[`modules/platform/gitlab/index createPr(branchName, title, body) uses default branch 1`] = ` { - "id": 1, - "iid": 12345, + "bodyStruct": { + "hash": "9a92616496f01e045bfc15bbb549a8627754d2ca4c56ead79a18fb580b8e301f", + }, + "hasAssignees": false, + "headPipelineSha": undefined, + "headPipelineStatus": undefined, + "labels": undefined, "number": 12345, + "reviewers": undefined, + "sha": undefined, "sourceBranch": "some-branch", + "state": undefined, + "targetBranch": "master", "title": "some title", } `; diff --git a/lib/modules/platform/gitlab/index.spec.ts b/lib/modules/platform/gitlab/index.spec.ts index 1fc1fce268550a..e0816a1e359d3e 100644 --- a/lib/modules/platform/gitlab/index.spec.ts +++ b/lib/modules/platform/gitlab/index.spec.ts @@ -1926,6 +1926,9 @@ describe('modules/platform/gitlab/index', () => { id: 1, iid: 12345, title: 'some title', + source_branch: 'some-branch', + target_branch: 'master', + description: 'the-body', }); const pr = await gitlab.createPr({ sourceBranch: 'some-branch', @@ -1946,6 +1949,9 @@ describe('modules/platform/gitlab/index', () => { id: 1, iid: 12345, title: 'some title', + source_branch: 'some-branch', + target_branch: 'master', + description: 'the-body', }); const pr = await gitlab.createPr({ sourceBranch: 'some-branch', @@ -1966,6 +1972,9 @@ describe('modules/platform/gitlab/index', () => { id: 1, iid: 12345, title: 'WIP: some title', + source_branch: 'some-branch', + target_branch: 'master', + description: 'the-body', }); const pr = await gitlab.createPr({ sourceBranch: 'some-branch', @@ -1986,6 +1995,9 @@ describe('modules/platform/gitlab/index', () => { id: 1, iid: 12345, title: 'Draft: some title', + source_branch: 'some-branch', + target_branch: 'master', + description: 'the-body', }); const pr = await gitlab.createPr({ sourceBranch: 'some-branch', @@ -2006,6 +2018,9 @@ describe('modules/platform/gitlab/index', () => { id: 1, iid: 12345, title: 'some title', + source_branch: 'some-branch', + target_branch: 'master', + description: 'the-body', }) .get('/api/v4/projects/undefined/merge_requests/12345') .reply(200) @@ -2024,9 +2039,7 @@ describe('modules/platform/gitlab/index', () => { usePlatformAutomerge: true, }, }), - ).toEqual({ - id: 1, - iid: 12345, + ).toMatchObject({ number: 12345, sourceBranch: 'some-branch', title: 'some title', @@ -2047,6 +2060,9 @@ describe('modules/platform/gitlab/index', () => { id: 1, iid: 12345, title: 'some title', + source_branch: 'some-branch', + target_branch: 'master', + description: 'the-body', }) .get('/api/v4/projects/undefined/merge_requests/12345') .reply(200, reply_body) @@ -2068,9 +2084,7 @@ describe('modules/platform/gitlab/index', () => { usePlatformAutomerge: true, }, }); - expect(pr).toEqual({ - id: 1, - iid: 12345, + expect(pr).toMatchObject({ number: 12345, sourceBranch: 'some-branch', title: 'some title', @@ -2104,6 +2118,9 @@ describe('modules/platform/gitlab/index', () => { id: 1, iid: 12345, title: 'some title', + source_branch: 'some-branch', + target_branch: 'master', + description: 'the-body', }) .get('/api/v4/projects/undefined/merge_requests/12345') .reply(200, reply_body) @@ -2123,9 +2140,7 @@ describe('modules/platform/gitlab/index', () => { usePlatformAutomerge: true, }, }); - expect(pr).toEqual({ - id: 1, - iid: 12345, + expect(pr).toMatchObject({ number: 12345, sourceBranch: 'some-branch', title: 'some title', @@ -2154,6 +2169,9 @@ describe('modules/platform/gitlab/index', () => { id: 1, iid: 12345, title: 'some title', + source_branch: 'some-branch', + target_branch: 'master', + description: 'the-body', }) .get('/api/v4/projects/undefined/merge_requests/12345') .reply(200, reply_body) @@ -2177,9 +2195,7 @@ describe('modules/platform/gitlab/index', () => { usePlatformAutomerge: true, }, }); - expect(pr).toEqual({ - id: 1, - iid: 12345, + expect(pr).toMatchObject({ number: 12345, sourceBranch: 'some-branch', title: 'some title', @@ -2225,6 +2241,9 @@ describe('modules/platform/gitlab/index', () => { id: 1, iid: 12345, title: 'some title', + source_branch: 'some-branch', + target_branch: 'master', + description: 'the-body', }) .get('/api/v4/projects/undefined/merge_requests/12345') .reply(200, reply_body) @@ -2239,9 +2258,7 @@ describe('modules/platform/gitlab/index', () => { usePlatformAutomerge: true, }, }); - expect(pr).toEqual({ - id: 1, - iid: 12345, + expect(pr).toMatchObject({ number: 12345, sourceBranch: 'some-branch', title: 'some title', @@ -2270,6 +2287,9 @@ describe('modules/platform/gitlab/index', () => { id: 1, iid: 12345, title: 'some title', + source_branch: 'some-branch', + target_branch: 'master', + description: 'the-body', }); const pr = await gitlab.createPr({ sourceBranch: 'some-branch', @@ -2302,6 +2322,9 @@ describe('modules/platform/gitlab/index', () => { id: 1, iid: 12345, title: 'some title', + source_branch: 'some-branch', + target_branch: 'master', + description: 'the-body', }); const pr = await gitlab.createPr({ sourceBranch: 'some-branch', @@ -2322,6 +2345,9 @@ describe('modules/platform/gitlab/index', () => { id: 1, iid: 12345, title: 'some title', + source_branch: 'some-branch', + target_branch: 'master', + description: 'the-body', }) .get('/api/v4/projects/undefined/merge_requests/12345') .reply(200) @@ -2353,15 +2379,11 @@ describe('modules/platform/gitlab/index', () => { gitLabIgnoreApprovals: true, }, }), - ).toMatchInlineSnapshot(` - { - "id": 1, - "iid": 12345, - "number": 12345, - "sourceBranch": "some-branch", - "title": "some title", - } - `); + ).toMatchObject({ + number: 12345, + sourceBranch: 'some-branch', + title: 'some title', + }); }); it('adds approval rule to ignore all approvals when platformAutomerge is false', async () => { @@ -2373,6 +2395,9 @@ describe('modules/platform/gitlab/index', () => { id: 1, iid: 12345, title: 'some title', + source_branch: 'some-branch', + target_branch: 'master', + description: 'the-body', }) .get('/api/v4/projects/undefined/merge_requests/12345/approval_rules') .reply(200, [ @@ -2398,9 +2423,7 @@ describe('modules/platform/gitlab/index', () => { gitLabIgnoreApprovals: true, }, }), - ).toEqual({ - id: 1, - iid: 12345, + ).toMatchObject({ number: 12345, sourceBranch: 'some-branch', title: 'some title', @@ -2416,6 +2439,9 @@ describe('modules/platform/gitlab/index', () => { id: 1, iid: 12345, title: 'some title', + source_branch: 'some-branch', + target_branch: 'master', + description: 'the-body', }) .get('/api/v4/projects/undefined/merge_requests/12345') .reply(200) @@ -2455,9 +2481,7 @@ describe('modules/platform/gitlab/index', () => { gitLabIgnoreApprovals: true, }, }), - ).toStrictEqual({ - id: 1, - iid: 12345, + ).toMatchObject({ number: 12345, sourceBranch: 'some-branch', title: 'some title', @@ -2473,6 +2497,9 @@ describe('modules/platform/gitlab/index', () => { id: 1, iid: 12345, title: 'some title', + source_branch: 'some-branch', + target_branch: 'master', + description: 'the-body', }) .get('/api/v4/projects/undefined/merge_requests/12345') .reply(200) @@ -2523,9 +2550,7 @@ describe('modules/platform/gitlab/index', () => { gitLabIgnoreApprovals: true, }, }), - ).toStrictEqual({ - id: 1, - iid: 12345, + ).toMatchObject({ number: 12345, sourceBranch: 'some-branch', title: 'some title', @@ -2541,6 +2566,9 @@ describe('modules/platform/gitlab/index', () => { id: 1, iid: 12345, title: 'some title', + source_branch: 'some-branch', + target_branch: 'master', + description: 'the-body', }) .get('/api/v4/projects/undefined/merge_requests/12345') .reply(200) @@ -2601,9 +2629,7 @@ describe('modules/platform/gitlab/index', () => { gitLabIgnoreApprovals: true, }, }), - ).toStrictEqual({ - id: 1, - iid: 12345, + ).toMatchObject({ number: 12345, sourceBranch: 'some-branch', title: 'some title', @@ -2619,6 +2645,9 @@ describe('modules/platform/gitlab/index', () => { id: 1, iid: 12345, title: 'some title', + source_branch: 'some-branch', + target_branch: 'master', + description: 'the-body', }) .get('/api/v4/projects/undefined/merge_requests/12345') .reply(200) @@ -2650,15 +2679,11 @@ describe('modules/platform/gitlab/index', () => { gitLabIgnoreApprovals: true, }, }), - ).toMatchInlineSnapshot(` - { - "id": 1, - "iid": 12345, - "number": 12345, - "sourceBranch": "some-branch", - "title": "some title", - } - `); + ).toMatchObject({ + number: 12345, + sourceBranch: 'some-branch', + title: 'some title', + }); }); it('silently ignores approval rules adding errors', async () => { @@ -2670,6 +2695,9 @@ describe('modules/platform/gitlab/index', () => { id: 1, iid: 12345, title: 'some title', + source_branch: 'some-branch', + target_branch: 'master', + description: 'the-body', }) .get('/api/v4/projects/undefined/merge_requests/12345') .reply(200) @@ -2701,15 +2729,11 @@ describe('modules/platform/gitlab/index', () => { gitLabIgnoreApprovals: true, }, }), - ).toMatchInlineSnapshot(` - { - "id": 1, - "iid": 12345, - "number": 12345, - "sourceBranch": "some-branch", - "title": "some title", - } - `); + ).toMatchObject({ + number: 12345, + sourceBranch: 'some-branch', + title: 'some title', + }); }); it('auto-approves when enabled', async () => { @@ -2721,6 +2745,9 @@ describe('modules/platform/gitlab/index', () => { id: 1, iid: 12345, title: 'some title', + source_branch: 'some-branch', + target_branch: 'master', + description: 'the-body', }) .post('/api/v4/projects/undefined/merge_requests/12345/approve') .reply(200); @@ -2735,9 +2762,7 @@ describe('modules/platform/gitlab/index', () => { autoApprove: true, }, }), - ).toStrictEqual({ - id: 1, - iid: 12345, + ).toMatchObject({ number: 12345, sourceBranch: 'some-branch', title: 'some title', @@ -2753,6 +2778,9 @@ describe('modules/platform/gitlab/index', () => { id: 1, iid: 12345, title: 'some title', + source_branch: 'some-branch', + target_branch: 'master', + description: 'the-body', }) .post('/api/v4/projects/undefined/merge_requests/12345/approve') .replyWithError('some error'); @@ -3110,7 +3138,6 @@ describe('modules/platform/gitlab/index', () => { number: 1, prTitle: 'title', prBody: 'body', - state: 'closed', addLabels: ['new_label'], removeLabels: ['old_label'], }), diff --git a/lib/modules/platform/gitlab/index.ts b/lib/modules/platform/gitlab/index.ts index c8ba81c0dfc894..bbb7b900b7042e 100644 --- a/lib/modules/platform/gitlab/index.ts +++ b/lib/modules/platform/gitlab/index.ts @@ -32,7 +32,6 @@ import { getQueryString, parseUrl, } from '../../../util/url'; -import { getPrBodyStruct } from '../pr-body'; import type { AutodiscoverConfig, BranchStatusConfig, @@ -72,6 +71,7 @@ import type { MergeMethod, RepoResponse, } from './types'; +import { prInfo } from './utils'; let config: { repository: string; @@ -560,24 +560,10 @@ async function fetchPrList(): Promise { const query = getQueryString(searchParams); const urlString = `projects/${config.repository}/merge_requests?${query}`; try { - const res = await gitlabApi.getJson< - { - iid: number; - source_branch: string; - title: string; - state: string; - created_at: string; - }[] - >(urlString, { paginate: true }); - return res.body.map((pr) => - massagePr({ - number: pr.iid, - sourceBranch: pr.source_branch, - title: pr.title, - state: pr.state === 'opened' ? 'open' : pr.state, - createdAt: pr.created_at, - }), - ); + const res = await gitlabApi.getJson(urlString, { + paginate: true, + }); + return res.body.map((pr) => massagePr(prInfo(pr))); } catch (err) /* istanbul ignore next */ { logger.debug({ err }, 'Error fetching PR list'); if (err.statusCode === 403) { @@ -764,7 +750,7 @@ export async function createPr({ } const description = sanitize(rawDescription); logger.debug(`Creating Merge Request: ${title}`); - const res = await gitlabApi.postJson( + const res = await gitlabApi.postJson( `projects/${config.repository}/merge_requests`, { body: { @@ -778,19 +764,19 @@ export async function createPr({ }, }, ); - const pr = res.body; - pr.number = pr.iid; - pr.sourceBranch = sourceBranch; + + const pr = prInfo(res.body); + // istanbul ignore if if (config.prList) { config.prList.push(pr); } if (platformPrOptions?.autoApprove) { - await approvePr(pr.iid); + await approvePr(pr.number); } - await tryPrAutomerge(pr.iid, platformPrOptions); + await tryPrAutomerge(pr.number, platformPrOptions); return massagePr(pr); } @@ -800,21 +786,7 @@ export async function getPr(iid: number): Promise { const mr = await getMR(config.repository, iid); // Harmonize fields with GitHub - const pr: GitlabPr = { - sourceBranch: mr.source_branch, - targetBranch: mr.target_branch, - number: mr.iid, - bodyStruct: getPrBodyStruct(mr.description), - state: mr.state === 'opened' ? 'open' : mr.state, - headPipelineStatus: mr.head_pipeline?.status, - headPipelineSha: mr.head_pipeline?.sha, - hasAssignees: !!(mr.assignee?.id ?? mr.assignees?.[0]?.id), - reviewers: mr.reviewers?.map(({ username }) => username), - title: mr.title, - labels: mr.labels, - sha: mr.sha, - }; - + const pr: GitlabPr = prInfo(mr); return massagePr(pr); } diff --git a/lib/modules/platform/gitlab/types.ts b/lib/modules/platform/gitlab/types.ts index 244ec8366361e6..190b702bde4b80 100644 --- a/lib/modules/platform/gitlab/types.ts +++ b/lib/modules/platform/gitlab/types.ts @@ -25,6 +25,7 @@ export interface GitLabMergeRequest { state: string; source_branch: string; target_branch: string; + created_at: string; description: string; diverged_commits_count: number; merge_status: string; diff --git a/lib/modules/platform/gitlab/utils.ts b/lib/modules/platform/gitlab/utils.ts new file mode 100644 index 00000000000000..494ad7565783cd --- /dev/null +++ b/lib/modules/platform/gitlab/utils.ts @@ -0,0 +1,19 @@ +import { getPrBodyStruct } from '../pr-body'; +import type { GitLabMergeRequest, GitlabPr } from './types'; + +export function prInfo(mr: GitLabMergeRequest): GitlabPr { + return { + sourceBranch: mr.source_branch, + targetBranch: mr.target_branch, + number: mr.iid, + bodyStruct: getPrBodyStruct(mr.description), + state: mr.state === 'opened' ? 'open' : mr.state, + headPipelineStatus: mr.head_pipeline?.status, + headPipelineSha: mr.head_pipeline?.sha, + hasAssignees: !!(mr.assignee?.id ?? mr.assignees?.[0]?.id), + reviewers: mr.reviewers?.map(({ username }) => username), + title: mr.title, + labels: mr.labels, + sha: mr.sha, + }; +} From 5910042db85fd31367618597874f5871d49e51a3 Mon Sep 17 00:00:00 2001 From: Rahul Gautam Singh Date: Sun, 10 Nov 2024 03:46:04 +0530 Subject: [PATCH 2/5] replace snaps with isMatchObject --- .../gitlab/__snapshots__/index.spec.ts.snap | 78 ------------------- lib/modules/platform/gitlab/index.spec.ts | 28 ++++++- 2 files changed, 24 insertions(+), 82 deletions(-) diff --git a/lib/modules/platform/gitlab/__snapshots__/index.spec.ts.snap b/lib/modules/platform/gitlab/__snapshots__/index.spec.ts.snap index 5638f4538f777f..0bb7c5d69a755d 100644 --- a/lib/modules/platform/gitlab/__snapshots__/index.spec.ts.snap +++ b/lib/modules/platform/gitlab/__snapshots__/index.spec.ts.snap @@ -38,84 +38,6 @@ exports[`modules/platform/gitlab/index createPr(branchName, title, body) raises } `; -exports[`modules/platform/gitlab/index createPr(branchName, title, body) returns the PR 1`] = ` -{ - "bodyStruct": { - "hash": "9a92616496f01e045bfc15bbb549a8627754d2ca4c56ead79a18fb580b8e301f", - }, - "hasAssignees": false, - "headPipelineSha": undefined, - "headPipelineStatus": undefined, - "labels": undefined, - "number": 12345, - "reviewers": undefined, - "sha": undefined, - "sourceBranch": "some-branch", - "state": undefined, - "targetBranch": "master", - "title": "some title", -} -`; - -exports[`modules/platform/gitlab/index createPr(branchName, title, body) supports draftPR on < 13.2 1`] = ` -{ - "bodyStruct": { - "hash": "9a92616496f01e045bfc15bbb549a8627754d2ca4c56ead79a18fb580b8e301f", - }, - "hasAssignees": false, - "headPipelineSha": undefined, - "headPipelineStatus": undefined, - "isDraft": true, - "labels": undefined, - "number": 12345, - "reviewers": undefined, - "sha": undefined, - "sourceBranch": "some-branch", - "state": undefined, - "targetBranch": "master", - "title": "some title", -} -`; - -exports[`modules/platform/gitlab/index createPr(branchName, title, body) supports draftPR on >= 13.2 1`] = ` -{ - "bodyStruct": { - "hash": "9a92616496f01e045bfc15bbb549a8627754d2ca4c56ead79a18fb580b8e301f", - }, - "hasAssignees": false, - "headPipelineSha": undefined, - "headPipelineStatus": undefined, - "isDraft": true, - "labels": undefined, - "number": 12345, - "reviewers": undefined, - "sha": undefined, - "sourceBranch": "some-branch", - "state": undefined, - "targetBranch": "master", - "title": "some title", -} -`; - -exports[`modules/platform/gitlab/index createPr(branchName, title, body) uses default branch 1`] = ` -{ - "bodyStruct": { - "hash": "9a92616496f01e045bfc15bbb549a8627754d2ca4c56ead79a18fb580b8e301f", - }, - "hasAssignees": false, - "headPipelineSha": undefined, - "headPipelineStatus": undefined, - "labels": undefined, - "number": 12345, - "reviewers": undefined, - "sha": undefined, - "sourceBranch": "some-branch", - "state": undefined, - "targetBranch": "master", - "title": "some title", -} -`; - exports[`modules/platform/gitlab/index getBranchPr(branchName) should return the PR object 1`] = ` { "bodyStruct": { diff --git a/lib/modules/platform/gitlab/index.spec.ts b/lib/modules/platform/gitlab/index.spec.ts index e0816a1e359d3e..328b9c29595254 100644 --- a/lib/modules/platform/gitlab/index.spec.ts +++ b/lib/modules/platform/gitlab/index.spec.ts @@ -1937,7 +1937,12 @@ describe('modules/platform/gitlab/index', () => { prBody: 'the-body', labels: null, }); - expect(pr).toMatchSnapshot(); + expect(pr).toMatchObject({ + number: 12345, + title: 'some title', + sourceBranch: 'some-branch', + targetBranch: 'master', + }); }); it('uses default branch', async () => { @@ -1960,7 +1965,12 @@ describe('modules/platform/gitlab/index', () => { prBody: 'the-body', labels: [], }); - expect(pr).toMatchSnapshot(); + expect(pr).toMatchObject({ + number: 12345, + title: 'some title', + sourceBranch: 'some-branch', + targetBranch: 'master', + }); }); it('supports draftPR on < 13.2', async () => { @@ -1983,7 +1993,12 @@ describe('modules/platform/gitlab/index', () => { prBody: 'the-body', draftPR: true, }); - expect(pr).toMatchSnapshot(); + expect(pr).toMatchObject({ + number: 12345, + title: 'some title', + sourceBranch: 'some-branch', + targetBranch: 'master', + }); }); it('supports draftPR on >= 13.2', async () => { @@ -2006,7 +2021,12 @@ describe('modules/platform/gitlab/index', () => { prBody: 'the-body', draftPR: true, }); - expect(pr).toMatchSnapshot(); + expect(pr).toMatchObject({ + number: 12345, + title: 'some title', + sourceBranch: 'some-branch', + targetBranch: 'master', + }); }); it('auto-accepts the MR when requested', async () => { From 82ba5d31a84a98c6e12b004908dca710ef0153ad Mon Sep 17 00:00:00 2001 From: Rahul Gautam Singh Date: Sun, 10 Nov 2024 03:49:39 +0530 Subject: [PATCH 3/5] more snap replace --- .../gitlab/__snapshots__/index.spec.ts.snap | 38 ------------------- lib/modules/platform/gitlab/index.spec.ts | 12 +++++- 2 files changed, 10 insertions(+), 40 deletions(-) diff --git a/lib/modules/platform/gitlab/__snapshots__/index.spec.ts.snap b/lib/modules/platform/gitlab/__snapshots__/index.spec.ts.snap index 0bb7c5d69a755d..c2b91671c51220 100644 --- a/lib/modules/platform/gitlab/__snapshots__/index.spec.ts.snap +++ b/lib/modules/platform/gitlab/__snapshots__/index.spec.ts.snap @@ -1,43 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`modules/platform/gitlab/index createPr(branchName, title, body) raises with squash enabled when repository squash option is always 1`] = ` -{ - "bodyStruct": { - "hash": "9a92616496f01e045bfc15bbb549a8627754d2ca4c56ead79a18fb580b8e301f", - }, - "hasAssignees": false, - "headPipelineSha": undefined, - "headPipelineStatus": undefined, - "labels": undefined, - "number": 12345, - "reviewers": undefined, - "sha": undefined, - "sourceBranch": "some-branch", - "state": undefined, - "targetBranch": "master", - "title": "some title", -} -`; - -exports[`modules/platform/gitlab/index createPr(branchName, title, body) raises with squash enabled when repository squash option is default_on 1`] = ` -{ - "bodyStruct": { - "hash": "9a92616496f01e045bfc15bbb549a8627754d2ca4c56ead79a18fb580b8e301f", - }, - "hasAssignees": false, - "headPipelineSha": undefined, - "headPipelineStatus": undefined, - "labels": undefined, - "number": 12345, - "reviewers": undefined, - "sha": undefined, - "sourceBranch": "some-branch", - "state": undefined, - "targetBranch": "master", - "title": "some title", -} -`; - exports[`modules/platform/gitlab/index getBranchPr(branchName) should return the PR object 1`] = ` { "bodyStruct": { diff --git a/lib/modules/platform/gitlab/index.spec.ts b/lib/modules/platform/gitlab/index.spec.ts index 328b9c29595254..7920a873c2a300 100644 --- a/lib/modules/platform/gitlab/index.spec.ts +++ b/lib/modules/platform/gitlab/index.spec.ts @@ -2318,7 +2318,11 @@ describe('modules/platform/gitlab/index', () => { prBody: 'the-body', labels: null, }); - expect(pr).toMatchSnapshot(); + expect(pr).toMatchObject({ + number: 12345, + sourceBranch: 'some-branch', + title: 'some title', + }); }); it('raises with squash enabled when repository squash option is always', async () => { @@ -2353,7 +2357,11 @@ describe('modules/platform/gitlab/index', () => { prBody: 'the-body', labels: null, }); - expect(pr).toMatchSnapshot(); + expect(pr).toMatchObject({ + number: 12345, + sourceBranch: 'some-branch', + title: 'some title', + }); }); it('adds approval rule to ignore all approvals', async () => { From 5a614f5f038c7e234ef283fc5ff6136679bc50b5 Mon Sep 17 00:00:00 2001 From: Rahul Gautam Singh Date: Sun, 10 Nov 2024 18:42:07 +0530 Subject: [PATCH 4/5] fix accidental removal --- lib/modules/platform/gitlab/index.spec.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/modules/platform/gitlab/index.spec.ts b/lib/modules/platform/gitlab/index.spec.ts index 7920a873c2a300..515bfd65beb643 100644 --- a/lib/modules/platform/gitlab/index.spec.ts +++ b/lib/modules/platform/gitlab/index.spec.ts @@ -3166,6 +3166,7 @@ describe('modules/platform/gitlab/index', () => { number: 1, prTitle: 'title', prBody: 'body', + state: 'open', addLabels: ['new_label'], removeLabels: ['old_label'], }), From 5c1777798c5ce2e74ba71b733cf1eda3c3e630a9 Mon Sep 17 00:00:00 2001 From: Rahul Gautam Singh Date: Fri, 22 Nov 2024 02:27:02 +0530 Subject: [PATCH 5/5] fix: lint --- lib/modules/platform/gitlab/index.ts | 17 ++--------------- lib/modules/platform/gitlab/types.ts | 1 - 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/lib/modules/platform/gitlab/index.ts b/lib/modules/platform/gitlab/index.ts index 4d8551870d2781..aa8b86b31d9c26 100644 --- a/lib/modules/platform/gitlab/index.ts +++ b/lib/modules/platform/gitlab/index.ts @@ -71,13 +71,12 @@ import type { MergeMethod, RepoResponse, } from './types'; -import { prInfo } from './utils'; import { DRAFT_PREFIX, DRAFT_PREFIX_DEPRECATED, prInfo } from './utils'; let config: { repository: string; email: string; - prList: any[] | undefined; + prList: GitlabPr[] | undefined; issueList: GitlabIssue[] | undefined; mergeMethod: MergeMethod; defaultBranch: string; @@ -535,18 +534,6 @@ export async function getBranchStatus( // Pull Request -function massagePr(prToModify: Pr): Pr { - const pr = prToModify; - if (pr.title.startsWith(DRAFT_PREFIX)) { - pr.title = pr.title.substring(DRAFT_PREFIX.length); - pr.isDraft = true; - } else if (pr.title.startsWith(DRAFT_PREFIX_DEPRECATED)) { - pr.title = pr.title.substring(DRAFT_PREFIX_DEPRECATED.length); - pr.isDraft = true; - } - return pr; -} - async function fetchPrList(): Promise { const searchParams = { per_page: '100', @@ -776,7 +763,7 @@ export async function createPr({ await tryPrAutomerge(pr.number, platformPrOptions); - return massagePr(pr); + return pr; } export async function getPr(iid: number): Promise { diff --git a/lib/modules/platform/gitlab/types.ts b/lib/modules/platform/gitlab/types.ts index 190b702bde4b80..244ec8366361e6 100644 --- a/lib/modules/platform/gitlab/types.ts +++ b/lib/modules/platform/gitlab/types.ts @@ -25,7 +25,6 @@ export interface GitLabMergeRequest { state: string; source_branch: string; target_branch: string; - created_at: string; description: string; diverged_commits_count: number; merge_status: string;