From b9cb21fcf051dc77b868cf533d89c164015bf75a Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Tue, 24 Jan 2023 15:05:08 +0000 Subject: [PATCH] feat(github): respect new `prAllowMaintainerEdits` option This commit ensures the GitHub platform logic respects the new `prallowMaintainerEdits` option. Whenever a PR is created, depending on the option value, the GitHub API post request will have the respective option `maintainer_can_modify` option set. This option was always enabled in fork mode before this commit, and without fork mode enabled, the option is a noop because maintainers have access to upstream branches regardless. Users may want to leverage the option and disable it to avoid issues with modifications in Renovate fork accounts, as described in: #19771 Fixes #19771. --- lib/modules/platform/github/index.spec.ts | 49 +++++++++++++++++++++++ lib/modules/platform/github/index.ts | 3 +- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/lib/modules/platform/github/index.spec.ts b/lib/modules/platform/github/index.spec.ts index d148c4187478108..6dae40862234a6e 100644 --- a/lib/modules/platform/github/index.spec.ts +++ b/lib/modules/platform/github/index.spec.ts @@ -2173,6 +2173,55 @@ describe('modules/platform/github/index', () => { expect(pr).toMatchObject({ number: 123 }); }); + it('should allow maintainer edits if enabled via options', async () => { + const scope = httpMock.scope(githubApiHost); + initRepoMock(scope, 'some/repo'); + scope + .post( + '/repos/some/repo/pulls', + // Ensure the `maintainer_can_modify` option is set in the REST API request. + (body) => body.maintainer_can_modify === true + ) + .reply(200, { + number: 123, + head: { repo: { full_name: 'some/repo' }, ref: 'some-branch' }, + }); + await github.initRepo({ repository: 'some/repo' }); + const pr = await github.createPr({ + sourceBranch: 'some-branch', + targetBranch: 'main', + prTitle: 'PR title', + prBody: 'PR can be edited by maintainers.', + labels: null, + allowMaintainerEdits: true, + }); + expect(pr).toMatchObject({ number: 123 }); + }); + + it('should not allow maintainer edits if not explicitly set', async () => { + const scope = httpMock.scope(githubApiHost); + initRepoMock(scope, 'some/repo'); + scope + .post( + '/repos/some/repo/pulls', + // Ensure the `maintainer_can_modify` option is unset in the REST API request. + (body) => body.maintainer_can_modify === undefined + ) + .reply(200, { + number: 123, + head: { repo: { full_name: 'some/repo' }, ref: 'some-branch' }, + }); + await github.initRepo({ repository: 'some/repo' }); + const pr = await github.createPr({ + sourceBranch: 'some-branch', + targetBranch: 'main', + prTitle: 'PR title', + prBody: 'PR *cannot* be edited by maintainers.', + labels: null, + }); + expect(pr).toMatchObject({ number: 123 }); + }); + describe('automerge', () => { const createdPrResp = { number: 123, diff --git a/lib/modules/platform/github/index.ts b/lib/modules/platform/github/index.ts index 573dc8bf70a84a9..9372fe05e6831ce 100644 --- a/lib/modules/platform/github/index.ts +++ b/lib/modules/platform/github/index.ts @@ -1474,6 +1474,7 @@ export async function createPr({ prBody: rawBody, labels, draftPR = false, + allowMaintainerEdits, platformOptions, }: CreatePRConfig): Promise { const body = sanitize(rawBody); @@ -1489,12 +1490,12 @@ export async function createPr({ base, body, draft: draftPR, + maintainer_can_modify: allowMaintainerEdits, }, }; // istanbul ignore if if (config.forkToken) { options.token = config.forkToken; - options.body.maintainer_can_modify = true; } logger.debug({ title, head, base, draft: draftPR }, 'Creating PR'); const ghPr = (