From 22c1dc23dcb5aa6745c9b0d905c837974e7e30e2 Mon Sep 17 00:00:00 2001 From: parteekcoder Date: Sat, 17 Jun 2023 21:53:26 +0530 Subject: [PATCH 1/8] contribute feature --- contribute | 23 ++++++ contribute.bat | 20 +++++ contribute.py | 181 +++++++++++++++++++++++++++++++++++++++++++++ fri/server/main.py | 31 +++++++- requirements.txt | 1 + 5 files changed, 255 insertions(+), 1 deletion(-) create mode 100644 contribute create mode 100644 contribute.bat create mode 100644 contribute.py diff --git a/contribute b/contribute new file mode 100644 index 0000000..4fc4fbb --- /dev/null +++ b/contribute @@ -0,0 +1,23 @@ +#!/bin/bash + +# Check if the first 3 arguments are provided +if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then + echo "Error: The first 3 arguments are mandatory." + echo "Usage: ./contribute " + exit 1 +fi + +# Set the last 3 arguments to "#" if not provided +arg4=$4 +arg5=$5 +arg6=$6 +if [ -z "$4" ]; then +arg4="#" +fi +if [ -z "$5" ]; then +arg5="#" +fi +if [ -z "$6" ]; then +arg6="#" +fi +python contribute.py $1 $2 $3 $arg4 $arg5 $arg6 diff --git a/contribute.bat b/contribute.bat new file mode 100644 index 0000000..4ffae56 --- /dev/null +++ b/contribute.bat @@ -0,0 +1,20 @@ +@echo off + +REM Check if the first 3 arguments are provided +if "%~1" == "" goto :missing_arg +if "%~2" == "" goto :missing_arg +if "%~3" == "" goto :missing_arg + +REM Check and set default values for the last three arguments if not provided +if "%~4"=="" (set arg4="#") else (set arg4=%~4) +if "%~5"=="" (set arg5="#") else (set arg5=%~5) +if "%~6"=="" (set arg6="#") else (set arg6=%~6) +python contribute.py %1 %2 %3 %arg4% %arg5% %arg6% +goto :eof + +:missing_arg +echo "Error: The first 3 arguments are mandatory." +echo "Usage: ./contribute.bat " + + + diff --git a/contribute.py b/contribute.py new file mode 100644 index 0000000..912df0d --- /dev/null +++ b/contribute.py @@ -0,0 +1,181 @@ +import github +from github import Github +import os,sys,time,platform,base64 + +# Intializing the Variables +# Hashed token +BOT_TOKEN = 'Z2l0aHViX3BhdF8xMUFYM1pBT1kwR0lTTm5jWFlObXdhX3hQa2xyOVdSZ0ZMRURGcERFNFBzbW9zZFFUTzZaNTBBZkcwY2dYSVNKZ2FCSUtNWUo3SGpXTnlxTFda' +REPO_NAME = 'concore' #repo-name +OWNER_NAME = 'parteekcoder' #bot +STUDY_NAME = sys.argv[1] +STUDY_NAME_PATH = sys.argv[2] +AUTHOR_NAME = sys.argv[3] +BRANCH_NAME = sys.argv[4] +PR_TITLE = sys.argv[5] +PR_BODY = sys.argv[6] + +# if author name has spaces +DIR_PATH = AUTHOR_NAME + '_' + STUDY_NAME +UPSTREAM_OWNER = 'parteekcoder123' + + +# Defining Functions +def checkInputValidity(): + if AUTHOR_NAME=="" or STUDY_NAME=="" or STUDY_NAME=="": + print("Please Provide necessary Inputs") + exit(0) + if not os.path.isdir(STUDY_NAME_PATH): + print("Directory doesnot Exists.Invalid Path") + exit(0) + + +def getPRs(upstream_repo): + try: + return upstream_repo.get_pulls(head=f'{OWNER_NAME}:{BRANCH_NAME}') + except Exception as e: + print("Not able to fetch Status of your example.Please try after some time.") + exit(0) + +def printPR(pr): + print(f'Check your example here https://github.com/{UPSTREAM_OWNER}/pulls/'+str(pr.number),end="") + +def anyOpenPR(upstream_repo): + pr = getPRs(upstream_repo) + openPr=None + for i in pr: + if i.state=="open": + openPr=i + break + return openPr + +def commitAndUpdateRef(upstream_repo,repo,tree_content,commit,branch): + try: + new_tree = repo.create_git_tree(tree=tree_content,base_tree=commit.commit.tree) + new_commit = repo.create_git_commit("commit message",new_tree,[commit.commit]) + if len(repo.compare(base=commit.commit.sha,head=new_commit.sha).files) == 0: + 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.") + exit(0) + ref = repo.get_git_ref("heads/"+branch.name) + ref.edit(new_commit.sha,True) + except Exception as e: + print("failed to Upload your example.Please try after some time.",end="") + exit(0) + + +def appendBlobInTree(repo,content,file_path,tree_content): + blob = repo.create_git_blob(content,'utf-8') + tree_content.append( github.InputGitTreeElement(path=file_path,mode="100644",type="blob",sha=blob.sha)) + + +def fetchUpstream(repo,base_sha,branch): + try: + result = repo.compare(base=base_sha,head=branch.commit.commit.sha) + if result.behind_by>0: + ref = repo.get_git_ref("heads/"+branch.name) + ref.edit(base_sha) + except Exception as e: + exit(0) + + +def runWorkflow(repo,upstream_repo): + openPR = anyOpenPR(upstream_repo) + if openPR==None: + workflow_runned = repo.get_workflow(id_or_name="pull_request.yml").create_dispatch(ref=BRANCH_NAME,inputs={'title':PR_TITLE,'body':PR_BODY}) + if not workflow_runned: + print("Some Error Occured.Please try after some time") + exit(0) + else: + printPRStatus(upstream_repo) + else: + print("Successfully uploaded all files,your example is in waiting.Please wait for us to accept it.",end="") + printPR(openPR) + +def printPRStatus(upstream_repo): + try: + time.sleep(15) + openPR = anyOpenPR(upstream_repo) + if openPR==None: + print("Someting went wrong or your example already exist.If this is not the case try with different fields") + exit(0) + printPR(openPR) + except Exception as e: + print("Your example successfully uploaded but unable to fetch status.Please try again") + + +def isImageFile(filename): + image_extensions = ['.jpeg', '.jpg', '.png'] + _, file_extension = os.path.splitext(filename) + return file_extension.lower() in image_extensions + +# Encode Github Token +def encode_token(token): + encoded_bytes = base64.b64encode(token.encode('utf-8')) + encoded_token = encoded_bytes.decode('utf-8') + return encoded_token + + +# Decode Github Token +def decode_token(encoded_token): + decoded_bytes = base64.b64decode(encoded_token.encode('utf-8')) + decoded_token = decoded_bytes.decode('utf-8') + return decoded_token + + +# check if directory path is Valid +checkInputValidity() + + +# Authenticating Github with Access token +try: + if BRANCH_NAME=="#": + BRANCH_NAME=AUTHOR_NAME+"_"+STUDY_NAME + if PR_TITLE=="#": + PR_TITLE="Contributing Study "+AUTHOR_NAME+" "+STUDY_NAME + if PR_BODY=="#": + PR_BODY="Study Contributed by "+ AUTHOR_NAME + g = Github(decode_token(BOT_TOKEN)) + repo = g.get_user(OWNER_NAME).get_repo(REPO_NAME) + upstream_repo = g.get_repo(f'{UPSTREAM_OWNER}/{REPO_NAME}') #controlcore-Project/concore + base_ref = upstream_repo.get_branch(repo.default_branch) + branches = repo.get_branches() + BRANCH_NAME = BRANCH_NAME.replace(" ","_") + DIR_PATH = DIR_PATH.replace(" ","_") + is_present = any(branch.name == BRANCH_NAME for branch in branches) +except: + print("Some error occured.Authentication failed",end="") + exit(0) + + +try: + # If creating PR First Time + # Create New Branch for that exmaple + if not is_present: + repo.create_git_ref(f'refs/heads/{BRANCH_NAME}', base_ref.commit.sha) + # Get current branch + branch = repo.get_branch(branch=BRANCH_NAME) +except Exception as e: + print("Not able to create study for you.Please try again after some time",end="") + exit(0) + + +tree_content = [] + +try: + for root, dirs, files in os.walk(STUDY_NAME_PATH): + for filename in files: + path = os.path.join(root, filename) + if isImageFile(filename): + with open(path, 'rb') as file: + image = file.read() + content = base64.b64encode(image).decode('utf-8') + else: + with open(path, 'r') as file: + content = file.read() + file_path = f'{DIR_PATH+path.removeprefix(STUDY_NAME_PATH)}' + if(platform.uname()[0]=='Windows'): file_path=file_path.replace("\\","/") + appendBlobInTree(repo,content,file_path,tree_content) + commitAndUpdateRef(upstream_repo,repo,tree_content,base_ref.commit,branch) + runWorkflow(repo,upstream_repo) +except Exception as e: + print("Some error Occured.Please try again after some time.",end="") + exit(0) \ No newline at end of file diff --git a/fri/server/main.py b/fri/server/main.py index b2b790e..8a1014d 100644 --- a/fri/server/main.py +++ b/fri/server/main.py @@ -2,7 +2,7 @@ from werkzeug.utils import secure_filename import os import subprocess -from subprocess import call +from subprocess import call,check_output from pathlib import Path import json import platform @@ -169,6 +169,35 @@ def clear(dir): resp.status_code = 500 return resp +@app.route('/contribute', methods=['POST']) +def contribute(): + try: + data = request.json + PR_TITLE = data.get('title') + PR_BODY = data.get('desc') + AUTHOR_NAME = data.get('auth') + STUDY_NAME = data.get('study') + STUDY_NAME_PATH = data.get('path') + BRANCH_NAME = data.get('branch') + if(platform.uname()[0]=='Windows'): + proc=check_output(["contribute",STUDY_NAME,STUDY_NAME_PATH,AUTHOR_NAME,BRANCH_NAME,PR_TITLE,PR_BODY],cwd=concore_path,shell=True) + else: + proc = check_output(["./contribute",STUDY_NAME,STUDY_NAME_PATH,AUTHOR_NAME,BRANCH_NAME,PR_TITLE,PR_BODY],cwd=concore_path) + output_string = proc.decode() + status=200 + if output_string.find("/pulls/")!=-1: + status=200 + elif output_string.find("error")!=-1: + status=501 + else: + status=400 + return jsonify({'message': output_string}),status + except Exception as e: + print(e) + output_string = "Some Error occured.Please try after some time" + status=501 + return jsonify({'message': output_string}),status + # to download /download/?fetch=. For example, /download/test?fetchDir=xyz&fetch=u @app.route('/download/', methods=['POST', 'GET']) def download(dir): diff --git a/requirements.txt b/requirements.txt index e51481e..067a3ec 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ numpy scipy matplotlib cvxopt +PyGithub \ No newline at end of file From ec607da81a6297b1e66498b711164bf2ae100bd4 Mon Sep 17 00:00:00 2001 From: parteekcoder Date: Sat, 17 Jun 2023 22:10:21 +0530 Subject: [PATCH 2/8] workflow --- .github/workflows/pull_request.yml | 31 ++++++++++++++++++++++++++++++ contribute.py | 15 +++++++-------- 2 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/pull_request.yml diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml new file mode 100644 index 0000000..511a37a --- /dev/null +++ b/.github/workflows/pull_request.yml @@ -0,0 +1,31 @@ +name: Automatic Pull request + +on: + workflow_dispatch: + inputs: + title: + description: 'title of example' + required: true + body: + description: 'description of example' + required: true + upstreamRepo: + description: 'Upstream repo' + required: true + botRepo: + description: 'bot repo' + required: true + repo: + description: 'repo name' + required: true + +jobs: + create-pull-request: + runs-on: ubuntu-latest + + steps: + - name: Create PR + run: | + gh pr create --repo ${{ github.event.inputs.upstreamRepo }}/${{ github.event.inputs.repo }} --head ${{ github.event.inputs.botRepo }}:${{ github.ref }} --base main --title "${{ github.event.inputs.title }}" --body "${{ github.event.inputs.body }}" + env: + GITHUB_TOKEN: ${{ secrets.token }} diff --git a/contribute.py b/contribute.py index 912df0d..2ce6ccf 100644 --- a/contribute.py +++ b/contribute.py @@ -4,19 +4,16 @@ # Intializing the Variables # Hashed token -BOT_TOKEN = 'Z2l0aHViX3BhdF8xMUFYM1pBT1kwR0lTTm5jWFlObXdhX3hQa2xyOVdSZ0ZMRURGcERFNFBzbW9zZFFUTzZaNTBBZkcwY2dYSVNKZ2FCSUtNWUo3SGpXTnlxTFda' -REPO_NAME = 'concore' #repo-name -OWNER_NAME = 'parteekcoder' #bot +BOT_TOKEN = 'Z2l0aHViX3BhdF8xMUFYS0pGVFkwUWVWZ3AzbkpkWk8yX3BOc1VncDFIVDMwZVNXcHhBNm9acHhMaGZGdU5CdE85TGpqdXF1UWRRNzI2S01aUk5HRUNGanFWNDZi' +REPO_NAME = 'concore' #repo name +OWNER_NAME = 'parteekcoder123' #bot account name STUDY_NAME = sys.argv[1] STUDY_NAME_PATH = sys.argv[2] AUTHOR_NAME = sys.argv[3] BRANCH_NAME = sys.argv[4] PR_TITLE = sys.argv[5] PR_BODY = sys.argv[6] - -# if author name has spaces -DIR_PATH = AUTHOR_NAME + '_' + STUDY_NAME -UPSTREAM_OWNER = 'parteekcoder123' +UPSTREAM_OWNER = 'parteekcoder' # upstream to which examples should be contributed # Defining Functions @@ -80,7 +77,7 @@ def fetchUpstream(repo,base_sha,branch): def runWorkflow(repo,upstream_repo): openPR = anyOpenPR(upstream_repo) if openPR==None: - workflow_runned = repo.get_workflow(id_or_name="pull_request.yml").create_dispatch(ref=BRANCH_NAME,inputs={'title':PR_TITLE,'body':PR_BODY}) + 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}) if not workflow_runned: print("Some Error Occured.Please try after some time") exit(0) @@ -133,6 +130,8 @@ def decode_token(encoded_token): PR_TITLE="Contributing Study "+AUTHOR_NAME+" "+STUDY_NAME if PR_BODY=="#": PR_BODY="Study Contributed by "+ AUTHOR_NAME + AUTHOR_NAME = AUTHOR_NAME.replace(" ","_") + DIR_PATH = AUTHOR_NAME + '_' + STUDY_NAME g = Github(decode_token(BOT_TOKEN)) repo = g.get_user(OWNER_NAME).get_repo(REPO_NAME) upstream_repo = g.get_repo(f'{UPSTREAM_OWNER}/{REPO_NAME}') #controlcore-Project/concore From ad479358e2f96ce0de6c77ff39c41cce950f2a9b Mon Sep 17 00:00:00 2001 From: Parteek Goyal Date: Thu, 29 Jun 2023 19:17:42 +0530 Subject: [PATCH 3/8] improvements in contribute feature --- .github/workflows/pull_request.yml | 31 ------------------------------ contribute | 10 +++++----- contribute.py | 19 +++++++++--------- fri/requirements.txt | 1 + fri/server/main.py | 6 ++++-- 5 files changed, 19 insertions(+), 48 deletions(-) delete mode 100644 .github/workflows/pull_request.yml mode change 100644 => 100755 contribute diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml deleted file mode 100644 index 511a37a..0000000 --- a/.github/workflows/pull_request.yml +++ /dev/null @@ -1,31 +0,0 @@ -name: Automatic Pull request - -on: - workflow_dispatch: - inputs: - title: - description: 'title of example' - required: true - body: - description: 'description of example' - required: true - upstreamRepo: - description: 'Upstream repo' - required: true - botRepo: - description: 'bot repo' - required: true - repo: - description: 'repo name' - required: true - -jobs: - create-pull-request: - runs-on: ubuntu-latest - - steps: - - name: Create PR - run: | - gh pr create --repo ${{ github.event.inputs.upstreamRepo }}/${{ github.event.inputs.repo }} --head ${{ github.event.inputs.botRepo }}:${{ github.ref }} --base main --title "${{ github.event.inputs.title }}" --body "${{ github.event.inputs.body }}" - env: - GITHUB_TOKEN: ${{ secrets.token }} diff --git a/contribute b/contribute old mode 100644 new mode 100755 index 4fc4fbb..5870b9e --- a/contribute +++ b/contribute @@ -1,7 +1,7 @@ #!/bin/bash # Check if the first 3 arguments are provided -if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then +if [ -z $1 ] || [ -z $2 ] || [ -z $3 ]; then echo "Error: The first 3 arguments are mandatory." echo "Usage: ./contribute " exit 1 @@ -11,13 +11,13 @@ fi arg4=$4 arg5=$5 arg6=$6 -if [ -z "$4" ]; then +if [ -z $4 ]; then arg4="#" fi -if [ -z "$5" ]; then +if [ -z $5 ]; then arg5="#" fi -if [ -z "$6" ]; then +if [ -z $6 ]; then arg6="#" fi -python contribute.py $1 $2 $3 $arg4 $arg5 $arg6 +python3 contribute.py $1 $2 $3 $arg4 $arg5 $arg6 diff --git a/contribute.py b/contribute.py index 2ce6ccf..39dc952 100644 --- a/contribute.py +++ b/contribute.py @@ -4,8 +4,8 @@ # Intializing the Variables # Hashed token -BOT_TOKEN = 'Z2l0aHViX3BhdF8xMUFYS0pGVFkwUWVWZ3AzbkpkWk8yX3BOc1VncDFIVDMwZVNXcHhBNm9acHhMaGZGdU5CdE85TGpqdXF1UWRRNzI2S01aUk5HRUNGanFWNDZi' -REPO_NAME = 'concore' #repo name +BOT_TOKEN = 'Z2l0aHViX3BhdF8xMUFYS0pGVFkwd2xwT0dmYldFOTBBXzN3Nkx2THpiaUFKek5pTDdqNlpLUzVwUUpoTlJWR3dtNnM0NWNDa0RmWTJaTTZLSUpHRHhERlhrZlJS' +REPO_NAME = 'concore-studies' #repo name OWNER_NAME = 'parteekcoder123' #bot account name STUDY_NAME = sys.argv[1] STUDY_NAME_PATH = sys.argv[2] @@ -13,8 +13,7 @@ BRANCH_NAME = sys.argv[4] PR_TITLE = sys.argv[5] PR_BODY = sys.argv[6] -UPSTREAM_OWNER = 'parteekcoder' # upstream to which examples should be contributed - +UPSTREAM_OWNER = 'ControlCore-Project' # upstream to which examples should be contributed # Defining Functions def checkInputValidity(): @@ -77,9 +76,9 @@ def fetchUpstream(repo,base_sha,branch): def runWorkflow(repo,upstream_repo): openPR = anyOpenPR(upstream_repo) if openPR==None: - 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}) + 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_OWNER,'botRepo':OWNER_NAME,'repo':REPO_NAME}) if not workflow_runned: - print("Some Error Occured.Please try after some time") + print("Some error occured.Please try after some time") exit(0) else: printPRStatus(upstream_repo) @@ -105,10 +104,10 @@ def isImageFile(filename): return file_extension.lower() in image_extensions # Encode Github Token -def encode_token(token): - encoded_bytes = base64.b64encode(token.encode('utf-8')) - encoded_token = encoded_bytes.decode('utf-8') - return encoded_token +# def encode_token(token): +# encoded_bytes = base64.b64encode(token.encode('utf-8')) +# encoded_token = encoded_bytes.decode('utf-8') +# return encoded_token # Decode Github Token diff --git a/fri/requirements.txt b/fri/requirements.txt index 8268e17..2c7516d 100644 --- a/fri/requirements.txt +++ b/fri/requirements.txt @@ -2,3 +2,4 @@ Flask gunicorn==20.1.0 FLASK_CORS jupyterlab +PyGithub \ No newline at end of file diff --git a/fri/server/main.py b/fri/server/main.py index 8a1014d..2e1d0e1 100644 --- a/fri/server/main.py +++ b/fri/server/main.py @@ -182,7 +182,10 @@ def contribute(): if(platform.uname()[0]=='Windows'): proc=check_output(["contribute",STUDY_NAME,STUDY_NAME_PATH,AUTHOR_NAME,BRANCH_NAME,PR_TITLE,PR_BODY],cwd=concore_path,shell=True) else: - proc = check_output(["./contribute",STUDY_NAME,STUDY_NAME_PATH,AUTHOR_NAME,BRANCH_NAME,PR_TITLE,PR_BODY],cwd=concore_path) + if len(BRANCH_NAME)==0: + proc = check_output(["./contribute",STUDY_NAME,STUDY_NAME_PATH,AUTHOR_NAME],cwd=concore_path) + else: + proc = check_output(["./contribute",STUDY_NAME,STUDY_NAME_PATH,AUTHOR_NAME,BRANCH_NAME,PR_TITLE,PR_BODY],cwd=concore_path) output_string = proc.decode() status=200 if output_string.find("/pulls/")!=-1: @@ -193,7 +196,6 @@ def contribute(): status=400 return jsonify({'message': output_string}),status except Exception as e: - print(e) output_string = "Some Error occured.Please try after some time" status=501 return jsonify({'message': output_string}),status From 96c8f99b4fa163ffaa70cc41fce9950cc20f92cc Mon Sep 17 00:00:00 2001 From: Parteek Goyal Date: Fri, 30 Jun 2023 23:17:50 +0530 Subject: [PATCH 4/8] Update contribute.py --- contribute.py | 38 +++++++++++--------------------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/contribute.py b/contribute.py index 39dc952..59a108b 100644 --- a/contribute.py +++ b/contribute.py @@ -5,15 +5,15 @@ # Intializing the Variables # Hashed token BOT_TOKEN = 'Z2l0aHViX3BhdF8xMUFYS0pGVFkwd2xwT0dmYldFOTBBXzN3Nkx2THpiaUFKek5pTDdqNlpLUzVwUUpoTlJWR3dtNnM0NWNDa0RmWTJaTTZLSUpHRHhERlhrZlJS' -REPO_NAME = 'concore-studies' #repo name -OWNER_NAME = 'parteekcoder123' #bot account name +BOT_REPO_NAME = 'concore-studies-staging' #bot repo name +UPSTREAM_REPO_NAME = 'concore-studies' #bot repo name +OWNER_NAME = 'ControlCore-Project' #account name STUDY_NAME = sys.argv[1] STUDY_NAME_PATH = sys.argv[2] AUTHOR_NAME = sys.argv[3] BRANCH_NAME = sys.argv[4] PR_TITLE = sys.argv[5] PR_BODY = sys.argv[6] -UPSTREAM_OWNER = 'ControlCore-Project' # upstream to which examples should be contributed # Defining Functions def checkInputValidity(): @@ -33,7 +33,7 @@ def getPRs(upstream_repo): exit(0) def printPR(pr): - print(f'Check your example here https://github.com/{UPSTREAM_OWNER}/pulls/'+str(pr.number),end="") + print(f'Check your example here https://github.com/{OWNER_NAME}/{UPSTREAM_REPO_NAME}/pulls/'+str(pr.number),end="") def anyOpenPR(upstream_repo): pr = getPRs(upstream_repo) @@ -44,7 +44,7 @@ def anyOpenPR(upstream_repo): break return openPr -def commitAndUpdateRef(upstream_repo,repo,tree_content,commit,branch): +def commitAndUpdateRef(repo,tree_content,commit,branch): try: new_tree = repo.create_git_tree(tree=tree_content,base_tree=commit.commit.tree) new_commit = repo.create_git_commit("commit message",new_tree,[commit.commit]) @@ -63,20 +63,10 @@ def appendBlobInTree(repo,content,file_path,tree_content): tree_content.append( github.InputGitTreeElement(path=file_path,mode="100644",type="blob",sha=blob.sha)) -def fetchUpstream(repo,base_sha,branch): - try: - result = repo.compare(base=base_sha,head=branch.commit.commit.sha) - if result.behind_by>0: - ref = repo.get_git_ref("heads/"+branch.name) - ref.edit(base_sha) - except Exception as e: - exit(0) - - def runWorkflow(repo,upstream_repo): openPR = anyOpenPR(upstream_repo) if openPR==None: - 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_OWNER,'botRepo':OWNER_NAME,'repo':REPO_NAME}) + 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}) if not workflow_runned: print("Some error occured.Please try after some time") exit(0) @@ -103,12 +93,6 @@ def isImageFile(filename): _, file_extension = os.path.splitext(filename) return file_extension.lower() in image_extensions -# Encode Github Token -# def encode_token(token): -# encoded_bytes = base64.b64encode(token.encode('utf-8')) -# encoded_token = encoded_bytes.decode('utf-8') -# return encoded_token - # Decode Github Token def decode_token(encoded_token): @@ -126,14 +110,14 @@ def decode_token(encoded_token): if BRANCH_NAME=="#": BRANCH_NAME=AUTHOR_NAME+"_"+STUDY_NAME if PR_TITLE=="#": - PR_TITLE="Contributing Study "+AUTHOR_NAME+" "+STUDY_NAME + PR_TITLE=f"Contributing Study {STUDY_NAME} by {AUTHOR_NAME}" if PR_BODY=="#": - PR_BODY="Study Contributed by "+ AUTHOR_NAME + PR_BODY=f"Study Name: {STUDY_NAME} \n Author Name: {AUTHOR_NAME}" AUTHOR_NAME = AUTHOR_NAME.replace(" ","_") DIR_PATH = AUTHOR_NAME + '_' + STUDY_NAME g = Github(decode_token(BOT_TOKEN)) - repo = g.get_user(OWNER_NAME).get_repo(REPO_NAME) - upstream_repo = g.get_repo(f'{UPSTREAM_OWNER}/{REPO_NAME}') #controlcore-Project/concore + repo = g.get_user(OWNER_NAME).get_repo(BOT_REPO_NAME) + upstream_repo = g.get_repo(f'{OWNER_NAME}/{UPSTREAM_REPO_NAME}') #controlcore-Project/concore base_ref = upstream_repo.get_branch(repo.default_branch) branches = repo.get_branches() BRANCH_NAME = BRANCH_NAME.replace(" ","_") @@ -172,7 +156,7 @@ def decode_token(encoded_token): file_path = f'{DIR_PATH+path.removeprefix(STUDY_NAME_PATH)}' if(platform.uname()[0]=='Windows'): file_path=file_path.replace("\\","/") appendBlobInTree(repo,content,file_path,tree_content) - commitAndUpdateRef(upstream_repo,repo,tree_content,base_ref.commit,branch) + commitAndUpdateRef(repo,tree_content,base_ref.commit,branch) runWorkflow(repo,upstream_repo) except Exception as e: print("Some error Occured.Please try again after some time.",end="") From fda45832eef30c3765671210bbcfbf01b690b302 Mon Sep 17 00:00:00 2001 From: Parteek Goyal Date: Thu, 13 Jul 2023 12:16:01 +0530 Subject: [PATCH 5/8] optimize --- contribute.py | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/contribute.py b/contribute.py index 59a108b..dc09b3c 100644 --- a/contribute.py +++ b/contribute.py @@ -1,13 +1,13 @@ import github from github import Github -import os,sys,time,platform,base64 +import os,sys,platform,base64 # Intializing the Variables # Hashed token -BOT_TOKEN = 'Z2l0aHViX3BhdF8xMUFYS0pGVFkwd2xwT0dmYldFOTBBXzN3Nkx2THpiaUFKek5pTDdqNlpLUzVwUUpoTlJWR3dtNnM0NWNDa0RmWTJaTTZLSUpHRHhERlhrZlJS' -BOT_REPO_NAME = 'concore-studies-staging' #bot repo name -UPSTREAM_REPO_NAME = 'concore-studies' #bot repo name -OWNER_NAME = 'ControlCore-Project' #account name +BOT_TOKEN = "Z2l0aHViX3BhdF8xMUFYS0pGVFkweWpsZUNKUk1XSU5MXzczekY5RjJjaERocGFvNWE1eGlWRmZZU2tMVWtyUjAwRHVKMXo1Q0Z5aXoyWVZYRkNCUDBYOUlkZHU1" +BOT_ACCOUNT = 'parteekcoder123' #bot repo name +REPO_NAME = 'concore-studies' #bot repo name +UPSTREAM_ACCOUNT = 'ControlCore-Project' #account name STUDY_NAME = sys.argv[1] STUDY_NAME_PATH = sys.argv[2] AUTHOR_NAME = sys.argv[3] @@ -27,13 +27,13 @@ def checkInputValidity(): def getPRs(upstream_repo): try: - return upstream_repo.get_pulls(head=f'{OWNER_NAME}:{BRANCH_NAME}') + return upstream_repo.get_pulls(head=f'{BOT_ACCOUNT}:{BRANCH_NAME}') except Exception as e: print("Not able to fetch Status of your example.Please try after some time.") exit(0) def printPR(pr): - print(f'Check your example here https://github.com/{OWNER_NAME}/{UPSTREAM_REPO_NAME}/pulls/'+str(pr.number),end="") + print(f'Check your example here https://github.com/{UPSTREAM_ACCOUNT}/{REPO_NAME}/pulls/{pr.number}',end="") def anyOpenPR(upstream_repo): pr = getPRs(upstream_repo) @@ -66,7 +66,7 @@ def appendBlobInTree(repo,content,file_path,tree_content): def runWorkflow(repo,upstream_repo): openPR = anyOpenPR(upstream_repo) if openPR==None: - 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}) + 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}) if not workflow_runned: print("Some error occured.Please try after some time") exit(0) @@ -78,20 +78,21 @@ def runWorkflow(repo,upstream_repo): def printPRStatus(upstream_repo): try: - time.sleep(15) - openPR = anyOpenPR(upstream_repo) - if openPR==None: - print("Someting went wrong or your example already exist.If this is not the case try with different fields") - exit(0) - printPR(openPR) + issues = upstream_repo.get_issues() + pulls = upstream_repo.get_pulls(state='all') + max_num = -1 + for i in issues: + max_num = max(max_num,i.number) + for i in pulls: + max_num = max(max_num,i.number) + print(f'Check your example here https://github.com/{UPSTREAM_ACCOUNT}/{REPO_NAME}/pulls/{max_num+1}',end="") except Exception as e: print("Your example successfully uploaded but unable to fetch status.Please try again") def isImageFile(filename): - image_extensions = ['.jpeg', '.jpg', '.png'] - _, file_extension = os.path.splitext(filename) - return file_extension.lower() in image_extensions + image_extensions = ['.jpeg', '.jpg', '.png','.gif'] + return any(filename.endswith(ext) for ext in image_extensions) # Decode Github Token @@ -114,16 +115,16 @@ def decode_token(encoded_token): if PR_BODY=="#": PR_BODY=f"Study Name: {STUDY_NAME} \n Author Name: {AUTHOR_NAME}" AUTHOR_NAME = AUTHOR_NAME.replace(" ","_") - DIR_PATH = AUTHOR_NAME + '_' + STUDY_NAME + DIR_PATH = STUDY_NAME g = Github(decode_token(BOT_TOKEN)) - repo = g.get_user(OWNER_NAME).get_repo(BOT_REPO_NAME) - upstream_repo = g.get_repo(f'{OWNER_NAME}/{UPSTREAM_REPO_NAME}') #controlcore-Project/concore + repo = g.get_user(BOT_ACCOUNT).get_repo(REPO_NAME) + upstream_repo = g.get_repo(f'{UPSTREAM_ACCOUNT}/{REPO_NAME}') #controlcore-Project/concore-studies base_ref = upstream_repo.get_branch(repo.default_branch) branches = repo.get_branches() BRANCH_NAME = BRANCH_NAME.replace(" ","_") DIR_PATH = DIR_PATH.replace(" ","_") is_present = any(branch.name == BRANCH_NAME for branch in branches) -except: +except Exception as e: print("Some error occured.Authentication failed",end="") exit(0) @@ -145,7 +146,7 @@ def decode_token(encoded_token): try: for root, dirs, files in os.walk(STUDY_NAME_PATH): for filename in files: - path = os.path.join(root, filename) + path = f"{root}/{filename}" if isImageFile(filename): with open(path, 'rb') as file: image = file.read() From a5fbf24c5b674a06fb25937c5e31e510f2c873ee Mon Sep 17 00:00:00 2001 From: Parteek Goyal Date: Mon, 17 Jul 2023 16:47:32 +0530 Subject: [PATCH 6/8] Update contribute.py --- contribute.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/contribute.py b/contribute.py index dc09b3c..cb61eab 100644 --- a/contribute.py +++ b/contribute.py @@ -4,10 +4,10 @@ # Intializing the Variables # Hashed token -BOT_TOKEN = "Z2l0aHViX3BhdF8xMUFYS0pGVFkweWpsZUNKUk1XSU5MXzczekY5RjJjaERocGFvNWE1eGlWRmZZU2tMVWtyUjAwRHVKMXo1Q0Z5aXoyWVZYRkNCUDBYOUlkZHU1" -BOT_ACCOUNT = 'parteekcoder123' #bot repo name -REPO_NAME = 'concore-studies' #bot repo name -UPSTREAM_ACCOUNT = 'ControlCore-Project' #account name +BOT_TOKEN = "Z2l0aHViX3BhdF8xMUFYS0pGVFkwODR5OEhoZlI5VEl1X0VZZnNaNjU0WGw4OU0ycXhJc0h3TXh3RkVIZGFRQ3gwa0daZFhKUUdYbUk2QzRTU1dDNkF4clUyQWRF" +BOT_ACCOUNT = 'concore-bot' #bot account name +REPO_NAME = 'concore-studies' #study repo name +UPSTREAM_ACCOUNT = 'ControlCore-Project' #upstream account name STUDY_NAME = sys.argv[1] STUDY_NAME_PATH = sys.argv[2] AUTHOR_NAME = sys.argv[3] From 9ccd3488e6bdfe8cd326d001392ae335207e7003 Mon Sep 17 00:00:00 2001 From: Parteek Goyal Date: Thu, 20 Jul 2023 11:08:01 +0530 Subject: [PATCH 7/8] Fixed issue of not updating title and body by user --- contribute | 2 +- contribute.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/contribute b/contribute index 5870b9e..265c4eb 100755 --- a/contribute +++ b/contribute @@ -20,4 +20,4 @@ fi if [ -z $6 ]; then arg6="#" fi -python3 contribute.py $1 $2 $3 $arg4 $arg5 $arg6 +python3 contribute.py "$1" "$2" "$3" "$arg4" "$arg5" "$arg6" diff --git a/contribute.py b/contribute.py index cb61eab..19ee2e4 100644 --- a/contribute.py +++ b/contribute.py @@ -47,7 +47,7 @@ def anyOpenPR(upstream_repo): def commitAndUpdateRef(repo,tree_content,commit,branch): try: new_tree = repo.create_git_tree(tree=tree_content,base_tree=commit.commit.tree) - new_commit = repo.create_git_commit("commit message",new_tree,[commit.commit]) + new_commit = repo.create_git_commit("commit study",new_tree,[commit.commit]) if len(repo.compare(base=commit.commit.sha,head=new_commit.sha).files) == 0: 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.") exit(0) From 1c3f8b82487710a49c64edc3726d5d159573f1d1 Mon Sep 17 00:00:00 2001 From: parteekcoder Date: Thu, 20 Jul 2023 06:03:00 +0530 Subject: [PATCH 8/8] fixing title and body issue in windows --- contribute.bat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contribute.bat b/contribute.bat index 4ffae56..ae0014c 100644 --- a/contribute.bat +++ b/contribute.bat @@ -9,7 +9,7 @@ REM Check and set default values for the last three arguments if not provided if "%~4"=="" (set arg4="#") else (set arg4=%~4) if "%~5"=="" (set arg5="#") else (set arg5=%~5) if "%~6"=="" (set arg6="#") else (set arg6=%~6) -python contribute.py %1 %2 %3 %arg4% %arg5% %arg6% +python contribute.py %1 %2 %3 "%arg4%" "%arg5%" "%arg6%" goto :eof :missing_arg