Skip to content

Commit

Permalink
test generate sitemap action
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMiao1 committed Apr 16, 2023
1 parent ae0afb1 commit 80edce0
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/generate_sitemap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import re
import sys

WEBSITE_ADDRESS = "https://example.com"
APPLICATION_PATH = "./index.express.js"
application_contents = open(APPLICATION_PATH, "r").read()

routes = []

express_name = None
app_name = None

for line in application_contents.splitlines():
if express_name is None:
if re.match(r"^((const|let|var) )?\w+ ?= ?require\(([\"'])express\3\);?$", line):
express_name = re.sub(r"(const|let|var) ", "", line[:line.index("=")]).strip()
continue

if app_name is None:
if re.match(rf"^((const|var|let) )?\w+ ?= ?{express_name}+\(\);?$", line):
app_name = re.sub(r"(const|let|var) ", "", line[:line.index("=")]).strip()
continue

route_match = re.match(rf"^{app_name}.get\([\"']/((\w|-|\.|%)/?)*[\"']", line)
if route_match:
routes.append(line[:route_match.span()[-1]].replace(f"{app_name}.get(", "").strip()[1:-1])


sitemap_urls = "\n".join(["\t" + line for line in "\n".join([f"""<url>
\t<loc>{WEBSITE_ADDRESS}{route}</loc>
</url>""" for route in routes]).splitlines()])

sitemap = f"""<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{sitemap_urls}
</urlset>
"""

if len(sys.argv) >= 2 and sys.argv[1] == "-c":
import os

if not os.path.exists("sitemap.xml"):
print("UPDATE-REQUIRED")
exit()

existing_sitemap = open("sitemap.xml", "r").read()
if existing_sitemap == sitemap:
print("NOT-REQUIRED")
else:
print("UPDATE-REQUIRED")

exit()

print(sitemap)
42 changes: 42 additions & 0 deletions .github/workflows/sitemap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Generate Sitemap

on:
push:
branches:
- "**"
pull_request:
branches:
- "**"

jobs:
create-release:
name: Generate Sitemap
runs-on: ubuntu-latest
permissions: write-all
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Setup Python 3.11
uses: actions/setup-python@v4
with:
python-version: "3.11"
- name: Check if the existing sitemap is up-to-date
id: requires_update
run: |
UPDATE_REQUIRED=`python3 ./.github/workflows/generate_sitemap.py -c`
echo "Check result: $UPDATE_REQUIRED"
echo "update_required=$UPDATE_REQUIRED" >> $GITHUB_OUTPUT
- name: Remove existing sitemaps
if: steps.requires_update.outputs.update_required == "UPDATE-REQUIRED"
run: rm -f sitemap.xml
- name: Create sitemap
if: steps.requires_update.outputs.update_required == "UPDATE-REQUIRED"
run: |
SITEMAP=`python3 ./.github/workflows/generate_sitemap.py`
echo "Generated sitemap:\n$SITEMAP"
echo "$SITEMAP" >> sitemap.xml
- name: Amend changes
if: steps.requires_update.outputs.update_required == "UPDATE-REQUIRED"
uses: EndBug/add-and-commit@v9
with:
- add: "sitemap.xml"
36 changes: 36 additions & 0 deletions index.express.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const express = require("express");
const path = require("path");

const app = express();
var requests = 0;

app.get("*", function(_, _, next) {
requests++;
next();
});

app.get("/", function(_, response) {
response.sendFile(path.join(process.cwd(), "index.html"));
});

app.get("*", function(_, response) {
response.status(404).end();
});

app.get("/test1", async function(request, response) {
response.sendFile(path.join(process.cwd(), "test1"));
});

app.get("/test2", async function(request, response) {
response.sendFile(path.join(process.cwd(), "test2"));
});

app.get("/test3/test3", async function(request, response) {
response.sendFile(path.join(process.cwd(), "test3"));
});

app.get("/test4--test4/test4/", async function(request, response) {
response.sendFile(path.join(process.cwd(), "test4"));
});

app.listen(3000);
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"license": "ISC",
"devDependencies": {
"eslint": "^8.34.0"
},
"dependencies": {
"express": "^4.18.2"
}
}

0 comments on commit 80edce0

Please sign in to comment.