Skip to content

Commit edcc46b

Browse files
authored
Merge pull request #91 from parteekcoder/code-optimizations
Code optimizations
2 parents 2d3cbba + a5fbf24 commit edcc46b

1 file changed

Lines changed: 23 additions & 22 deletions

File tree

contribute.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import github
22
from github import Github
3-
import os,sys,time,platform,base64
3+
import os,sys,platform,base64
44

55
# Intializing the Variables
66
# Hashed token
7-
BOT_TOKEN = 'Z2l0aHViX3BhdF8xMUFYS0pGVFkwd2xwT0dmYldFOTBBXzN3Nkx2THpiaUFKek5pTDdqNlpLUzVwUUpoTlJWR3dtNnM0NWNDa0RmWTJaTTZLSUpHRHhERlhrZlJS'
8-
BOT_REPO_NAME = 'concore-studies-staging' #bot repo name
9-
UPSTREAM_REPO_NAME = 'concore-studies' #bot repo name
10-
OWNER_NAME = 'ControlCore-Project' #account name
7+
BOT_TOKEN = "Z2l0aHViX3BhdF8xMUFYS0pGVFkwODR5OEhoZlI5VEl1X0VZZnNaNjU0WGw4OU0ycXhJc0h3TXh3RkVIZGFRQ3gwa0daZFhKUUdYbUk2QzRTU1dDNkF4clUyQWRF"
8+
BOT_ACCOUNT = 'concore-bot' #bot account name
9+
REPO_NAME = 'concore-studies' #study repo name
10+
UPSTREAM_ACCOUNT = 'ControlCore-Project' #upstream account name
1111
STUDY_NAME = sys.argv[1]
1212
STUDY_NAME_PATH = sys.argv[2]
1313
AUTHOR_NAME = sys.argv[3]
@@ -27,13 +27,13 @@ def checkInputValidity():
2727

2828
def getPRs(upstream_repo):
2929
try:
30-
return upstream_repo.get_pulls(head=f'{OWNER_NAME}:{BRANCH_NAME}')
30+
return upstream_repo.get_pulls(head=f'{BOT_ACCOUNT}:{BRANCH_NAME}')
3131
except Exception as e:
3232
print("Not able to fetch Status of your example.Please try after some time.")
3333
exit(0)
3434

3535
def printPR(pr):
36-
print(f'Check your example here https://github.com/{OWNER_NAME}/{UPSTREAM_REPO_NAME}/pulls/'+str(pr.number),end="")
36+
print(f'Check your example here https://github.com/{UPSTREAM_ACCOUNT}/{REPO_NAME}/pulls/{pr.number}',end="")
3737

3838
def anyOpenPR(upstream_repo):
3939
pr = getPRs(upstream_repo)
@@ -66,7 +66,7 @@ def appendBlobInTree(repo,content,file_path,tree_content):
6666
def runWorkflow(repo,upstream_repo):
6767
openPR = anyOpenPR(upstream_repo)
6868
if openPR==None:
69-
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_NAME,'account':OWNER_NAME})
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})
7070
if not workflow_runned:
7171
print("Some error occured.Please try after some time")
7272
exit(0)
@@ -78,20 +78,21 @@ def runWorkflow(repo,upstream_repo):
7878

7979
def printPRStatus(upstream_repo):
8080
try:
81-
time.sleep(15)
82-
openPR = anyOpenPR(upstream_repo)
83-
if openPR==None:
84-
print("Someting went wrong or your example already exist.If this is not the case try with different fields")
85-
exit(0)
86-
printPR(openPR)
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+
print(f'Check your example here https://github.com/{UPSTREAM_ACCOUNT}/{REPO_NAME}/pulls/{max_num+1}',end="")
8789
except Exception as e:
8890
print("Your example successfully uploaded but unable to fetch status.Please try again")
8991

9092

9193
def isImageFile(filename):
92-
image_extensions = ['.jpeg', '.jpg', '.png']
93-
_, file_extension = os.path.splitext(filename)
94-
return file_extension.lower() in image_extensions
94+
image_extensions = ['.jpeg', '.jpg', '.png','.gif']
95+
return any(filename.endswith(ext) for ext in image_extensions)
9596

9697

9798
# Decode Github Token
@@ -114,16 +115,16 @@ def decode_token(encoded_token):
114115
if PR_BODY=="#":
115116
PR_BODY=f"Study Name: {STUDY_NAME} \n Author Name: {AUTHOR_NAME}"
116117
AUTHOR_NAME = AUTHOR_NAME.replace(" ","_")
117-
DIR_PATH = AUTHOR_NAME + '_' + STUDY_NAME
118+
DIR_PATH = STUDY_NAME
118119
g = Github(decode_token(BOT_TOKEN))
119-
repo = g.get_user(OWNER_NAME).get_repo(BOT_REPO_NAME)
120-
upstream_repo = g.get_repo(f'{OWNER_NAME}/{UPSTREAM_REPO_NAME}') #controlcore-Project/concore
120+
repo = g.get_user(BOT_ACCOUNT).get_repo(REPO_NAME)
121+
upstream_repo = g.get_repo(f'{UPSTREAM_ACCOUNT}/{REPO_NAME}') #controlcore-Project/concore-studies
121122
base_ref = upstream_repo.get_branch(repo.default_branch)
122123
branches = repo.get_branches()
123124
BRANCH_NAME = BRANCH_NAME.replace(" ","_")
124125
DIR_PATH = DIR_PATH.replace(" ","_")
125126
is_present = any(branch.name == BRANCH_NAME for branch in branches)
126-
except:
127+
except Exception as e:
127128
print("Some error occured.Authentication failed",end="")
128129
exit(0)
129130

@@ -145,7 +146,7 @@ def decode_token(encoded_token):
145146
try:
146147
for root, dirs, files in os.walk(STUDY_NAME_PATH):
147148
for filename in files:
148-
path = os.path.join(root, filename)
149+
path = f"{root}/{filename}"
149150
if isImageFile(filename):
150151
with open(path, 'rb') as file:
151152
image = file.read()

0 commit comments

Comments
 (0)