Skip to content

Commit eaa73c9

Browse files
committed
Add aggregate script
1 parent d72c05d commit eaa73c9

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

.github/workflows/aggregate.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Aggregate applicant data
2+
3+
on:
4+
push:
5+
paths:
6+
- "data/**"
7+
8+
# allow for manual deploys
9+
workflow_dispatch:
10+
11+
jobs:
12+
aggregate:
13+
runs-on: ubuntu-latest
14+
if: vars.PROCESSOR_ENABLED == 'true'
15+
steps:
16+
- name: Checkout repository
17+
uses: actions/checkout@v4
18+
19+
- name: Fetch processor
20+
uses: dsaltares/[email protected]
21+
with:
22+
file: "processor"
23+
target: "./processor"
24+
25+
- name: Aggregate applicant data
26+
run: |
27+
chmod +x ./processor
28+
./processor aggregate
29+
env:
30+
SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
31+
OP_BOT_PAT: ${{ secrets.OP_BOT_PAT }}
32+
REPOSITORY_OWNER: ${{ github.repository_owner }}
33+
REPOSITORY_NAME: ${{ github.event.repository.name }}

data.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

script/aggregator.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"log"
6+
"os"
7+
"path/filepath"
8+
"time"
9+
10+
"github.com/getsentry/sentry-go"
11+
)
12+
13+
type ProjectEntry struct {
14+
Project Project `json:"project"`
15+
CreatedAt time.Time `json:"created_at"`
16+
}
17+
18+
type Aggregator struct {
19+
gitHub *GitHub
20+
}
21+
22+
func (a *Aggregator) Aggregate() {
23+
if err := a.gitHub.Init(); err != nil {
24+
a.logErrorAndExit("could not initialize GitHub client", err)
25+
}
26+
27+
dataDir := "./data"
28+
var projectsList []ProjectEntry
29+
30+
if err := filepath.Walk(dataDir, func(path string, info os.FileInfo, err error) error {
31+
if err != nil {
32+
return err
33+
}
34+
35+
// There may in the future be a non-JSON file living in
36+
// this directory, so let's safeguard against that
37+
if !info.IsDir() && filepath.Ext(path) == ".json" {
38+
file, err := os.ReadFile(path)
39+
if err != nil {
40+
return err
41+
}
42+
43+
var app Application
44+
err = json.Unmarshal(file, &app)
45+
if err != nil {
46+
return err
47+
}
48+
49+
projectEntry := ProjectEntry{
50+
Project: app.Project,
51+
CreatedAt: app.CreatedAt,
52+
}
53+
54+
projectsList = append(projectsList, projectEntry)
55+
}
56+
57+
return nil
58+
}); err != nil {
59+
a.logErrorAndExit("failed to aggregate applicant data", err)
60+
}
61+
62+
aggregateData, err := json.Marshal(projectsList)
63+
if err != nil {
64+
a.logErrorAndExit("failed to marshal projects into JSON", err)
65+
}
66+
67+
outputFile := "./data.json"
68+
69+
if err := a.gitHub.UpdateFile(
70+
outputFile,
71+
aggregateData,
72+
"Update applicant data",
73+
); err != nil {
74+
a.logErrorAndExit(
75+
"could not create commit",
76+
err,
77+
)
78+
}
79+
}
80+
81+
func (a *Aggregator) logErrorAndExit(message string, err error) {
82+
sentry.CaptureException(err)
83+
log.Fatalf("Error aggregating data: %s: %s\n", message, err.Error())
84+
}

script/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ func main() {
6666
application: &application,
6767
}
6868
approver.Approve()
69+
case "aggregate":
70+
aggregator := Aggregator{
71+
gitHub: &github,
72+
}
73+
aggregator.Aggregate()
6974
default:
7075
fmt.Printf("Invalid command: %s\n", command)
7176
printUsageAndExit()

0 commit comments

Comments
 (0)