Skip to content

Commit 2d3cbba

Browse files
authored
Merge pull request #82 from parteekcoder/contribute_action
Contribute action
2 parents 59cd014 + 96c8f99 commit 2d3cbba

5 files changed

Lines changed: 23 additions & 68 deletions

File tree

.github/workflows/pull_request.yml

Lines changed: 0 additions & 31 deletions
This file was deleted.

contribute

100644100755
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

33
# Check if the first 3 arguments are provided
4-
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
4+
if [ -z $1 ] || [ -z $2 ] || [ -z $3 ]; then
55
echo "Error: The first 3 arguments are mandatory."
66
echo "Usage: ./contribute <Study-Name> <Full-Path-To-Study> <Author-Name> <Branch-Name> <PR-title> <PR-Body>"
77
exit 1
@@ -11,13 +11,13 @@ fi
1111
arg4=$4
1212
arg5=$5
1313
arg6=$6
14-
if [ -z "$4" ]; then
14+
if [ -z $4 ]; then
1515
arg4="#"
1616
fi
17-
if [ -z "$5" ]; then
17+
if [ -z $5 ]; then
1818
arg5="#"
1919
fi
20-
if [ -z "$6" ]; then
20+
if [ -z $6 ]; then
2121
arg6="#"
2222
fi
23-
python contribute.py $1 $2 $3 $arg4 $arg5 $arg6
23+
python3 contribute.py $1 $2 $3 $arg4 $arg5 $arg6

contribute.py

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44

55
# Intializing the Variables
66
# Hashed token
7-
BOT_TOKEN = 'Z2l0aHViX3BhdF8xMUFYS0pGVFkwUWVWZ3AzbkpkWk8yX3BOc1VncDFIVDMwZVNXcHhBNm9acHhMaGZGdU5CdE85TGpqdXF1UWRRNzI2S01aUk5HRUNGanFWNDZi'
8-
REPO_NAME = 'concore' #repo name
9-
OWNER_NAME = 'parteekcoder123' #bot account name
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
1011
STUDY_NAME = sys.argv[1]
1112
STUDY_NAME_PATH = sys.argv[2]
1213
AUTHOR_NAME = sys.argv[3]
1314
BRANCH_NAME = sys.argv[4]
1415
PR_TITLE = sys.argv[5]
1516
PR_BODY = sys.argv[6]
16-
UPSTREAM_OWNER = 'parteekcoder' # upstream to which examples should be contributed
17-
1817

1918
# Defining Functions
2019
def checkInputValidity():
@@ -34,7 +33,7 @@ def getPRs(upstream_repo):
3433
exit(0)
3534

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

3938
def anyOpenPR(upstream_repo):
4039
pr = getPRs(upstream_repo)
@@ -45,7 +44,7 @@ def anyOpenPR(upstream_repo):
4544
break
4645
return openPr
4746

48-
def commitAndUpdateRef(upstream_repo,repo,tree_content,commit,branch):
47+
def commitAndUpdateRef(repo,tree_content,commit,branch):
4948
try:
5049
new_tree = repo.create_git_tree(tree=tree_content,base_tree=commit.commit.tree)
5150
new_commit = repo.create_git_commit("commit message",new_tree,[commit.commit])
@@ -64,22 +63,12 @@ def appendBlobInTree(repo,content,file_path,tree_content):
6463
tree_content.append( github.InputGitTreeElement(path=file_path,mode="100644",type="blob",sha=blob.sha))
6564

6665

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-
7766
def runWorkflow(repo,upstream_repo):
7867
openPR = anyOpenPR(upstream_repo)
7968
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})
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})
8170
if not workflow_runned:
82-
print("Some Error Occured.Please try after some time")
71+
print("Some error occured.Please try after some time")
8372
exit(0)
8473
else:
8574
printPRStatus(upstream_repo)
@@ -104,12 +93,6 @@ def isImageFile(filename):
10493
_, file_extension = os.path.splitext(filename)
10594
return file_extension.lower() in image_extensions
10695

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-
11396

11497
# Decode Github Token
11598
def decode_token(encoded_token):
@@ -127,14 +110,14 @@ def decode_token(encoded_token):
127110
if BRANCH_NAME=="#":
128111
BRANCH_NAME=AUTHOR_NAME+"_"+STUDY_NAME
129112
if PR_TITLE=="#":
130-
PR_TITLE="Contributing Study "+AUTHOR_NAME+" "+STUDY_NAME
113+
PR_TITLE=f"Contributing Study {STUDY_NAME} by {AUTHOR_NAME}"
131114
if PR_BODY=="#":
132-
PR_BODY="Study Contributed by "+ AUTHOR_NAME
115+
PR_BODY=f"Study Name: {STUDY_NAME} \n Author Name: {AUTHOR_NAME}"
133116
AUTHOR_NAME = AUTHOR_NAME.replace(" ","_")
134117
DIR_PATH = AUTHOR_NAME + '_' + STUDY_NAME
135118
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
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
138121
base_ref = upstream_repo.get_branch(repo.default_branch)
139122
branches = repo.get_branches()
140123
BRANCH_NAME = BRANCH_NAME.replace(" ","_")
@@ -173,7 +156,7 @@ def decode_token(encoded_token):
173156
file_path = f'{DIR_PATH+path.removeprefix(STUDY_NAME_PATH)}'
174157
if(platform.uname()[0]=='Windows'): file_path=file_path.replace("\\","/")
175158
appendBlobInTree(repo,content,file_path,tree_content)
176-
commitAndUpdateRef(upstream_repo,repo,tree_content,base_ref.commit,branch)
159+
commitAndUpdateRef(repo,tree_content,base_ref.commit,branch)
177160
runWorkflow(repo,upstream_repo)
178161
except Exception as e:
179162
print("Some error Occured.Please try again after some time.",end="")

fri/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ Flask
22
gunicorn==20.1.0
33
FLASK_CORS
44
jupyterlab
5+
PyGithub

fri/server/main.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,10 @@ def contribute():
182182
if(platform.uname()[0]=='Windows'):
183183
proc=check_output(["contribute",STUDY_NAME,STUDY_NAME_PATH,AUTHOR_NAME,BRANCH_NAME,PR_TITLE,PR_BODY],cwd=concore_path,shell=True)
184184
else:
185-
proc = check_output(["./contribute",STUDY_NAME,STUDY_NAME_PATH,AUTHOR_NAME,BRANCH_NAME,PR_TITLE,PR_BODY],cwd=concore_path)
185+
if len(BRANCH_NAME)==0:
186+
proc = check_output(["./contribute",STUDY_NAME,STUDY_NAME_PATH,AUTHOR_NAME],cwd=concore_path)
187+
else:
188+
proc = check_output(["./contribute",STUDY_NAME,STUDY_NAME_PATH,AUTHOR_NAME,BRANCH_NAME,PR_TITLE,PR_BODY],cwd=concore_path)
186189
output_string = proc.decode()
187190
status=200
188191
if output_string.find("/pulls/")!=-1:
@@ -193,7 +196,6 @@ def contribute():
193196
status=400
194197
return jsonify({'message': output_string}),status
195198
except Exception as e:
196-
print(e)
197199
output_string = "Some Error occured.Please try after some time"
198200
status=501
199201
return jsonify({'message': output_string}),status

0 commit comments

Comments
 (0)