-
Notifications
You must be signed in to change notification settings - Fork 34
/
main.ts
132 lines (121 loc) · 3.57 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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'
import { removeRevisionLine, replaceFields } from './replace-formula-fields'
import calculateDownloadChecksum from './calculate-download-checksum'
import { context } from '@actions/github'
function tarballForRelease(
owner: string,
repo: string,
tagName: string
): string {
return `https://github.com/${owner}/${repo}/archive/${tagName}.tar.gz`
}
export function commitForRelease(
messageTemplate: string,
params: { [key: string]: string } = {}
): string {
return messageTemplate.replace(
/\{\{(\w+)\}\}/g,
(m: string, key: string): string => {
if (Object.hasOwnProperty.call(params, key)) {
return params[key]
}
return m
}
)
}
export default async function (api: (token: string) => API): Promise<void> {
const internalToken =
process.env.GITHUB_TOKEN || process.env.COMMITTER_TOKEN || ''
const externalToken = process.env.COMMITTER_TOKEN || ''
const options = await prepareEdit(
context,
api(internalToken),
api(externalToken)
)
const createdUrl = await editGitHubBlob(options)
console.log(createdUrl)
}
type Context = {
ref: string
sha: string
repo: {
owner: string
repo: string
}
}
export async function prepareEdit(
ctx: Context,
sameRepoClient: API,
crossRepoClient: API
): Promise<EditOptions> {
const tagName =
getInput('tag-name') ||
((ref) => {
if (!ref.startsWith('refs/tags/')) throw `invalid ref: ${ref}`
return ref.replace('refs/tags/', '')
})(ctx.ref)
const [owner, repo] = getInput('homebrew-tap', { required: true }).split('/')
let pushTo: { owner: string; repo: string } | undefined
const pushToSpec = getInput('push-to')
if (pushToSpec) {
const [pushToOwner, pushToRepo] = pushToSpec.split('/')
pushTo = { owner: pushToOwner, repo: pushToRepo }
}
const formulaName = getInput('formula-name') || ctx.repo.repo.toLowerCase()
const branch = getInput('base-branch')
const filePath = getInput('formula-path') || `Formula/${formulaName}.rb`
const version = tagName.replace(/^v(\d)/, '$1')
const downloadUrl =
getInput('download-url') ||
tarballForRelease(ctx.repo.owner, ctx.repo.repo, tagName)
const messageTemplate = getInput('commit-message', { required: true })
let 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)
if (downloadUrl.endsWith('.git')) {
replacements.set('tag', tagName)
replacements.set(
'revision',
await (async () => {
if (ctx.ref == `refs/tags/${tagName}`) return ctx.sha
else {
const res = await sameRepoClient.rest.git.getRef({
...ctx.repo,
ref: `tags/${tagName}`,
})
return res.data.object.sha
}
})()
)
} else {
replacements.set(
'sha256',
getInput('download-sha256') ||
(await calculateDownloadChecksum(sameRepoClient, downloadUrl, 'sha256'))
)
}
const commitMessage = commitForRelease(messageTemplate, {
formulaName,
version,
})
return {
apiClient: crossRepoClient,
owner,
repo,
branch,
filePath,
commitMessage,
pushTo,
makePR,
replace(oldContent: string) {
return removeRevisionLine(replaceFields(oldContent, replacements))
},
}
}