56 lines
1.0 KiB
Go
56 lines
1.0 KiB
Go
|
package forge
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
var _ Forger = (*Gitea)(nil)
|
||
|
|
||
|
type Gitea struct {
|
||
|
BaseURL string
|
||
|
Owner string
|
||
|
Repo string
|
||
|
}
|
||
|
|
||
|
type GiteaRelease struct {
|
||
|
TagName string `json:"tag_name"`
|
||
|
Assets []struct {
|
||
|
Name string `json:"name"`
|
||
|
BrowserDownloadURL string `json:"browser_download_url"`
|
||
|
} `json:"assets"`
|
||
|
}
|
||
|
|
||
|
func (g Gitea) Latest() (Release, error) {
|
||
|
var release Release
|
||
|
|
||
|
var releases []GiteaRelease
|
||
|
u := fmt.Sprintf("%s/api/v1/repos/%s/%s/releases?draft=false&pre-release=false", g.BaseURL, g.Owner, g.Repo)
|
||
|
res, err := http.Get(u)
|
||
|
if err != nil {
|
||
|
return release, err
|
||
|
}
|
||
|
defer res.Body.Close()
|
||
|
|
||
|
if err := json.NewDecoder(res.Body).Decode(&releases); err != nil {
|
||
|
return release, err
|
||
|
}
|
||
|
|
||
|
if len(releases) == 0 {
|
||
|
return release, errors.New("no releases found")
|
||
|
}
|
||
|
r := releases[0]
|
||
|
|
||
|
release.Name = r.TagName
|
||
|
for _, a := range r.Assets {
|
||
|
release.Assets = append(release.Assets, Asset{
|
||
|
Name: a.Name,
|
||
|
DownloadURL: a.BrowserDownloadURL,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
return release, nil
|
||
|
}
|