mirror of
https://github.com/saltstack/salt.git
synced 2025-04-10 06:41:40 +00:00
Add tools command to sync known OS'es with GH test labels
This commit is contained in:
parent
7cc7595167
commit
8d51ca91f2
5 changed files with 199 additions and 8 deletions
12
.github/workflows/ci.yml
vendored
12
.github/workflows/ci.yml
vendored
|
@ -957,7 +957,7 @@ jobs:
|
|||
|
||||
macos-12-pkg-tests:
|
||||
name: macOS 12 Package Test
|
||||
if: ${{ fromJSON(needs.prepare-workflow.outputs.jobs)['test-pkg'] && fromJSON(needs.prepare-workflow.outputs.runners)['github-hosted'] && contains(fromJSON('["macos-12", "all"]'), needs.prepare-workflow.outputs.os-labels) }}
|
||||
if: ${{ fromJSON(needs.prepare-workflow.outputs.jobs)['test-pkg'] && fromJSON(needs.prepare-workflow.outputs.runners)['github-hosted'] && contains(fromJSON('["macos-12", "macos-12", "all"]'), needs.prepare-workflow.outputs.os-labels) }}
|
||||
needs:
|
||||
- prepare-workflow
|
||||
- build-pkgs-onedir
|
||||
|
@ -978,7 +978,7 @@ jobs:
|
|||
|
||||
macos-13-pkg-tests:
|
||||
name: macOS 13 Package Test
|
||||
if: ${{ fromJSON(needs.prepare-workflow.outputs.jobs)['test-pkg'] && fromJSON(needs.prepare-workflow.outputs.runners)['github-hosted'] && contains(fromJSON('["macos-13", "all"]'), needs.prepare-workflow.outputs.os-labels) }}
|
||||
if: ${{ fromJSON(needs.prepare-workflow.outputs.jobs)['test-pkg'] && fromJSON(needs.prepare-workflow.outputs.runners)['github-hosted'] && contains(fromJSON('["macos-13", "macos-13", "all"]'), needs.prepare-workflow.outputs.os-labels) }}
|
||||
needs:
|
||||
- prepare-workflow
|
||||
- build-pkgs-onedir
|
||||
|
@ -999,7 +999,7 @@ jobs:
|
|||
|
||||
macos-13-xlarge-pkg-tests:
|
||||
name: macOS 13 Arm64 Package Test
|
||||
if: ${{ fromJSON(needs.prepare-workflow.outputs.jobs)['test-pkg'] && fromJSON(needs.prepare-workflow.outputs.runners)['github-hosted'] && contains(fromJSON('["macos-13-xlarge", "all"]'), needs.prepare-workflow.outputs.os-labels) }}
|
||||
if: ${{ fromJSON(needs.prepare-workflow.outputs.jobs)['test-pkg'] && fromJSON(needs.prepare-workflow.outputs.runners)['github-hosted'] && contains(fromJSON('["macos-13-xlarge", "macos-13-arm64", "all"]'), needs.prepare-workflow.outputs.os-labels) }}
|
||||
needs:
|
||||
- prepare-workflow
|
||||
- build-pkgs-onedir
|
||||
|
@ -1209,7 +1209,7 @@ jobs:
|
|||
|
||||
macos-12:
|
||||
name: macOS 12 Test
|
||||
if: ${{ fromJSON(needs.prepare-workflow.outputs.jobs)['test'] && fromJSON(needs.prepare-workflow.outputs.runners)['github-hosted'] && contains(fromJSON('["macos-12", "all"]'), needs.prepare-workflow.outputs.os-labels) }}
|
||||
if: ${{ fromJSON(needs.prepare-workflow.outputs.jobs)['test'] && fromJSON(needs.prepare-workflow.outputs.runners)['github-hosted'] && contains(fromJSON('["macos-12", "macos-12", "all"]'), needs.prepare-workflow.outputs.os-labels) }}
|
||||
needs:
|
||||
- prepare-workflow
|
||||
- build-ci-deps
|
||||
|
@ -1230,7 +1230,7 @@ jobs:
|
|||
|
||||
macos-13:
|
||||
name: macOS 13 Test
|
||||
if: ${{ fromJSON(needs.prepare-workflow.outputs.jobs)['test'] && fromJSON(needs.prepare-workflow.outputs.runners)['github-hosted'] && contains(fromJSON('["macos-13", "all"]'), needs.prepare-workflow.outputs.os-labels) }}
|
||||
if: ${{ fromJSON(needs.prepare-workflow.outputs.jobs)['test'] && fromJSON(needs.prepare-workflow.outputs.runners)['github-hosted'] && contains(fromJSON('["macos-13", "macos-13", "all"]'), needs.prepare-workflow.outputs.os-labels) }}
|
||||
needs:
|
||||
- prepare-workflow
|
||||
- build-ci-deps
|
||||
|
@ -1251,7 +1251,7 @@ jobs:
|
|||
|
||||
macos-13-xlarge:
|
||||
name: macOS 13 Arm64 Test
|
||||
if: ${{ fromJSON(needs.prepare-workflow.outputs.jobs)['test'] && fromJSON(needs.prepare-workflow.outputs.runners)['github-hosted'] && contains(fromJSON('["macos-13-xlarge", "all"]'), needs.prepare-workflow.outputs.os-labels) }}
|
||||
if: ${{ fromJSON(needs.prepare-workflow.outputs.jobs)['test'] && fromJSON(needs.prepare-workflow.outputs.runners)['github-hosted'] && contains(fromJSON('["macos-13-xlarge", "macos-13-arm64", "all"]'), needs.prepare-workflow.outputs.os-labels) }}
|
||||
needs:
|
||||
- prepare-workflow
|
||||
- build-ci-deps
|
||||
|
|
|
@ -43,7 +43,7 @@
|
|||
<%- if workflow_slug != "ci" or slug in mandatory_os %>
|
||||
if: ${{ fromJSON(needs.prepare-workflow.outputs.jobs)['test-pkg'] && fromJSON(needs.prepare-workflow.outputs.runners)['github-hosted'] }}
|
||||
<%- else %>
|
||||
if: ${{ fromJSON(needs.prepare-workflow.outputs.jobs)['test-pkg'] && fromJSON(needs.prepare-workflow.outputs.runners)['github-hosted'] && contains(fromJSON('["<{ slug }>", "all"]'), needs.prepare-workflow.outputs.os-labels) }}
|
||||
if: ${{ fromJSON(needs.prepare-workflow.outputs.jobs)['test-pkg'] && fromJSON(needs.prepare-workflow.outputs.runners)['github-hosted'] && contains(fromJSON('["<{ slug }>", "<{ slug.replace('xlarge', 'arm64') }>", "all"]'), needs.prepare-workflow.outputs.os-labels) }}
|
||||
<%- endif %>
|
||||
needs:
|
||||
- prepare-workflow
|
||||
|
|
|
@ -43,7 +43,7 @@
|
|||
<%- if workflow_slug != "ci" or slug in mandatory_os %>
|
||||
if: ${{ fromJSON(needs.prepare-workflow.outputs.jobs)['test'] && fromJSON(needs.prepare-workflow.outputs.runners)['github-hosted'] }}
|
||||
<%- else %>
|
||||
if: ${{ fromJSON(needs.prepare-workflow.outputs.jobs)['test'] && fromJSON(needs.prepare-workflow.outputs.runners)['github-hosted'] && contains(fromJSON('["<{ slug }>", "all"]'), needs.prepare-workflow.outputs.os-labels) }}
|
||||
if: ${{ fromJSON(needs.prepare-workflow.outputs.jobs)['test'] && fromJSON(needs.prepare-workflow.outputs.runners)['github-hosted'] && contains(fromJSON('["<{ slug }>", "<{ slug.replace('xlarge', 'arm64') }>", "all"]'), needs.prepare-workflow.outputs.os-labels) }}
|
||||
<%- endif %>
|
||||
needs:
|
||||
- prepare-workflow
|
||||
|
|
|
@ -32,6 +32,7 @@ ptscripts.set_default_config(DEFAULT_REQS_CONFIG)
|
|||
ptscripts.register_tools_module("tools.changelog")
|
||||
ptscripts.register_tools_module("tools.ci")
|
||||
ptscripts.register_tools_module("tools.docs")
|
||||
ptscripts.register_tools_module("tools.gh")
|
||||
ptscripts.register_tools_module("tools.pkg")
|
||||
ptscripts.register_tools_module("tools.pkg.repo")
|
||||
ptscripts.register_tools_module("tools.pkg.build")
|
||||
|
|
190
tools/gh.py
Normal file
190
tools/gh.py
Normal file
|
@ -0,0 +1,190 @@
|
|||
"""
|
||||
These commands are used to interact and make changes to GitHub.
|
||||
"""
|
||||
|
||||
# pylint: disable=resource-leakage,broad-except,3rd-party-module-not-gated
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from ptscripts import Context, command_group
|
||||
|
||||
import tools.utils
|
||||
import tools.utils.gh
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
WORKFLOWS = tools.utils.REPO_ROOT / ".github" / "workflows"
|
||||
TEMPLATES = WORKFLOWS / "templates"
|
||||
|
||||
# Define the command group
|
||||
cgroup = command_group(
|
||||
name="gh",
|
||||
help="GitHub Related Commands",
|
||||
description=__doc__,
|
||||
)
|
||||
|
||||
|
||||
@cgroup.command(
|
||||
name="sync-os-labels",
|
||||
arguments={
|
||||
"repository": {
|
||||
"help": "Github repository.",
|
||||
},
|
||||
},
|
||||
)
|
||||
def sync_os_labels(
|
||||
ctx: Context, repository: str = "saltstack/salt", color: str = "C2E0C6"
|
||||
):
|
||||
"""
|
||||
Synchronize the GitHub labels to the OS known to be tested.
|
||||
"""
|
||||
description_prefix = "Run Tests Against"
|
||||
known_os = {
|
||||
"test:os:all": {
|
||||
"name": "test:os:all",
|
||||
"color": color,
|
||||
"description": f"{description_prefix} ALL OS'es",
|
||||
},
|
||||
"test:os:macos-12": {
|
||||
"name": "test:os:macos-12",
|
||||
"color": color,
|
||||
"description": f"{description_prefix} MacOS 12",
|
||||
},
|
||||
"test:os:macos-13": {
|
||||
"name": "test:os:macos-13",
|
||||
"color": color,
|
||||
"description": f"{description_prefix} MacOS 13",
|
||||
},
|
||||
"test:os:macos-13-arm64": {
|
||||
"name": "test:os:macos-13-arm64",
|
||||
"color": color,
|
||||
"description": f"{description_prefix} MacOS 13 Arm64",
|
||||
},
|
||||
}
|
||||
for slug, details in tools.utils.get_golden_images().items():
|
||||
name = f"test:os:{slug}"
|
||||
ami_description = (
|
||||
details["ami_description"]
|
||||
.replace("CI Image of ", "")
|
||||
.replace("arm64", "Arm64")
|
||||
)
|
||||
known_os[name] = {
|
||||
"name": name,
|
||||
"color": color,
|
||||
"description": f"{description_prefix} {ami_description}",
|
||||
}
|
||||
|
||||
ctx.info(known_os)
|
||||
|
||||
github_token = tools.utils.gh.get_github_token(ctx)
|
||||
if github_token is None:
|
||||
ctx.error("Querying labels requires being authenticated to GitHub.")
|
||||
ctx.info(
|
||||
"Either set 'GITHUB_TOKEN' to a valid token, or configure the 'gh' tool such that "
|
||||
"'gh auth token' returns a token."
|
||||
)
|
||||
ctx.exit(1)
|
||||
|
||||
existing_labels = set()
|
||||
labels_to_update = []
|
||||
labels_to_delete = set()
|
||||
shared_context = tools.utils.get_cicd_shared_context()
|
||||
for slug in shared_context["mandatory_os_slugs"]:
|
||||
label = f"test:os:{slug}"
|
||||
labels_to_delete.add(label)
|
||||
|
||||
headers = {
|
||||
"Accept": "application/vnd.github+json",
|
||||
"Authorization": f"Bearer {github_token}",
|
||||
"X-GitHub-Api-Version": "2022-11-28",
|
||||
}
|
||||
with ctx.web as web:
|
||||
web.headers.update(headers)
|
||||
page = 0
|
||||
while True:
|
||||
page += 1
|
||||
params = {
|
||||
"per_page": 100,
|
||||
"page": page,
|
||||
}
|
||||
ret = web.get(
|
||||
f"https://api.github.com/repos/{repository}/labels",
|
||||
params=params,
|
||||
)
|
||||
if ret.status_code != 200:
|
||||
ctx.error(
|
||||
f"Failed to get the labels for repository {repository!r}: {ret.reason}"
|
||||
)
|
||||
ctx.exit(1)
|
||||
data = ret.json()
|
||||
if not data:
|
||||
break
|
||||
for details in data:
|
||||
label = details["name"]
|
||||
if not label.startswith("test:os:"):
|
||||
continue
|
||||
|
||||
existing_labels.add(label)
|
||||
|
||||
if label not in known_os:
|
||||
labels_to_delete.add(details["name"])
|
||||
continue
|
||||
|
||||
if label in known_os:
|
||||
update_details = known_os.pop(label)
|
||||
if label in labels_to_delete:
|
||||
continue
|
||||
for key, value in update_details.items():
|
||||
if details[key] != value:
|
||||
labels_to_update.append(update_details)
|
||||
break
|
||||
continue
|
||||
|
||||
for slug in shared_context["mandatory_os_slugs"]:
|
||||
label = f"test:os:{slug}"
|
||||
if label in known_os:
|
||||
labels_to_delete.add(label)
|
||||
known_os.pop(label)
|
||||
|
||||
if label in labels_to_update:
|
||||
labels_to_delete.add(label)
|
||||
known_os.pop(label)
|
||||
|
||||
for label in labels_to_delete:
|
||||
if label not in existing_labels:
|
||||
continue
|
||||
ctx.info(f"Deleting label '{label}' ...")
|
||||
ret = web.delete(
|
||||
f"https://api.github.com/repos/{repository}/labels/{label}",
|
||||
)
|
||||
if ret.status_code != 204:
|
||||
ctx.error(
|
||||
f"Failed to delete label '{label}' for repository {repository!r}: {ret.reason}"
|
||||
)
|
||||
|
||||
ctx.info("Updating OS Labels in GitHub...")
|
||||
for details in labels_to_update:
|
||||
label = details["name"]
|
||||
ctx.info(f"Updating label '{label}' ...")
|
||||
ret = web.patch(
|
||||
f"https://api.github.com/repos/{repository}/labels/{label}",
|
||||
params=details,
|
||||
)
|
||||
if ret.status_code != 200:
|
||||
ctx.error(
|
||||
f"Failed to update label '{details['name']}' for repository {repository!r}: {ret.reason}"
|
||||
)
|
||||
|
||||
for label, details in known_os.items():
|
||||
details["name"] = label
|
||||
ctx.info(f"Creating label: {details} ...")
|
||||
ret = web.post(
|
||||
f"https://api.github.com/repos/{repository}/labels",
|
||||
json=details,
|
||||
)
|
||||
if ret.status_code != 201:
|
||||
ctx.error(
|
||||
f"Failed to create label '{details['name']}' for repository {repository!r}: {ret.reason}"
|
||||
)
|
||||
print(ret.content)
|
Loading…
Add table
Reference in a new issue