Welcome to mirror list, hosted at ThFree Co, Russian Federation.

benchmark-comment.yml « workflows « .github - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: de86c3c132bb7af7a7adecc8ef63576201d1fd84 (plain)
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
---
name: Benchmark CLI - Comment

on:
  issue_comment:
    types: [created, edited]

jobs:
  comment-handler:
    name: Trigger Benchmarks

    runs-on: ubuntu-latest

    steps:
      - name: Handle Incoming Comment
        env:
          DISPATCH_REPO: "benchmarks"
          DISPATCH_OWNER: "npm"
          EVENT_NAME: ${{ github.event_name }}
          EVENT_ACTION: ${{ github.event.action }}
          OWNER: ${{ github.event.repository.owner.login }}
          REPO: ${{ github.event.repository.name }}
          ISSUE_NUMBER: ${{ github.event.issue.number }}
          COMMENT_BODY: ${{ github.event.comment.body }}
          COMMENT_ID: ${{ github.event.comment.id }}
          COMMENT_NODE_ID: ${{ github.event.comment.node_id }}
          COMMENT_ACTIONABLE: ${{ startsWith(github.event.comment.body, 'test this please ✅') }}
          AUTH_TOKEN: ${{ secrets.NPM_DEPLOY_USER_PAT }}
        run: |
          # Comment Handler

          # Creates an exit early condition if there are errors
          # Exit early if `jq` is not present
          set -e
          jq --version

          # Figure out if comment came from pull-request or issue
          IS_PR=$(curl -s https://api.github.com/repos/${OWNER}/${REPO}/issues/${ISSUE_NUMBER} | jq -cr '.pull_request.url')

          if [ "${IS_PR}" != "null" ]; then
            echo "Comment from pull/${ISSUE_NUMBER}."

            # It is a pull-request; check comment body for correct phrase
            if [ "${COMMENT_ACTIONABLE}" == "true" ]; then
              # Fetch pull-request information
              PR_DATA=$(curl -s "${IS_PR}")
              PR_OWNER=$(echo "${PR_DATA}" | jq '.head.repo.owner.login')
              PR_REPO=$(echo "${PR_DATA}" | jq '.head.repo.name')
              PR_COMMIT_SHA=$(curl -s "${IS_PR}/commits" | jq -r '.[0].sha')

              # dispatch request for benchmarks
              echo "Dispatching request..."
              curl \
                -s \
                -X POST https://api.github.com/repos/${DISPATCH_OWNER}/${DISPATCH_REPO}/dispatches \
                -H "Accept: application/vnd.github.everest-preview+json" \
                -H "Authorization: token ${AUTH_TOKEN}" \
                -d \
                '
                {
                  "event_type": "'"${EVENT_NAME}"'",
                  "client_payload": {
                    "pr_id": "'"${ISSUE_NUMBER}"'",
                    "repo": "'"${PR_REPO}"'",
                    "owner": "'"${PR_OWNER}"'",
                    "commit_sha": "'"${PR_COMMIT_SHA}"'"
                  }
                }'

              # Create reaction on comment to confirm dispatch was sent
              curl \
                -s \
                -X POST https://api.github.com/graphql \
                -H "Content-Type: application/json" \
                -H "Authorization: token ${AUTH_TOKEN}" \
                -d \
                '
                {
                  "query": "mutation($inputData:AddReactionInput!) { addReaction(input:$inputData) { reaction { content } } }",
                  "variables": {
                    "inputData": {
                      "subjectId": "'"${COMMENT_NODE_ID}"'",
                      "content": "ROCKET"
                    }
                  }
                }'
            else
              echo "Comment not actionable."
            fi
          else
            echo "Comment not from pull-request."
          fi