aboutsummaryrefslogtreecommitdiff
path: root/dev_status
diff options
context:
space:
mode:
authorBobby <[email protected]>2024-02-11 21:30:12 -0500
committerBobby <[email protected]>2024-02-11 21:30:12 -0500
commiteecd7db0d9caff2131a4d1142a2cf0063acda3a8 (patch)
tree10b498e81901a1d973fb1d8ed911e5d4c4f9fdf4 /dev_status
parent5074ea783da1971926436f7c670273cf6009df33 (diff)
downloadthatcomputerscientist-eecd7db0d9caff2131a4d1142a2cf0063acda3a8.tar.xz
thatcomputerscientist-eecd7db0d9caff2131a4d1142a2cf0063acda3a8.zip
Display commits on Repository View
Diffstat (limited to 'dev_status')
-rw-r--r--dev_status/utils.py25
-rw-r--r--dev_status/views.py139
2 files changed, 141 insertions, 23 deletions
diff --git a/dev_status/utils.py b/dev_status/utils.py
new file mode 100644
index 00000000..f271e91b
--- /dev/null
+++ b/dev_status/utils.py
@@ -0,0 +1,25 @@
+from datetime import datetime
+
+
+def relative_date(entry):
+ committedDate = datetime.strptime(
+ entry["commit"]["committedDate"], "%Y-%m-%dT%H:%M:%SZ"
+ )
+ now = datetime.now()
+ diff = now - committedDate
+ if diff.days > 365:
+ entry["commit"]["committedDate"] = str(diff.days // 365) + " years ago"
+ elif diff.days > 30:
+ entry["commit"]["committedDate"] = str(diff.days // 30) + " months ago"
+ elif diff.days > 7:
+ entry["commit"]["committedDate"] = str(diff.days // 7) + " weeks ago"
+ elif diff.days > 0:
+ entry["commit"]["committedDate"] = str(diff.days) + " days ago"
+ elif diff.seconds > 3600:
+ entry["commit"]["committedDate"] = str(diff.seconds // 3600) + " hours ago"
+ elif diff.seconds > 60:
+ entry["commit"]["committedDate"] = str(diff.seconds // 60) + " minutes ago"
+ else:
+ entry["commit"]["committedDate"] = "just now"
+
+ return entry
diff --git a/dev_status/views.py b/dev_status/views.py
index 97a3890c..b6329ee6 100644
--- a/dev_status/views.py
+++ b/dev_status/views.py
@@ -5,6 +5,7 @@ import requests
from django.shortcuts import render
from dotenv import load_dotenv
from github import Github
+from dev_status.utils import relative_date
load_dotenv()
g = Github(os.getenv("GH_TOKEN"))
@@ -65,7 +66,17 @@ def home(request):
)
data = requests.post(url, json={"query": query}, headers=headers).json()
repos = [
- {"name": repo["node"]["name"], "description": repo["node"]["description"], "forkCount": repo["node"]["forkCount"], "homepageUrl": repo["node"]["homepageUrl"], "isArchived": repo["node"]["isArchived"], "isFork": repo["node"]["isFork"], "licenseInfo": repo["node"]["licenseInfo"], "pushedAt": repo["node"]["pushedAt"], "stargazerCount": repo["node"]["stargazerCount"]}
+ {
+ "name": repo["node"]["name"],
+ "description": repo["node"]["description"],
+ "forkCount": repo["node"]["forkCount"],
+ "homepageUrl": repo["node"]["homepageUrl"],
+ "isArchived": repo["node"]["isArchived"],
+ "isFork": repo["node"]["isFork"],
+ "licenseInfo": repo["node"]["licenseInfo"],
+ "pushedAt": repo["node"]["pushedAt"],
+ "stargazerCount": repo["node"]["stargazerCount"],
+ }
for repo in data["data"]["user"]["repositories"]["edges"]
]
total_count = data["data"]["user"]["repositories"]["totalCount"]
@@ -74,7 +85,6 @@ def home(request):
for repo in repos:
repo["pushedAt"] = repo["pushedAt"].split("T")[0]
-
context["search"] = search
if search:
context["repos"] = [
@@ -92,6 +102,7 @@ def home(request):
context["page"] = int(page)
context["items"] = int(items)
context["sort"] = sort
+ context["title"] = "My Repositories" if not search else ": Search " + search
context["direction"] = direction
context["num_pages"] = math.ceil(context["total_count"] / context["items"])
context["repos"] = context["repos"][
@@ -101,26 +112,108 @@ def home(request):
return render(request, "dev_status/home.html", context)
-def get_repo(request, r="thatcomputerscientist", p=None):
- repository = "luciferreeves/{}".format(r)
- parent = None
- if p and not len(p.split("/")) == 0:
- parent = "/".join(p.split("/")[:-1])
- contents = g.get_repo(repository).get_contents(p or "")
- context = {}
+def get_repo(request, r=None, p=None):
+ # this function handles request for browsing the contents of a repository
+ # the path parameter is optional and is used to browse the contents of a directory
+ # if the path is not provided, the root directory is displayed
+ # if the path is a file, the file is displayed
+
+ r = r or "thatcomputerscientist" # repository.
+ p = p or "" # path.
+
+ url = "https://api.github.com/graphql"
+ headers = {"Authorization": "token " + os.getenv("GH_TOKEN")}
+ parent = "/".join(p.split("/")[:-1]) if p and not len(p.split("/")) == 0 else None
+ print(parent, p)
+
+ # get the contents of the repository along with the latest commit associated with each file or directory
+ query = """
+ query {{
+ repository(owner: "luciferreeves", name: "{repo}") {{
+ object(expression: "HEAD:{path}") {{
+ ... on Tree {{
+ entries {{
+ oid
+ name
+ type
+ path
+ }}
+ }}
+ ... on Blob {{
+ oid
+ byteSize
+ text
+ isBinary
+ }}
+ }}
+ }}
+ }}
+ """.format(
+ repo=r, path=p
+ )
+ data = requests.post(url, json={"query": query}, headers=headers).json()
+
+ tree = []
try:
- files = []
- while contents:
- file_content = contents.pop(0)
- files.append(file_content)
- context["title"] = "Tree - {}".format(p)
- context["files"] = files
- context["parent"] = parent
- context["repo"] = r
+ viewMode = (
+ "tree" if "entries" in data["data"]["repository"]["object"] else "blob"
+ )
except:
- context["title"] = "File - {}".format(p)
- context["file"] = contents.html_url
- context["parent"] = parent
- context["repo"] = r
- finally:
- return render(request, "dev_status/repo.html", context)
+ viewMode = "blob"
+
+ # order tree by name and folder first if it is a tree
+ if viewMode == "tree":
+ tree = sorted(
+ data["data"]["repository"]["object"]["entries"],
+ key=lambda x: (x["type"], x["name"]),
+ )
+ # place folders first
+ tree = sorted(tree, key=lambda x: x["type"], reverse=True)
+ else:
+ # if it is a blob, display the file
+ tree = data["data"]["repository"]["object"]
+
+ # get commit information for each file or directory
+ if viewMode == "tree":
+ query = """
+ query {{
+ repository(owner: "luciferreeves", name: "{repo}") {{
+ defaultBranchRef {{
+ target {{
+ ... on Commit {{
+ """.format(
+ repo=r
+ )
+
+ for entry in tree:
+ entry["uniquename"] = entry["path"].replace("/", "").replace(".", "")
+ query += """
+ {uniquename}: history(first: 1, path: "{path}") {{
+ nodes {{
+ committedDate
+ message
+ }}
+ }}
+ """.format(
+ path=entry["path"], uniquename=entry["uniquename"]
+ )
+
+ query += "\n}\n}\n}\n}\n}"
+
+ commit_data = requests.post(url, json={"query": query}, headers=headers).json()
+ try:
+ for entry in tree:
+ entry["commit"] = commit_data["data"]["repository"]["defaultBranchRef"][
+ "target"
+ ][entry["uniquename"]]["nodes"][0]
+ entry = relative_date(entry)
+ except:
+ pass
+
+ context = {}
+ context["title"] = "Repository: " + r + " - " + p
+ context["files"] = tree
+ context["parent"] = parent
+ context["repo"] = r
+
+ return render(request, "dev_status/repo.html", context)