Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create-branch=false option #152

Merged
merged 4 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
51 changes: 51 additions & 0 deletions src/edit-github-blob-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
5 changes: 4 additions & 1 deletion src/edit-github-blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type Options = {
repo: string
}
makePR?: boolean
makeBranch?: boolean
}

export default async function (params: Options): Promise<string> {
Expand Down Expand Up @@ -65,7 +66,9 @@ export default async function (params: Options): Promise<string> {
branch: baseBranch,
})
const needsBranch =
inFork || branchRes.data.protected || params.makePR === true
params.makeBranch == null
? inFork || branchRes.data.protected || params.makePR === true
: params.makeBranch

if (makeFork) {
const res = await Promise.all([
Expand Down
12 changes: 12 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>()
replacements.set('version', version)
replacements.set('url', downloadUrl)
Expand Down Expand Up @@ -146,6 +157,7 @@ export async function prepareEdit(
commitMessage,
pushTo,
makePR,
makeBranch,
replace(oldContent: string) {
return removeRevisionLine(replaceFields(oldContent, replacements))
},
Expand Down