Files
zeekayandHanzo Dev 1ab6e799c1 cd: the module is github.com/hanzoai/cd, where it is actually hosted
go.mod still declared `module github.com/hanzoai/deploy` after the repo was
renamed. A Go module path is not cosmetic — it is the resolvable location. So
`go get github.com/hanzoai/cd` could not work, and anything importing this
module had to spell a name that no longer exists at that address. The build log
of v3.6.0 is where it surfaced: the compiler was still emitting
github.com/hanzoai/deploy/cmd/cd/commands.

3541 import paths across 690 .go, 19 .proto, 4 .yaml, 3 .sh, 2 .mod, Procfile
and Makefile. Plus the 62 ghcr.io/hanzoai/deploy image references, moved to
ghcr.io/hanzoai/cd to match hanzo.yml's build target — a distinct string from
the module path, replaced separately so neither could clobber the other.

go mod tidy clean, CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ./... clean.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-28 21:16:24 -07:00

164 lines
4.7 KiB
Go

package scm_provider
import (
"context"
"fmt"
"net/http"
"os"
"github.com/google/go-github/v69/github"
appsetutils "github.com/hanzoai/cd/applicationset/utils"
)
type GithubProvider struct {
client *github.Client
organization string
allBranches bool
excludeArchivedRepos bool
}
var _ SCMProviderService = &GithubProvider{}
func NewGithubProvider(organization string, token string, url string, allBranches bool, excludeArchivedRepos bool, optionalHTTPClient ...*http.Client) (*GithubProvider, error) {
// Undocumented environment variable to set a default token, to be used in testing to dodge anonymous rate limits.
if token == "" {
token = os.Getenv("GITHUB_TOKEN")
}
var client *github.Client
httpClient := appsetutils.GetOptionalHTTPClient(optionalHTTPClient...)
if url == "" {
if token == "" {
client = github.NewClient(httpClient)
} else {
client = github.NewClient(httpClient).WithAuthToken(token)
}
} else {
var err error
if token == "" {
client, err = github.NewClient(httpClient).WithEnterpriseURLs(url, url)
} else {
client, err = github.NewClient(httpClient).WithAuthToken(token).WithEnterpriseURLs(url, url)
}
if err != nil {
return nil, err
}
}
return &GithubProvider{client: client, organization: organization, allBranches: allBranches, excludeArchivedRepos: excludeArchivedRepos}, nil
}
func (g *GithubProvider) GetBranches(ctx context.Context, repo *Repository) ([]*Repository, error) {
repos := []*Repository{}
branches, err := g.listBranches(ctx, repo)
if err != nil {
return nil, fmt.Errorf("error listing branches for %s/%s: %w", repo.Organization, repo.Repository, err)
}
for _, branch := range branches {
repos = append(repos, &Repository{
Organization: repo.Organization,
Repository: repo.Repository,
URL: repo.URL,
Branch: branch.GetName(),
SHA: branch.GetCommit().GetSHA(),
Labels: repo.Labels,
RepositoryId: repo.RepositoryId,
})
}
return repos, nil
}
func (g *GithubProvider) ListRepos(ctx context.Context, cloneProtocol string) ([]*Repository, error) {
opt := &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{PerPage: 100},
}
repos := []*Repository{}
for {
githubRepos, resp, err := g.client.Repositories.ListByOrg(ctx, g.organization, opt)
if err != nil {
return nil, fmt.Errorf("error listing repositories for %s: %w", g.organization, err)
}
for _, githubRepo := range githubRepos {
var url string
switch cloneProtocol {
// Default to SSH if unspecified (i.e. if "").
case "", "ssh":
url = githubRepo.GetSSHURL()
case "https":
url = githubRepo.GetCloneURL()
default:
return nil, fmt.Errorf("unknown clone protocol for GitHub %v", cloneProtocol)
}
if g.excludeArchivedRepos && githubRepo.GetArchived() {
continue
}
repos = append(repos, &Repository{
Organization: githubRepo.Owner.GetLogin(),
Repository: githubRepo.GetName(),
Branch: githubRepo.GetDefaultBranch(),
URL: url,
Labels: githubRepo.Topics,
RepositoryId: githubRepo.ID,
})
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
return repos, nil
}
func (g *GithubProvider) RepoHasPath(ctx context.Context, repo *Repository, path string) (bool, error) {
_, _, resp, err := g.client.Repositories.GetContents(ctx, repo.Organization, repo.Repository, path, &github.RepositoryContentGetOptions{
Ref: repo.Branch,
})
// 404s are not an error here, just a normal false.
if resp != nil && resp.StatusCode == http.StatusNotFound {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
func (g *GithubProvider) listBranches(ctx context.Context, repo *Repository) ([]github.Branch, error) {
// If we don't specifically want to query for all branches, just use the default branch and call it a day.
if !g.allBranches {
defaultBranch, resp, err := g.client.Repositories.GetBranch(ctx, repo.Organization, repo.Repository, repo.Branch, 0)
if err != nil {
if resp.StatusCode == http.StatusNotFound {
// Default branch doesn't exist, so the repo is empty.
return []github.Branch{}, nil
}
return nil, err
}
return []github.Branch{*defaultBranch}, nil
}
// Otherwise, scrape the ListBranches API.
opt := &github.BranchListOptions{
ListOptions: github.ListOptions{PerPage: 100},
}
branches := []github.Branch{}
for {
githubBranches, resp, err := g.client.Repositories.ListBranches(ctx, repo.Organization, repo.Repository, opt)
if err != nil {
return nil, err
}
for _, githubBranch := range githubBranches {
branches = append(branches, *githubBranch)
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
return branches, nil
}