diff --git a/lib/platform/gitlab/__snapshots__/index.spec.ts.snap b/lib/platform/gitlab/__snapshots__/index.spec.ts.snap index 4a26be961c2328fe56c8ba817d630303ccdb0b27..3dc019f9e179d275e9bb82aacf3f515d9c271976 100644 --- a/lib/platform/gitlab/__snapshots__/index.spec.ts.snap +++ b/lib/platform/gitlab/__snapshots__/index.spec.ts.snap @@ -408,6 +408,132 @@ Array [ ] `; +exports[`platform/gitlab/index createPr(branchName, title, body) raises with squash enabled when repository squash option is always 1`] = ` +Object { + "displayNumber": "Merge Request #12345", + "id": 1, + "iid": 12345, + "number": 12345, + "sourceBranch": "some-branch", + "title": "some title", +} +`; + +exports[`platform/gitlab/index createPr(branchName, title, body) raises with squash enabled when repository squash option is always 2`] = ` +Array [ + Object { + "headers": Object { + "accept": "application/json", + "accept-encoding": "gzip, deflate, br", + "authorization": "Bearer some-token", + "host": "gitlab.com", + "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)", + }, + "method": "GET", + "url": "https://gitlab.com/api/v4/user", + }, + Object { + "headers": Object { + "accept": "application/json", + "accept-encoding": "gzip, deflate, br", + "authorization": "Bearer some-token", + "host": "gitlab.com", + "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)", + }, + "method": "GET", + "url": "https://gitlab.com/api/v4/version", + }, + Object { + "headers": Object { + "accept": "application/json", + "accept-encoding": "gzip, deflate, br", + "authorization": "Bearer abc123", + "host": "gitlab.com", + "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)", + }, + "method": "GET", + "url": "https://gitlab.com/api/v4/projects/some%2Frepo", + }, + Object { + "body": "{\\"source_branch\\":\\"some-branch\\",\\"target_branch\\":\\"master\\",\\"remove_source_branch\\":true,\\"title\\":\\"some-title\\",\\"description\\":\\"the-body\\",\\"labels\\":null,\\"squash\\":true}", + "headers": Object { + "accept": "application/json", + "accept-encoding": "gzip, deflate, br", + "authorization": "Bearer abc123", + "content-length": "158", + "content-type": "application/json", + "host": "gitlab.com", + "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)", + }, + "method": "POST", + "url": "https://gitlab.com/api/v4/projects/some%2Frepo/merge_requests", + }, +] +`; + +exports[`platform/gitlab/index createPr(branchName, title, body) raises with squash enabled when repository squash option is default_on 1`] = ` +Object { + "displayNumber": "Merge Request #12345", + "id": 1, + "iid": 12345, + "number": 12345, + "sourceBranch": "some-branch", + "title": "some title", +} +`; + +exports[`platform/gitlab/index createPr(branchName, title, body) raises with squash enabled when repository squash option is default_on 2`] = ` +Array [ + Object { + "headers": Object { + "accept": "application/json", + "accept-encoding": "gzip, deflate, br", + "authorization": "Bearer some-token", + "host": "gitlab.com", + "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)", + }, + "method": "GET", + "url": "https://gitlab.com/api/v4/user", + }, + Object { + "headers": Object { + "accept": "application/json", + "accept-encoding": "gzip, deflate, br", + "authorization": "Bearer some-token", + "host": "gitlab.com", + "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)", + }, + "method": "GET", + "url": "https://gitlab.com/api/v4/version", + }, + Object { + "headers": Object { + "accept": "application/json", + "accept-encoding": "gzip, deflate, br", + "authorization": "Bearer abc123", + "host": "gitlab.com", + "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)", + }, + "method": "GET", + "url": "https://gitlab.com/api/v4/projects/some%2Frepo", + }, + Object { + "body": "{\\"source_branch\\":\\"some-branch\\",\\"target_branch\\":\\"master\\",\\"remove_source_branch\\":true,\\"title\\":\\"some-title\\",\\"description\\":\\"the-body\\",\\"labels\\":null,\\"squash\\":true}", + "headers": Object { + "accept": "application/json", + "accept-encoding": "gzip, deflate, br", + "authorization": "Bearer abc123", + "content-length": "158", + "content-type": "application/json", + "host": "gitlab.com", + "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)", + }, + "method": "POST", + "url": "https://gitlab.com/api/v4/projects/some%2Frepo/merge_requests", + }, +] +`; + exports[`platform/gitlab/index createPr(branchName, title, body) returns the PR 1`] = ` Object { "displayNumber": "Merge Request #12345", diff --git a/lib/platform/gitlab/index.spec.ts b/lib/platform/gitlab/index.spec.ts index 5ad17600ec672bb6bc9c508150c40b9f99e651d0..b293bcf761db405dceedb3b0402e357ea89cd492 100644 --- a/lib/platform/gitlab/index.spec.ts +++ b/lib/platform/gitlab/index.spec.ts @@ -1380,6 +1380,72 @@ describe(getName(), () => { expect(httpMock.getTrace()).toMatchSnapshot(); }); + it('raises with squash enabled when repository squash option is default_on', async () => { + await initPlatform('14.0.0'); + + httpMock + .scope(gitlabApiHost) + .get('/api/v4/projects/some%2Frepo') + .reply(200, { + squash_option: 'default_on', + default_branch: 'master', + url: 'https://some-url', + }); + await gitlab.initRepo({ + repository: 'some/repo', + }); + httpMock + .scope(gitlabApiHost) + .post('/api/v4/projects/some%2Frepo/merge_requests') + .reply(200, { + id: 1, + iid: 12345, + title: 'some title', + }); + const pr = await gitlab.createPr({ + sourceBranch: 'some-branch', + targetBranch: 'master', + prTitle: 'some-title', + prBody: 'the-body', + labels: null, + }); + expect(pr).toMatchSnapshot(); + expect(httpMock.getTrace()).toMatchSnapshot(); + }); + + it('raises with squash enabled when repository squash option is always', async () => { + await initPlatform('14.0.0'); + + httpMock + .scope(gitlabApiHost) + .get('/api/v4/projects/some%2Frepo') + .reply(200, { + squash_option: 'always', + default_branch: 'master', + url: 'https://some-url', + }); + await gitlab.initRepo({ + repository: 'some/repo', + }); + httpMock + .scope(gitlabApiHost) + .post('/api/v4/projects/some%2Frepo/merge_requests') + .reply(200, { + id: 1, + iid: 12345, + title: 'some title', + }); + const pr = await gitlab.createPr({ + sourceBranch: 'some-branch', + targetBranch: 'master', + prTitle: 'some-title', + prBody: 'the-body', + labels: null, + }); + expect(pr).toMatchSnapshot(); + expect(httpMock.getTrace()).toMatchSnapshot(); + }); + it('adds approval rule to ignore all approvals', async () => { await initPlatform('13.3.6-ee'); httpMock diff --git a/lib/platform/gitlab/index.ts b/lib/platform/gitlab/index.ts index 390770348b46b4a3a39cf3e99b24dc9fe8ba3158..d8c1f89d929f865ec6d82a216aceb4d0ecd0cdc7 100644 --- a/lib/platform/gitlab/index.ts +++ b/lib/platform/gitlab/index.ts @@ -59,6 +59,7 @@ let config: { defaultBranch: string; cloneSubmodules: boolean; ignorePrAuthor: boolean; + squash: boolean; } = {} as any; const defaults = { @@ -210,6 +211,11 @@ export async function initRepo({ throw new Error(TEMPORARY_ERROR); } config.mergeMethod = res.body.merge_method || 'merge'; + if (res.body.squash_option) { + config.squash = + res.body.squash_option === 'always' || + res.body.squash_option === 'default_on'; + } logger.debug(`${repository} default branch = ${config.defaultBranch}`); delete config.prList; logger.debug('Enabling Git FS'); @@ -521,6 +527,7 @@ export async function createPr({ title, description, labels: is.array(labels) ? labels.join(',') : null, + squash: config.squash, }, } ); diff --git a/lib/platform/gitlab/types.ts b/lib/platform/gitlab/types.ts index 11c2fff864ca5c4add978e26ae0dda5231fc182e..4a59858488afc6770e94c2f8dd356a6e70bba237 100644 --- a/lib/platform/gitlab/types.ts +++ b/lib/platform/gitlab/types.ts @@ -53,6 +53,7 @@ export interface RepoResponse { merge_requests_access_level: 'disabled' | 'private' | 'enabled'; merge_method: MergeMethod; path_with_namespace: string; + squash_option?: 'never' | 'always' | 'default_on' | 'default_off'; } // See https://gitlab.com/gitlab-org/gitlab/-/blob/master/app/graphql/types/user_status_type.rb