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

97 lines
1.8 KiB
Go
Raw Normal View History

package forge
import (
"encoding/json"
"errors"
"fmt"
"net/http"
)
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) latestRelease() (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
}
func (g Gitea) tagRelease(tag string) (Release, error) {
var release Release
u := fmt.Sprintf("%s/api/v1/repos/%s/%s/releases/tags/%s", g.BaseURL, g.Owner, g.Repo, tag)
res, err := http.Get(u)
if err != nil {
return release, err
}
defer res.Body.Close()
var r GiteaRelease
if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
return release, err
}
release.Name = r.TagName
for _, a := range r.Assets {
release.Assets = append(release.Assets, Asset{
Name: a.Name,
DownloadURL: a.BrowserDownloadURL,
})
}
return release, nil
}