# Copyright 2025 Google LLC
#
# 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
#
#      https://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: Get job
description: Gets the action job ID and URL, it fails if not found.

inputs:
  job-name:
    description: The name of the job.
    default: ${{ github.job }}
  repository-owner:
    description: The owner of the repository.
    default: ${{ github.repository_owner }}
  repository-name:
    description: The name of the repository.
    default: ${{ github.event.repository.name }}
  run-id:
    description: The ID of the workflow run.
    default: ${{ github.run_id }}

outputs:
  id:
    description: The action job ID.
    value: ${{ fromJson(steps.job.outputs.result).id }}
  url:
    description: The action job URL.
    value: ${{ fromJson(steps.job.outputs.result).url }}

runs:
  using: composite
  steps:
    - uses: actions/github-script@v7
      id: job
      with:
        script: |
          async function* listJobs({owner, repo, run_id}) {
            console.log(`Listing jobs for ${run_id}`);
            for (let page = 1; page < 100; page++) {
              // https://docs.github.com/en/rest/actions/workflow-jobs?apiVersion=2022-11-28#list-jobs-for-a-workflow-run
              // https://octokit.github.io/rest.js/v21/#actions-list-jobs-for-workflow-run
              const response = await github.rest.actions.listJobsForWorkflowRun({
                owner: owner,
                repo: repo,
                run_id: run_id,
                page: page;
              });
              if (response.data.jobs.length === 0) {
                break;
              }
              for (const job of response.data.jobs) {
                console.log(`- ${job.name}`);
                yield job;
              }
            }
          }

          const jobs = listJobs({
            owner: "${{ inputs.repository-owner }}",
            repo: "${{ inputs.repository-name }}",
            run_id: "${{ inputs.run-id }}",
          });
          for await (const job of jobs) {
            if (job.name === "${{ inputs.job-name }}" || job.name.endsWith(" / ${{ inputs.job-name }}")) {
              console.log(`Job found: ${job.name}`);
              console.log(`id:  ${job.id}`);
              console.log(`url: ${job.html_url}`);
              return {id: job.id, url: job.html_url};
            }
          }
          core.setFailed(`Job not found: '${{ inputs.job-name }}'`);
