From 277ce5992cc59da376d7a89235185296b6f6c398 Mon Sep 17 00:00:00 2001 From: Tushar <30565750+tushar5526@users.noreply.github.com> Date: Sun, 31 Dec 2023 03:36:54 +0530 Subject: [PATCH] comment test --- action/action.yml | 11 +++++++--- action/main.py | 45 +++++++++++++++++------------------------ action/requirements.txt | 2 +- action/utils.py | 40 ++++++++++++++++++++++++------------ 4 files changed, 55 insertions(+), 43 deletions(-) diff --git a/action/action.yml b/action/action.yml index c420ad8..d8bc983 100644 --- a/action/action.yml +++ b/action/action.yml @@ -13,11 +13,16 @@ runs: inputs: sarthi_server_url: required: true - description: URL of the deployed sarthi server + description: 'URL of the deployed sarthi server' compose_file: - required: false - description: Name of docker compose file in your project + description: 'Name of docker compose file in your project' default: docker-compose.yml sarthi_secret: required: true description: Secret text for authentication + fork_repo_url: + description: 'GitHub URL of Head Repo when a PR is created, default empty for push events' + default: ${{github.event.pull_request.head.repo.full_name}} + GITHUB_TOKEN: + description: 'Github token of the repository (automatically created by Github)' + default: ${{ github.token }} diff --git a/action/main.py b/action/main.py index 6beed0a..4a1eea3 100644 --- a/action/main.py +++ b/action/main.py @@ -1,34 +1,27 @@ import os -import sys -import typing +from utils import deploy, comment_on_gh_pr -def main(args: typing.List[str]) -> None: - """main function - Args: - args: STDIN arguments - """ - import os +def main() -> None: + event_name = os.environ.get("GITHUB_EVENT_NAME") + project_url = f"https://github.com/{os.environ.get('INPUT_FORK_REPO_URL') or os.environ.get('GITHUB_REPOSITORY')}.git" + branch_name = ( + os.environ.get("GITHUB_HEAD_REF") + if event_name == "pull_request" + else os.environ.get("GITHUB_REF_NAME") + ) + sarthi_secret = os.environ.get("INPUT_SARTHI_SECRET") + sarthi_server_url = os.environ.get("INPUT_SARTHI_SERVER_URL") - # Get all environment variables - env_vars = os.environ - - # Print each environment variable - for key, value in env_vars.items(): - print(f'{key}: {value}') - - # GITHUB_REPOSITORY : octocat/Hello-World - project_name = os.environ.get("GITHUB_REPOSITORY").split("/")[1] - branch_name = os.environ.get("GITHUB_HEAD_REF") - username = os.environ.get("INPUT_REMOTE_USER") - password = os.environ.get("INPUT_REMOTE_PASSWORD") - host = os.environ.get("INPUT_REMOTE_HOST") - port = os.environ.get("INPUT_PORT") or 22 - deployment_domain = os.environ.get("INPUT_DEPLOYMENT_DOMAIN") - github_token = os.environ.get("GITHUB_TOKEN") - sarthi_secret = os.environ.get('INPUT_SECRET') + # service_urls = deploy( + # project_git_url=project_url, + # branch=branch_name, + # sarthi_secret=sarthi_secret, + # sarthi_server_url=sarthi_server_url, + # ) + comment_on_gh_pr("Hey boooyy") if __name__ == "__main__": - main(sys.argv) + main() diff --git a/action/requirements.txt b/action/requirements.txt index 32469f6..b126a46 100644 --- a/action/requirements.txt +++ b/action/requirements.txt @@ -1,2 +1,2 @@ requests -pyjwt \ No newline at end of file +pyjwt diff --git a/action/utils.py b/action/utils.py index 72e5db1..bc2c23a 100644 --- a/action/utils.py +++ b/action/utils.py @@ -1,24 +1,38 @@ -import requests -import os +import datetime import json +import os import jwt -import datetime - -GITHUB_API_URL = os.environ.get('GITHUB_API_URL') +import requests def comment_on_gh_pr(comment): - pass + GITHUB_API_URL = os.environ.get("GITHUB_API_URL") + GITHUB_TOKEN = os.environ.get('INPUT_GITHUB_TOKEN') + + pr_number = os.environ.get("GITHUB_REF_NAME").split('/')[0] + + url = f"{GITHUB_API_URL}/repos/{os.environ.get('GITHUB_REPOSITORY')}/issues/{pr_number}/comments" + + headers = { + "Authorization": f"Bearer {GITHUB_TOKEN}", + "Accept": "application/vnd.github.v3+json", + } + + data = {"body": comment} + + response = requests.post(url, headers=headers, json=data) + response.raise_for_status() def _generate_bearer_token(secret) -> str: payload = { - 'sub': 'sarthi', - 'iat': datetime.datetime.utcnow(), # Issued at time - 'exp': datetime.datetime.utcnow() + datetime.timedelta(minutes=1) # Expiration time + "sub": "sarthi", + "iat": datetime.datetime.utcnow(), # Issued at time + "exp": datetime.datetime.utcnow() + + datetime.timedelta(minutes=1), # Expiration time } - token = jwt.encode(payload, secret, algorithm='HS256') + token = jwt.encode(payload, secret, algorithm="HS256") return f'Bearer {token.decode("utf-8")}' @@ -28,9 +42,9 @@ def deploy(project_git_url, branch, sarthi_secret, sarthi_server_url): "project_git_url": project_git_url, "branch": branch, } - response = requests.post(url=sarthi_server_url, headers=headers, data=json.dumps(body)) + response = requests.post( + url=sarthi_server_url, headers=headers, data=json.dumps(body) + ) response.raise_for_status() service_urls = response.json() return service_urls - -