main.ts 3.4 KB
Newer Older
sinat_25235033's avatar
sinat_25235033 已提交
1
import * as core from '@actions/core'
sinat_25235033's avatar
sinat_25235033 已提交
2 3
import * as github from '@actions/github'
import * as webhook from '@octokit/webhooks'
sinat_25235033's avatar
sinat_25235033 已提交
4 5
import LanguageDetect from 'languagedetect'
import translate from '@k3rn31p4nic/google-translate-api'
sinat_25235033's avatar
sinat_25235033 已提交
6 7 8

async function run(): Promise<void> {
  try {
sinat_25235033's avatar
sinat_25235033 已提交
9
    if (
sinat_25235033's avatar
sinat_25235033 已提交
10 11
      github.context.eventName !== 'issue_comment' ||
      github.context.payload.action !== 'created'
sinat_25235033's avatar
sinat_25235033 已提交
12
    ) {
sinat_25235033's avatar
sinat_25235033 已提交
13 14
      core.setFailed(
        `The status of the action must be created on issue_comment, no applicable - ${github.context.payload.action} on ${github.context.eventName}, return`
sinat_25235033's avatar
sinat_25235033 已提交
15
      )
sinat_25235033's avatar
sinat_25235033 已提交
16 17
      return
    }
sinat_25235033's avatar
sinat_25235033 已提交
18 19
    const issueCommentPayload = github.context
      .payload as webhook.EventPayloads.WebhookPayloadIssueComment
20
    const issue_number = issueCommentPayload.issue.number
sinat_25235033's avatar
sinat_25235033 已提交
21
    const issue_origin_comment_body = issueCommentPayload.comment.body
sinat_25235033's avatar
sinat_25235033 已提交
22

sinat_25235033's avatar
sinat_25235033 已提交
23 24
    // detect comment body is english
    if (detectIsEnglish(issue_origin_comment_body)) {
25
      core.info('the issue comment body is english already, ignore return.')
sinat_25235033's avatar
sinat_25235033 已提交
26 27
      return
    }
sinat_25235033's avatar
sinat_25235033 已提交
28

29 30 31 32 33
    // ignore when bot comment issue himself
    const myToken = core.getInput('BOT_GITHUB_TOKEN')
    let octokit = null;
    const issue_user = issueCommentPayload.comment.user.login
    let bot_login_name = core.getInput('BOT_LOGIN_NAME')
sinat_25235033's avatar
sinat_25235033 已提交
34
    core.info(`bot_login_name1: ${bot_login_name}`)
35 36 37
    if (bot_login_name === null) {
      octokit = github.getOctokit(myToken)
      const botInfo = await octokit.request('GET /user')
sinat_25235033's avatar
sinat_25235033 已提交
38
      core.info(JSON.stringify(botInfo))
39
      bot_login_name = botInfo.data.login
sinat_25235033's avatar
sinat_25235033 已提交
40
      core.info(`bot_login_name2: ${bot_login_name}`)
41 42 43 44 45 46 47 48
    }
    if (bot_login_name === issue_user ) {
      core.info("The issue comment user is bot self, ignore return.")
      return
    }
    

    // translate issue comment body to english
sinat_25235033's avatar
sinat_25235033 已提交
49
    const issue_translate_comment_body = await translateCommentBody(
sinat_25235033's avatar
sinat_25235033 已提交
50 51
      issue_origin_comment_body
    )
52

sinat_25235033's avatar
sinat_25235033 已提交
53 54 55 56
    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.")
57 58 59 60
      return
    }

    // create comment by bot
sinat_25235033's avatar
sinat_25235033 已提交
61
    core.info(issue_translate_comment_body)
62 63 64 65
    if (octokit === null) {
      octokit = github.getOctokit(myToken)
    }
    await createComment(issue_number, issue_translate_comment_body, octokit)
sinat_25235033's avatar
sinat_25235033 已提交
66
    core.setOutput('complete time', new Date().toTimeString())
sinat_25235033's avatar
sinat_25235033 已提交
67 68 69 70 71
  } catch (error) {
    core.setFailed(error.message)
  }
}

sinat_25235033's avatar
sinat_25235033 已提交
72 73 74
function detectIsEnglish(body: string): boolean | true {
  const lngDetector = new LanguageDetect()
  const detectResult = lngDetector.detect(body, 1)
sinat_25235033's avatar
sinat_25235033 已提交
75
  core.info(`detect comment body result is: ${detectResult[0][0]}, sorce: ${detectResult[0][1]}`)
sinat_25235033's avatar
sinat_25235033 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  return detectResult.length === 1 && detectResult[0][0] === 'english'
}

async function translateCommentBody(body: string): Promise<string> {
  let result = ''
  await translate(body, {to: 'en'})
    .then(res => {
      result = res.text
    })
    .catch(err => {
      core.error(err)
      core.setFailed(err.message)
    })
  return result
}

92
async function createComment(issueId: number, body: string, octokit: any): Promise<void> {
sinat_25235033's avatar
sinat_25235033 已提交
93
  const {owner, repo} = github.context.repo
sinat_25235033's avatar
sinat_25235033 已提交
94 95 96 97 98 99 100 101
  const issue_url = github.context.payload.issue?.html_url
  await octokit.issues.createComment({
    owner,
    repo,
    issue_number: issueId,
    body
  }) 
  core.info(`complete to push translate issue comment: ${body} in ${issue_url}.`)
sinat_25235033's avatar
sinat_25235033 已提交
102 103
}

sinat_25235033's avatar
sinat_25235033 已提交
104
run()