Skip to content

Commit b72e2b1

Browse files
committed
feat: 关闭议题时同时删除拉取请求对应的分支
1 parent 6ccb2fe commit b72e2b1

File tree

5 files changed

+50
-31
lines changed

5 files changed

+50
-31
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ jobs:
2222
with:
2323
token: ${{ secrets.GITHUB_TOKEN }}
2424
base: main
25+
path: docs/.vuepress/public/plugins.json

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ inputs:
88
base:
99
required: true
1010
description: 'The pull request base branch'
11+
path:
12+
required: true
13+
description: 'Path for plugins.json'
1114
runs:
1215
using: 'node12'
1316
main: 'dist/index.js'

dist/index.js

Lines changed: 23 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ function extractIssueNumberFromRef(ref: string): number | undefined {
6161
/**更新 plugins.json
6262
* 并提交到 Git
6363
*/
64-
async function updatePluginsFileAndCommit(
65-
pluginInfo: PluginInfo
64+
async function updatePluginsFileAndCommitPush(
65+
pluginInfo: PluginInfo,
66+
branchName: string
6667
): Promise<void> {
6768
if (process.env.GITHUB_WORKSPACE) {
68-
const pluginJsonFilePath = `${process.env.GITHUB_WORKSPACE}/docs/.vuepress/public/plugins.json`
69+
const path: string = core.getInput('path', {required: true})
70+
const pluginJsonFilePath = `${process.env.GITHUB_WORKSPACE}/${path}`
6971
// 写入新数据
7072
fs.readFile(pluginJsonFilePath, 'utf8', (err, data) => {
7173
if (err) {
@@ -88,6 +90,7 @@ async function updatePluginsFileAndCommit(
8890
await exec.exec('git', ['config', '--global', 'user.email', useremail])
8991
await exec.exec('git', ['add', '-A'])
9092
await exec.exec('git', ['commit', '-m', commitMessage])
93+
await exec.exec('git', ['push', 'origin', branchName, '-f'])
9194
}
9295
}
9396

@@ -152,11 +155,11 @@ async function createPullRequest(
152155
issue_number: pr.data.number,
153156
labels: ['Plugin']
154157
})
155-
} catch (e) {
156-
if (e.message.includes(`A pull request already exists for`)) {
157-
core.info('拉取请求已经创建,请查看')
158+
} catch (error) {
159+
if (error.message.includes(`A pull request already exists for`)) {
160+
core.info('该分支的拉取请求已创建,请前往查看')
158161
} else {
159-
throw e
162+
throw error
160163
}
161164
}
162165
}
@@ -187,8 +190,6 @@ async function rebaseAllOpenPullRequests(
187190
await exec.exec('git', ['checkout', '-b', pull.head.ref])
188191
// 重置之前的提交
189192
await exec.exec('git', ['reset', '--hard', base])
190-
// 合并修改
191-
await exec.exec('git', ['merge', base])
192193
const issue_number = extractIssueNumberFromRef(pull.head.ref)
193194
if (issue_number) {
194195
core.info(`正在处理 ${pull.title}`)
@@ -200,9 +201,10 @@ async function rebaseAllOpenPullRequests(
200201
issue.data.body,
201202
issue.data.user.login
202203
)
203-
await updatePluginsFileAndCommit(pluginInfo)
204-
await exec.exec('git', ['push', 'origin', pull.head.ref, '-f'])
204+
await updatePluginsFileAndCommitPush(pluginInfo, pull.head.ref)
205205
core.info(`拉取请求更新完毕`)
206+
} else {
207+
core.setFailed(`无法获取 ${pull.title} 对应的议题`)
206208
}
207209
}
208210
}
@@ -239,8 +241,12 @@ async function run(): Promise<void> {
239241
const ref: string = github.context.payload.pull_request?.head.ref
240242
const relatedIssueNumber = extractIssueNumberFromRef(ref)
241243
if (relatedIssueNumber) {
242-
closeIssue(relatedIssueNumber, octokit)
244+
await closeIssue(relatedIssueNumber, octokit)
245+
core.info(`议题 #${relatedIssueNumber} 已关闭`)
246+
await exec.exec('git', ['push', 'origin', '--delete', ref])
243247
}
248+
} else {
249+
core.info('不是拉取请求关闭事件,已跳过')
244250
}
245251
return
246252
}
@@ -249,8 +255,11 @@ async function run(): Promise<void> {
249255
if (github.context.eventName === 'push') {
250256
const commitMessage: string = github.context.payload.head_commit.message
251257
if (commitMessage.includes(':beers: publish')) {
258+
core.info('发现提交为插件发布,准备更新拉取请求的提交')
252259
const pullRequests = await getAllPluginPullRequest(octokit)
253260
rebaseAllOpenPullRequests(pullRequests, base, octokit)
261+
} else {
262+
core.info('该提交不是插件发布,已跳过')
254263
}
255264
return
256265
}
@@ -282,10 +291,7 @@ async function run(): Promise<void> {
282291

283292
// 更新 plugins.json 并提交更改
284293
const pluginInfo = extractPluginInfo(issueBody, username)
285-
await updatePluginsFileAndCommit(pluginInfo)
286-
287-
// 提交更改到远程分支
288-
await exec.exec('git', ['push', 'origin', branchName, '-f'])
294+
await updatePluginsFileAndCommitPush(pluginInfo, branchName)
289295

290296
// 提交 Pull Request
291297
// 标题里要注明 issue 编号

0 commit comments

Comments
 (0)