This repository has been archived on 2023-11-08. You can view files and clone it, but cannot push or open issues/pull-requests.
eget/forge/gitea.go

75 lines
1.3 KiB
Go
Raw Normal View History

package forge
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
)
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 NewGitea(uri string) (Gitea, error) {
var g Gitea
base, paths, err := splitURI(uri)
if err != nil {
return g, err
}
g.BaseURL = base
g.Owner = paths[0]
g.Repo = paths[1]
return g, nil
}
func (g Gitea) Name() string {
return g.Repo
}
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
}