Begin PIS implementation
All checks were successful
Go Test / test (push) Successful in 2m3s

This commit is contained in:
Fred Boniface 2024-05-02 15:35:00 +01:00
parent c5df223509
commit 90e8fd5649
6 changed files with 112 additions and 3 deletions

26
dbAccess/pis.go Normal file
View File

@ -0,0 +1,26 @@
package dbAccess
import (
"context"
"errors"
"git.fjla.uk/owlboard/go-types/pkg/database"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
func GetPisMetadata() (*database.PisMetadata, error) {
coll := MongoClient.Database(DatabaseName).Collection(MetaCollection)
filter := bson.M{"type": "PisMetadata"}
var result database.PisMetadata
err := coll.FindOne(context.Background(), filter).Decode(&result)
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
return nil, nil
}
return nil, err
}
return &result, nil
}

2
go.mod
View File

@ -3,7 +3,7 @@ module git.fjla.uk/owlboard/timetable-mgr
go 1.21 go 1.21
require ( require (
git.fjla.uk/owlboard/go-types v1.1.6 git.fjla.uk/owlboard/go-types v1.1.8
github.com/go-stomp/stomp/v3 v3.1.0 github.com/go-stomp/stomp/v3 v3.1.0
go.mongodb.org/mongo-driver v1.15.0 go.mongodb.org/mongo-driver v1.15.0
go.uber.org/zap v1.27.0 go.uber.org/zap v1.27.0

4
go.sum
View File

@ -1,5 +1,5 @@
git.fjla.uk/owlboard/go-types v1.1.6 h1:zz6mdzvyq9HKbjQQ8NAMqi2PMDn9uC9KOoSF3654cGI= git.fjla.uk/owlboard/go-types v1.1.8 h1:vykPsn23Qh1u2v9+HuyHZ8CVdYFVq3756ReR0B1vDbI=
git.fjla.uk/owlboard/go-types v1.1.6/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= git.fjla.uk/owlboard/go-types v1.1.8/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-stomp/stomp/v3 v3.1.0 h1:JnvRJuua/fX2Lq5Ie5DXzrOL18dnzIUenCZXM6rr8/0= github.com/go-stomp/stomp/v3 v3.1.0 h1:JnvRJuua/fX2Lq5Ie5DXzrOL18dnzIUenCZXM6rr8/0=

67
pis/check.go Normal file
View File

@ -0,0 +1,67 @@
package pis
import (
"encoding/json"
"fmt"
"io"
"net/http"
"git.fjla.uk/owlboard/timetable-mgr/dbAccess"
"git.fjla.uk/owlboard/timetable-mgr/log"
"go.uber.org/zap"
)
// Checks the Gitea API for the latest release and compares to metadata in the database
// to determine if a PIS update is required.
func Check() {
repoName := "data"
repoOwner := "owlboard"
baseUrl := "https://git.fjla.uk"
apiUrl := fmt.Sprintf("%s/api/v1/repos/%s/%s/releases/latest", baseUrl, repoOwner, repoName)
resp, err := http.Get(apiUrl)
if err != nil {
log.Error("Error GETting Gitea API", zap.Error(err))
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Error("Errot GETting Gitea API", zap.Error(fmt.Errorf("response status code %d", resp.StatusCode)))
return
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Error("Error reading API Response", zap.Error(err))
return
}
var apiResp GiteaReleaseData
err = json.Unmarshal(body, &apiResp)
if err != nil {
log.Error("Error unmarshalling API response", zap.Error(err))
return
}
oldMetadata, err := dbAccess.GetPisMetadata()
if err != nil {
log.Error("Error reading PIS Metadata from database")
return
}
if oldMetadata != nil {
if oldMetadata.LastVersion != apiResp.Name {
log.Info("PIS Data is up to date")
return
}
}
log.Info("PIS Data being updated")
err = runUpdate(apiResp.TarballUrl)
if err == nil {
log.Error("Error updating PIS Data", zap.Error(err))
}
}

9
pis/types.go Normal file
View File

@ -0,0 +1,9 @@
package pis
// Omits data which is not required
type GiteaReleaseData struct {
Name string `json:"name"`
TarballUrl string `json:"tarball_url"`
Draft bool `json:"draft"`
Prerelease bool `json:"prerelease"`
}

7
pis/update.go Normal file
View File

@ -0,0 +1,7 @@
package pis
// Downloads the release tarball, extracts then applies to database
func runUpdate(tarballUrl string) error {
return nil
}