From 1315b59d0b02b02b38184a8834c07c32e9a088e2 Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Mon, 26 Jun 2023 18:21:35 +0100 Subject: [PATCH] Switch to using `tools` and re-use the event payload available instead of querying again Signed-off-by: Pedro Algarvio --- .github/actions/get-pull-labels/action.yml | 23 ------ .github/actions/get-pull-number/action.yml | 46 ------------ .github/actions/get-pull-request/action.yml | 30 -------- .github/workflows/ci.yml | 31 ++------ .github/workflows/nightly.yml | 31 ++------ .github/workflows/scheduled.yml | 31 ++------ .github/workflows/staging.yml | 31 ++------ .github/workflows/templates/layout.yml.jinja | 31 ++------ tools/ci.py | 76 ++++++++++++++++++++ 9 files changed, 96 insertions(+), 234 deletions(-) delete mode 100644 .github/actions/get-pull-labels/action.yml delete mode 100644 .github/actions/get-pull-number/action.yml delete mode 100644 .github/actions/get-pull-request/action.yml diff --git a/.github/actions/get-pull-labels/action.yml b/.github/actions/get-pull-labels/action.yml deleted file mode 100644 index 2da0a2c9dae6..000000000000 --- a/.github/actions/get-pull-labels/action.yml +++ /dev/null @@ -1,23 +0,0 @@ ---- -name: get-pull-labels -description: Get Pull Labels -inputs: - pull-request: - type: string - -outputs: - labels: - value: ${{ steps.get-pull-labels.outputs.labels }} - -runs: - using: composite - steps: - - name: Get Pull Labels - id: get-pull-labels - shell: bash - env: - PULL_REQUEST: ${{ inputs.pull-request }} - run: | - labels=$(jq -c '[.labels[].name]' <<< $PULL_REQUEST) - echo $labels - echo "labels=$labels" >> "$GITHUB_OUTPUT" diff --git a/.github/actions/get-pull-number/action.yml b/.github/actions/get-pull-number/action.yml deleted file mode 100644 index 00fd0425affa..000000000000 --- a/.github/actions/get-pull-number/action.yml +++ /dev/null @@ -1,46 +0,0 @@ - ---- -name: get-pull-number -description: Get Pull Number -inputs: - owner: - type: string - repo: - type: string - sha: - type: string - pull-number: - default: null - -outputs: - number: - value: ${{ steps.get-pull-number.outputs.number }} - -runs: - using: composite - steps: - - name: Get Pull Number - id: get-pull-number - shell: bash - env: - GITHUB_OWNER: ${{ inputs.owner }} - GITHUB_REPO: ${{ inputs.repo }} - GITHUB_SHA: ${{ inputs.sha }} - GITHUB_PULL_NUMBER: ${{ inputs.pull-number }} - run: | - if [ -z "$GITHUB_PULL_NUMBER" ] - then - echo "Searching For Pull Number" - echo $GITHUB_OWNER - echo $GITHUB_REPO - echo $GITHUB_SHA - pulls=$(gh api repos/$GITHUB_OWNER/$GITHUB_REPO/commits/$GITHUB_SHA/pulls) - echo $pulls - full_name=$GITHUB_OWNER/$GITHUB_REPO - number=$(jq -c --arg r "$full_name" '[.[] | select(.url | contains($r))][0].number' <<< $pulls ) - else - echo "Given Pull Number" - number=$GITHUB_PULL_NUMBER - fi - echo $number - echo "number=$number" >> "$GITHUB_OUTPUT" diff --git a/.github/actions/get-pull-request/action.yml b/.github/actions/get-pull-request/action.yml deleted file mode 100644 index 781aa24fe98a..000000000000 --- a/.github/actions/get-pull-request/action.yml +++ /dev/null @@ -1,30 +0,0 @@ - ---- -name: get-pull-request -description: Get Pull Request -inputs: - owner: - type: string - repo: - type: string - pull-number: - type: number - -outputs: - pull-request: - value: ${{ steps.get-pull-request.outputs.request }} - -runs: - using: composite - steps: - - name: Get Pull Request - id: get-pull-request - shell: bash - env: - GITHUB_OWNER: ${{ inputs.owner }} - GITHUB_REPO: ${{ inputs.repo }} - GITHUB_PULL_NUMBER: ${{ inputs.pull-number }} - run: | - pull=$(gh api repos/$GITHUB_OWNER/$GITHUB_REPO/pulls/$GITHUB_PULL_NUMBER) - echo $pull - echo "request=$pull" >> "$GITHUB_OUTPUT" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 11db8968e25a..882899e00571 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -139,35 +139,12 @@ jobs: salt-version: "" validate-version: true - - name: Get Pull Number - if: ${{ github.event_name == 'pull_request' }} - id: get-pull-number - uses: ./.github/actions/get-pull-number - with: - owner: ${{ github.repository_owner }} - repo: ${{ github.event.repository.name }} - sha: ${{ github.sha }} - pull-number: ${{ github.event.pull_request.number }} - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - name: Get Pull Request - if: ${{ github.event_name == 'pull_request' }} - id: get-pull-request - uses: ./.github/actions/get-pull-request - with: - owner: ${{ github.repository_owner }} - repo: ${{ github.event.repository.name }} - pull-number: ${{ steps.get-pull-number.outputs.number }} + - name: Get Pull Request Labels + id: get-pull-labels env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - name: Get Pull Labels - if: ${{ github.event_name == 'pull_request' }} - id: get-pull-labels - uses: ./.github/actions/get-pull-labels - with: - pull-request: ${{ steps.get-pull-request.outputs.pull-request }} + run: | + tools ci get-pr-labels --repository ${{ github.repository }} - name: Write Changed Files To A Local File run: diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index a5371f7878bb..0c9873530b74 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -192,35 +192,12 @@ jobs: salt-version: "" validate-version: true - - name: Get Pull Number - if: ${{ github.event_name == 'pull_request' }} - id: get-pull-number - uses: ./.github/actions/get-pull-number - with: - owner: ${{ github.repository_owner }} - repo: ${{ github.event.repository.name }} - sha: ${{ github.sha }} - pull-number: ${{ github.event.pull_request.number }} - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - name: Get Pull Request - if: ${{ github.event_name == 'pull_request' }} - id: get-pull-request - uses: ./.github/actions/get-pull-request - with: - owner: ${{ github.repository_owner }} - repo: ${{ github.event.repository.name }} - pull-number: ${{ steps.get-pull-number.outputs.number }} + - name: Get Pull Request Labels + id: get-pull-labels env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - name: Get Pull Labels - if: ${{ github.event_name == 'pull_request' }} - id: get-pull-labels - uses: ./.github/actions/get-pull-labels - with: - pull-request: ${{ steps.get-pull-request.outputs.pull-request }} + run: | + tools ci get-pr-labels --repository ${{ github.repository }} - name: Write Changed Files To A Local File run: diff --git a/.github/workflows/scheduled.yml b/.github/workflows/scheduled.yml index 2597688b0c3a..072e98752cc6 100644 --- a/.github/workflows/scheduled.yml +++ b/.github/workflows/scheduled.yml @@ -182,35 +182,12 @@ jobs: salt-version: "" validate-version: true - - name: Get Pull Number - if: ${{ github.event_name == 'pull_request' }} - id: get-pull-number - uses: ./.github/actions/get-pull-number - with: - owner: ${{ github.repository_owner }} - repo: ${{ github.event.repository.name }} - sha: ${{ github.sha }} - pull-number: ${{ github.event.pull_request.number }} - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - name: Get Pull Request - if: ${{ github.event_name == 'pull_request' }} - id: get-pull-request - uses: ./.github/actions/get-pull-request - with: - owner: ${{ github.repository_owner }} - repo: ${{ github.event.repository.name }} - pull-number: ${{ steps.get-pull-number.outputs.number }} + - name: Get Pull Request Labels + id: get-pull-labels env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - name: Get Pull Labels - if: ${{ github.event_name == 'pull_request' }} - id: get-pull-labels - uses: ./.github/actions/get-pull-labels - with: - pull-request: ${{ steps.get-pull-request.outputs.pull-request }} + run: | + tools ci get-pr-labels --repository ${{ github.repository }} - name: Write Changed Files To A Local File run: diff --git a/.github/workflows/staging.yml b/.github/workflows/staging.yml index ff5a727408e9..556fec8ef966 100644 --- a/.github/workflows/staging.yml +++ b/.github/workflows/staging.yml @@ -172,35 +172,12 @@ jobs: salt-version: "${{ inputs.salt-version }}" validate-version: true - - name: Get Pull Number - if: ${{ github.event_name == 'pull_request' }} - id: get-pull-number - uses: ./.github/actions/get-pull-number - with: - owner: ${{ github.repository_owner }} - repo: ${{ github.event.repository.name }} - sha: ${{ github.sha }} - pull-number: ${{ github.event.pull_request.number }} - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - name: Get Pull Request - if: ${{ github.event_name == 'pull_request' }} - id: get-pull-request - uses: ./.github/actions/get-pull-request - with: - owner: ${{ github.repository_owner }} - repo: ${{ github.event.repository.name }} - pull-number: ${{ steps.get-pull-number.outputs.number }} + - name: Get Pull Request Labels + id: get-pull-labels env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - name: Get Pull Labels - if: ${{ github.event_name == 'pull_request' }} - id: get-pull-labels - uses: ./.github/actions/get-pull-labels - with: - pull-request: ${{ steps.get-pull-request.outputs.pull-request }} + run: | + tools ci get-pr-labels --repository ${{ github.repository }} - name: Check Existing Releases env: diff --git a/.github/workflows/templates/layout.yml.jinja b/.github/workflows/templates/layout.yml.jinja index 855c8a786543..56b8cda21000 100644 --- a/.github/workflows/templates/layout.yml.jinja +++ b/.github/workflows/templates/layout.yml.jinja @@ -191,35 +191,12 @@ jobs: salt-version: "<{ prepare_workflow_salt_version_input }>" validate-version: true - - name: Get Pull Number - if: ${{ github.event_name == 'pull_request' }} - id: get-pull-number - uses: ./.github/actions/get-pull-number - with: - owner: ${{ github.repository_owner }} - repo: ${{ github.event.repository.name }} - sha: ${{ github.sha }} - pull-number: ${{ github.event.pull_request.number }} - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - name: Get Pull Request - if: ${{ github.event_name == 'pull_request' }} - id: get-pull-request - uses: ./.github/actions/get-pull-request - with: - owner: ${{ github.repository_owner }} - repo: ${{ github.event.repository.name }} - pull-number: ${{ steps.get-pull-number.outputs.number }} + - name: Get Pull Request Labels + id: get-pull-labels env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - name: Get Pull Labels - if: ${{ github.event_name == 'pull_request' }} - id: get-pull-labels - uses: ./.github/actions/get-pull-labels - with: - pull-request: ${{ steps.get-pull-request.outputs.pull-request }} + run: | + tools ci get-pr-labels --repository ${{ github.repository }} <%- if prepare_actual_release %> diff --git a/tools/ci.py b/tools/ci.py index ba7a7c2f8496..949c24392ddd 100644 --- a/tools/ci.py +++ b/tools/ci.py @@ -672,3 +672,79 @@ def get_releases(ctx: Context, repository: str = "saltstack/salt"): wfh.write(f"latest-release={latest}\n") wfh.write(f"releases={json.dumps(str_releases)}\n") ctx.exit(0) + + +@ci.command( + name="get-pr-labels", + arguments={ + "pr": { + "help": "Pull request number", + }, + "repository": { + "help": "Github repository. %default", + }, + }, +) +def get_pr_labels(ctx: Context, repository: str = "saltstack/salt", pr: int = None): + """ + Set the pull-request labels. + """ + gh_event_path = os.environ.get("GITHUB_EVENT_PATH") or None + if gh_event_path is None: + if pr is None: + ctx.error( + "Could not find the 'GITHUB_EVENT_PATH' variable and the " + "--pr flag was not passed. Unable to detect pull-request number." + ) + ctx.exit(1) + with ctx.web as web: + headers = { + "Accept": "application/vnd.github+json", + } + if "GITHUB_TOKEN" in os.environ: + headers["Authorization"] = f"Bearer {os.environ['GITHUB_TOKEN']}" + web.headers.update(headers) + ret = web.get(f"https://api.github.com/repos/{repository}/pulls/{pr}") + if ret.status_code != 200: + ctx.error( + f"Failed to get the #{pr} pull-request details on repository {repository!r}: {ret.reason}" + ) + ctx.exit(1) + pr_details = ret.json() + else: + if TYPE_CHECKING: + assert gh_event_path is not None + + try: + gh_event = json.loads(open(gh_event_path).read()) + except Exception as exc: + ctx.error( + f"Could not load the GH Event payload from {gh_event_path!r}:\n", exc + ) + ctx.exit(1) + + if "pull_request" not in gh_event: + ctx.error("The 'pull_request' key was not found on the event payload.") + ctx.exit(1) + + pr_details = gh_event["pull_request"] + + labels = [label["name"] for label in pr_details["labels"]] + if labels: + ctx.info(f"Labels for pull-request #{pr} on {repository}:") + for label in labels: + ctx.info(f" - {label}") + else: + ctx.info(f"No labels for pull-request #{pr} on {repository}") + + github_output = os.environ.get("GITHUB_OUTPUT") + if github_output is None: + ctx.exit(0) + + if TYPE_CHECKING: + assert github_output is not None + + ctx.info("Writing 'labels' to the github outputs file") + with open(github_output, "a", encoding="utf-8") as wfh: + wfh.write(f"runners={json.dumps(labels)}\n") + ctx.exit(0)