Skip to content

Commit 31edfa8

Browse files
authored
Accept org name from env variable (#20)
* fix test config for sonar * generalize authentication to github * upgrade go version in go.mod * return back sonar config * accept org name from env vars too * add run*.sh files to gitignore
1 parent 81654a7 commit 31edfa8

12 files changed

+41
-38
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
1313
artifacts/
14-
run.sh
14+
run*.sh
1515
ghctl

.sonarcloud.properties

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
# Path to sources
22
sonar.sources=.
3-
#sonar.exclusions=
3+
#sonar.exclusions=**/*_test.go
44
#sonar.inclusions=
55

66
# Path to tests
77
#sonar.tests=.
88
#sonar.test.exclusions=
9-
#sonar.test.inclusions=**/**_test.go
9+
#sonar.test.inclusions=**/*_test.go
1010

1111
# Source encoding
1212
#sonar.sourceEncoding=UTF-8
1313

1414
# Exclusions for copy-paste detection
15-
#sonar.cpd.exclusions=
15+
#sonar.cpd.exclusions=
16+
17+
# Properties specific to Go
18+
#sonar.go.tests.reportPaths=report.json
19+
#sonar.go.coverage.reportPaths=coverage.out

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change log
22

3+
## v0.1.8 (2019-11-08)
4+
5+
- Accept org name from env variable: GITHUB_ORG
6+
37
## v0.1.7 (2019-11-01)
48

59
- Update go-arg to version 1.2.0

client.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"context"
5+
6+
"github.com/google/go-github/v28/github"
7+
"golang.org/x/oauth2"
8+
)
9+
10+
func createGithubClient(ctx context.Context) *github.Client {
11+
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: args.Token})
12+
tc := oauth2.NewClient(ctx, ts)
13+
client := github.NewClient(tc)
14+
return client
15+
}

flag.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ package main
22

33
type Args struct {
44
Token string `arg:"env:GITHUB_TOKEN,required"`
5-
Org string `arg:"required"`
6-
OutputFormat string `arg:"-o" help:"output format: normal, json"`
5+
Org string `arg:"env:GITHUB_ORG,required"`
6+
OutputFormat string `arg:"-o" help:"output format: normal, json" default:"normal"`
7+
Verbose bool `arg:"-v" default:"false"`
78

89
Get *Get `arg:"subcommand:get"`
910
Create *Create `arg:"subcommand:create"`
1011
Add *Add `arg:"subcommand:add"`
1112
}
1213

1314
func (Args) Version() string {
14-
return "v0.1.7"
15+
return "v0.1.8"
1516
}
1617

1718
type Get struct {
@@ -82,7 +83,7 @@ type Files struct {
8283
Files []string `arg:"-f,required"`
8384
GitName string `arg:"-n,required"`
8485
GitEmail string `arg:"-e,required"`
85-
CommitMessage string `arg:"-m"`
86+
CommitMessage string `arg:"-m,--gitmessage"`
8687
}
8788
type Collaborator struct {
8889
Repo string `arg:"-r,required"`

git.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const gitUsername = "some-user"
2222
func createBranch(org string, repo string, branch string, format string) {
2323
auth := &http.BasicAuth{
2424
Username: gitUsername, // anything except an empty string
25-
Password: githubToken(),
25+
Password: args.Token,
2626
}
2727

2828
// Clone the given repository to the memory
@@ -59,7 +59,7 @@ func createBranch(org string, repo string, branch string, format string) {
5959
func addFiles(org, repo, branch string, files []string, commitmessage, gitName, gitEmail, format string) {
6060
auth := &http.BasicAuth{
6161
Username: gitUsername, // anything except an empty string
62-
Password: githubToken(),
62+
Password: args.Token,
6363
}
6464

6565
dir, err := ioutil.TempDir("", repo+"-"+branch)

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/amirashad/ghctl
22

3-
go 1.13
3+
go 1.13.4
44

55
require (
66
github.com/alexflint/go-arg v1.2.0

main.go

-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
var args Args
88

99
func main() {
10-
args.OutputFormat = "normal"
1110
arg.MustParse(&args)
1211
// fmt.Println(args)
1312

@@ -53,7 +52,3 @@ func main() {
5352
args.Create.Protection.RequiredStatusChecks)
5453
}
5554
}
56-
57-
func githubToken() string {
58-
return args.Token
59-
}

member.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@ import (
88
"sort"
99

1010
"github.com/google/go-github/v28/github"
11-
"golang.org/x/oauth2"
1211
)
1312

1413
func getMembers(org string, format string) {
1514
ctx := context.Background()
16-
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: githubToken()})
17-
tc := oauth2.NewClient(ctx, ts)
18-
client := github.NewClient(tc)
15+
client := createGithubClient(ctx)
1916

2017
opt := &github.ListMembersOptions{ListOptions: github.ListOptions{PerPage: 100}}
2118
var objsAll []*github.User

protection.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@ import (
55
"fmt"
66

77
"github.com/google/go-github/v28/github"
8-
"golang.org/x/oauth2"
98
)
109

1110
func createProtection(org, repoName, protectionPattern string, minApprove int, dismissStalePrApprovals, codeOwner bool,
1211
requireBranchesUptodate, includeAdmins bool,
1312
canDismiss, canDismissTeams, canPush, canPushTeams []string,
1413
requiredStatusChecks []string) {
1514
ctx := context.Background()
16-
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: githubToken()})
17-
tc := oauth2.NewClient(ctx, ts)
18-
client := github.NewClient(tc)
15+
client := createGithubClient(ctx)
1916

2017
preq := &github.ProtectionRequest{
2118
RequiredPullRequestReviews: &github.PullRequestReviewsEnforcementRequest{

repo.go

+3-10
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@ import (
88
"sort"
99

1010
"github.com/google/go-github/v28/github"
11-
"golang.org/x/oauth2"
1211
)
1312

1413
func getRepos(org string, format string) {
1514
ctx := context.Background()
16-
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: githubToken()})
17-
tc := oauth2.NewClient(ctx, ts)
18-
client := github.NewClient(tc)
15+
client := createGithubClient(ctx)
1916

2017
opt := &github.RepositoryListByOrgOptions{ /*Type: "private", */ ListOptions: github.ListOptions{PerPage: 100}}
2118
var objsAll []*github.Repository
@@ -53,9 +50,7 @@ func createRepo(org string,
5350
noMergeCommit, noSquashMerge, noRebaseMerge *bool,
5451
format string) {
5552
ctx := context.Background()
56-
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: githubToken()})
57-
tc := oauth2.NewClient(ctx, ts)
58-
client := github.NewClient(tc)
53+
client := createGithubClient(ctx)
5954

6055
repo := &github.Repository{
6156
Name: name,
@@ -102,9 +97,7 @@ func not(o *bool) *bool {
10297
func addCollaboratorToRepo(org string,
10398
repo, user, permission string) {
10499
ctx := context.Background()
105-
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: githubToken()})
106-
tc := oauth2.NewClient(ctx, ts)
107-
client := github.NewClient(tc)
100+
client := createGithubClient(ctx)
108101

109102
perm := &github.RepositoryAddCollaboratorOptions{
110103
Permission: permission,

team.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@ import (
88
"sort"
99

1010
"github.com/google/go-github/v28/github"
11-
"golang.org/x/oauth2"
1211
)
1312

1413
func getTeams(org string, format string) {
1514
ctx := context.Background()
16-
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: githubToken()})
17-
tc := oauth2.NewClient(ctx, ts)
18-
client := github.NewClient(tc)
15+
client := createGithubClient(ctx)
1916

2017
opt := &github.ListOptions{PerPage: 100}
2118
var objsAll []*github.Team

0 commit comments

Comments
 (0)