# ------------------------------------------------------------
# Copyright 2021 The Dapr Authors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#     http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------------------------------------

name: dapr-bot

on:
  issue_comment: {types: created}

jobs:
  daprbot:
    name: bot-processor
    runs-on: ubuntu-latest
    steps:
      - name: Comment analyzer
        uses: actions/github-script@v1
        with:
          github-token: ${{secrets.DAPR_BOT_TOKEN}}
          script: |
            // list of owner who can control dapr-bot workflow
            // TODO: Read owners from OWNERS file.
            const owners = [
              "yaron2",
              "berndverst",
              "artursouza",
              "mukundansundar",
              "halspang",
              "tanvigour",
              "pkedy",
              "amuluyavarote",
              "daixiang0",
              "ItalyPaleAle",
              "jjcollinge",
              "pravinpushkar",
              "shivamkm07",
              "shubham1172",
              "skyao",
              "msfussell",
              "Taction",
              "RyanLettieri",
              "DeepanshuA",
              "yash-nisar",
              "addjuarez",
              "tmacam",
            ];
            const payload = context.payload;
            const issue = context.issue;
            const isFromPulls = !!payload.issue.pull_request;
            const commentBody = payload.comment.body;

            if (!isFromPulls && commentBody && commentBody.indexOf("/assign") == 0) {
              if (!issue.assignees || issue.assignees.length === 0) {
                await github.issues.addAssignees({
                  owner: issue.owner,
                  repo: issue.repo,
                  issue_number: issue.number,
                  assignees: [context.actor],
                })
              }

              return;
            }

            // actions above this check are enabled for everyone.
            if (owners.indexOf(context.actor) < 0) {
              return;
            }

            if (isFromPulls && commentBody) {
              if (commentBody.indexOf("/ok-to-test") == 0) {
                // Get pull request
                const pull = await github.pulls.get({
                  owner: issue.owner,
                  repo: issue.repo,
                  pull_number: issue.number
                });
                if (pull && pull.data) {
                  // Get commit id and repo from pull head
                  const testPayload = {
                    pull_head_ref: pull.data.head.sha,
                    pull_head_repo: pull.data.head.repo.full_name,
                    command: "ok-to-test",
                    issue: issue,
                  };

                  // Fire repository_dispatch event to trigger certification test
                  await github.repos.createDispatchEvent({
                    owner: issue.owner,
                    repo: issue.repo,
                    event_type: "certification-test",
                    client_payload: testPayload,
                  });

                  // Fire repository_dispatch event to trigger conformance test
                  await github.repos.createDispatchEvent({
                    owner: issue.owner,
                    repo: issue.repo,
                    event_type: "conformance-test",
                    client_payload: testPayload,
                  });
                }
              }
            }
