2023-01-12 05:11:26 +00:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2023-01-13 05:07:57 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-01-15 03:31:58 +00:00
|
|
|
func (g Gitea) latestRelease() (Release, error) {
|
2023-01-12 05:11:26 +00:00
|
|
|
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
|
|
|
|
}
|
2023-01-15 03:31:58 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|