Skip to content

Commit

Permalink
Refactor the workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
wxh06 committed Nov 10, 2023
1 parent e0402ff commit 1ceb2a4
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 69 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
src/contests.json filter=lfs diff=lfs merge=lfs -text
public/users.json filter=lfs diff=lfs merge=lfs -text
35 changes: 35 additions & 0 deletions .github/workflows/extract.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
name: Extract user data

on:
push:
branches: [main]

permissions:
contents: write

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
lfs: true

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: stable

- name: Extract
run: go run ./cmd/extract

- name: Commit
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add -f public/users/
git commit --allow-empty -m "Extract user data"
- name: Push
run: git push origin main:dist
12 changes: 5 additions & 7 deletions .github/workflows/data.yml → .github/workflows/fetch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
name: Fetch data

on:
push:
branches: [main]
schedule:
# Runs at 00:00 UTC.
- cron: "0 0 * * *"
Expand All @@ -16,23 +14,23 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
lfs: true

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: stable

- run: git checkout -mB dist

- name: Fetch
run: go run .
run: go run ./cmd/fetch

- name: Commit
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add -f src/contests.json public/users/
git commit --allow-empty -m "Fetch data"
git diff-index --quiet HEAD || git commit -m "Fetch data"
- name: Push
run: git push --set-upstream origin dist
run: git push
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,3 @@ coverage

# Vite SSG
.vite-ssg-temp/

# Fetched data
src/contests.json
3 changes: 3 additions & 0 deletions .husky/post-checkout
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
command -v git-lfs >/dev/null 2>&1 || { echo >&2 "\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting the 'post-checkout' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\n"; exit 2; }
git lfs post-checkout "$@"
3 changes: 3 additions & 0 deletions .husky/post-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
command -v git-lfs >/dev/null 2>&1 || { echo >&2 "\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting the 'post-commit' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\n"; exit 2; }
git lfs post-commit "$@"
3 changes: 3 additions & 0 deletions .husky/post-merge
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
command -v git-lfs >/dev/null 2>&1 || { echo >&2 "\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting the 'post-merge' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\n"; exit 2; }
git lfs post-merge "$@"
3 changes: 3 additions & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
command -v git-lfs >/dev/null 2>&1 || { echo >&2 "\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting the 'pre-push' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\n"; exit 2; }
git lfs pre-push "$@"
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pnpm-lock.yaml

# Fetched data
public/users/
src/contests.json
public/users.json
public/users/
22 changes: 22 additions & 0 deletions cmd/extract/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"fmt"

"github.com/wxh06/lgtracker/internal/datafile"
)

func main() {
var users = map[int]map[int]map[string]int{}

if err := datafile.Read("public/users.json", &users); err != nil {
panic(err)
}

// 存储每个用户的通过情况
for user, passed := range users {
if err := datafile.Write(fmt.Sprintf("public/users/%d.json", user), passed); err != nil {
panic(err)
}
}
}
68 changes: 10 additions & 58 deletions fetch.go → cmd/fetch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package main

import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"log"
"os"
"strconv"
"strings"
"time"

"github.com/wxh06/lgtracker/internal/datafile"
"github.com/wxh06/luogu-cli/pkg/luogu"
)

Expand Down Expand Up @@ -91,42 +91,12 @@ func main() {
// 每场比赛的详情与题目摘要
var contests = make(map[int]Contest)

// 从文件中读取已保存的比赛数据
if f, err := os.Open("src/contests.json"); err == nil {
if err = json.NewDecoder(f).Decode(&contests); err != nil {
panic(err)
}
if err = f.Close(); err != nil {
panic(err)
}
}

// 从文件中读取每个保存的用户通过情况
entries, err := os.ReadDir("public/users")
if err != nil {
// 从文件中读取先前保存过的比赛与用户通过情况数据
if err = datafile.Read("src/contests.json", &contests); err != nil && !errors.Is(err, fs.ErrNotExist) {
panic(err)
}
for _, entry := range entries {
name := entry.Name()
uid, err := strconv.Atoi(strings.Split(name, ".")[0])
if err != nil {
continue
}

f, err := os.Open("public/users/" + name)
if err != nil {
panic(err)
}

var passed map[int]map[string]int
if err = json.NewDecoder(f).Decode(&passed); err != nil {
panic(err)
}
users[uid] = passed

if err = f.Close(); err != nil {
panic(err)
}
if err = datafile.Read("public/users.json", &users); err != nil && !errors.Is(err, fs.ErrNotExist) {
panic(err)
}

for _, contest := range data.CurrentData.Contests.Result {
Expand All @@ -149,29 +119,11 @@ func main() {
}
}

// 储存比赛数据
f, err := os.Create("src/contests.json")
if err != nil {
panic(err)
}
if err = json.NewEncoder(f).Encode(contests); err != nil {
// 将数据写入文件
if err = datafile.Write("src/contests.json", contests); err != nil {
panic(err)
}
if err = f.Close(); err != nil {
if err = datafile.Write("public/users.json", users); err != nil {
panic(err)
}

// 存储每个用户的通过情况
for user, passed := range users {
f, err = os.Create(fmt.Sprintf("public/users/%d.json", user))
if err != nil {
panic(err)
}
if err = json.NewEncoder(f).Encode(passed); err != nil {
panic(err)
}
if err = f.Close(); err != nil {
panic(err)
}
}
}
28 changes: 28 additions & 0 deletions internal/datafile/io.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package datafile

import (
"encoding/json"
"os"
)

func Read(name string, v any) (err error) {
f, err := os.Open(name)
if err != nil {
return
}
defer f.Close()

err = json.NewDecoder(f).Decode(v)
return
}

func Write(name string, v any) (err error) {
f, err := os.Create(name)
if err != nil {
return
}
defer f.Close()

err = json.NewEncoder(f).Encode(v)
return
}
3 changes: 3 additions & 0 deletions public/users.json
Git LFS file not shown
3 changes: 3 additions & 0 deletions src/contests.json
Git LFS file not shown

0 comments on commit 1ceb2a4

Please sign in to comment.