# This workflow is triggered on issue being opened, closed, reopened.
# The CompFuzzCI fuzzer needs to keep track of active issues in the repository to ensure that the fuzzer does not report the same issue multiple times.
# For open and reopen events: It dispatches workflow on CompFuzzCI repository, where the issue is added to the database.
# For close event: It dispatches workflow on CompFuzzCI repository, where the issue is removed from the database.

name: Issue Update for Fuzzer
on:
  issues:
    branches:
      - master
    types: [opened, closed, reopened]

jobs:
  UpdateIssueOpened:
    if: github.event.action == 'opened' || github.event.action == 'reopened'
    runs-on: ubuntu-latest
    steps:
      - name: Trigger CompFuzzCI
        uses: actions/github-script@v7
        with:
            github-token: ${{ secrets.COMPFUZZCI_PAT }}
            script: |
              await github.rest.actions.createWorkflowDispatch({
                  owner: 'CompFuzzCI',
                  repo: 'DafnyCompilerFuzzer',
                  workflow_id: 'update_issue_open.yaml',
                  ref: 'main',
                  inputs: {
                      issue_number: '${{github.event.issue.number}}',
                      issuer: '${{github.event.issue.user.login}}',
                      commit: '${{ github.sha }}'          
                  }
              })
  UpdateIssueClosed:
    if: github.event.action == 'closed'
    runs-on: ubuntu-latest
    steps:
      - name: Trigger CompFuzzCI
        uses: actions/github-script@v7
        with:
            github-token: ${{ secrets.COMPFUZZCI_PAT }}
            script: |
              await github.rest.actions.createWorkflowDispatch({
                  owner: 'CompFuzzCI',
                  repo: 'DafnyCompilerFuzzer',
                  workflow_id: 'update_issue_close.yaml',
                  ref: 'main',
                  inputs: {
                      issue_number: '${{github.event.issue.number}}'
                  }
              })