beretta/src/repo.go
2024-09-28 06:47:27 +02:00

55 lines
1.2 KiB
Go

package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
type Repo interface {
GetLatestTag() (Tag, error)
}
// Description of a repository (owner and name)
type RepoConfig struct {
Owner string `json:"owner"`
Repo string `json:"repo"`
}
func (r RepoConfig) GetLatestTag() (Tag, error) {
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/tags", r.Owner, r.Repo)
resp, err := http.Get(url)
if err != nil {
log.Printf("Failed to fetch releases for %s/%s: %v", r.Owner, r.Repo, err)
return Tag{}, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Printf("Unexpected response status for %s/%s: %d", r.Owner, r.Repo, resp.StatusCode)
return Tag{}, fmt.Errorf("unexpected response status: %d", resp.StatusCode)
}
var tags = []Tag{}
if err := json.NewDecoder(resp.Body).Decode(&tags); err != nil {
log.Printf("Failed to decode response for %s/%s: %v", r.Owner, r.Repo, err)
return Tag{}, err
}
return tags[0], nil
}
type Commit struct {
Sha string `json:"sha"`
URL string `json:"url"`
}
type Tag struct {
Name string `json:"name"`
ZipballURL string `json:"zipball_url"`
TarballURL string `json:"tarball_url"`
Commit Commit `json:"commit"`
NodeID string `json:"node_id"`
}