name: Issue Opened

on:
  issues:
    types:
      - opened

permissions: {}

jobs:
  add-to-issue-triage:
    if: ${{ contains(github.event.issue.labels.*.name, 'bug :beetle:') }}
    runs-on: ubuntu-latest
    steps:
      - name: Generate GitHub App token
        uses: electron/github-app-auth-action@384fd19694fe7b6dcc9a684746c6976ad78228ae # v1.1.1
        id: generate-token
        with:
          creds: ${{ secrets.ISSUE_TRIAGE_GH_APP_CREDS }}
          org: electron
      - name: Add to Issue Triage
        uses: dsanders11/project-actions/add-item@438b25e007c2f4efec324497fadc6402e7cc61a6 # v1.4.0
        with:
          field: Reporter
          field-value: ${{ github.event.issue.user.login }}
          project-number: 90
          token: ${{ steps.generate-token.outputs.token }}
  set-labels:
    if: ${{ contains(github.event.issue.labels.*.name, 'bug :beetle:') }}
    runs-on: ubuntu-latest
    steps:
      - name: Generate GitHub App token
        uses: electron/github-app-auth-action@384fd19694fe7b6dcc9a684746c6976ad78228ae # v1.1.1
        id: generate-token
        with:
          creds: ${{ secrets.ISSUE_TRIAGE_GH_APP_CREDS }}
          org: electron
      - run: npm install @electron/fiddle-core@1.3.3 mdast-util-from-markdown@2.0.0 unist-util-select@5.1.0 semver@7.6.0
      - name: Add labels
        uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
        id: add-labels
        env:
          ISSUE_BODY: ${{ github.event.issue.body }}
        with:
          github-token: ${{ steps.generate-token.outputs.token }}
          script: |
            const { fromMarkdown } = await import('${{ github.workspace }}/node_modules/mdast-util-from-markdown/index.js');
            const { select } = await import('${{ github.workspace }}/node_modules/unist-util-select/index.js');
            const semver = await import('${{ github.workspace }}/node_modules/semver/index.js');

            const [ owner, repo ] = '${{ github.repository }}'.split('/');
            const issue_number = ${{ github.event.issue.number }};

            const tree = fromMarkdown(process.env.ISSUE_BODY);

            const labels = [];

            const electronVersion = select('heading:has(> text[value="Electron Version"]) + paragraph > text', tree)?.value.trim();
            if (electronVersion !== undefined) {
              // It's possible for multiple versions to be listed -
              // for now check for comma or space separated version.
              const versions = electronVersion.split(/, | /);
              for (const version of versions) {
                const major = semver.coerce(version, { loose: true })?.major;
                if (major) {
                  const versionLabel = `${major}-x-y`;
                  let labelExists = false;

                  try {
                    await github.rest.issues.getLabel({
                      owner,
                      repo,
                      name: versionLabel,
                    });
                    labelExists = true;
                  } catch {}

                  if (labelExists) {
                    // Check if it's an unsupported major
                    const { ElectronVersions } = await import('${{ github.workspace }}/node_modules/@electron/fiddle-core/dist/index.js');
                    const versions = await ElectronVersions.create(undefined, { ignoreCache: true });

                    const validVersions = [...versions.supportedMajors, ...versions.prereleaseMajors];
                    if (validVersions.includes(major)) {
                      labels.push(versionLabel);
                    }
                  }
                }
              }
              if (labels.length === 0) {
                core.setOutput('unsupportedMajor', true);
                labels.push('blocked/need-info ❌');
              }
            }

            const operatingSystems = select('heading:has(> text[value="What operating system(s) are you using?"]) + paragraph > text', tree)?.value.trim().split(', ');
            const platformLabels = new Set();
            for (const operatingSystem of (operatingSystems ?? [])) {
              switch (operatingSystem) {
                case 'Windows':
                  platformLabels.add('platform/windows');
                  break;
                case 'macOS':
                  platformLabels.add('platform/macOS');
                  break;
                case 'Ubuntu':
                case 'Other Linux':
                  platformLabels.add('platform/linux');
                  break;
              }
            }

            if (platformLabels.size === 3) {
              labels.push('platform/all');
            } else {
              labels.push(...platformLabels);
            }

            const gistUrl = select('heading:has(> text[value="Testcase Gist URL"]) + paragraph > text', tree)?.value.trim();
            if (gistUrl !== undefined && gistUrl.startsWith('https://gist.github.com/')) {
              labels.push('has-repro-gist');
            }

            if (labels.length) {
              await github.rest.issues.addLabels({
                owner,
                repo,
                issue_number,
                labels,
              });
            }
      - name: Create unsupported major comment
        if: ${{ steps.add-labels.outputs.unsupportedMajor }}
        uses: actions-cool/issues-helper@a610082f8ac0cf03e357eb8dd0d5e2ba075e017e # v3.6.0
        with:
          actions: 'create-comment'
          token: ${{ steps.generate-token.outputs.token }}
          body: |
            <!-- end-of-life -->

            Hello @${{ github.event.issue.user.login }}. Thanks for reporting this and helping to make Electron better!
            
            The version of Electron reported in this issue has reached end-of-life and is [no longer supported](https://www.electronjs.org/docs/latest/tutorial/electron-timelines#timeline). If you're still experiencing this issue on a [supported version](https://www.electronjs.org/releases/stable) of Electron, please update this issue to reflect that version of Electron.

            Now adding the https://github.com/electron/electron/labels/blocked%2Fneed-info%20%E2%9D%8C label for this reason. This issue will be closed in 10 days if the above is not addressed.
