Skip to content

Commit

Permalink
Fix datetime formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Eclipse-Dominator committed Jun 1, 2023
1 parent 3b02e5f commit 7dc87c2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down
23 changes: 22 additions & 1 deletion gql_queries.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from os import environ
from datetime import datetime
import sys
import helper
import requests
from string import Template
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down
14 changes: 11 additions & 3 deletions helper.py
Original file line number Diff line number Diff line change
@@ -1,13 +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")
def convertToDateTime(x: str) -> datetime:
dt = datetime.strptime(x, "%Y-%m-%dT%H:%M:%SZ")
return dt.replace(tzinfo=utc)

def format_date(dt: datetime) -> str:
return dt.astimezone(localtz).strftime("%Y-%m-%d %H:%M:%S")

def trim_and_format(x: str) -> str:
if len(x) > 200:
x = x[:200] + "..."
return ">" + x.strip().replace("\n", "\n> ")

def get_n_day_prior(n: int) -> datetime:
return datetime.now() - timedelta(days=n)
return datetime.now(utc) - timedelta(days=n)

0 comments on commit 7dc87c2

Please sign in to comment.