timetable-mgr/pis/check.go
Fred Boniface fc5bb2534a
All checks were successful
Go Test / test (push) Successful in 2m36s
Add additional logging to debug PIS update process
2024-11-11 11:03:25 +00:00

71 lines
1.5 KiB
Go

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() {
log.Info("PIS Update Check started")
repoName := "data"
repoOwner := "owlboard"
baseUrl := "https://git.fjla.uk"
apiUrl := fmt.Sprintf("%s/api/v1/repos/%s/%s/releases/latest", baseUrl, repoOwner, repoName)
log.Info("Initiating PIS Check")
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))
}
}