Skip to content

Commit

Permalink
Allow specifying that a PR should always be opened
Browse files Browse the repository at this point in the history
Additionally, allow overriding the SHA256 checksum.

Finally, use the `mergeUpstream` operation in the fork before creating
the new branch from the latest state of upstream. This will ensure that
the refs are available in the fork.

Fixes #23, fixes #17
  • Loading branch information
mislav committed Dec 21, 2021
1 parent 2958320 commit ea36ca9
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 17 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,19 @@ archive for this release.

Defaults to `https://github.com/OWNER/REPO/archive/<tag-name>.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:

Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions src/calculate-download-checksum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ function stream(
})
}

async function resolveDownload(api: API, url: URL): Promise<URL> {
async function resolveDownload(apiClient: API, url: URL): Promise<URL> {
if (url.hostname == 'github.com') {
const api = apiClient.rest
const archive = url.pathname.match(
/^\/([^/]+)\/([^/]+)\/archive\/([^/]+)(\.tar\.gz|\.zip)$/
)
Expand Down Expand Up @@ -66,7 +67,7 @@ async function resolveDownload(api: API, url: URL): Promise<URL> {
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' },
})
Expand Down
21 changes: 13 additions & 8 deletions src/edit-github-blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type Options = {
apiClient: API
replace: (oldContent: string) => string
commitMessage?: string
makePR?: boolean
}

export default async function (params: Options): Promise<string> {
Expand All @@ -37,7 +38,7 @@ export default async function (params: Options): Promise<string> {
}
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
Expand All @@ -60,13 +61,17 @@ export default async function (params: Options): Promise<string> {
}
}

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,
Expand Down Expand Up @@ -106,7 +111,7 @@ export default async function (params: Options): Promise<string> {
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')
Expand All @@ -120,6 +125,6 @@ export default async function (params: Options): Promise<string> {
})
return prRes.data.html_url
} else {
return commitRes.data.commit.html_url
return commitRes.data.commit.html_url || ''
}
}
13 changes: 10 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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<string, string>()
replacements.set('version', version)
replacements.set('url', downloadUrl)
Expand All @@ -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}`,
})
Expand All @@ -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'))
)
}

Expand All @@ -106,6 +112,7 @@ export async function prepareEdit(
branch,
filePath,
commitMessage,
makePR,
replace(oldContent: string) {
return removeRevisionLine(replaceFields(oldContent, replacements))
},
Expand Down

0 comments on commit ea36ca9

Please sign in to comment.