Convert to Go
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type GiteaClient struct {
|
||||
BaseURL string
|
||||
Token string
|
||||
}
|
||||
|
||||
type Repository struct {
|
||||
Name string `json:"name"`
|
||||
CloneURL string `json:"clone_url"`
|
||||
Owner Owner `json:"owner"`
|
||||
FullName string `json:"full_name"`
|
||||
}
|
||||
|
||||
type Owner struct {
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
func (g *GiteaClient) getRepositories(org string) (list []Repository, rerr error) {
|
||||
url := fmt.Sprintf("%s/api/v1/orgs/%s/repos", g.BaseURL, org)
|
||||
req, _ := http.NewRequest("GET", url, nil)
|
||||
req.Header.Set("Authorization", "token "+g.Token)
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
err := Body.Close()
|
||||
if err != nil {
|
||||
rerr = err
|
||||
}
|
||||
}(resp.Body)
|
||||
|
||||
var repos []Repository
|
||||
if err := json.NewDecoder(resp.Body).Decode(&repos); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return repos, nil
|
||||
}
|
||||
|
||||
func (g *GiteaClient) hasPKGBUILD(repo Repository) (present bool, rerr error) {
|
||||
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/contents/PKGBUILD", g.BaseURL, repo.Owner.Username, repo.Name)
|
||||
req, _ := http.NewRequest("GET", url, nil)
|
||||
req.Header.Set("Authorization", "token "+g.Token)
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
err := Body.Close()
|
||||
if err != nil {
|
||||
rerr = err
|
||||
}
|
||||
}(resp.Body)
|
||||
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
return false, nil
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return false, errors.New("unexpected status code")
|
||||
}
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
return len(body) > 0, nil
|
||||
}
|
||||
Reference in New Issue
Block a user