68 lines
1.5 KiB
Go
68 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() {
|
||
|
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))
|
||
|
}
|
||
|
}
|