Update Top Rank #38780
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on: | |
workflow_dispatch: | |
schedule: [{ cron: "*/5 * * * *" }] | |
push: { branches: ["master", "main"] } | |
name: Update Top Rank | |
permissions: | |
contents: write | |
pages: write | |
id-token: write | |
jobs: | |
daily_update: | |
runs-on: ubuntu-20.04 | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
with: | |
fetch-depth: 0 | |
- name: Setup Go | |
uses: actions/setup-go@v2 | |
with: | |
go-version: 1.17 | |
- name: Build latest binary | |
run: go get -d -v . && CGO_ENABLED=0 go install -v . | |
- name: Switch to gh-pages branch | |
run: | | |
git checkout -b gh-pages origin/gh-pages | |
git config user.name "eplus-bot" | |
git config user.email "[email protected]" | |
- name: perform update and push commits (if applicable) | |
shell: python | |
env: | |
GITHUB_TOKEN: ${{ secrets.API_TOKEN }} | |
run: | | |
import csv, datetime, glob, os, re, subprocess, sys | |
MAX_IN_RUN = 3 | |
STALE_DAYS = 2 | |
GENERATED = { | |
"%s.md": "---\ntype: location\nlocation: %s\nmode: commits\n---\n", | |
"%s_private.md": "---\ntype: location\nlocation: %s\nmode: all\n---\n", | |
"%s_public.md": "---\ntype: location\nlocation: %s\nmode: contributions\n---\n", | |
"rank_only/%s.json": "---\nlayout: rank_only\nlocation: %s\n---\n", | |
} | |
subprocess.run("mkdir -p rank_only", shell=True, check=True) | |
preset_data = subprocess.run(["most-active-github-users-counter", "--list-presets"], capture_output=True, text=True).stdout | |
all_presets = dict([row.pop('preset'), row] for row in csv.DictReader(preset_data.split('\n'))) | |
regenerated = False | |
to_process = [] | |
# generate markdown files for each preset | |
expected = ["index.md", "README.md"] # shouldn't be deleted! | |
flookup = {} | |
for preset in sorted(all_presets.keys()): | |
filename = preset.replace(" ", "_") | |
flookup[filename] = preset | |
expected.append("_data/locations/%s.yml" % filename) | |
if not os.path.exists(expected[-1]): | |
to_process.append(filename) | |
else: | |
match = re.search("^definition_checksum: (\w+)$", open(expected[-1]).read(), re.M) | |
if not match or match.group(1) != all_presets[preset]['definition_checksum']: | |
to_process.append(filename) | |
for name, content in GENERATED.items(): | |
expected.append(name % filename) | |
if not os.path.exists(expected[-1]): | |
regenerated = True | |
with open(expected[-1], "w") as f: | |
f.write(content % filename) | |
# remove files for presets no longer supported (if any) | |
for filename in sorted(glob.glob("*.md") + glob.glob("_data/locations/*.yml") + glob.glob("rank_only/*.json")): | |
if filename not in expected: | |
os.remove(filename) | |
regenerated = True | |
# find if any location data is stale | |
get_mtime = lambda n: datetime.date.fromisoformat(subprocess.run(["git", "log", "-1", "--pretty=%as", n], capture_output=True, text=True).stdout.strip()) | |
locations = sorted([get_mtime(n), os.path.basename(n)] for n in glob.glob("_data/locations/*.yml")) | |
today = datetime.date.today() | |
for mtime, filename in locations: | |
if (today - mtime).days >= STALE_DAYS: | |
to_process.append(os.path.splitext(filename)[0]) | |
if not regenerated and len(to_process) == 0: | |
sys.exit(0) | |
if regenerated: | |
subprocess.run('git add *.md _data/locations/*.yml rank_only/*.json && git commit -am "regenerate location pages"', shell=True, check=True) | |
for key in to_process[0:MAX_IN_RUN]: | |
print("Running: %s" % key) | |
preset = flookup[key] | |
status = subprocess.run(["most-active-github-users-counter", "--token", os.environ["GITHUB_TOKEN"], "--preset", preset, "--output", "yaml"], capture_output=True, text=True) | |
if status.returncode == 0: | |
with open("_data/locations/%s.yml" % key, "w") as f: | |
f.write("page: %s.html\n%s" % (key, status.stdout)) | |
subprocess.run('git add _data/locations/%s.yml && git commit -m "%s: updates for %s"' % (key, preset, today.isoformat()), shell=True, check=True) | |
else: | |
subprocess.run("git push origin gh-pages", shell=True) # push successful commits, if any | |
print("FAILED with exit code %d\n--- stdout ---\n%s\n--- stderr ---\n%s" % (status.returncode, status.stdout, status.stderr)) | |
sys.exit(1) | |
subprocess.run("git push origin gh-pages", shell=True, check=True) |