daily update #21176
This file contains hidden or 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: '45 * * * *' | |
| name: daily update | |
| jobs: | |
| daily_update: | |
| runs-on: ubuntu-24.04 | |
| steps: | |
| - uses: actions/checkout@v6 | |
| with: | |
| fetch-depth: 0 | |
| - name: setup Go | |
| uses: actions/setup-go@v6 | |
| 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 "github-actions[bot]" | |
| git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
| - name: perform update and push commits (if applicable) | |
| shell: python | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| import csv, datetime, glob, os, random, re, subprocess, sys | |
| MAX_IN_RUN = 3 | |
| STALE_DAYS = 5 | |
| 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: | |
| file_content = open(expected[-1]).read() | |
| checksum_match = re.search("^definition_checksum: (\w+)$", file_content, re.M) | |
| usercount_match = re.search("^total_user_count: (\d+)$", file_content, re.M) | |
| if not checksum_match or checksum_match.group(1) != all_presets[preset]['definition_checksum'] or not usercount_match or usercount_match.group(1) == '0': | |
| 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) | |
| random.shuffle(to_process) # in case we're getting failures for a specific preset, this should let others move forward | |
| 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) |