๐Ÿ“ฆ SeleniumHQ / selenium-assistant

๐Ÿ“„ index.js ยท 100 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100/**
 * This is the main entrypoint to your Probot app
 * @param {import('probot').Application} app
 */

const getConfig = require('probot-config')
const needsTriagingLabel = 'needs-triaging'

module.exports = async app => {
  app.log('Selenium Assistant has started!')

  app.on('issues.opened', triageOpenedIssues)

  async function triageOpenedIssues (context) {
    const config = await getConfig(context, 'selenium-assistant.yml')
    if (!context.isBot && config) {
      const issueBody = context.payload.issue.body
      if (await issueIsAQuestionOrSupportRequest(issueBody, config)) {
        await greetUser(context, config)
        // Post comment indicating that this is a question or a request for support
        await postComment(context, config.closeQuestionsAndSupportRequestsComment)
        if (config.closeQuestionsAndSupportRequests) {
          // If enabled in the config, close the issue
          await closeIssue(context)
        }
      } else if (!await issueIsOneOfTheSupportedTypes(issueBody, config)) {
        await greetUser(context, config)
        // Issue does not start with any of the configured strings, therefore this is not a supported issue type
        // Post a comment indicating that this is not a supported issue type
        await postComment(context, config.closeNotSupportedIssueTypesComment)
        if (config.closeNotSupportedIssueTypes) {
          // If enabled in the config, close the issue
          await closeIssue(context)
        }
      } else {
        // The bot cannot triage it, it labels the issue
        await ensureNeedsTriagingLabelExists(context)
        return context.github.issues.addLabels({
          owner: context.issue().owner,
          repo: context.issue().repo,
          issue_number: context.issue().number,
          labels: [needsTriagingLabel]
        })
      }
    }
  }

  async function greetUser (context, config) {
    if (config.openIssueGreetingComment) {
      const comment = context.issue({ body: config.openIssueGreetingComment })
      return context.github.issues.createComment(comment)
    }
  }

  async function issueIsAQuestionOrSupportRequest (issueBody, config) {
    if (config.questionsAndSupportRequestsStrings) {
      return config.questionsAndSupportRequestsStrings.some((questionsAndSupportRequestsString) => {
        return issueBody.toLowerCase().includes(questionsAndSupportRequestsString.toLowerCase())
      })
    }
  }

  async function issueIsOneOfTheSupportedTypes (issueBody, config) {
    if (config.issueTypes) {
      return config.issueTypes.some((issueType) => {
        return issueBody.toLowerCase().startsWith(issueType.toLowerCase())
      })
    }
  }

  async function postComment (context, commentBody) {
    const comment = context.issue({ body: commentBody })
    return context.github.issues.createComment(comment)
  }

  async function closeIssue (context) {
    return context.github.issues.update({
      owner: context.issue().owner,
      repo: context.issue().repo,
      issue_number: context.issue().number,
      state: 'closed'
    })
  }

  async function ensureNeedsTriagingLabelExists (context) {
    return context.github.issues.getLabel({
      owner: context.issue().owner,
      repo: context.issue().repo,
      name: needsTriagingLabel
    }).catch(() => {
      return context.github.issues.createLabel({
        owner: context.issue().owner,
        repo: context.issue().repo,
        name: needsTriagingLabel,
        color: 'ffffff'
      })
    })
  }
}