diff options
| author | Bobby <[email protected]> | 2024-02-11 21:30:12 -0500 |
|---|---|---|
| committer | Bobby <[email protected]> | 2024-02-11 21:30:12 -0500 |
| commit | eecd7db0d9caff2131a4d1142a2cf0063acda3a8 (patch) | |
| tree | 10b498e81901a1d973fb1d8ed911e5d4c4f9fdf4 | |
| parent | 5074ea783da1971926436f7c670273cf6009df33 (diff) | |
| download | thatcomputerscientist-eecd7db0d9caff2131a4d1142a2cf0063acda3a8.tar.xz thatcomputerscientist-eecd7db0d9caff2131a4d1142a2cf0063acda3a8.zip | |
Display commits on Repository View
| -rw-r--r-- | dev_status/utils.py | 25 | ||||
| -rw-r--r-- | dev_status/views.py | 139 | ||||
| -rw-r--r-- | templates/dev_status/repo.html | 97 |
3 files changed, 199 insertions, 62 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) diff --git a/templates/dev_status/repo.html b/templates/dev_status/repo.html index a5b60496..83073fc4 100644 --- a/templates/dev_status/repo.html +++ b/templates/dev_status/repo.html @@ -1,40 +1,59 @@ -{% extends 'blog/partials/base.html' %} {% block content %} -<div class="main"> - <h3 style="margin-bottom: 0px"> - <em>Source Code</em> - {% comment %} <ul class="topnav" style="float:right;"> - <li><a href="#">Commit History</a></li> - <li><a href="#">Build History</a></li> - <li><a href="#">Issues</a></li> - <li><a href="#">Pull Requests</a></li> - </ul> {% endcomment %} - </h3> - <hr> - <br> - <div class="files"> - {% if parent %} - <div class="file"> - <div class="ft-dir"></div> - <a class="file-name" href="{% url 'dev_status:repo-path' repo parent %}">..</a> - </div> - {% endif %} - {% if parent == '' %} - <div class="file"> - <div class="ft-dir"></div> - <a class="file-name" href="{% url 'dev_status:repo' repo %}">..</a> - </div> - {% endif %} - {% if files %} - {% for file in files %} - <div class="file"> - <div class="ft-{{ file.type }}"></div> - <a class="file-name" href="{% url 'dev_status:repo-path' repo file.path %}">{{ file.name }}</a> - </div> - {% endfor %} - {% endif %} - {% if file %} - <script src = "https://emgithub.com/embed-v2.js?target={{ file }}&style=github-dark-dimmed&type=code&showBorder=on&showLineNumbers=on&showFileMeta=on&showFullPath=on&showCopy=on"></script> - {% endif %} - </div> -</div> +{% extends 'blog/partials/base.html' %} {% block content %} {% load static %} +<img + style="width: 730px" + src="{% url 'ignis:socialify' %}?repo=luciferreeves/{{repo}}&theme=Dark&font=Koho&pattern=Floating%20Cogs&name=1&description=0&language_1=1&language_2=1&stargazers=1&forks=1&issues=1&pulls=1" + alt="Socialify" +/> +{% if parent is not None %} +<p> + <span title="Go to parent directory" style="margin-right: 5px"> + <img + src="{% static 'images/site/icons/folder.gif' %}" + alt="Folder" + style="display: inline-block; vertical-align: middle; height: 16px" + /> + </span> + <a href="{% if parent == '' %}{% url 'dev_status:repo' repo %}{% else %}{% url 'dev_status:repo-path' repo parent %}{% endif %}">..</a> + {% endif %} +</p> +{% if files and "byteSize" not in files %} +<table id="file-list" width="100%"> + <thead> + <tr style="margin-bottom: 10px; border-bottom: 1px solid #ddd"> + <th>Name</th> + <th>Last Commit</th> + <th>Updated</th> + </tr> + </thead> + <tbody> + {% for file in files %} + <tr> + <td> + {% if file.type == 'tree' %} + <span title="Directory" style="margin-right: 5px"> + <img + src="{% static 'images/site/icons/folder.gif' %}" + alt="Folder" + style="display: inline-block; vertical-align: middle; height: 16px" + /> + </span> + {% else %} + <span title="File" style="margin-right: 5px"> + <img + src="{% static 'images/icons/notepad_file-2.png' %}" + alt="File" + style="display: inline-block; vertical-align: middle; height: 16px" + /> + </span> + {% endif %} + <a href="{% url 'dev_status:repo-path' repo file.path %}">{{ file.name }}</a> + </td> + <td>{{ file.commit.message }}</td> + <td>{{ file.commit.committedDate }}</td> + </tr> + {% endfor %} + </tbody> +</table> +{% endif %} + {% endblock %} |
