|
| 1 | +on: |
| 2 | + workflow_dispatch: |
| 3 | + schedule: |
| 4 | + - cron: '5,25,45 * * * 5,6' |
| 5 | + |
| 6 | +name: weekly update |
| 7 | +jobs: |
| 8 | + weekly_update: |
| 9 | + runs-on: ubuntu-20.04 |
| 10 | + steps: |
| 11 | + - uses: actions/checkout@v2 |
| 12 | + with: |
| 13 | + fetch-depth: 0 |
| 14 | + - name: setup Go |
| 15 | + uses: actions/setup-go@v2 |
| 16 | + with: |
| 17 | + go-version: 1.17 |
| 18 | + - name: build latest binary |
| 19 | + run: go get -d -v . && CGO_ENABLED=0 go install -v . |
| 20 | + - name: switch to website branch |
| 21 | + run: | |
| 22 | + git checkout -b website origin/website |
| 23 | + git config user.name "github-actions[bot]" |
| 24 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 25 | + - name: perform update and push commits (if applicable) |
| 26 | + shell: python |
| 27 | + env: |
| 28 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 29 | + run: | |
| 30 | + import datetime, glob, os, subprocess, sys |
| 31 | +
|
| 32 | + STALEDATA = 5 |
| 33 | + GENERATED = { |
| 34 | + "%s.md": "---\ntype: location\nlocation: %s\nmode: commits\n---\n", |
| 35 | + "%s_private.md": "---\ntype: location\nlocation: %s\nmode: all\n---\n", |
| 36 | + "%s_public.md": "---\ntype: location\nlocation: %s\nmode: contributions\n---\n", |
| 37 | + } |
| 38 | +
|
| 39 | + preset_list = subprocess.run(["most-active-github-users-counter", "--list-presets"], capture_output=True, text=True).stdout |
| 40 | + regenerated = False |
| 41 | +
|
| 42 | + # generate markdown files for each preset |
| 43 | + expected = ["index.md"] |
| 44 | + flookup = {} |
| 45 | + for preset, title in sorted(line.split(" = ") for line in preset_list.strip().split("\n")): |
| 46 | + filename = preset.replace(" ", "_") |
| 47 | + flookup[filename] = { "preset": preset, "title": title } |
| 48 | + expected.append("_data/locations/%s.yml" % filename) |
| 49 | + for name, content in GENERATED.items(): |
| 50 | + expected.append(name % filename) |
| 51 | + if not os.path.exists(expected[-1]): |
| 52 | + regenerated = True |
| 53 | + with open(expected[-1], "w") as f: |
| 54 | + f.write(content % filename) |
| 55 | +
|
| 56 | + # remove files for presets no longer supported (if any) |
| 57 | + for filename in sorted(glob.glob("*.md") + glob.glob("_data/locations/*.yml")): |
| 58 | + if filename not in expected: |
| 59 | + os.remove(filename) |
| 60 | + regenerated = True |
| 61 | +
|
| 62 | + # find if any location data is stale |
| 63 | + get_mtime = lambda n: datetime.date.fromisoformat(subprocess.run(["git", "log", "-1", "--pretty=%as", n], capture_output=True, text=True).stdout.strip()) |
| 64 | + locations = sorted([get_mtime(n), os.path.basename(n)] for n in glob.glob("_data/locations/*.yml")) |
| 65 | + today = datetime.date.today() |
| 66 | + to_process = None |
| 67 | + for mtime, filename in locations: |
| 68 | + if (today - mtime).days >= STALEDATA: |
| 69 | + to_process = os.path.splitext(filename)[0] |
| 70 | + break |
| 71 | +
|
| 72 | + if not regenerated and to_process is None: |
| 73 | + sys.exit(0) |
| 74 | +
|
| 75 | + if regenerated: |
| 76 | + subprocess.run('git add *.md _data/locations/*.yml && git commit -am "regenerate location pages"', shell=True, check=True) |
| 77 | +
|
| 78 | + if to_process: |
| 79 | + preset = flookup[to_process]["preset"] |
| 80 | + status = subprocess.run(["most-active-github-users-counter", "--token", os.environ["GITHUB_TOKEN"], "--preset", preset, "--output", "yaml"], capture_output=True, text=True) |
| 81 | + if status.returncode == 0: |
| 82 | + with open("_data/locations/%s.yml" % to_process, "w") as f: |
| 83 | + f.write("page: %s.html\ntitle: %s\n%s" % (to_process, flookup[to_process]["title"], status.stdout)) |
| 84 | + subprocess.run('git add _data/locations/%s.yml && git commit -m "%s: updates for %s"' % (to_process, preset, today.isoformat()), shell=True, check=True) |
| 85 | + else: |
| 86 | + print("FAILED with exit code %d\n--- stdout ---\n%s\n--- stderr ---\n%s" % (status.returncode, status.stdout, status.stderr)) |
| 87 | + sys.exit(1) |
| 88 | +
|
| 89 | + subprocess.run("git push origin website", shell=True, check=True) |
0 commit comments