This repository has been archived on 2023-11-08. You can view files and clone it, but cannot push or open issues/pull-requests.
2023-01-12 05:11:26 +00:00
|
|
|
package forge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2023-01-13 05:07:57 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2023-01-12 05:11:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
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-12 05:11:26 +00:00
|
|
|
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
|
|
|
|
}
|