Skip to content

Commit

Permalink
chore(release): v0.3
Browse files Browse the repository at this point in the history
修改分支名为 publish
升级依赖
将处理事件的步骤独立成函数
  • Loading branch information
he0119 authored Jul 26, 2021
2 parents 93af2c5 + 3c98da5 commit b0854ff
Show file tree
Hide file tree
Showing 11 changed files with 1,771 additions and 1,366 deletions.
1,324 changes: 891 additions & 433 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions dist/licenses.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1,506 changes: 696 additions & 810 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@
"@actions/github": "^4.0.0"
},
"devDependencies": {
"@types/jest": "^26.0.20",
"@types/node": "^14.14.27",
"@typescript-eslint/parser": "^4.15.0",
"@types/jest": "^26.0.24",
"@types/node": "^14.17.6",
"@typescript-eslint/parser": "^4.28.4",
"@vercel/ncc": "^0.27.0",
"eslint": "^7.20.0",
"eslint-plugin-github": "^4.1.1",
"eslint-plugin-jest": "^24.1.3",
"eslint": "^7.31.0",
"eslint-plugin-github": "^4.1.5",
"eslint-plugin-jest": "^24.4.0",
"jest": "^26.6.3",
"jest-circus": "^26.6.3",
"js-yaml": "^4.0.0",
"js-yaml": "^4.1.0",
"prettier": "2.2.1",
"ts-jest": "^26.5.1",
"typescript": "^4.1.5"
"ts-jest": "^26.5.6",
"typescript": "^4.3.5"
}
}
201 changes: 113 additions & 88 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,120 @@ import {
resolveConflictPullRequests,
updateFile
} from './utils'
import {OctokitType} from './types/github'

/** 处理拉取请求 */
async function processPullRequest(
octokit: OctokitType,
base: string
): Promise<void> {
// 因为合并拉取请求只会触发 closed 事件
// 其他事件均对商店发布流程无影响
if (github.context.payload.action !== 'closed') {
core.info('事件不是关闭拉取请求,已跳过')
return
}

// 只处理支持标签的拉取请求
const issueType = checkLabel(github.context.payload.pull_request?.labels)
if (issueType) {
const ref: string = github.context.payload.pull_request?.head.ref
const relatedIssueNumber = extractIssueNumberFromRef(ref)
if (relatedIssueNumber) {
await closeIssue(octokit, relatedIssueNumber)
core.info(`议题 #${relatedIssueNumber} 已关闭`)
try {
await exec.exec('git', ['push', 'origin', '--delete', ref])
core.info('已删除对应分支')
} catch (error) {
core.info('对应分支不存在或已删除')
}
}
if (github.context.payload.pull_request?.merged) {
core.info('发布的拉取请求已合并,准备更新拉取请求的提交')
const pullRequests = await getPullRequests(octokit, issueType)
resolveConflictPullRequests(octokit, pullRequests, base)
} else {
core.info('发布的拉取请求未合并,已跳过')
}
} else {
core.info('拉取请求与插件无关,已跳过')
}
}
/** 处理提交 */
async function processPush(octokit: OctokitType, base: string): Promise<void> {
const publishType = checkCommitType(
github.context.payload.head_commit.message
)
if (publishType) {
core.info('发现提交为发布,准备更新拉取请求的提交')
const pullRequests = await getPullRequests(octokit, publishType)
resolveConflictPullRequests(octokit, pullRequests, base)
} else {
core.info('该提交不是发布,已跳过')
}
}
/** 处理议题 */
async function processIssues(
octokit: OctokitType,
base: string
): Promise<void> {
if (
github.context.payload.action &&
['opened', 'reopened', 'edited'].includes(github.context.payload.action)
) {
// 从 GitHub Context 中获取议题的相关信息
const issue_number = github.context.payload.issue?.number
const issueBody = github.context.payload.issue?.body

if (!issue_number || !issueBody) {
core.setFailed('无法获取议题的信息')
return
}

// 检查是否为指定类型的提交
const publishType = checkTitle(github.context.payload.issue?.title)
if (!publishType) {
core.info('不是商店发布议题,已跳过')
return
}

// 创建新分支
// 命名示例 publish/issue123
const branchName = `publish/issue${issue_number}`
await exec.exec('git', ['checkout', '-b', branchName])

// 插件作者信息
const username = github.context.payload.issue?.user.login

// 提取信息
const info = extractInfo(publishType, issueBody, username)

// 自动给议题添加标签
await octokit.issues.addLabels({
...github.context.repo,
issue_number,
labels: [info.type]
})

// 更新文件并提交更改
await updateFile(info)
await commitandPush(branchName, info)

// 创建拉取请求
await createPullRequest(octokit, info, issue_number, branchName, base)
} else {
core.info('事件不是议题开启,重新开启或修改,已跳过')
}
}

async function run(): Promise<void> {
try {
const base: string = core.getInput('base', {required: true})
const token: string = core.getInput('token')

if (!token) {
core.info('无法获得 Token,跳过此次操作')
return
core.setFailed('无法获得 Token,跳过此次操作')
}
// 初始化 GitHub 客户端
const octokit = github.getOctokit(token)
Expand All @@ -32,101 +137,21 @@ async function run(): Promise<void> {
core.info(`action type: ${github.context.payload.action}`)

// 处理 pull_request 事件
if (
github.context.eventName === 'pull_request' &&
github.context.payload.action === 'closed'
) {
// 只处理支持标签的拉取请求
const issueType = checkLabel(github.context.payload.pull_request?.labels)
if (issueType) {
const ref: string = github.context.payload.pull_request?.head.ref
const relatedIssueNumber = extractIssueNumberFromRef(ref)
if (relatedIssueNumber) {
await closeIssue(octokit, relatedIssueNumber)
core.info(`议题 #${relatedIssueNumber} 已关闭`)
try {
await exec.exec('git', ['push', 'origin', '--delete', ref])
core.info('已删除对应分支')
} catch (error) {
core.info('对应分支不存在或已删除')
}
}
if (github.context.payload.pull_request?.merged) {
core.info('发布的拉取请求已合并,准备更新拉取请求的提交')
const pullRequests = await getPullRequests(octokit, issueType)
resolveConflictPullRequests(octokit, pullRequests, base)
} else {
core.info('发布的拉取请求未合并,已跳过')
}
} else {
core.info('拉取请求与插件无关,已跳过')
}
if (github.context.eventName === 'pull_request') {
await processPullRequest(octokit, base)
return
}

// 处理 push 事件
if (github.context.eventName === 'push') {
const publishType = checkCommitType(
github.context.payload.head_commit.message
)
if (publishType) {
core.info('发现提交为发布,准备更新拉取请求的提交')
const pullRequests = await getPullRequests(octokit, publishType)
resolveConflictPullRequests(octokit, pullRequests, base)
} else {
core.info('该提交不是发布,已跳过')
}
await processPush(octokit, base)
return
}

// 处理 issues 事件
if (
github.context.eventName === 'issues' &&
github.context.payload.action &&
['opened', 'reopened', 'edited'].includes(github.context.payload.action)
) {
// 从 GitHub Context 中获取议题的相关信息
const issue_number = github.context.payload.issue?.number
const issueBody = github.context.payload.issue?.body

if (!issue_number || !issueBody) {
core.setFailed('无法获取议题的信息')
return
}

// 检查是否为指定类型的提交
const publishType = checkTitle(github.context.payload.issue?.title)
if (!publishType) {
core.info('不是商店发布议题,已跳过')
return
}

// 创建新分支
// 命名示例 plugin/issue123
const branchName = `plugin/issue${issue_number}`
await exec.exec('git', ['checkout', '-b', branchName])

// 插件作者信息
const username = github.context.payload.issue?.user.login

// 提取信息
const info = extractInfo(publishType, issueBody, username)

// 自动给议题添加标签
await octokit.issues.addLabels({
...github.context.repo,
issue_number,
labels: [info.type]
})

// 更新文件并提交更改
await updateFile(info)
await commitandPush(branchName, info)

// 创建拉取请求
await createPullRequest(octokit, info, issue_number, branchName, base)
} else {
core.info('事件不是议题开启,重新开启或修改,已跳过')
if (github.context.eventName === 'issues') {
await processIssues(octokit, base)
return
}
} catch (error) {
core.setFailed(error.message)
Expand Down
10 changes: 5 additions & 5 deletions src/types/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**协议所需要的信息 */
export interface AdapterInfo {
type: 'Adapter'
/**PyPI 项目名 */
id: string
/**协议 import 包名 */
id: string
/**PyPI 项目名 */
link: string
/**协议名称 */
name: string
Expand Down Expand Up @@ -39,10 +39,10 @@ export function extractInfo(body: string, author: string): AdapterInfo {
type: 'Adapter',
id,
link,
author,
desc,
name,
repo
desc,
repo,
author
}
}
throw new Error('无法匹配成功')
Expand Down
6 changes: 3 additions & 3 deletions src/types/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export function extractInfo(body: string, author: string): BotInfo {
if (desc && name && repo) {
return {
type: 'Bot',
author,
desc,
name,
repo
desc,
repo,
author
}
}
throw new Error('无法匹配成功')
Expand Down
12 changes: 12 additions & 0 deletions src/types/github.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as github from '@actions/github'
import {GetResponseDataTypeFromEndpointMethod} from '@octokit/types'

const octokit = github.getOctokit('')

export type OctokitType = typeof octokit
export type PullsListResponseDataType = GetResponseDataTypeFromEndpointMethod<
typeof octokit.pulls.list
>
export type IssuesGetResponseDataType = GetResponseDataTypeFromEndpointMethod<
typeof octokit.issues.get
>
10 changes: 5 additions & 5 deletions src/types/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**插件所需要的信息 */
export interface PluginInfo {
type: 'Plugin'
/**PyPI 项目名 */
id: string
/**插件 import 包名 */
id: string
/**PyPI 项目名 */
link: string
/**插件名称 */
name: string
Expand Down Expand Up @@ -39,10 +39,10 @@ export function extractInfo(body: string, author: string): PluginInfo {
type: 'Plugin',
id,
link,
author,
desc,
name,
repo
desc,
repo,
author
}
}
throw new Error('无法匹配成功')
Expand Down
Loading

0 comments on commit b0854ff

Please sign in to comment.