提交 d9ec1640 编写于 作者: sinat_25235033's avatar sinat_25235033

support issue trigger

上级 2083e22c
name: 'build-test'
on: # rebuild any PRs and main branch changes
issue_comment:
issue:
branches:
- main
- 'releases/*'
......
......@@ -6,11 +6,14 @@ The action for translating non-English issues comment content to English.
## Usage
> Use the default bot @Issues-translate-bot
#### Create a workflow from this action
````
name: 'issue-comment-translator'
on: # only support issue_comment
on: # support issue_comment issue
issue:
issue_comment:
branches:
- main
......@@ -19,19 +22,28 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: tomsun28/issues-translate-action@v1
- uses: tomsun28/issues-translate-action@v1.2
````
## Custom
## Advanced Custom
#### Create a workflow from this action
> Use your own bot by add BOT_GITHUB_TOKEN
>
1. Create a new github account as your bot
2. Use the account to generate a new token as BOT_GITHUB_TOKEN
3. Add the Secrets BOT_GITHUB_TOKEN = ${token} in your project
4. Create a workflow from this action
````
name: 'issue-comment-translator'
on: # only support issue_comment
on: # support issue_comment issue
issue:
issue_comment:
branches:
- main
......@@ -40,7 +52,7 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: tomsun28/issues-translate-action@v1
- uses: tomsun28/issues-translate-action@v1.2
with:
BOT_GITHUB_TOKEN: ${{ secrets.BOT_GITHUB_TOKEN }} # required, input your bot github token
# BOT_LOGIN_NAME: nameValue - not required, suggest not input, action will get name from BOT_GITHUB_TOKEN
......
此差异由.gitattributes 抑制。
此差异由.gitattributes 抑制。
......@@ -8,7 +8,7 @@ let franc = require('franc-min')
async function run(): Promise<void> {
try {
if (
github.context.eventName !== 'issue_comment' ||
(github.context.eventName !== 'issue_comment' && github.context.eventName != 'issue') ||
github.context.payload.action !== 'created'
) {
core.setFailed(
......@@ -16,57 +16,68 @@ async function run(): Promise<void> {
)
return
}
const issueCommentPayload = github.context
let issueNumber = null
let originBody = null
let issueUser = null
if (github.context.eventName === 'issue_comment') {
const issueCommentPayload = github.context
.payload as webhook.EventPayloads.WebhookPayloadIssueComment
const issue_number = issueCommentPayload.issue.number
const issue_origin_comment_body = issueCommentPayload.comment.body
issueNumber = issueCommentPayload.issue.number
issueUser = issueCommentPayload.comment.user.login
originBody = issueCommentPayload.comment.body
} else {
const issuePayload = github.context.payload as webhook.EventPayloads.WebhookPayloadIssues
issueNumber = issuePayload.issue.number
issueUser = issuePayload.issue.user.login
originBody = issuePayload.issue.body
}
// detect comment body is english
if (detectIsEnglish(issue_origin_comment_body)) {
if (detectIsEnglish(originBody)) {
core.info('Detect the issue comment body is english already, ignore return.')
return
}
// ignore when bot comment issue himself
let myToken = core.getInput('BOT_GITHUB_TOKEN')
let bot_login_name = core.getInput('BOT_LOGIN_NAME')
if (myToken === null || myToken === undefined || myToken === '') {
let botToken = core.getInput('BOT_GITHUB_TOKEN')
let botLoginName = core.getInput('BOT_LOGIN_NAME')
if (botToken === null || botToken === undefined || botToken === '') {
// use the default github bot token
const myTokenBase64 = 'Y2I4M2EyNjE0NThlMzIwMjA3MGJhODRlY2I5NTM0ZjBmYTEwM2ZlNg=='
myToken = Buffer.from(myTokenBase64, 'base64').toString()
bot_login_name = 'Issues-translate-bot'
const defaultBotTokenBase64 = 'Y2I4M2EyNjE0NThlMzIwMjA3MGJhODRlY2I5NTM0ZjBmYTEwM2ZlNg=='
const defaultBotLoginName = 'Issues-translate-bot'
botToken = Buffer.from(defaultBotTokenBase64, 'base64').toString()
botLoginName = defaultBotLoginName
}
let octokit = null;
const issue_user = issueCommentPayload.comment.user.login
if (bot_login_name === null || bot_login_name === undefined || bot_login_name === '') {
octokit = github.getOctokit(myToken)
if (botLoginName === null || botLoginName === undefined || botLoginName === '') {
octokit = github.getOctokit(botToken)
const botInfo = await octokit.request('GET /user')
bot_login_name = botInfo.data.login
botLoginName = botInfo.data.login
}
if (bot_login_name === issue_user) {
core.info(`The issue comment user is bot ${bot_login_name} himself, ignore return.`)
if (botLoginName === issueUser) {
core.info(`The issue comment user is bot ${botLoginName} himself, ignore return.`)
return
}
// translate issue comment body to english
const issue_translate_comment_body = await translateCommentBody(
issue_origin_comment_body, issue_user
const translateBody = await translateCommentBody(
originBody, issueUser
)
if (issue_translate_comment_body === null
|| issue_translate_comment_body === ''
|| issue_translate_comment_body === issue_origin_comment_body) {
core.warning("The issue_translate_comment_body is null or same, ignore return.")
if (translateBody === null
|| translateBody === ''
|| translateBody === originBody) {
core.warning("The translateBody is null or same, ignore return.")
return
}
// create comment by bot
if (octokit === null) {
octokit = github.getOctokit(myToken)
octokit = github.getOctokit(botToken)
}
await createComment(issue_number, issue_translate_comment_body, octokit)
await createComment(issueNumber, translateBody, octokit)
core.setOutput('complete time', new Date().toTimeString())
} catch (error) {
core.setFailed(error.message)
......@@ -85,13 +96,13 @@ function detectIsEnglish(body: string): boolean | true {
return detectResult === 'eng'
}
async function translateCommentBody(body: string, issue_user: string): Promise<string> {
async function translateCommentBody(body: string, issueUser: string): Promise<string> {
let result = ''
await translate(body, {to: 'en'})
.then(res => {
result =
`
> @${issue_user}
> @${issueUser}
> Bot detected the comment body's language is not English, translate it automatically. For the convenience of others, please use English next time👯.
----
......@@ -105,13 +116,13 @@ ${res.text}
return result
}
async function createComment(issueId: number, body: string, octokit: any): Promise<void> {
async function createComment(issueNumber: number, body: string, octokit: any): Promise<void> {
const {owner, repo} = github.context.repo
const issue_url = github.context.payload.issue?.html_url
await octokit.issues.createComment({
owner,
repo,
issue_number: issueId,
issue_number: issueNumber,
body
})
core.info(`complete to push translate issue comment: ${body} in ${issue_url} `)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册