forked from chivalryq/velad-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
256 lines (214 loc) · 7.86 KB
/
index.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* This is the entry point for your Probot App.
* @param {import('probot').Application} app - Probot's Application class.
*/
const { exec } = require('child_process')
const path = require('path')
const fs = require('fs')
const simpleGit = require('simple-git')
const os = require('os')
const util = require('util')
const { Octokit } = require('@octokit/rest')
const { createAppAuth } = require('@octokit/auth-app')
const execAsync = util.promisify(exec)
const velaRepo = 'kubevela/kubevela'
const testRepo = process.env.TEST_REPO
const testRepoBranch = process.env.TEST_REPO_BRANCH
const veladURL = 'https://github.com/kubevela/velad'
const catalogRepo = 'kubevela/catalog'
const catalogRepoMainBranch = 'master'
let VelaRepo = velaRepo
let CatalogRepo = catalogRepo
let CatalogRepoMainBranch = catalogRepoMainBranch
if (process.env.TEST_REPO !== '') {
VelaRepo = testRepo
CatalogRepo = testRepo
}
if (process.env.TEST_REPO_BRANCH !== '') {
CatalogRepoMainBranch = testRepoBranch
}
let [catalogRepoOwner, catalogRepoName] = CatalogRepo.split('/')
const privateKey = process.env.PRIVATE_KEY.replace(/\\n/g, '\n');
const appId = process.env.APP_ID
const installationId = process.env.INSTALLATION_ID
const VeladPRTitlePrefix = 'Bump kubevela version to '
const tmpParentDir = path.join(os.tmpdir(), 'velad')
if (!fs.existsSync(tmpParentDir)) {
fs.mkdirSync(tmpParentDir)
}
async function cloneRepository (repoUrl, repoPath) {
const git = simpleGit(repoPath)
await git.clone(repoUrl, repoPath)
return repoPath
}
async function upgradeVela (repoPath, tagName) {
const upgradeScript = path.join(repoPath, 'hack', 'upgrade_vela.sh')
const { stdout, stderr } = await execAsync(`${upgradeScript} ${tagName}`, { cwd: repoPath })
if (stderr) {
console.error(stderr)
}
console.log(`stdout: ${stdout}`)
}
async function upgradeVelaUX (repoPath, tagName) {
const upgradeScript = path.join(repoPath, 'hack', 'upgrade_velaux.sh')
const { stdout, stderr } = await execAsync(`${upgradeScript} ${tagName} ${tagName}`, { cwd: repoPath })
if (stderr) {
console.error(stderr)
}
console.log(`stdout: ${stdout}`)
}
async function upgradeAndPushBranch (gitRepo, repoPath, tagName, upgradeFunc, branchName, commitMessage,) {
await gitRepo.addConfig('user.name', 'velad-bot')
await gitRepo.addConfig('user.email', 'chivalry.pp@gmail.com')
await upgradeFunc(repoPath, tagName)
await gitRepo.checkoutLocalBranch(branchName)
await gitRepo.add('./*')
await gitRepo.commit(commitMessage, { '--signoff': null })
const gitToken = process.env.GITHUB_TOKEN
const remoteUrlWithToken = veladURL.replace('https://', `https://${gitToken}@`)
await gitRepo.addRemote('authenticated', remoteUrlWithToken)
await gitRepo.push(['--set-upstream', 'authenticated', branchName])
}
async function createPullRequest (octokit, prDetails) {
await octokit.pulls.create(prDetails)
}
module.exports = (app) => {
const auth = {
appId,
privateKey,
installationId,
}
const octokit = new Octokit({
auth: auth,
authStrategy: createAppAuth,
})
app.on(['release.published'], async (context) => {
if (context.payload.repository.full_name === VelaRepo) {
const tagName = context.payload.release.tag_name
const prTitle = VeladPRTitlePrefix + tagName
const prBody = 'Update kubevela/velad'
try {
const repoTmpDir = fs.mkdtempSync(path.join(tmpParentDir, 'velad-'))
const repoPath = path.join(repoTmpDir, 'velad')
if (!fs.existsSync(repoPath)) {
fs.mkdirSync(repoPath)
}
await cloneRepository(veladURL, repoPath)
app.log.info('Cloned ' + veladURL + ' to ' + repoPath)
const branchName = 'velad-bot/bump-kubevela-version-' + tagName
const gitRepo = simpleGit(repoPath)
await upgradeAndPushBranch(gitRepo, repoPath, tagName, upgradeVela,
branchName, prTitle)
app.log.info('Pushed to ' + branchName)
// Create a pull request
const pr = {
owner: 'kubevela',
repo: 'velad',
title: prTitle,
head: branchName,
base: 'main',
body: prBody,
}
await createPullRequest(octokit, pr)
app.log.info('Created PR ' + prTitle)
} catch (error) {
app.log.error('failed to create PR', error)
}
}
})
// on catalog repo if there is a new pr merged, then find the pr in velad repo and update the pr
app.on(['pull_request.closed'], async (context) => {
const { payload } = context
const { repository, pull_request } = payload
// Check if the PR was merged
if (!pull_request.merged) {
app.log.info('PR was closed without merging')
return
}
if (repository.full_name !== catalogRepo) {
app.log.info('Not in the watch repo', repository.full_name)
return
}
// Get the list of changed files in the merged PR
const { data: files } = await octokit.pulls.listFiles({
owner: repository.owner.login,
repo: repository.name,
pull_number: pull_request.number,
})
filenames = files.map((file) => file.filename)
// Check if the PR contains a file named 'metadata.yaml'
app.log.info('PR merged:', pull_request.number)
app.log.info('Changed files:', filenames)
if (!filenames.includes('addons/velaux/metadata.yaml')) {
app.log.info('VelaUX metadata.yaml not modified, skip')
}
// Get the metadata.yaml file from the catalog repo
// This indicates that PR is merged to master branch
const { data: metadata } = await octokit.repos.getContent({
owner: catalogRepoOwner,
repo: catalogRepoName,
path: 'addons/velaux/metadata.yaml',
ref: catalogRepoMainBranch,
mediaType: {
format: 'raw',
}
})
// Get version like v1.7.6
const newVersion = /version: (\S+)/.exec(metadata)[1]
app.log.info('Got VelaUX addon current version in catalog:', newVersion)
// Get the PR in VelaD repo. Will try to get the latest one, update the VELAUX_VERSION in Makefile
const { data: veladPRs }
= await octokit.pulls.list({
owner: 'kubevela',
repo: 'velad',
state: 'open',
head: 'velad-bot',
base: 'main',
sort: 'updated',
direction: 'desc',
})
// Get the latest PR with title "Bump kubevela version to"
const veladPR = veladPRs.find((pr) => pr.title.startsWith(VeladPRTitlePrefix))
if (!veladPR) {
app.log.info('No VELAD PR found')
return
}
const { number: prNumber, title: prTitle, body: prBody, head: prHead } = veladPR
app.log.info('Got VELAD PR:', prNumber)
// Clone this PR branch to local
const repoTmpDir = fs.mkdtempSync(path.join(tmpParentDir, 'velad-'))
const repoPath = path.join(repoTmpDir, 'velad')
if (!fs.existsSync(repoPath)) {
fs.mkdirSync(repoPath)
}
// Clone the repository and set up the local repo
await cloneRepository(veladURL, repoPath)
app.log.info('Cloned ' + veladURL + ' to ' + repoPath)
const gitRepo = simpleGit(repoPath)
// Fetch the PR's branch and check it out
const prRef = `pull/${prNumber}/head`
await gitRepo.fetch('origin', prRef)
await gitRepo.checkout(prHead.ref)
const branchName = 'velad-bot/bump-velaux-version-' + newVersion
const commitMessage = 'Bump VelaUX version to ' + newVersion
await upgradeAndPushBranch(gitRepo, repoPath, newVersion, upgradeVelaUX, branchName, commitMessage)
// Create a new PR with the new branch
const { data: newPR } = await octokit.pulls.create({
owner: 'kubevela',
repo: 'velad',
title: prTitle,
head: branchName,
base: 'main',
body: prBody,
})
app.log.info('Created new VelaD PR with new branch', newPR)
// Close the old PR
await octokit.pulls.update({
owner: 'kubevela',
repo: 'velad',
pull_number: prNumber,
state: 'closed',
})
app.log.info('Closed old VelaD PR')
})
}