1
+ import requests
2
+ import argparse
3
+
4
+ # Set up argument parser
5
+ parser = argparse .ArgumentParser (description = "Check master and main branches in a GitHub organization" )
6
+ parser .add_argument ("org_name" , help = "Name of the GitHub organization" )
7
+ parser .add_argument ("access_token" , help = "GitHub Personal Access Token" )
8
+ args = parser .parse_args ()
9
+
10
+ ORG_NAME = args .org_name
11
+ ACCESS_TOKEN = args .access_token
12
+
13
+ BASE_URL = f"https://api.github.com/orgs/{ ORG_NAME } /repos"
14
+ headers = {"Authorization" : f"token { ACCESS_TOKEN } " }
15
+ repositories = []
16
+
17
+ def get_repositories (page ):
18
+ response = requests .get (BASE_URL , headers = headers , params = {"per_page" : 100 , "page" : page })
19
+ if response .status_code == 200 :
20
+ return response .json ()
21
+ else :
22
+ print (f"An error occurred while fetching repositories: { response .status_code } - { response .text } " )
23
+ return []
24
+
25
+ def branch_exists (repo , branch ):
26
+ branch_url = repo ["branches_url" ].replace ("{/branch}" , f"/{ branch } " )
27
+ branch_response = requests .get (branch_url , headers = headers , allow_redirects = False )
28
+
29
+ if branch_response .status_code == 200 :
30
+ branch_info = branch_response .json ()
31
+ return branch_info ["name" ] == branch
32
+ else :
33
+ return False
34
+
35
+ page = 1
36
+ while True :
37
+ repo_list = get_repositories (page )
38
+ if repo_list :
39
+ repositories .extend (repo_list )
40
+ page += 1
41
+ else :
42
+ break
43
+
44
+ for repo in repositories :
45
+ has_master_branch = branch_exists (repo , "master" )
46
+ has_main_branch = branch_exists (repo , "main" )
47
+
48
+ if has_master_branch and has_main_branch :
49
+ print (f"●master ●main : { repo ['name' ]} " )
50
+ elif has_master_branch :
51
+ print (f"●master ○main : { repo ['name' ]} " )
52
+ elif has_main_branch :
53
+ print (f"○master ●main : { repo ['name' ]} " )
54
+ else :
55
+ print (f"○master ○main : { repo ['name' ]} " )
0 commit comments