-
Notifications
You must be signed in to change notification settings - Fork 99
/
pullRequest.js
executable file
·104 lines (96 loc) · 2.68 KB
/
pullRequest.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
/* eslint-disable @typescript-eslint/no-var-requires */
// 把changelog和version整合成pr body
// commit and pr
// 删除changelog
const fs = require('fs-extra')
const execa = require('execa')
const { Octokit } = require('@octokit/core')
const version = require('./lerna.json').version
// 在根目录创建"temp.js", 向Git管理者获取webhooks的token, 设置 module.exports = { gitAuth: 'xxxxxxxxxxxxxxxxxxx' }
const gitAuth = require('./temp.js').gitAuth
const octokit = new Octokit({
auth: gitAuth,
})
async function exec() {
const log = fs.readFileSync('./CHANGELOG.md', 'utf8')
let body = log.split('\n## [')[0]
body = body
.replace('# [](', `## [${version}](`)
.replace(`...v)`, `...v${version})`)
try {
await execa('git', ['checkout', '-b', `release/${version}`], {
stdio: 'inherit',
})
} catch (error) {
console.error(error)
process.exit(1)
}
try {
await execa('git', ['add', '.'], { stdio: 'inherit' })
await execa(
'git',
['commit', '-m', `chore(release): publish ${version}`, '-n'],
{
stdio: 'inherit',
},
)
await execa('git', ['push', '-u', 'origin', `release/${version}`], {
stdio: 'inherit',
})
} catch (error) {
console.error(error)
process.exit(1)
}
// 发起PR
const cRes = await octokit.request('POST /repos/{owner}/{repo}/pulls', {
owner: 'AntmJS',
repo: 'vantui',
title: `chore(release): publish ${version}`,
head: `release/${version}`,
base: 'main',
body: body,
request: {
timeout: 150 * 1000,
},
})
if (cRes.status === 201) {
const mRes = await octokit.request(
'PUT /repos/{owner}/{repo}/pulls/{pull_number}/merge',
{
owner: 'AntmJS',
repo: 'vantui',
// eslint-disable-next-line camelcase
pull_number: cRes.data.number,
// eslint-disable-next-line camelcase
merge_method: 'squash',
// eslint-disable-next-line camelcase
commit_message: '',
},
)
if (mRes.status === 200) {
// 删除文件
try {
await execa('git', ['checkout', 'main'], { stdio: 'inherit' })
await execa('git', ['branch', '-D', `release/${version}`], {
stdio: 'inherit',
})
} catch (error) {
console.error(error)
process.exit(1)
}
// github 设置了merge之后自动删除分支
// try {
// await execa('git', ['push', 'origin', `:release/${version}`], {
// stdio: 'inherit',
// })
// } catch {}
} else {
console.error('merge a pr error!')
process.exit(1)
}
} else {
console.error('create a pr error!')
process.exit(1)
}
}
exec()