未验证 提交 32b435be 编写于 作者: sinat_25235033's avatar sinat_25235033 提交者: GitHub

support modify issue title (#43)

上级 d7f080b9
...@@ -13,3 +13,6 @@ jobs: ...@@ -13,3 +13,6 @@ jobs:
- uses: ./ - uses: ./
# with: # with:
# BOT_GITHUB_TOKEN: ${{ secrets.BOT_GITHUB_TOKEN }} # BOT_GITHUB_TOKEN: ${{ secrets.BOT_GITHUB_TOKEN }}
with:
IS_MODIFY_TITLE: true
...@@ -9,6 +9,8 @@ inputs: ...@@ -9,6 +9,8 @@ inputs:
description: 'The issue comment bot GITHUB_TOKEN.' description: 'The issue comment bot GITHUB_TOKEN.'
BOT_LOGIN_NAME: BOT_LOGIN_NAME:
description: 'The issue comment bot github login name.' description: 'The issue comment bot github login name.'
IS_MODIFY_TITLE:
description: 'Is need modify issue title, true or false, default false.'
runs: runs:
using: 'node12' using: 'node12'
main: 'dist/index.js' main: 'dist/index.js'
此差异由.gitattributes 抑制。
此差异由.gitattributes 抑制。
...@@ -17,28 +17,41 @@ async function run(): Promise<void> { ...@@ -17,28 +17,41 @@ async function run(): Promise<void> {
return return
} }
let issueNumber = null let issueNumber = null
let originBody = null let originComment = null
let originTitle = null
let issueUser = null let issueUser = null
let botNote = "Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿"
let isModifyTitle = core.getInput('IS_MODIFY_TITLE')
if (github.context.eventName === 'issue_comment') { if (github.context.eventName === 'issue_comment') {
const issueCommentPayload = github.context const issueCommentPayload = github.context
.payload as webhook.EventPayloads.WebhookPayloadIssueComment .payload as webhook.EventPayloads.WebhookPayloadIssueComment
issueNumber = issueCommentPayload.issue.number issueNumber = issueCommentPayload.issue.number
issueUser = issueCommentPayload.comment.user.login issueUser = issueCommentPayload.comment.user.login
originBody = issueCommentPayload.comment.body originComment = issueCommentPayload.comment.body
} else { } else {
const issuePayload = github.context.payload as webhook.EventPayloads.WebhookPayloadIssues const issuePayload = github.context.payload as webhook.EventPayloads.WebhookPayloadIssues
issueNumber = issuePayload.issue.number issueNumber = issuePayload.issue.number
issueUser = issuePayload.issue.user.login issueUser = issuePayload.issue.user.login
originBody = originComment = issuePayload.issue.body
` originTitle = issuePayload.issue.title
**Title:** ${issuePayload.issue.title}
${issuePayload.issue.body} if (isModifyTitle === 'true') {
` originComment = issuePayload.issue.body
originTitle = issuePayload.issue.title
} else {
originComment =
`
**Title:** ${issuePayload.issue.title}
${issuePayload.issue.body}
`
}
} }
// detect comment body is english let translateOrigin = originComment + '@====@' + originTitle
if (detectIsEnglish(originBody)) {
// detect issue title comment body is english
if (detectIsEnglish(translateOrigin)) {
core.info('Detect the issue comment body is english already, ignore return.') core.info('Detect the issue comment body is english already, ignore return.')
return return
} }
...@@ -67,29 +80,67 @@ ${issuePayload.issue.body} ...@@ -67,29 +80,67 @@ ${issuePayload.issue.body}
// translate issue comment body to english // translate issue comment body to english
const translateBody = await translateCommentBody( const translateTmp = await translateIssueOrigin(translateOrigin)
originBody, issueUser
)
if (translateBody === null if (translateTmp === null
|| translateBody === '' || translateTmp === ''
|| translateBody === originBody) { || translateTmp === translateOrigin) {
core.warning("The translateBody is null or same, ignore return.") core.warning("The translateBody is null or same, ignore return.")
return return
} }
let translateBody:string[] = translateTmp.split('@====@')
let translateComment = null
let translateTitle = null
if (translateBody.length == 1) {
translateComment = translateBody[0]
} else if (translateBody.length == 2) {
translateComment = translateBody[0]
translateTitle = translateBody[1]
} else {
core.setFailed(`the translateBody is ${translateTmp}`)
}
// create comment by bot // create comment by bot
if (octokit === null) { if (octokit === null) {
octokit = github.getOctokit(botToken) octokit = github.getOctokit(botToken)
} }
await createComment(issueNumber, translateBody, octokit) if (translateTitle !== null && isModifyTitle === 'false') {
translateComment =
`
> ${botNote}
----
**Title:** ${translateTitle}
${translateComment}
`
} else {
translateComment =
`
> ${botNote}
----
${translateComment}
`
}
if (isModifyTitle === 'true') {
await modifyTitle(issueNumber, translateTitle, octokit)
}
await createComment(issueNumber, translateComment, octokit)
core.setOutput('complete time', new Date().toTimeString()) core.setOutput('complete time', new Date().toTimeString())
} catch (error) { } catch (error) {
core.setFailed(error.message) core.setFailed(error.message)
} }
} }
function detectIsEnglish(body: string): boolean | true { function detectIsEnglish(body: string | null): boolean | true {
if (body === null) {
return true
}
const detectResult = franc(body) const detectResult = franc(body)
if (detectResult === 'und' if (detectResult === 'und'
|| detectResult === undefined || detectResult === undefined
...@@ -101,19 +152,13 @@ function detectIsEnglish(body: string): boolean | true { ...@@ -101,19 +152,13 @@ function detectIsEnglish(body: string): boolean | true {
return detectResult === 'eng' return detectResult === 'eng'
} }
async function translateCommentBody(body: string, issueUser: string): Promise<string> { async function translateIssueOrigin(body: string): Promise<string> {
let result = '' let result = ''
await translate(body, {to: 'en'}) await translate(body, {to: 'en'})
.then(res => { .then(res => {
if (res.text !== body) { if (res.text !== body) {
result = result = res.text
` }
> Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿
----
${res.text}
`
}
}) })
.catch(err => { .catch(err => {
core.error(err) core.error(err)
...@@ -122,7 +167,7 @@ ${res.text} ...@@ -122,7 +167,7 @@ ${res.text}
return result return result
} }
async function createComment(issueNumber: number, body: string, octokit: any): Promise<void> { async function createComment(issueNumber: number, body: string | null, octokit: any): Promise<void> {
const {owner, repo} = github.context.repo const {owner, repo} = github.context.repo
const issue_url = github.context.payload.issue?.html_url const issue_url = github.context.payload.issue?.html_url
await octokit.issues.createComment({ await octokit.issues.createComment({
...@@ -134,4 +179,16 @@ async function createComment(issueNumber: number, body: string, octokit: any): P ...@@ -134,4 +179,16 @@ async function createComment(issueNumber: number, body: string, octokit: any): P
core.info(`complete to push translate issue comment: ${body} in ${issue_url} `) core.info(`complete to push translate issue comment: ${body} in ${issue_url} `)
} }
async function modifyTitle(issueNumber: number, title: string | null, octokit: any): Promise<void> {
const {owner, repo} = github.context.repo
const issue_url = github.context.payload.issue?.html_url
await octokit.issues.update({
owner,
repo,
issue_number: issueNumber,
title
})
core.info(`complete to modify translate issue title: ${title} in ${issue_url} `)
}
run() run()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册