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..59a108b 100644 --- a/contribute.py +++ b/contribute.py @@ -4,17 +4,16 @@ # Intializing the Variables # Hashed token -BOT_TOKEN = 'Z2l0aHViX3BhdF8xMUFYS0pGVFkwUWVWZ3AzbkpkWk8yX3BOc1VncDFIVDMwZVNXcHhBNm9acHhMaGZGdU5CdE85TGpqdXF1UWRRNzI2S01aUk5HRUNGanFWNDZi' -REPO_NAME = 'concore' #repo name -OWNER_NAME = 'parteekcoder123' #bot account name +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 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 = 'parteekcoder' # upstream to which examples should be contributed - # Defining Functions def checkInputValidity(): @@ -34,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) @@ -45,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]) @@ -64,22 +63,12 @@ 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_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_REPO_NAME,'account':OWNER_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) @@ -104,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): @@ -127,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(" ","_") @@ -173,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="") 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