main.ts 3.8 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)) {
sinat_25235033's avatar
sinat_25235033 已提交
25
      core.info('Detect 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
    if (bot_login_name === null || bot_login_name === undefined || bot_login_name === '') {
35 36
      octokit = github.getOctokit(myToken)
      const botInfo = await octokit.request('GET /user')
sinat_25235033's avatar
sinat_25235033 已提交
37
      core.info(JSON.stringify(botInfo))
38
      bot_login_name = botInfo.data.login
sinat_25235033's avatar
sinat_25235033 已提交
39
      core.info(`bot_login_name2: ${bot_login_name}`)
40
    }
sinat_25235033's avatar
sinat_25235033 已提交
41
    if (bot_login_name === issue_user) {
sinat_25235033's avatar
sinat_25235033 已提交
42
      core.info(`The issue comment user is bot ${bot_login_name} himself, ignore return.`)
43 44 45 46 47
      return
    }
    

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

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

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

sinat_25235033's avatar
sinat_25235033 已提交
71 72 73
function detectIsEnglish(body: string): boolean | true {
  const lngDetector = new LanguageDetect()
  const detectResult = lngDetector.detect(body, 1)
sinat_25235033's avatar
sinat_25235033 已提交
74
  if (detectResult === undefined || detectResult === null || detectResult.length !== 1) {
75 76
    core.warning(`Can not detect the comment body: ${body}`)
    return false
sinat_25235033's avatar
sinat_25235033 已提交
77 78
  }
  core.info(`Detect comment body language result is: ${detectResult[0][0]}, similar sorce: ${detectResult[0][1]}`)
sinat_25235033's avatar
sinat_25235033 已提交
79 80 81 82 83 84 85
  return detectResult.length === 1 && detectResult[0][0] === 'english'
}

async function translateCommentBody(body: string): Promise<string> {
  let result = ''
  await translate(body, {to: 'en'})
    .then(res => {
sinat_25235033's avatar
sinat_25235033 已提交
86 87 88 89 90 91 92
      result = `
      > Bot detected the comment body's language is not English, translate it automatically. For the convenience of others, please use English next time.   

      ----    

      ${res.text}  
      `
sinat_25235033's avatar
sinat_25235033 已提交
93 94 95 96 97 98 99 100
    })
    .catch(err => {
      core.error(err)
      core.setFailed(err.message)
    })
  return result
}

101
async function createComment(issueId: number, body: string, octokit: any): Promise<void> {
sinat_25235033's avatar
sinat_25235033 已提交
102
  const {owner, repo} = github.context.repo
sinat_25235033's avatar
sinat_25235033 已提交
103 104 105 106 107 108 109 110
  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 已提交
111 112
}

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