Skip to content

Commit

Permalink
Add aggregate script
Browse files Browse the repository at this point in the history
  • Loading branch information
jodyheavener committed Jun 3, 2024
1 parent d72c05d commit cc10868
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/aggregate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Aggregate applicant data

on:
push:
paths:
- "data/**"

# allow for manual deploys
workflow_dispatch:

jobs:
aggregate:
runs-on: ubuntu-latest
if: vars.PROCESSOR_ENABLED == 'true' && contains(github.event.issue.labels.*.name, 'application')
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Fetch processor
uses: dsaltares/fetch-gh-release-asset@1.1.1
with:
file: "processor"
target: "./processor"

- name: Aggregate applicant data
run: |
chmod +x ./processor
./processor aggregate
env:
SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
OP_BOT_PAT: ${{ secrets.OP_BOT_PAT }}
REPOSITORY_OWNER: ${{ github.repository_owner }}
REPOSITORY_NAME: ${{ github.event.repository.name }}
1 change: 1 addition & 0 deletions data.json

Large diffs are not rendered by default.

84 changes: 84 additions & 0 deletions script/aggregator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package main

import (
"encoding/json"
"log"
"os"
"path/filepath"
"time"

"github.com/getsentry/sentry-go"
)

type ProjectEntry struct {
Project Project `json:"project"`
CreatedAt time.Time `json:"created_at"`
}

type Aggregator struct {
gitHub *GitHub
}

func (a *Aggregator) Aggregate() {
if err := a.gitHub.Init(); err != nil {
a.logErrorAndExit("could not initialize GitHub client", err)
}

dataDir := "./data"
var projectsList []ProjectEntry

if err := filepath.Walk(dataDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

// There may in the future be a non-JSON file living in
// this directory, so let's safeguard against that
if !info.IsDir() && filepath.Ext(path) == ".json" {
file, err := os.ReadFile(path)
if err != nil {
return err
}

var app Application
err = json.Unmarshal(file, &app)
if err != nil {
return err
}

projectEntry := ProjectEntry{
Project: app.Project,
CreatedAt: app.CreatedAt,
}

projectsList = append(projectsList, projectEntry)
}

return nil
}); err != nil {
a.logErrorAndExit("failed to aggregate applicant data", err)
}

aggregateData, err := json.Marshal(projectsList)
if err != nil {
a.logErrorAndExit("failed to marshal projects into JSON", err)
}

outputFile := "./data.json"

if err := a.gitHub.UpdateFile(
outputFile,
aggregateData,
"Update applicant data",
); err != nil {
a.logErrorAndExit(
"could not create commit",
err,
)
}
}

func (a *Aggregator) logErrorAndExit(message string, err error) {
sentry.CaptureException(err)
log.Fatalf("Error aggregating data: %s: %s\n", message, err.Error())
}
5 changes: 5 additions & 0 deletions script/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ func main() {
application: &application,
}
approver.Approve()
case "aggregate":
aggregator := Aggregator{
gitHub: &github,
}
aggregator.Aggregate()
default:
fmt.Printf("Invalid command: %s\n", command)
printUsageAndExit()
Expand Down

0 comments on commit cc10868

Please sign in to comment.