Begin PIS implementation
This commit is contained in:
67
pis/check.go
Normal file
67
pis/check.go
Normal 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
9
pis/types.go
Normal 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
7
pis/update.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package pis
|
||||
|
||||
// Downloads the release tarball, extracts then applies to database
|
||||
func runUpdate(tarballUrl string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user