Skip to content

Commit

Permalink
Switch to using tools and re-use the event payload available instea…
Browse files Browse the repository at this point in the history
…d of querying again

Signed-off-by: Pedro Algarvio <palgarvio@vmware.com>
  • Loading branch information
s0undt3ch committed Jun 26, 2023
1 parent 617f9e9 commit 1315b59
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 234 deletions.
23 changes: 0 additions & 23 deletions .github/actions/get-pull-labels/action.yml

This file was deleted.

46 changes: 0 additions & 46 deletions .github/actions/get-pull-number/action.yml

This file was deleted.

30 changes: 0 additions & 30 deletions .github/actions/get-pull-request/action.yml

This file was deleted.

31 changes: 4 additions & 27 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
31 changes: 4 additions & 27 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
31 changes: 4 additions & 27 deletions .github/workflows/scheduled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
31 changes: 4 additions & 27 deletions .github/workflows/staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
31 changes: 4 additions & 27 deletions .github/workflows/templates/layout.yml.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>

Expand Down
76 changes: 76 additions & 0 deletions tools/ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 1315b59

Please sign in to comment.