From 89da9997ba82084e6e7bb34463d614bc5e03e31d Mon Sep 17 00:00:00 2001 From: kjuulh Date: Thu, 6 Oct 2022 22:03:10 +0200 Subject: [PATCH] add github provider (WIP) --- go.mod | 3 ++ go.sum | 9 +++-- internal/gitproviders/github.go | 64 +++++++++++++++++++++++++++++++++ scripts/push_github.sh | 2 +- 4 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 internal/gitproviders/github.go diff --git a/go.mod b/go.mod index 42aca25..09d8725 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/gin-contrib/zap v0.0.2 github.com/gin-gonic/gin v1.8.1 github.com/go-git/go-git/v5 v5.4.2 + github.com/google/go-github v17.0.0+incompatible github.com/google/uuid v1.3.0 github.com/spf13/cobra v1.5.0 github.com/stretchr/testify v1.8.0 @@ -31,6 +32,8 @@ require ( github.com/go-playground/universal-translator v0.18.0 // indirect github.com/go-playground/validator/v10 v10.10.0 // indirect github.com/goccy/go-json v0.9.7 // indirect + github.com/google/go-cmp v0.5.9 // indirect + github.com/google/go-querystring v1.1.0 // indirect github.com/hashicorp/go-version v1.2.1 // indirect github.com/imdario/mergo v0.3.12 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect diff --git a/go.sum b/go.sum index d326efc..b190172 100644 --- a/go.sum +++ b/go.sum @@ -64,8 +64,14 @@ github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGF github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY= +github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= +github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= +github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -230,7 +236,6 @@ golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw= diff --git a/internal/gitproviders/github.go b/internal/gitproviders/github.go new file mode 100644 index 0000000..f08b58f --- /dev/null +++ b/internal/gitproviders/github.go @@ -0,0 +1,64 @@ +package gitproviders + +import ( + "context" + "net/http" + "sync" + + "github.com/google/go-github/github" + "go.uber.org/zap" +) + +type Github struct { + logger *zap.Logger + githubmu sync.Mutex + githubClient map[string]*github.Client +} + +func NewGitHub(logger *zap.Logger) *Github { + return &Github{ + logger: logger, + githubmu: sync.Mutex{}, + } +} + +func (g *Github) ListRepositoriesForOrganization( + ctx context.Context, + server string, + organisation string, +) ([]string, error) { + client, err := g.getOrCreateClient(ctx, server) + + repos, _, err := client.Repositories.ListByOrg(ctx, organisation, &github.RepositoryListByOrgOptions{ + ListOptions: github.ListOptions{ + Page: 0, + PerPage: 0, + }, + }) + + if err != nil { + return nil, err + } + + repoUrls := make([]string, len(repos)) + for i, repo := range repos { + repoUrls[i] = repo.GetCloneURL() + } + + return repoUrls, nil +} + +func (g *Github) getOrCreateClient(ctx context.Context, server string) (*github.Client, error) { + g.githubmu.Lock() + defer g.githubmu.Unlock() + + client, ok := g.githubClient[server] + if !ok || client == nil { + httpClient := &http.Client{} + client := github.NewClient(httpClient) + g.githubClient[server] = client + return client, nil + } + + return client, nil +} diff --git a/scripts/push_github.sh b/scripts/push_github.sh index f8032db..70439fd 100755 --- a/scripts/push_github.sh +++ b/scripts/push_github.sh @@ -4,4 +4,4 @@ set -e git remote add github git@github.com:kjuulh/octopush.git || true -git push -f github main +git push -f github --all