diff --git a/action.yml b/action.yml index f4d010c..6aa0f7b 100644 --- a/action.yml +++ b/action.yml @@ -14,6 +14,10 @@ inputs: description: 'directory to save the digest setting file, defaults to .github/digests' required: false default: ".github/digests" + timezone: + description: 'Timezone to use for the digest, defaults to UTC' + required: false + default: "UTC" branding: icon: 'align-justify' @@ -40,6 +44,7 @@ runs: GIT_SECRET: ${{ inputs.secret }} GIT_REPO: ${{ inputs.repo || env.GITHUB_REPOSITORY }} DIGEST_SAVE_DIR: ${{ inputs.save }} + TIMEZONE: ${{ inputs.timezone }} run: | python ${{ github.action_path }}/app.py shell: bash diff --git a/digest_manager.py b/digest_manager.py index 5b3c94f..6b1ef23 100644 --- a/digest_manager.py +++ b/digest_manager.py @@ -36,7 +36,7 @@ def __init__(self, target_repo:str, local_repo:str, target_issue:str, ignore_num self.target_issue = target_issue self.complete = False self.ignore_numbers = ignore_numbers - self.last_update_time = datetime.now() - timedelta(days=1) + self.last_update_time = helper.get_now() - timedelta(days=1) self.create_issue() self.update_last_change_date() @@ -45,7 +45,7 @@ def run_query(self, additional_queries: list[str] = []) -> dict: additional_queries.append( self.query.partial_query( self.target_repo, - self.last_update_time.strftime("%Y-%m-%dT%H:%M:%SZ"), + helper.format_to_utc(self.last_update_time), self.cursor) ) res = run_queries(additional_queries) @@ -66,7 +66,7 @@ def get_result(self) -> list[GitIssue]: ret[key].read_paginated_comments(ret[key].comments_query.read_result(res)) return [ret[key] for key in ret] - + def update_cursor(self, graphqlResult: dict): self.cursor = graphqlResult["endCursor"] self.complete = not graphqlResult["hasNextPage"] @@ -74,14 +74,14 @@ def update_cursor(self, graphqlResult: dict): def convert_data(self, graphqlResult: dict, ret: dict[str, GitIssue]): for raw_issue in graphqlResult: if (raw_issue and raw_issue["number"] not in self.ignore_numbers): - issue = GitIssue(raw_issue, (self.last_update_time, datetime.now())) + issue = GitIssue(raw_issue, (self.last_update_time, helper.get_now())) ret[issue.id] = issue def send_data(self, issues: list[GitIssue]): r1 = UpdateIssue("update_issue").partial_query(self.target_issue, digest_content) r2 = AddComment("new_digest").partial_query(self.target_issue, digest_header.format( - time_start=self.last_update_time.strftime("%Y-%m-%d %H:%M:%S"), - time_end=datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ"), + time_start=helper.format_local(self.last_update_time), + time_end=helper.format_local(helper.get_now()), all_changes=sum([issue.total_changes for issue in issues]), issues_changed=len(issues), body='\n'.join([issue.to_markdown() for issue in issues]) diff --git a/git_structures.py b/git_structures.py index 8847904..c076b17 100644 --- a/git_structures.py +++ b/git_structures.py @@ -59,7 +59,7 @@ def to_markdown(self): return comment_template.format( author=self.last_change_author, link=self.source_link, - date=helper.format_date(self.last_change_date), + date=helper.format_local(self.last_change_date), body=helper.trim_and_format(self.body), status=self.get_status_str(self.time_range) ) @@ -111,7 +111,7 @@ def to_markdown(self) -> str: temp = issue_template header += temp.format( author=self.last_change_author, - date=helper.format_date(self.last_change_date), + date=helper.format_local(self.last_change_date), status=self.get_status_str(self.time_range), body=helper.trim_and_format(self.body) ) diff --git a/gql_queries.py b/gql_queries.py index 769b6ab..f96a126 100644 --- a/gql_queries.py +++ b/gql_queries.py @@ -1,5 +1,6 @@ from os import environ from datetime import datetime +import sys import helper import requests from string import Template @@ -8,18 +9,36 @@ try: API_KEY = environ["GIT_SECRET"] except KeyError: - API_KEY = "Token not available!" + print("Token not available!", file=sys.stderr) + exit(1) url = "https://api.github.com/graphql" headers = { "Authorization": f"token {API_KEY}", } +def handle_errors(response: requests.Response) -> None: + """ + If query fails, print the error message and exit the program + """ + if response.status_code != 200: + print("Query failed to run by returning code of {}. {}".format(response.status_code, response.text), file=sys.stderr) + exit(1) + + data = response.json() + + if 'errors' in data: + errors = data["errors"] + for error in errors: + print("Error: {}".format(error["message"]), file=sys.stderr) + exit(1) + def run_queries(queries: list[str]) -> dict: payload = { "query": f"{{{','.join([q for q in queries])}}}" } response = requests.post(url, json=payload, headers=headers) + handle_errors(response) return response.json()["data"] def run_mutations(queries: list[str]) -> dict: @@ -28,6 +47,7 @@ def run_mutations(queries: list[str]) -> dict: } response = requests.post(url, json=payload, headers=headers) + handle_errors(response) return response.json()["data"] class GithubQuery: @@ -46,6 +66,7 @@ def run(self, **kwargs) -> dict: } response = requests.post(url, json=payload, headers=headers) + handle_errors(response) return response.json()["data"] def partial_query(self, **kwargs) -> str: diff --git a/helper.py b/helper.py index 7f0d427..ebba689 100644 --- a/helper.py +++ b/helper.py @@ -1,8 +1,21 @@ from datetime import datetime, timedelta +import pytz +from os import environ -convertToDateTime = lambda x: datetime.strptime(x, "%Y-%m-%dT%H:%M:%SZ") +utc = pytz.utc +localtz = pytz.timezone(environ["TIMEZONE"]) -format_date = lambda x: x.strftime("%Y-%m-%d %H:%M:%S") +get_now = lambda :datetime.now(utc) + +def convertToDateTime(x: str) -> datetime: + dt = datetime.strptime(x, "%Y-%m-%dT%H:%M:%SZ") + return dt.replace(tzinfo=utc) + +def format_local(dt: datetime) -> str: + return dt.astimezone(localtz).strftime("%Y-%m-%d %H:%M:%S") + +def format_to_utc(dt: datetime) -> str: + return dt.astimezone(utc).strftime("%Y-%m-%dT%H:%M:%SZ") def trim_and_format(x: str) -> str: if len(x) > 200: @@ -10,4 +23,4 @@ def trim_and_format(x: str) -> str: return ">" + x.strip().replace("\n", "\n> ") def get_n_day_prior(n: int) -> datetime: - return datetime.now() - timedelta(days=n) \ No newline at end of file + return datetime.now(utc) - timedelta(days=n) diff --git a/requirements.txt b/requirements.txt index e521d0c..d3e670a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ charset-normalizer==3.1.0 idna==3.4 requests==2.28.2 urllib3==1.26.15 +pytz==2023.3 \ No newline at end of file