1+ import github
2+ from github import Github
3+ import os ,sys ,time ,platform ,base64
4+
5+ # Intializing the Variables
6+ # Hashed token
7+ BOT_TOKEN = 'Z2l0aHViX3BhdF8xMUFYS0pGVFkwUWVWZ3AzbkpkWk8yX3BOc1VncDFIVDMwZVNXcHhBNm9acHhMaGZGdU5CdE85TGpqdXF1UWRRNzI2S01aUk5HRUNGanFWNDZi'
8+ REPO_NAME = 'concore' #repo name
9+ OWNER_NAME = 'parteekcoder123' #bot account name
10+ STUDY_NAME = sys .argv [1 ]
11+ STUDY_NAME_PATH = sys .argv [2 ]
12+ AUTHOR_NAME = sys .argv [3 ]
13+ BRANCH_NAME = sys .argv [4 ]
14+ PR_TITLE = sys .argv [5 ]
15+ PR_BODY = sys .argv [6 ]
16+ UPSTREAM_OWNER = 'parteekcoder' # upstream to which examples should be contributed
17+
18+
19+ # Defining Functions
20+ def checkInputValidity ():
21+ if AUTHOR_NAME == "" or STUDY_NAME == "" or STUDY_NAME == "" :
22+ print ("Please Provide necessary Inputs" )
23+ exit (0 )
24+ if not os .path .isdir (STUDY_NAME_PATH ):
25+ print ("Directory doesnot Exists.Invalid Path" )
26+ exit (0 )
27+
28+
29+ def getPRs (upstream_repo ):
30+ try :
31+ return upstream_repo .get_pulls (head = f'{ OWNER_NAME } :{ BRANCH_NAME } ' )
32+ except Exception as e :
33+ print ("Not able to fetch Status of your example.Please try after some time." )
34+ exit (0 )
35+
36+ def printPR (pr ):
37+ print (f'Check your example here https://github.com/{ UPSTREAM_OWNER } /pulls/' + str (pr .number ),end = "" )
38+
39+ def anyOpenPR (upstream_repo ):
40+ pr = getPRs (upstream_repo )
41+ openPr = None
42+ for i in pr :
43+ if i .state == "open" :
44+ openPr = i
45+ break
46+ return openPr
47+
48+ def commitAndUpdateRef (upstream_repo ,repo ,tree_content ,commit ,branch ):
49+ try :
50+ new_tree = repo .create_git_tree (tree = tree_content ,base_tree = commit .commit .tree )
51+ new_commit = repo .create_git_commit ("commit message" ,new_tree ,[commit .commit ])
52+ if len (repo .compare (base = commit .commit .sha ,head = new_commit .sha ).files ) == 0 :
53+ print ("Your don't have any new changes.May be your example is already accepted.If this is not the case try with different fields." )
54+ exit (0 )
55+ ref = repo .get_git_ref ("heads/" + branch .name )
56+ ref .edit (new_commit .sha ,True )
57+ except Exception as e :
58+ print ("failed to Upload your example.Please try after some time." ,end = "" )
59+ exit (0 )
60+
61+
62+ def appendBlobInTree (repo ,content ,file_path ,tree_content ):
63+ blob = repo .create_git_blob (content ,'utf-8' )
64+ tree_content .append ( github .InputGitTreeElement (path = file_path ,mode = "100644" ,type = "blob" ,sha = blob .sha ))
65+
66+
67+ def fetchUpstream (repo ,base_sha ,branch ):
68+ try :
69+ result = repo .compare (base = base_sha ,head = branch .commit .commit .sha )
70+ if result .behind_by > 0 :
71+ ref = repo .get_git_ref ("heads/" + branch .name )
72+ ref .edit (base_sha )
73+ except Exception as e :
74+ exit (0 )
75+
76+
77+ def runWorkflow (repo ,upstream_repo ):
78+ openPR = anyOpenPR (upstream_repo )
79+ if openPR == None :
80+ workflow_runned = repo .get_workflow (id_or_name = "pull_request.yml" ).create_dispatch (ref = BRANCH_NAME ,inputs = {'title' :PR_TITLE ,'body' :PR_BODY ,'upstreamRepo' :upstream_repo ,'botRepo' :OWNER_NAME ,'repo' :REPO_NAME })
81+ if not workflow_runned :
82+ print ("Some Error Occured.Please try after some time" )
83+ exit (0 )
84+ else :
85+ printPRStatus (upstream_repo )
86+ else :
87+ print ("Successfully uploaded all files,your example is in waiting.Please wait for us to accept it." ,end = "" )
88+ printPR (openPR )
89+
90+ def printPRStatus (upstream_repo ):
91+ try :
92+ time .sleep (15 )
93+ openPR = anyOpenPR (upstream_repo )
94+ if openPR == None :
95+ print ("Someting went wrong or your example already exist.If this is not the case try with different fields" )
96+ exit (0 )
97+ printPR (openPR )
98+ except Exception as e :
99+ print ("Your example successfully uploaded but unable to fetch status.Please try again" )
100+
101+
102+ def isImageFile (filename ):
103+ image_extensions = ['.jpeg' , '.jpg' , '.png' ]
104+ _ , file_extension = os .path .splitext (filename )
105+ return file_extension .lower () in image_extensions
106+
107+ # Encode Github Token
108+ def encode_token (token ):
109+ encoded_bytes = base64 .b64encode (token .encode ('utf-8' ))
110+ encoded_token = encoded_bytes .decode ('utf-8' )
111+ return encoded_token
112+
113+
114+ # Decode Github Token
115+ def decode_token (encoded_token ):
116+ decoded_bytes = base64 .b64decode (encoded_token .encode ('utf-8' ))
117+ decoded_token = decoded_bytes .decode ('utf-8' )
118+ return decoded_token
119+
120+
121+ # check if directory path is Valid
122+ checkInputValidity ()
123+
124+
125+ # Authenticating Github with Access token
126+ try :
127+ if BRANCH_NAME == "#" :
128+ BRANCH_NAME = AUTHOR_NAME + "_" + STUDY_NAME
129+ if PR_TITLE == "#" :
130+ PR_TITLE = "Contributing Study " + AUTHOR_NAME + " " + STUDY_NAME
131+ if PR_BODY == "#" :
132+ PR_BODY = "Study Contributed by " + AUTHOR_NAME
133+ AUTHOR_NAME = AUTHOR_NAME .replace (" " ,"_" )
134+ DIR_PATH = AUTHOR_NAME + '_' + STUDY_NAME
135+ g = Github (decode_token (BOT_TOKEN ))
136+ repo = g .get_user (OWNER_NAME ).get_repo (REPO_NAME )
137+ upstream_repo = g .get_repo (f'{ UPSTREAM_OWNER } /{ REPO_NAME } ' ) #controlcore-Project/concore
138+ base_ref = upstream_repo .get_branch (repo .default_branch )
139+ branches = repo .get_branches ()
140+ BRANCH_NAME = BRANCH_NAME .replace (" " ,"_" )
141+ DIR_PATH = DIR_PATH .replace (" " ,"_" )
142+ is_present = any (branch .name == BRANCH_NAME for branch in branches )
143+ except :
144+ print ("Some error occured.Authentication failed" ,end = "" )
145+ exit (0 )
146+
147+
148+ try :
149+ # If creating PR First Time
150+ # Create New Branch for that exmaple
151+ if not is_present :
152+ repo .create_git_ref (f'refs/heads/{ BRANCH_NAME } ' , base_ref .commit .sha )
153+ # Get current branch
154+ branch = repo .get_branch (branch = BRANCH_NAME )
155+ except Exception as e :
156+ print ("Not able to create study for you.Please try again after some time" ,end = "" )
157+ exit (0 )
158+
159+
160+ tree_content = []
161+
162+ try :
163+ for root , dirs , files in os .walk (STUDY_NAME_PATH ):
164+ for filename in files :
165+ path = os .path .join (root , filename )
166+ if isImageFile (filename ):
167+ with open (path , 'rb' ) as file :
168+ image = file .read ()
169+ content = base64 .b64encode (image ).decode ('utf-8' )
170+ else :
171+ with open (path , 'r' ) as file :
172+ content = file .read ()
173+ file_path = f'{ DIR_PATH + path .removeprefix (STUDY_NAME_PATH )} '
174+ if (platform .uname ()[0 ]== 'Windows' ): file_path = file_path .replace ("\\ " ,"/" )
175+ appendBlobInTree (repo ,content ,file_path ,tree_content )
176+ commitAndUpdateRef (upstream_repo ,repo ,tree_content ,base_ref .commit ,branch )
177+ runWorkflow (repo ,upstream_repo )
178+ except Exception as e :
179+ print ("Some error Occured.Please try again after some time." ,end = "" )
180+ exit (0 )
0 commit comments