Skip to content

Commit 7637d8d

Browse files
authored
Merge pull request #140 from saksham-gera/dev
Optimized PR Checking, Branch Handling, And Workflow Timing Attempts, Along With Some Minor Issues Fixed
2 parents ac124d7 + b5e10a3 commit 7637d8d

1 file changed

Lines changed: 53 additions & 63 deletions

File tree

contribute.py

Lines changed: 53 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,36 @@
1717

1818
# Defining Functions
1919
def checkInputValidity():
20-
if AUTHOR_NAME=="" or STUDY_NAME=="" or STUDY_NAME=="":
20+
if not AUTHOR_NAME or not STUDY_NAME or not STUDY_NAME_PATH:
2121
print("Please Provide necessary Inputs")
22-
exit(0)
22+
exit(1)
2323
if not os.path.isdir(STUDY_NAME_PATH):
2424
print("Directory doesnot Exists.Invalid Path")
25-
exit(0)
26-
27-
28-
def getPRs(upstream_repo):
29-
try:
30-
return upstream_repo.get_pulls(head=f'{BOT_ACCOUNT}:{BRANCH_NAME}')
31-
except Exception as e:
32-
print("Not able to fetch Status of your example.Please try after some time.")
33-
exit(0)
25+
exit(1)
3426

3527
def printPR(pr):
3628
print(f'Check your example here https://github.com/{UPSTREAM_ACCOUNT}/{REPO_NAME}/pulls/{pr.number}',end="")
3729

3830
def anyOpenPR(upstream_repo):
39-
pr = getPRs(upstream_repo)
40-
openPr=None
41-
for i in pr:
42-
if i.state=="open":
43-
openPr=i
44-
break
45-
return openPr
31+
try:
32+
prs = upstream_repo.get_pulls(state='open', head=f'{BOT_ACCOUNT}:{BRANCH_NAME}')
33+
return prs[0] if prs.totalCount > 0 else None
34+
except Exception:
35+
print("Unable to fetch PR status. Try again later.")
36+
exit(1)
4637

4738
def commitAndUpdateRef(repo,tree_content,commit,branch):
4839
try:
4940
new_tree = repo.create_git_tree(tree=tree_content,base_tree=commit.commit.tree)
50-
new_commit = repo.create_git_commit("commit study",new_tree,[commit.commit])
41+
new_commit = repo.create_git_commit(f"Committing Study Named {STUDY_NAME}",new_tree,[commit.commit])
5142
if len(repo.compare(base=commit.commit.sha,head=new_commit.sha).files) == 0:
5243
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.")
53-
exit(0)
44+
exit(1)
5445
ref = repo.get_git_ref("heads/"+branch.name)
5546
ref.edit(new_commit.sha,True)
5647
except Exception as e:
5748
print("failed to Upload your example.Please try after some time.",end="")
58-
exit(0)
49+
exit(1)
5950

6051

6152
def appendBlobInTree(repo,content,file_path,tree_content):
@@ -65,31 +56,33 @@ def appendBlobInTree(repo,content,file_path,tree_content):
6556

6657
def runWorkflow(repo,upstream_repo):
6758
openPR = anyOpenPR(upstream_repo)
68-
if openPR==None:
69-
workflow_runned = repo.get_workflow(id_or_name="pull_request.yml").create_dispatch(ref=BRANCH_NAME,inputs={'title':f"[BOT]: {PR_TITLE}",'body':PR_BODY,'upstreamRepo':UPSTREAM_ACCOUNT,'botRepo':BOT_ACCOUNT,'repo':REPO_NAME})
70-
if not workflow_runned:
71-
print("Some error occured. Please try after some time")
72-
exit(0)
73-
else:
59+
if not openPR:
60+
try:
61+
repo.get_workflow("pull_request.yml").create_dispatch(
62+
ref=BRANCH_NAME,
63+
inputs={'title': f"[BOT]: {PR_TITLE}", 'body': PR_BODY, 'upstreamRepo': UPSTREAM_ACCOUNT, 'botRepo': BOT_ACCOUNT, 'repo': REPO_NAME}
64+
)
7465
printPRStatus(upstream_repo)
66+
except Exception as e:
67+
print(f"Error triggering workflow. Try again later.\n ERROR: {e}")
68+
exit(1)
7569
else:
76-
print("Successfully uploaded all files, your example is in waiting.Please wait for us to accept it.",end="")
77-
printPR(openPR)
70+
print(f"Successfully uploaded. Waiting for approval: https://github.com/{UPSTREAM_ACCOUNT}/{REPO_NAME}/pull/{openPR.number}")
7871

7972
def printPRStatus(upstream_repo):
80-
try:
81-
issues = upstream_repo.get_issues()
82-
pulls = upstream_repo.get_pulls(state='all')
83-
max_num = -1
84-
for i in issues:
85-
max_num = max(max_num,i.number)
86-
for i in pulls:
87-
max_num = max(max_num,i.number)
88-
time.sleep(4)
89-
print(f'Check your example here https://github.com/{UPSTREAM_ACCOUNT}/{REPO_NAME}/pulls/{max_num+1}',end="")
90-
except Exception as e:
91-
print("Your example successfully uploaded but unable to fetch status. Please try again")
92-
73+
attempts = 5
74+
delay = 2
75+
for i in range(attempts):
76+
print(f"Attempt: {i}")
77+
try:
78+
latest_pr = upstream_repo.get_pulls(state='open', sort='created', direction='desc')[0]
79+
print(f"Check your example here: https://github.com/{UPSTREAM_ACCOUNT}/{REPO_NAME}/pull/{latest_pr.number}")
80+
return
81+
except Exception:
82+
time.sleep(delay)
83+
delay *= 2
84+
print("Uploaded successfully, but unable to fetch status.")
85+
9386

9487
def isImageFile(filename):
9588
image_extensions = ['.jpeg', '.jpg', '.png','.gif']
@@ -115,37 +108,34 @@ def decode_token(encoded_token):
115108

116109
# Authenticating Github with Access token
117110
try:
118-
if BRANCH_NAME=="#":
119-
BRANCH_NAME=AUTHOR_NAME+"_"+STUDY_NAME
120-
if PR_TITLE=="#":
121-
PR_TITLE=f"Contributing Study {STUDY_NAME} by {AUTHOR_NAME}"
122-
if PR_BODY=="#":
123-
PR_BODY=f"Study Name: {STUDY_NAME} \n Author Name: {AUTHOR_NAME}"
124-
AUTHOR_NAME = AUTHOR_NAME.replace(" ","_")
111+
BRANCH_NAME = AUTHOR_NAME.replace(" ", "_") + "_" + STUDY_NAME if BRANCH_NAME == "#" else BRANCH_NAME.replace(" ", "_")
112+
PR_TITLE = f"Contributing Study {STUDY_NAME} by {AUTHOR_NAME}" if PR_TITLE == "#" else PR_TITLE
113+
PR_BODY = f"Study Name: {STUDY_NAME}\nAuthor Name: {AUTHOR_NAME}" if PR_BODY == "#" else PR_BODY
125114
DIR_PATH = STUDY_NAME
115+
DIR_PATH = DIR_PATH.replace(" ","_")
126116
g = Github(decode_token(BOT_TOKEN))
127117
repo = g.get_user(BOT_ACCOUNT).get_repo(REPO_NAME)
128118
upstream_repo = g.get_repo(f'{UPSTREAM_ACCOUNT}/{REPO_NAME}') #controlcore-Project/concore-studies
129119
base_ref = upstream_repo.get_branch(repo.default_branch)
130-
branches = repo.get_branches()
131-
BRANCH_NAME = BRANCH_NAME.replace(" ","_")
132-
DIR_PATH = DIR_PATH.replace(" ","_")
133-
is_present = any(branch.name == BRANCH_NAME for branch in branches)
120+
121+
try:
122+
repo.get_branch(BRANCH_NAME)
123+
is_present = True
124+
except github.GithubException:
125+
print(f"No Branch is available with the name {BRANCH_NAME}")
126+
is_present = False
134127
except Exception as e:
135128
print("Authentication failed", end="")
136-
exit(0)
129+
exit(1)
137130

138131

139132
try:
140-
# If creating PR First Time
141-
# Create New Branch for that exmaple
142133
if not is_present:
143-
repo.create_git_ref(f'refs/heads/{BRANCH_NAME}', base_ref.commit.sha)
144-
# Get current branch
145-
branch = repo.get_branch(branch=BRANCH_NAME)
146-
except Exception as e:
147-
print("Not able to create study for you. Please try again after some time", end="")
148-
exit(0)
134+
repo.create_git_ref(f"refs/heads/{BRANCH_NAME}", base_ref.commit.sha)
135+
branch = repo.get_branch(BRANCH_NAME)
136+
except Exception:
137+
print("Unable to create study. Try again later.")
138+
exit(1)
149139

150140

151141
tree_content = []
@@ -170,4 +160,4 @@ def decode_token(encoded_token):
170160
except Exception as e:
171161
print(e)
172162
print("Some error Occured.Please try again after some time.",end="")
173-
exit(0)
163+
exit(1)

0 commit comments

Comments
 (0)