Skip to content

Commit

Permalink
Merge pull request #25 from stakater/add-providers
Browse files Browse the repository at this point in the history
Add providers
  • Loading branch information
rasheedamir authored Aug 9, 2023
2 parents fff2965 + bdfc17d commit d9f1b5d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
4 changes: 4 additions & 0 deletions scripts/python/fetch-params/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Fetch Params

The fetch params python script uses github, gitlab, and bitbucket apis to fetch required information about a commit such as the PR number.
Use this in a tekton task to get values needed for running a pipeline using pipeline as cide
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,6 @@ def fetch_params_bitbucket(provider, username, password, hash, workspace, reposi
if commit['hash'] == hash:
print(f"Found hash in PR {pull_request_id}")
found = True
return pull_request_id
break

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("provider", help="Name of git provider")
parser.add_argument("username", help="Username for git provider")
parser.add_argument("password", help="Password/Token for provider")
parser.add_argument("hash", help="Hash of the commit that triggered the pipeline")
parser.add_argument("workspace", help="Workspace/Organization")
parser.add_argument("repository", help="Git repository name")
args = parser.parse_args()
fetch_params_bitbucket(args.provider, args.username, args.password, args.hash, args.workspace, args.repository)
return None
38 changes: 38 additions & 0 deletions scripts/python/fetch-params/fetch_params_github.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import json
import requests
from requests.auth import HTTPBasicAuth

def send_api_request_github(url, password):

try:
headers = {"Authorization": f"token {password}"}
response = requests.get(url, headers=headers)
if response.status_code == 200:
parsed_data = json.loads(response.text)
return parsed_data
else:
print(f"Request failed with status code: {response.status_code}")
print(f"Response content: {response.text}")
return None
except requests.exceptions.RequestException as e:
print(f"Error occurred during the API request: {e}")
return None

def fetch_params_github(provider, username, password, hash, workspace, repository):

if provider == "github":
url = f"https://github.com/repos/{workspace}/{repository}/pulls"
print(url)
pull_requests = send_api_request (url, password)
if pull_requests:
for pr in pull_requests:
pull_request_id = pr['number']
url = f"https://github.com/repos/{workspace}/{repository}/pulls/{pull_request_id}/commits"
commits = send_api_request(url,"", password, "github")
if commits:
for commit in commits:
print(f"Commit SHA: {commit['sha']}")
if commit['sha'] == hash:
print(f"Found hash in PR {pull_request_id}")
found = True
break
20 changes: 20 additions & 0 deletions scripts/python/fetch-params/find_hash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from fetch_params_bitbucket import fetch_params_bitbucket
from fetch_params_github import fetch_params_github
import argparse

def find_hash(provider, username, password, hash, workspace, repository):
if provider == "bitbucket":
pr_number = fetch_params_bitbucket(provider, username, password, hash, workspace, repository)
elif provider == "github":
pr_number = fetch_params_github(provider, username, password, hash, workspace, repository)

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("provider", help="Name of git provider")
parser.add_argument("username", help="Username for git provider")
parser.add_argument("password", help="Password/Token for provider")
parser.add_argument("hash", help="Hash of the commit that triggered the pipeline")
parser.add_argument("workspace", help="Workspace/Organization")
parser.add_argument("repository", help="Git repository name")
args = parser.parse_args()
find_hash(args.provider, args.username, args.password, args.hash, args.workspace, args.repository)

0 comments on commit d9f1b5d

Please sign in to comment.