@@ -2,21 +2,24 @@ package github
22
33import (
44 "context"
5- "os"
65
7- "github.com/google/go-github/github"
86 "github.com/google/go-github/v53/github"
9- "github.com/gov4git/lib4git/must"
107 "github.com/gov4git/vendor4git"
118 "golang.org/x/oauth2"
129)
1310
14- type GitHubVendor struct {}
11+ type gitHubVendor struct {
12+ accessToken string
13+ }
14+
15+ func NewGitHubVendor (accessToken string ) vendor4git.Vendor {
16+ return & gitHubVendor {accessToken : accessToken }
17+ }
1518
16- func (GitHubVendor ) CreateRepo (ctx context.Context , name string , org string , private bool ) (* Repository , error ) {
19+ func (x * gitHubVendor ) CreateRepo (ctx context.Context , name string , owner string , private bool ) (* vendor4git. Repository , error ) {
1720
1821 ts := oauth2 .StaticTokenSource (
19- & oauth2.Token {AccessToken : os . Getenv ( "GITHUB_ACCESS_TOKEN" )}, //XXX
22+ & oauth2.Token {AccessToken : x . accessToken },
2023 )
2124 tc := oauth2 .NewClient (ctx , ts )
2225 client := github .NewClient (tc )
@@ -25,15 +28,34 @@ func (GitHubVendor) CreateRepo(ctx context.Context, name string, org string, pri
2528 Name : github .String (name ),
2629 Private : github .Bool (private ),
2730 }
28- repo , _ , err := client .Repositories .Create (ctx , org , repo )
29- must .NoError (ctx , err ) //XXX
31+ repo , _ , err := client .Repositories .Create (ctx , owner , repo )
32+ errResp , ok := err .(* github.ErrorResponse )
33+ if ok && errResp .Response .StatusCode == 422 {
34+ return nil , vendor4git .ErrRepoExists
35+ }
36+ if err != nil {
37+ return nil , err
38+ }
3039
3140 return & vendor4git.Repository {
3241 HTTPSURL : repo .GetCloneURL (),
3342 SSHURL : repo .GetSSHURL (),
3443 }, nil
3544}
3645
37- func (GitHubVendor ) RemoveRepo (name string , org string ) error {
38- panic ("XXX" )
46+ func (x * gitHubVendor ) RemoveRepo (ctx context.Context , name string , owner string ) error {
47+
48+ ts := oauth2 .StaticTokenSource (
49+ & oauth2.Token {AccessToken : x .accessToken },
50+ )
51+ tc := oauth2 .NewClient (ctx , ts )
52+ client := github .NewClient (tc )
53+
54+ _ , err := client .Repositories .Delete (ctx , owner , name )
55+ ghErr , ok := err .(* github.ErrorResponse )
56+ if ghErr != nil && ok && ghErr .Response .StatusCode == 404 {
57+ return vendor4git .ErrRepoNotFound
58+ }
59+
60+ return err
3961}
0 commit comments