-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08f9a35
commit 273f6b2
Showing
20 changed files
with
386 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,64 @@ | ||
import math | ||
from fabric.api import * | ||
import requests | ||
from ci_variables import ci_env | ||
import json | ||
|
||
from typing import Dict, List, Any | ||
|
||
# Github URL related constants | ||
gh_repo_api_url = f"{ci_env['GITHUB_API_URL']}/repos/{ci_env['GITHUB_REPOSITORY']}" | ||
gh_issues_api_url = f"{gh_repo_api_url}/issues" | ||
gha_api_url = f"{gh_repo_api_url}/actions" | ||
gha_runners_api_url = f"{gha_api_url}/runners" | ||
gha_runs_api_url = f"{gha_api_url}/runs" | ||
gha_workflow_api_url = f"{gha_runs_api_url}/{ci_env['GITHUB_RUN_ID']}" | ||
|
||
def get_header(gh_token: str) -> Dict[str, str]: | ||
return {"Authorization": f"token {gh_token.strip()}", "Accept": "application/vnd.github json"} | ||
|
||
def get_runners(gh_token: str) -> List: | ||
r = requests.get(gha_runners_api_url, headers=get_header(gh_token)) | ||
if r.status_code != 200: | ||
raise Exception(f"Unable to retrieve count of GitHub Actions Runners\nFull Response Below:\n{r}") | ||
res_dict = r.json() | ||
runner_count = res_dict["total_count"] | ||
|
||
runners = [] | ||
for page_idx in range(math.ceil(runner_count / 30)): | ||
r = requests.get(gha_runners_api_url, params={"per_page" : 30, "page" : page_idx 1}, headers=get_header(gh_token)) | ||
if r.status_code != 200: | ||
raise Exception(f"Unable to retrieve (sub)list of GitHub Actions Runners\nFull Response Below\n{r}") | ||
res_dict = r.json() | ||
runners = runners res_dict["runners"] | ||
|
||
return runners | ||
|
||
def delete_runner(gh_token: str, runner: Dict[str, Any]) -> bool: | ||
r = requests.delete(f"""{gha_runners_api_url}/{runner["id"]}""", headers=get_header(gh_token)) | ||
if r.status_code != 204: | ||
print(f"""Unable to delete runner {runner["name"]} with id: {runner["id"]}\nFull Response Below\n{r}""") | ||
return False | ||
return True | ||
|
||
def deregister_offline_runners(gh_token: str) -> None: | ||
runners = get_runners(gh_token) | ||
for runner in runners: | ||
if runner["status"] == "offline": | ||
delete_runner(gh_token, runner) | ||
|
||
def deregister_runners(gh_token: str, runner_name: str) -> None: | ||
runners = get_runners(gh_token) | ||
for runner in runners: | ||
if runner_name in runner["name"]: | ||
delete_runner(gh_token, runner) | ||
|
||
def issue_post(gh_token: str, body: str) -> None: | ||
with open(ci_env['GITHUB_EVENT_PATH']) as f: | ||
event_payload = json.load(f) | ||
gh_issue_id = event_payload["number"] | ||
|
||
res = requests.post(f"{gh_issues_api_url}/{gh_issue_id}/comments", | ||
json={"body": body}, headers=get_header(gh_token)) | ||
if res.status_code != 201: | ||
raise Exception(f"HTTP POST error: {res} {res.json()}\nUnable to post GitHub PR comment.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.