diff --git a/README.md b/README.md index cb7b80b..8efa5e7 100644 --- a/README.md +++ b/README.md @@ -79,12 +79,19 @@ archive for this release. Defaults to `https://github.com/OWNER/REPO/archive/.tar.gz` +* `download-sha256`: the SHA256 checksum of the archive at `download-url`. + Defaults to calculating the checksum by fetching the URL. + * `homebrew-tap`: the repository where the formula should be updated. Defaults to `Homebrew/homebrew-core`. * `base-branch`: the branch name in the `homebrew-tap` repository where the formula should be updated. Defaults to the main branch. +* `create-pullrequest`: a boolean value to either force or prohibit submitting + a pull request to `homebrew-tap`. Defaults to false if `COMMITTER_TOKEN` has + the privileges to directly push to `base-branch` in `homebrew-tap`. + * `commit-message`: the git commit message template to use when updating the formula. The following placeholders be expanded: @@ -146,6 +153,7 @@ If the token has push access, but the default branch of the tap repo is protected, a pull request will be opened from a new branch in the same repo. Otherwise, the formula will be edited via a direct push to the default branch. +This can be overriden by setting `create-pullrequest`. ## Manual trigger diff --git a/action.yml b/action.yml index ecee893..c48bd22 100644 --- a/action.yml +++ b/action.yml @@ -13,11 +13,15 @@ inputs: description: The git tag name to bump the formula to (defaults to the currently pushed tag) download-url: description: The package download URL for the Homebrew formula (defaults to the release tarball) + download-sha256: + description: The SHA256 checksum of the archive at download-url (defaults to calculating it) homebrew-tap: description: The repository where the formula should be updated default: Homebrew/homebrew-core base-branch: description: The branch name in the homebrew-tap repository where the formula should be updated + create-pullrequest: + description: Set to a boolean value to either force or prohibit making a pull request to homebrew-tap commit-message: description: The git commit message template to use when updating the formula default: | diff --git a/package.json b/package.json index 907920a..1d1cc89 100644 --- a/package.json +++ b/package.json @@ -6,11 +6,11 @@ "test": "tsc --sourceMap && ava" }, "dependencies": { - "@actions/core": "^1.2.6", - "@actions/github": "^4.0.0", - "@octokit/core": "^3.2.5", + "@actions/core": "^1.6.0", + "@actions/github": "^5.0.0", + "@octokit/core": "^3.5.1", "@octokit/plugin-request-log": "^1.0.3", - "@octokit/plugin-rest-endpoint-methods": "^4.10.1" + "@octokit/plugin-rest-endpoint-methods": "^5.13.0" }, "devDependencies": { "@types/node": "^14.14.25", diff --git a/src/calculate-download-checksum.ts b/src/calculate-download-checksum.ts index 25a1402..83581bd 100644 --- a/src/calculate-download-checksum.ts +++ b/src/calculate-download-checksum.ts @@ -32,8 +32,9 @@ function stream( }) } -async function resolveDownload(api: API, url: URL): Promise { +async function resolveDownload(apiClient: API, url: URL): Promise { if (url.hostname == 'github.com') { + const api = apiClient.rest const archive = url.pathname.match( /^\/([^/]+)\/([^/]+)\/archive\/([^/]+)(\.tar\.gz|\.zip)$/ ) @@ -66,7 +67,7 @@ async function resolveDownload(api: API, url: URL): Promise { if (asset == null) { throw new Error(`could not find asset '${path}' in '${tag}' release`) } - const assetRes = await api.request(asset.url, { + const assetRes = await apiClient.request(asset.url, { headers: { accept: 'application/octet-stream' }, request: { redirect: 'manual' }, }) diff --git a/src/edit-github-blob.ts b/src/edit-github-blob.ts index 24f9804..0957c56 100644 --- a/src/edit-github-blob.ts +++ b/src/edit-github-blob.ts @@ -28,6 +28,7 @@ export type Options = { apiClient: API replace: (oldContent: string) => string commitMessage?: string + makePR?: boolean } export default async function (params: Options): Promise { @@ -37,7 +38,7 @@ export default async function (params: Options): Promise { } let headRepo = baseRepo const filePath = params.filePath - const api = params.apiClient + const api = params.apiClient.rest const repoRes = await api.repos.get(baseRepo) const baseBranch = params.branch ? params.branch : repoRes.data.default_branch @@ -60,13 +61,17 @@ export default async function (params: Options): Promise { } } - const needsPr = needsFork || branchRes.data.protected - if (needsPr) { + const needsBranch = + needsFork || branchRes.data.protected || params.makePR === true + if (needsBranch) { const timestamp = Math.round(Date.now() / 1000) headBranch = `update-${basename(filePath)}-${timestamp}` - } - - if (needsPr) { + if (needsFork) { + await api.repos.mergeUpstream({ + ...headRepo, + branch: repoRes.data.default_branch, + }) + } await retry(needsFork ? 6 : 0, 5000, async () => { await api.git.createRef({ ...headRepo, @@ -106,7 +111,7 @@ export default async function (params: Options): Promise { branch: headBranch, }) - if (needsPr) { + if (needsBranch && params.makePR !== false) { const parts = commitMessage.split('\n\n') const title = parts[0] const body = parts.slice(1).join('\n\n') @@ -120,6 +125,6 @@ export default async function (params: Options): Promise { }) return prRes.data.html_url } else { - return commitRes.data.commit.html_url + return commitRes.data.commit.html_url || '' } } diff --git a/src/main.ts b/src/main.ts index 7b0fbe7..ac7fbe0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,4 @@ -import { getInput } from '@actions/core' +import { getInput, getBooleanInput } from '@actions/core' import type { API } from './api' import editGitHubBlob from './edit-github-blob' import { Options as EditOptions } from './edit-github-blob' @@ -69,6 +69,11 @@ export async function prepareEdit( tarballForRelease(ctx.repo.owner, ctx.repo.repo, tagName) const messageTemplate = getInput('commit-message', { required: true }) + var makePR: boolean | undefined + if (getInput('create-pullrequest')) { + makePR = getBooleanInput('create-pullrequest') + } + const replacements = new Map() replacements.set('version', version) replacements.set('url', downloadUrl) @@ -79,7 +84,7 @@ export async function prepareEdit( await (async () => { if (ctx.ref == `refs/tags/${tagName}`) return ctx.sha else { - const res = await sameRepoClient.git.getRef({ + const res = await sameRepoClient.rest.git.getRef({ ...ctx.repo, ref: `tags/${tagName}`, }) @@ -90,7 +95,8 @@ export async function prepareEdit( } else { replacements.set( 'sha256', - await calculateDownloadChecksum(sameRepoClient, downloadUrl, 'sha256') + getInput('download-sha256') || + (await calculateDownloadChecksum(sameRepoClient, downloadUrl, 'sha256')) ) } @@ -106,6 +112,7 @@ export async function prepareEdit( branch, filePath, commitMessage, + makePR, replace(oldContent: string) { return removeRevisionLine(replaceFields(oldContent, replacements)) },