From 0936744ab6922accaa64fccedd25922879a91839 Mon Sep 17 00:00:00 2001 From: Yusef Mohamadi Date: Mon, 11 Apr 2022 22:49:05 +0200 Subject: [PATCH 1/3] Add create repository with an initial commit with empty README --- example/newrepowithreadme/main.go | 50 +++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 example/newrepowithreadme/main.go diff --git a/example/newrepowithreadme/main.go b/example/newrepowithreadme/main.go new file mode 100644 index 00000000000..e2748593c8c --- /dev/null +++ b/example/newrepowithreadme/main.go @@ -0,0 +1,50 @@ +// Copyright 2018 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// The newrepo command utilizes go-github as a cli tool for +// creating new repositories. It takes an auth token as +// an environment variable and creates the new repo under +// the account affiliated with that token. +package main + +import ( + "context" + "flag" + "fmt" + "log" + "os" + + "github.com/google/go-github/v43/github" + "golang.org/x/oauth2" +) + +var ( + name = flag.String("name", "", "Name of repo to create in authenticated user's GitHub account.") + description = flag.String("description", "", "Description of created repo.") + private = flag.Bool("private", false, "Will created repo be private.") + autoInit = flag.Bool("auto-init", false, "Pass true to create an initial commit with empty README.") +) + +func main() { + flag.Parse() + token := os.Getenv("GITHUB_AUTH_TOKEN") + if token == "" { + log.Fatal("Unauthorized: No token present") + } + if *name == "" { + log.Fatal("No name: New repos must be given a name") + } + ctx := context.Background() + ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}) + tc := oauth2.NewClient(ctx, ts) + client := github.NewClient(tc) + + r := &github.Repository{Name: name, Private: private, Description: description,AutoInit: autoInit} + repo, _, err := client.Repositories.Create(ctx, "", r) + if err != nil { + log.Fatal(err) + } + fmt.Printf("Successfully created new repo: %v\n", repo.GetName()) +} From 51df4647e1a6f8565afb511fba405b883d36e4c3 Mon Sep 17 00:00:00 2001 From: Yusef Mohamadi Date: Mon, 11 Apr 2022 22:54:52 +0200 Subject: [PATCH 2/3] Update newRepo example with autoInit flag --- example/newrepo/main.go | 3 +- example/newrepowithreadme/main.go | 50 ------------------------------- 2 files changed, 2 insertions(+), 51 deletions(-) delete mode 100644 example/newrepowithreadme/main.go diff --git a/example/newrepo/main.go b/example/newrepo/main.go index a6d7c10489f..e2748593c8c 100644 --- a/example/newrepo/main.go +++ b/example/newrepo/main.go @@ -24,6 +24,7 @@ var ( name = flag.String("name", "", "Name of repo to create in authenticated user's GitHub account.") description = flag.String("description", "", "Description of created repo.") private = flag.Bool("private", false, "Will created repo be private.") + autoInit = flag.Bool("auto-init", false, "Pass true to create an initial commit with empty README.") ) func main() { @@ -40,7 +41,7 @@ func main() { tc := oauth2.NewClient(ctx, ts) client := github.NewClient(tc) - r := &github.Repository{Name: name, Private: private, Description: description} + r := &github.Repository{Name: name, Private: private, Description: description,AutoInit: autoInit} repo, _, err := client.Repositories.Create(ctx, "", r) if err != nil { log.Fatal(err) diff --git a/example/newrepowithreadme/main.go b/example/newrepowithreadme/main.go deleted file mode 100644 index e2748593c8c..00000000000 --- a/example/newrepowithreadme/main.go +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright 2018 The go-github AUTHORS. All rights reserved. -// -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// The newrepo command utilizes go-github as a cli tool for -// creating new repositories. It takes an auth token as -// an environment variable and creates the new repo under -// the account affiliated with that token. -package main - -import ( - "context" - "flag" - "fmt" - "log" - "os" - - "github.com/google/go-github/v43/github" - "golang.org/x/oauth2" -) - -var ( - name = flag.String("name", "", "Name of repo to create in authenticated user's GitHub account.") - description = flag.String("description", "", "Description of created repo.") - private = flag.Bool("private", false, "Will created repo be private.") - autoInit = flag.Bool("auto-init", false, "Pass true to create an initial commit with empty README.") -) - -func main() { - flag.Parse() - token := os.Getenv("GITHUB_AUTH_TOKEN") - if token == "" { - log.Fatal("Unauthorized: No token present") - } - if *name == "" { - log.Fatal("No name: New repos must be given a name") - } - ctx := context.Background() - ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}) - tc := oauth2.NewClient(ctx, ts) - client := github.NewClient(tc) - - r := &github.Repository{Name: name, Private: private, Description: description,AutoInit: autoInit} - repo, _, err := client.Repositories.Create(ctx, "", r) - if err != nil { - log.Fatal(err) - } - fmt.Printf("Successfully created new repo: %v\n", repo.GetName()) -} From 43b4efeeb99721cb470919a191939bd8da4837f0 Mon Sep 17 00:00:00 2001 From: Yusef Mohamadi Date: Mon, 11 Apr 2022 23:12:38 +0200 Subject: [PATCH 3/3] gofmt --- example/newrepo/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example/newrepo/main.go b/example/newrepo/main.go index e2748593c8c..ed3442473a0 100644 --- a/example/newrepo/main.go +++ b/example/newrepo/main.go @@ -24,7 +24,7 @@ var ( name = flag.String("name", "", "Name of repo to create in authenticated user's GitHub account.") description = flag.String("description", "", "Description of created repo.") private = flag.Bool("private", false, "Will created repo be private.") - autoInit = flag.Bool("auto-init", false, "Pass true to create an initial commit with empty README.") + autoInit = flag.Bool("auto-init", false, "Pass true to create an initial commit with empty README.") ) func main() { @@ -41,7 +41,7 @@ func main() { tc := oauth2.NewClient(ctx, ts) client := github.NewClient(tc) - r := &github.Repository{Name: name, Private: private, Description: description,AutoInit: autoInit} + r := &github.Repository{Name: name, Private: private, Description: description, AutoInit: autoInit} repo, _, err := client.Repositories.Create(ctx, "", r) if err != nil { log.Fatal(err)