-
Notifications
You must be signed in to change notification settings - Fork 11
/
repository-updates.js
162 lines (141 loc) · 6.11 KB
/
repository-updates.js
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const callProg = (prog, parameters, cwd) => {
const { spawnSync } = require('child_process')
const child = spawnSync(prog, parameters, {
stdio: ['ignore', 'pipe', 'inherit'],
cwd
})
if (child.error) throw child.error
if (child.status !== 0) throw new Error(`${prog} ${parameters.join(' ')} failed with status ${child.status}`)
return child.stdout.toString('utf-8').replace(/\r?\n$/, '')
}
const callGit = (parameters, cwd) => {
return callProg('git', parameters, cwd)
}
const getWorkflowRunArtifact = async (context, token, owner, repo, workflowRunId, name) => {
const { getWorkflowRunArtifactsURLs, downloadAndUnZip } = require('./github-release')
const urls = await getWorkflowRunArtifactsURLs(context, token, owner, repo, workflowRunId)
context.log(`Downloading ${name}`)
await downloadAndUnZip(token, urls[name], name)
}
const mergeBundle = (gitDir, worktree, bundlePath, refName) => {
callGit(['--git-dir', gitDir, 'fetch', bundlePath, refName])
// If there is nothing to merge, return early
if (callGit(['--git-dir', gitDir, 'rev-list', '--count', `${refName}..FETCH_HEAD`]) === '0') return
if (worktree) {
callGit(['branch', '-M', refName], worktree)
callGit(['merge', '--no-edit', 'FETCH_HEAD'], worktree)
} else {
// If it fast-forwards, we do not need a worktree
if (callGit(['--git-dir', gitDir, 'rev-list', '--count', `FETCH_HEAD..${refName}`]) === '0') {
callGit(['--git-dir', gitDir, 'update-ref', `refs/heads/${refName}`, 'FETCH_HEAD'])
} else {
// Fine. We need a worktree. But we don't have one. So let's create one.
worktree = `${gitDir}/tmp-worktree`
callGit(['--git-dir', gitDir, 'worktree', 'add', '--no-checkout', worktree])
// No need for a full checkout, it's not as if we want to resolve any merge conflicts...
callGit(['sparse-checkout', 'set'], worktree)
callGit(['switch', '-d', refName], worktree)
// Perform the merge
callGit(['fetch', bundlePath, refName], worktree)
callGit(['merge', '--no-edit', 'FETCH_HEAD'], worktree)
callGit(['update-ref', `refs/heads/${refName}`, 'HEAD'], worktree)
}
}
}
const getPushAuthorizationHeader = async (context, setSecret, appId, privateKey, owner, repo) => {
const getAppInstallationId = require('./get-app-installation-id')
const installationId = await getAppInstallationId(
context,
appId,
privateKey,
owner,
repo
)
const getInstallationAccessToken = require('./get-installation-access-token')
const { token: accessToken } = await getInstallationAccessToken(
context,
appId,
privateKey,
installationId
)
const auth = Buffer.from(`PAT:${accessToken}`).toString('base64')
if (setSecret) setSecret(auth)
return `Authorization: Basic ${auth}`
}
const pushRepositoryUpdate = async (context, setSecret, appId, privateKey, owner, repo, refName, bundlePath) => {
context.log(`Pushing updates to ${owner}/${repo}`)
// Updates to `build-extra` and `git-for-windows.github.io` need a worktree
const bare = ['build-extra', 'git-for-windows.github.io'].includes(repo) ? '' : ['--bare']
const gitDir = `${repo}${bare ? '' : '/.git'}`
callGit(['clone', ...bare,
'--single-branch', '--branch', 'main', '--depth', '50',
`https://github.com/${owner}/${repo}`, repo
])
if (bundlePath) {
// Allow Git to fetch non-local objects by pretending to be a partial clone
callGit(['--git-dir', gitDir, 'config', 'remote.origin.promisor', 'true'])
callGit(['--git-dir', gitDir, 'config', 'remote.origin.partialCloneFilter', 'blob:none'])
mergeBundle(gitDir, !bare && repo, bundlePath, refName)
}
if (repo === 'build-extra') {
// Add `versions/package-versions-$ver*.txt`
const fs = require('fs')
const ver = fs.readFileSync('bundle-artifacts/ver').toString().trim()
fs.renameSync(
'installer-x86_64/package-versions.txt',
`${repo}/versions/package-versions-${ver}.txt`
)
fs.renameSync(
'mingit-x86_64/package-versions.txt',
`${repo}/versions/package-versions-${ver}-MinGit.txt`
)
callGit([
'add',
`versions/package-versions-${ver}.txt`,
`versions/package-versions-${ver}-MinGit.txt`
], repo)
callGit([
'commit',
'-s',
'-m', `versions: add v${ver}`,
`versions/package-versions-${ver}.txt`,
`versions/package-versions-${ver}-MinGit.txt`
], repo)
// Update `download-stats.sh`
callProg('sh', ['./download-stats.sh', '--update'], repo)
callGit(['commit', '-s', '-m', 'download-stats: new Git for Windows version', './download-stats.sh'], repo)
} else if (repo === 'git-for-windows.github.io') {
callGit(['switch', '-C', 'main', 'origin/main'], repo)
callProg('node', ['bump-version.js', '--auto'], repo)
callGit(['commit', '-a', '-s', '-m', 'New Git for Windows version'], repo)
}
const authorizationHeader = await getPushAuthorizationHeader(context, setSecret, appId, privateKey, owner, repo)
callGit(['--git-dir', gitDir,
'-c', `http.extraHeader=${authorizationHeader}`,
'push', `https://github.com/${owner}/${repo}`, refName
])
context.log(`Done pushing ref ${refName} to ${owner}/${repo}`)
}
const pushGitBranch = async (context, setSecret, appId, privateKey, owner, repo, pushRefSpec) => {
context.log(`Pushing ${pushRefSpec} to ${owner}/${repo}`)
callGit(['clone', '--bare', '--filter=blob:none',
'--single-branch', '--branch', 'main', '--depth', '50',
`https://github.com/${owner}/${repo}`, repo
])
// Allow Git to fetch non-local objects by pretending to be a partial clone
callGit(['--git-dir', repo, 'config', 'remote.origin.promisor', 'true'])
callGit(['--git-dir', repo, 'config', 'remote.origin.partialCloneFilter', 'blob:none'])
const authorizationHeader = await getPushAuthorizationHeader(context, setSecret, appId, privateKey, owner, repo)
callGit(['--git-dir', repo,
'-c', `http.extraHeader=${authorizationHeader}`,
'push', `https://github.com/${owner}/${repo}`, pushRefSpec
])
context.log(`Done pushing ref ${pushRefSpec} to ${owner}/${repo}`)
}
module.exports = {
mergeBundle,
callGit,
getWorkflowRunArtifact,
pushRepositoryUpdate,
pushGitBranch
}