softwatch/github.go
2023-08-06 14:25:24 +02:00

46 lines
862 B
Go

package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/rs/zerolog/log"
"io"
"net/http"
"strings"
)
type Github struct {
}
type githubAPIResponse struct {
TagName string `json:"tag_name"`
}
func (g Github) getLastVersion(s software) (string, error) {
repo := strings.TrimPrefix(s.Data["url"], "https://github.com/")
repo = strings.TrimSuffix(repo, "/")
url := fmt.Sprintf("https://api.github.com/repos/%s/releases?per_page=1", repo)
log.Debug().Str("url", url).Msg("url for github request")
res, err := http.Get(url)
if err != nil {
return "", err
}
body, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}
var data []githubAPIResponse
err = json.Unmarshal(body, &data)
if err != nil {
return "", err
}
if len(data) != 1 {
return "", errors.New("cant find any release")
}
return data[0].TagName, nil
}