commit ed2fad0bea71086fb18f3be97e202343f978c9ba Author: Deenath Geeganage Date: Thu Dec 8 21:22:15 2022 +0530 Add sample-script to the repo diff --git a/sample-script.py b/sample-script.py new file mode 100644 index 0000000..fd0df4b --- /dev/null +++ b/sample-script.py @@ -0,0 +1,122 @@ +from requests.auth import HTTPBasicAuth +from os import environ +import requests +import argparse +import json + +import logging + +# log configuration +# curretly set to info level out of (debug, info, warning, error, critical ) +logging.basicConfig( + # level=logging.DEBUG, + level=logging.INFO, + format="{asctime} {levelname:<8} {message}", + style='{', + filemode='a', + filename='sample-script.log') + +# res = requests.post('https://api.github.com/user', auth=HTTPBasicAuth('user', 'password')) + +# common variables +params = None +endpoint = "http://localhost:3000/api/v1/repos/" +username = "test" +password = "test123" +# + +# get values from the env variables +endpoint = environ.get('gitea_endpoint') +username = environ.get('gitea_user') +password = environ.get('gitea_pass') +# + +# tests the parameters passed + + +def initializeParams(): + global params + parser = argparse.ArgumentParser() + parser.add_argument("-u", "--user", help="user", required=True) + parser.add_argument("-r", "--repo", help="repository", required=True) + parser.add_argument("-i", "--prId", type=int, help="pr_id", required=True) + parser.add_argument("-b", "--body", type=str, help="message") + parser.add_argument("-e", "--event", action="store_true", + help="build status") + + args = parser.parse_args() + params = vars(args) +# + +# set the endpoint with values + + +def setEndpoint(user, repository, pr_id): + global endpoint + endpoint = endpoint+user+"/"+repository+"/pulls/"+pr_id+"/reviews" +# + +# initialize payload + + +def setPayload(event, message): + global payload + if (event): + eventInString = "APPROVED" + body = None + else: + eventInString = "REQUEST_CHANGES" + body = message + payload = { + "event": eventInString, + "body": body + } + payload = json.dumps(payload) +# + +# sends the API call + + +def sendRequest(): + global params + global endpoint + global payload + global username + global password + setEndpoint(params['user'], params['repo'], str(params['prId'])) + # print(endpoint) + setPayload(params['event'], params['body']) + # print(payload) + + # headers = { + # 'accept': 'application/json', + # 'Content-Type': 'application/json', + # 'authorization': 'Basic dGVzdDp0ZXN0MTIz' + # } + + headers = { + 'accept': 'application/json', + 'Content-Type': 'application/json' + } + # response = requests.post(url=endpoint, headers=headers, data=payload) + response = requests.post(url=endpoint, headers=headers, + data=payload, auth=HTTPBasicAuth(username, password)) + eventString = "Approved by Jenkins" if params['event'] else "Changes Requested by Jenkins" + if response.status_code == 200: + print("Request Succeeded") + logging.info("< Repository- "+params['user']+"/"+params['repo'] + + " > --- < Pull-request-Id - " + str(params['prId'])+" > " + eventString) + # logging.info("Repository - "+params['user']+"/"+params['repo']+" ---- Pull request ID - " + str(params['prId'])+" " + eventString) + else: + print("Request Failed") + logging.error("API request Failed") + logging.error("< Repository - "+params['user']+"/"+params['repo'] + + " >--- < Pull-request-Id - " + str(params['prId'])+" > " + eventString) + print(response) +# + + +initializeParams() +# print(params) +# print("--------------------------") +sendRequest()