From 2dbaa9f6011817972c4b641b12c2e1ba792ad7cf Mon Sep 17 00:00:00 2001 From: David Bushong Date: Fri, 16 Feb 2024 08:26:45 -0800 Subject: [PATCH 1/4] feat: create-branch=false option --- src/edit-github-blob.ts | 4 +++- src/main.ts | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/edit-github-blob.ts b/src/edit-github-blob.ts index 48a1cca..542b2d3 100644 --- a/src/edit-github-blob.ts +++ b/src/edit-github-blob.ts @@ -38,6 +38,7 @@ export type Options = { repo: string } makePR?: boolean + makeBranch?: boolean } export default async function (params: Options): Promise { @@ -65,7 +66,8 @@ export default async function (params: Options): Promise { branch: baseBranch, }) const needsBranch = - inFork || branchRes.data.protected || params.makePR === true + params.makeBranch !== false && + (inFork || branchRes.data.protected || params.makePR === true) if (makeFork) { const res = await Promise.all([ diff --git a/src/main.ts b/src/main.ts index 411447a..0066cbb 100644 --- a/src/main.ts +++ b/src/main.ts @@ -106,6 +106,17 @@ export async function prepareEdit( makePR = getBooleanInput('create-pullrequest') } + let makeBranch: boolean | undefined + if (getInput('create-branch')) { + makeBranch = getBooleanInput('create-branch') + } + + if (makePR === true && makeBranch === false) { + throw new Error( + 'create-pullrequest cannot be true if create-branch is false' + ) + } + const replacements = new Map() replacements.set('version', version) replacements.set('url', downloadUrl) @@ -146,6 +157,7 @@ export async function prepareEdit( commitMessage, pushTo, makePR, + makeBranch, replace(oldContent: string) { return removeRevisionLine(replaceFields(oldContent, replacements)) }, From 1a6526d0e810f8ea032d8f0ce2400128231231e5 Mon Sep 17 00:00:00 2001 From: David Bushong Date: Fri, 16 Feb 2024 08:33:43 -0800 Subject: [PATCH 2/4] fix: add missing docs, entries, and logic --- README.md | 5 +++++ action.yml | 2 ++ src/edit-github-blob.ts | 5 +++-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b28c80d..d04eccf 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,11 @@ Repository parameters: a pull request to `homebrew-tap`. Defaults to false if `COMMITTER_TOKEN` has the privileges to directly push to `base-branch` in `homebrew-tap`. +- `create-branch`: a boolean value to either force or prohibit creating a + branch on `homebrew-tap`. Defaults to false if `COMMITTER_TOKEN` has + the privileges to directly push to `base-branch` in `homebrew-tap`. + You cannot set this to `false` if `create-pullrequest` is set to `true`. + - `commit-message`: the git commit message template to use when updating the formula. The following placeholders be expanded: diff --git a/action.yml b/action.yml index 173fc08..0099a44 100644 --- a/action.yml +++ b/action.yml @@ -24,6 +24,8 @@ inputs: description: The branch name in the homebrew-tap repository to update the formula in create-pullrequest: description: Set to a boolean value to either force or prohibit making a pull request to homebrew-tap + create-branch: + description: Set to a boolean value to either force or prohibit creating a separate branch on homebrew-tap commit-message: description: The git commit message template to use when updating the formula default: | diff --git a/src/edit-github-blob.ts b/src/edit-github-blob.ts index 542b2d3..7be8b6e 100644 --- a/src/edit-github-blob.ts +++ b/src/edit-github-blob.ts @@ -66,8 +66,9 @@ export default async function (params: Options): Promise { branch: baseBranch, }) const needsBranch = - params.makeBranch !== false && - (inFork || branchRes.data.protected || params.makePR === true) + params.makeBranch === undefined + ? inFork || branchRes.data.protected || params.makePR === true + : params.makeBranch if (makeFork) { const res = await Promise.all([ From ca37f7513073687add1409176317061397ec5f77 Mon Sep 17 00:00:00 2001 From: David Bushong Date: Fri, 16 Feb 2024 08:35:20 -0800 Subject: [PATCH 3/4] refactor: use looser test --- src/edit-github-blob.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/edit-github-blob.ts b/src/edit-github-blob.ts index 7be8b6e..0b9028a 100644 --- a/src/edit-github-blob.ts +++ b/src/edit-github-blob.ts @@ -66,7 +66,7 @@ export default async function (params: Options): Promise { branch: baseBranch, }) const needsBranch = - params.makeBranch === undefined + params.makeBranch == null ? inFork || branchRes.data.protected || params.makePR === true : params.makeBranch From 8fcde402f81f8b77fbb754e09e4fbb9efd0eb4bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Tue, 19 Nov 2024 15:31:05 +0100 Subject: [PATCH 4/4] Add test for create-branch: false --- src/edit-github-blob-test.ts | 51 ++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/edit-github-blob-test.ts b/src/edit-github-blob-test.ts index a8cd2a0..dd7c69b 100644 --- a/src/edit-github-blob-test.ts +++ b/src/edit-github-blob-test.ts @@ -273,3 +273,54 @@ test('edit-github-blob with pushTo the base repo', async (t) => { }) t.is(url, 'https://github.com/OWNER/REPO/commit/NEWSHA') }) + +test('edit-github-blob with create-branch set to false', async (t) => { + const stubbedFetch = function (url: string, options: fetchOptions) { + function route(method: string, path: string): boolean { + return ( + method.toUpperCase() === options.method.toUpperCase() && + `https://api.github.com/${path}` === url + ) + } + + if (route('GET', 'repos/OWNER/REPO')) { + return replyJSON(200, { + default_branch: 'main', + permissions: { push: true, admin: false }, + }) + } else if (route('GET', 'repos/OWNER/REPO/branches/main')) { + return replyJSON(200, { + commit: { sha: 'COMMITSHA' }, + protected: true, + }) + } else if ( + route('GET', `repos/OWNER/REPO/contents/formula%2Ftest.rb?ref=main`) + ) { + return replyJSON(200, { + content: Buffer.from(`old content`).toString('base64'), + }) + } else if (route('PUT', 'repos/OWNER/REPO/contents/formula%2Ftest.rb')) { + const payload = JSON.parse(options.body || '') + t.is(payload.branch, 'main') + t.is(payload.message, 'Update formula/test.rb') + t.is( + Buffer.from(payload.content, 'base64').toString('utf8'), + 'OLD CONTENT' + ) + return replyJSON(200, { + commit: { html_url: 'https://github.com/OWNER/REPO/commit/NEWSHA' }, + }) + } + throw `not stubbed: ${options.method} ${url}` + } + + const url = await editGithubBlob({ + apiClient: api('ATOKEN', { fetch: stubbedFetch, logRequests: false }), + owner: 'OWNER', + repo: 'REPO', + filePath: 'formula/test.rb', + makeBranch: false, + replace: (oldContent) => oldContent.toUpperCase(), + }) + t.is(url, 'https://github.com/OWNER/REPO/commit/NEWSHA') +})