-
Notifications
You must be signed in to change notification settings - Fork 0
/
kill_actions.py
45 lines (37 loc) · 1.72 KB
/
kill_actions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import requests
import os
import argparse
# Set these variables with appropriate values
GITHUB_TOKEN = os.getenv('GITHUB_TOKEN') # Personal Access Token
# REPO_OWNER = os.getenv('GITHUB_REPOSITORY_OWNER') # Repository owner
# REPO_NAME = os.getenv('GITHUB_REPOSITORY').split('/')[1] # Repository name
def get_running_workflows(repo_owner, repo_name):
url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/actions/runs?status=in_progress"
headers = {
"Authorization": f"Bearer {GITHUB_TOKEN}",
"Accept": "application/vnd.github.v3+json",
}
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()["workflow_runs"]
def cancel_workflow(run_id, repo_owner, repo_name):
url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/actions/runs/{run_id}/cancel"
headers = {
"Authorization": f"Bearer {GITHUB_TOKEN}",
"Accept": "application/vnd.github.v3+json",
}
response = requests.post(url, headers=headers)
response.raise_for_status()
print(f"Cancelled workflow: {run_id}")
def main(repo_owner='compasslabs', repo_name='dojo'):
parser = argparse.ArgumentParser(description='Cancel all running GitHub workflows.')
parser.add_argument('repo_owner', type=str, help='The owner of the repository.')
parser.add_argument('repo_name', type=str, help='The name of the repository.')
args = parser.parse_args()
if not GITHUB_TOKEN:
raise ValueError("GITHUB_TOKEN environment variable is not set")
workflows = get_running_workflows(args.repo_owner, args.repo_name)
for workflow in workflows:
cancel_workflow(workflow["id"],args.repo_owner, args.repo_name)
if __name__ == "__main__":
main()