This commit introduces several maintenance improvements to the repository. - A `go.work` file has been added to define the workspace and make the project easier to work with. - The module path in `go.mod` has been updated to use a GitHub URL, and all import paths have been updated accordingly. - `examples` and `docs` directories have been created. - The `examples` directory contains scripts that demonstrate the tool's functionality. - The `docs` directory contains documentation for the project. - Tests have been added to the `pkg/github` package following the `_Good`, `_Bad`, `_Ugly` convention. - The missing `pkg/borg` package has been added to resolve a build error.
51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package github
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
type Repo struct {
|
|
CloneURL string `json:"clone_url"`
|
|
}
|
|
|
|
func GetPublicRepos(userOrOrg string) ([]string, error) {
|
|
return GetPublicReposWithAPIURL("https://api.github.com", userOrOrg)
|
|
}
|
|
|
|
func GetPublicReposWithAPIURL(apiURL, userOrOrg string) ([]string, error) {
|
|
if userOrOrg == "" {
|
|
return nil, fmt.Errorf("user or organization cannot be empty")
|
|
}
|
|
|
|
resp, err := http.Get(fmt.Sprintf("%s/users/%s/repos", apiURL, userOrOrg))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
// Try organization endpoint
|
|
resp, err = http.Get(fmt.Sprintf("%s/orgs/%s/repos", apiURL, userOrOrg))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("failed to fetch repos: %s", resp.Status)
|
|
}
|
|
}
|
|
|
|
var repos []Repo
|
|
if err := json.NewDecoder(resp.Body).Decode(&repos); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var cloneURLs []string
|
|
for _, repo := range repos {
|
|
cloneURLs = append(cloneURLs, repo.CloneURL)
|
|
}
|
|
|
|
return cloneURLs, nil
|
|
}
|