Skip to content

Commit b1e706e

Browse files
DEVOPS-72 fixing script and adding print stmts
1 parent 1882cc6 commit b1e706e

File tree

2 files changed

+102
-3
lines changed

2 files changed

+102
-3
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,25 @@
1-
# guthub-create-and-add-labels-all-repos-using-python
1+
# github-create-and-add-labels-all-repos-using-python
22
Create and add GitHub labels to repositories which can be later used for pull requests and issues
3+
4+
# How code works
5+
6+
* we use _**GitHub REST API**_ to programatically create Labels
7+
* The program `create_new_labels.py` have 2 parts
8+
1. It uses GitHub endpoint for listing repos under an organization to list all repositories
9+
2. It uses create labels end point to create labels in repos
10+
* Proper logs (*error* OR *success messgaes*) are displayed
11+
12+
13+
# Authentication method used
14+
15+
`I am using fine grained personal access token`
16+
17+
18+
* The fine-grained token must have at least one of the following permission sets:
19+
20+
* "Issues" repository permissions (write)
21+
* "Pull requests" repository permissions (write)
22+
23+
# Reference
24+
25+
[Create Github Labels](https://docs.github.com/en/rest/issues/labels?apiVersion=2022-11-28#create-a-label)

create_new_labels.py

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import requests
3+
import random
34
from dotenv import load_dotenv
45

56
def list_repos_in_org(org_name: str):
@@ -20,13 +21,88 @@ def list_repos_in_org(org_name: str):
2021

2122
response = requests.get(url=repo_url, headers=headers)
2223
response_json = response.json()
23-
print(response_json)
24+
# print(response_json)
25+
26+
repo_names = []
27+
28+
for repo in response_json:
29+
repo_names.append(repo['name'])
30+
31+
print(f'Listing Public repos in {org_name} completed')
32+
return repo_names
33+
34+
def create_new_label(repo_names: list[str], org_name: str):
35+
"""
36+
create new labels in github
37+
:return:
38+
"""
39+
colors = ["#d954a0", "#9067b6", "#c6ff00", "#2f562a", "#0036ff", "#ff8f38", "#ff3845", "#814e51", "#743747",
40+
"#90395d", "#e3a89e", "#00ff21", "#1f2937", "#111827", "#ff9da4", "#c67b9e", "#f89ac6", "#876baa",
41+
"#588550", "#ff4500", "#d4c5aa", "#c3d5aa", "#ff1493", "#ffb82e", "#23284e", "#7b8f7e", "#0034c3",
42+
"#000032", "#00002e", "#00002a", "#000025", "#00001d", "#000013", "#ff99cc", "#80008e", "#ffeef7",
43+
"#bcd7ff", "#cfe5fd", "#f1e7f7", "#ffeef7", "#bcd7ff", "#bee6ff"]
44+
45+
color1 = random.choice(colors)
46+
color2 = random.choice(colors)
47+
for repository in repo_names:
48+
api_endpoint=f'https://api.github.com/repos/{org_name}/{repository}/labels'
49+
50+
headers = {
51+
"Accept": "application/vnd.github+json",
52+
"Authorization": f"Bearer {os.getenv('GH_TOKEN')}",
53+
"X-GitHub-Api-Version": "2022-11-28"
54+
}
55+
56+
data1 = {
57+
"name": "pip dependencies",
58+
"description": "python package manager",
59+
"color": color1.replace("#", "")
60+
}
61+
62+
data2 = {
63+
"name": "pip-package",
64+
"description": "python package manager",
65+
"color": color2.replace("#", "")
66+
}
67+
68+
response1 = requests.post(url=api_endpoint, headers=headers, json=data1)
69+
status_code1 = response1.status_code
70+
response2 = requests.post(url=api_endpoint, headers=headers, json=data2)
71+
status_code2 = response2.status_code
72+
response1_json =response1.json()
73+
# print(response1_json)
74+
response2_json =response2.json()
75+
# print(response2_json)
76+
77+
if status_code1 == 201:
78+
print(f'New label {data1["name"]} created for repository - {repository}')
79+
elif status_code1 == 404:
80+
print(f'Resource not found')
81+
elif status_code1 == 422:
82+
print(f"{response1_json['message']}. {response1_json['errors'][0]['resource']} {response1_json['errors'][0]['field']} {data1['name']} {response1_json['errors'][0]['code']} in the repository {repository}")
83+
# print(f'Validation failed, or the endpoint has been spammed')
84+
else:
85+
print('Something is wrong. Please try again')
86+
87+
if status_code2 == 201:
88+
print(f'New label {data2["name"]} created for repository - {repository}')
89+
elif status_code2 == 404:
90+
print(f'Resource not found')
91+
elif status_code2 == 422:
92+
print(f"{response2_json['message']}. {response2_json['errors'][0]['resource']} {response2_json['errors'][0]['field']} {data2['name']} {response2_json['errors'][0]['code']} in the repository {repository}")
93+
# print(f'Validation failed, or the endpoint has been spammed')
94+
else:
95+
print('Something is wrong. Please try again')
96+
97+
2498

2599
def main():
26100
""" To test the script"""
27101
load_dotenv()
28102
org_name = os.getenv('ORGANIZATION')
29-
list_repos_in_org(org_name=org_name)
103+
repo_names = list_repos_in_org(org_name=org_name)
104+
create_new_label(repo_names=repo_names, org_name=org_name)
105+
30106

31107
if __name__ == "__main__":
32108
main()

0 commit comments

Comments
 (0)