Skip to content

Commit c5c6c7c

Browse files
authored
Merge pull request #5 from felipem1210/feat/pr-without-org-arg
feat/pr-without-org-arg
2 parents 3d9be4e + c83a78a commit c5c6c7c

17 files changed

+84
-84
lines changed

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Change the version for the [version](https://github.com/felipem1210/git-helper/t
1313
### Linux amd64
1414

1515
```sh
16-
export GITHELPER_VERSION=0.1.0
16+
export GITHELPER_VERSION=0.1.1
1717
curl -L "https://github.com/felipem1210/git-helper/releases/download/v${GITHELPER_VERSION}/git-helper_${GITHELPER_VERSION}_linux_amd64.tar.gz" |tar xzv -C /tmp
1818
sudo mv /tmp/git-helper /usr/local/bin/git-helper
1919
```
@@ -26,13 +26,13 @@ sudo mv /tmp/git-helper /usr/local/bin/git-helper
2626
export WORKING_DIR=$(pwd)
2727
```
2828

29-
Define `GIT_ACCESS_USER` and `GIT_ACCESS_TOKEN` with your Github Username and Token
29+
* Define `GIT_ACCESS_USER` and `GIT_ACCESS_TOKEN` with your Github Username and Token
3030

3131
## Git integration
3232

33-
You can run local git commands alongside multiple repositories that are inside an specific folder:
33+
You can run local git commands alongside multiple repositories that are inside the `$WORKING_DIR`:
3434

35-
* clone (Repos grouped in a Gitlab Group or Github Org).
35+
* clone (Repos grouped in a Github Org).
3636
* checkout
3737
* add all new content
3838
* create new branch locally
@@ -42,6 +42,7 @@ You can run local git commands alongside multiple repositories that are inside a
4242
* rebase
4343
* push
4444
* fetch
45+
* status
4546

4647
## Github integration
4748

cmd/add.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ var addCmd = &cobra.Command{
2828
Long: `Add all content to each repository, equivalent to make git add -A.`,
2929
Run: func(cmd *cobra.Command, args []string) {
3030
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
31-
repoNames := githelper.GetFromJsonReturnArray(json_file, "Name")
31+
myRepos := githelper.MyRepos{}
32+
repoNames := myRepos.GithubGetRepoNames(json_file)
3233
githelper.GitAdd(repoNames)
3334
},
3435
}

cmd/checkout.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ var checkoutCmd = &cobra.Command{
2929
Run: func(cmd *cobra.Command, args []string) {
3030
branch, _ := cmd.Flags().GetString("branch")
3131
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
32-
repoNames := githelper.GetFromJsonReturnArray(json_file, "Name")
32+
myRepos := githelper.MyRepos{}
33+
repoNames := myRepos.GithubGetRepoNames(json_file)
3334
githelper.GitCheckout(repoNames, branch)
3435
},
3536
}

cmd/clone.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ var cloneCmd = &cobra.Command{
3838
org, _ := cmd.Flags().GetString("org")
3939
if provider == "github" && json_file != "" {
4040
myRepos := githelper.MyRepos{}
41+
repoNames = myRepos.GithubGetRepoNames(json_file)
42+
repoUrls = myRepos.GithubGetCloneUrls(json_file)
4143
if _, err := os.Stat(json_file); errors.Is(err, os.ErrNotExist) {
4244
myRepos = myRepos.GetGithubRepositoriesInfo(org)
4345
err := githelper.WriteReposToJson(myRepos, json_file)
@@ -47,10 +49,7 @@ var cloneCmd = &cobra.Command{
4749
color.Green("The json file %s with repo info was written sucessfully", json_file)
4850
}
4951
}
50-
repoNames = githelper.GetFromJsonReturnArray(json_file, "Name")
51-
repoUrls = githelper.GetFromJsonReturnArray(json_file, "CloneURL")
5252
}
53-
5453
githelper.GitClone(repoNames, repoUrls)
5554
},
5655
}

cmd/commit.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ var commitCmd = &cobra.Command{
2929
Run: func(cmd *cobra.Command, args []string) {
3030
message, _ := cmd.Flags().GetString("message")
3131
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
32-
repoNames := githelper.GetFromJsonReturnArray(json_file, "Name")
32+
myRepos := githelper.MyRepos{}
33+
repoNames := myRepos.GithubGetRepoNames(json_file)
3334
githelper.GitCommit(repoNames, message)
3435
},
3536
}

cmd/createBranch.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ var createBranchCmd = &cobra.Command{
2929
Run: func(cmd *cobra.Command, args []string) {
3030
branch, _ := cmd.Flags().GetString("branch")
3131
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
32-
repoNames := githelper.GetFromJsonReturnArray(json_file, "Name")
32+
myRepos := githelper.MyRepos{}
33+
repoNames := myRepos.GithubGetRepoNames(json_file)
3334
githelper.GitCreateBranch(repoNames, branch)
3435
},
3536
}

cmd/fetch.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ var fetchCmd = &cobra.Command{
2828
Long: `Do a git fetch of all branches on each repo. Equivalent to do: git fetch --all.`,
2929
Run: func(cmd *cobra.Command, args []string) {
3030
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
31-
repoNames := githelper.GetFromJsonReturnArray(json_file, "Name")
31+
myRepos := githelper.MyRepos{}
32+
repoNames := myRepos.GithubGetRepoNames(json_file)
3233
githelper.GitFetch(repoNames)
3334
},
3435
}

cmd/pr.go

+7-10
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,28 @@ var prCmd = &cobra.Command{
3131
repos_json_file, _ := cmd.Flags().GetString("repo-info-json-file")
3232
new_pr_json_file, _ := cmd.Flags().GetString("new-pr-json-file")
3333
pr_info_json_file, _ := cmd.Flags().GetString("pr-info-json-file")
34-
org, _ := cmd.Flags().GetString("org")
3534
reviewers, _ := cmd.Flags().GetStringSlice("reviewers")
3635
create, _ := cmd.Flags().GetBool("create")
3736
update, _ := cmd.Flags().GetBool("update")
3837
merge, _ := cmd.Flags().GetBool("merge")
3938

40-
repoNames := githelper.GetFromJsonReturnArray(repos_json_file, "Name")
39+
repos := githelper.MyRepos{}
40+
repoNames := repos.GithubGetRepoNames(repos_json_file)
41+
org := repos.GithubGetOrg(repos_json_file)
4142

4243
myPrs := githelper.MyPrs{}
43-
myPrsJson := githelper.MyPrsJson{}
4444
if create {
45-
myPrsJson = myPrs.GithubCreatePr(org, repoNames, new_pr_json_file, reviewers)
46-
err := githelper.WritePrsToJson(myPrsJson, pr_info_json_file)
45+
myPrs = myPrs.GithubCreatePr(org, repoNames, new_pr_json_file, reviewers)
46+
err := githelper.WritePrsToJson(myPrs, pr_info_json_file)
4747
if err != nil {
4848
githelper.CheckIfError(err)
4949
} else {
5050
color.Green("The json file %s with pr info was written sucessfully", pr_info_json_file)
5151
}
5252
} else if update {
53-
myPrsJson.GithubEditPr(org, repoNames, pr_info_json_file)
53+
myPrs.GithubEditPr(org, repoNames, pr_info_json_file)
5454
} else if merge {
55-
myPrsJson.GithubMergePr(org, repoNames, pr_info_json_file)
55+
myPrs.GithubMergePr(org, repoNames, pr_info_json_file)
5656
}
5757
},
5858
}
@@ -66,7 +66,4 @@ func init() {
6666
prCmd.PersistentFlags().BoolP("update", "u", false, "The json file with info of the PRs created.")
6767
prCmd.PersistentFlags().BoolP("merge", "m", false, "The json file with info of the PRs created.")
6868
prCmd.Flags().StringSliceVarP(&reviewers, "reviewers", "r", []string{}, "List of usernames of reviewers for the pull request")
69-
prCmd.PersistentFlags().StringP("org", "o", "", "The github Organization to work with")
70-
prCmd.MarkPersistentFlagRequired("org")
71-
7269
}

cmd/pull.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ var pullCmd = &cobra.Command{
2828
Long: `Pull changes of a branch from remote. It will make the pull of current branch`,
2929
Run: func(cmd *cobra.Command, args []string) {
3030
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
31-
repoNames := githelper.GetFromJsonReturnArray(json_file, "Name")
31+
myRepos := githelper.MyRepos{}
32+
repoNames := myRepos.GithubGetRepoNames(json_file)
3233
githelper.GitPull(repoNames)
3334
},
3435
}

cmd/push.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ var pushCmd = &cobra.Command{
2828
Long: `Push changes of a branch from remote. It will make the Push of current branch.`,
2929
Run: func(cmd *cobra.Command, args []string) {
3030
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
31-
repoNames := githelper.GetFromJsonReturnArray(json_file, "Name")
31+
myRepos := githelper.MyRepos{}
32+
repoNames := myRepos.GithubGetRepoNames(json_file)
3233
githelper.GitPush(repoNames)
3334
},
3435
}

cmd/rebase.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ If you have conflicts you can pass the argument with "continue" or "abort" optio
3333
fmt.Println("rebase called")
3434
base_branch, _ := cmd.Flags().GetString("base-branch")
3535
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
36-
repoNames := githelper.GetFromJsonReturnArray(json_file, "Name")
36+
myRepos := githelper.MyRepos{}
37+
repoNames := myRepos.GithubGetRepoNames(json_file)
3738
githelper.GitRebase(repoNames, base_branch)
3839
},
3940
}

cmd/reset.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ var resetCmd = &cobra.Command{
2828
Long: `Hard Reset to HEAD in all repositories.`,
2929
Run: func(cmd *cobra.Command, args []string) {
3030
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
31-
repoNames := githelper.GetFromJsonReturnArray(json_file, "Name")
31+
myRepos := githelper.MyRepos{}
32+
repoNames := myRepos.GithubGetRepoNames(json_file)
3233
githelper.GitReset(repoNames)
3334
},
3435
}

cmd/root.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
// rootCmd represents the base command when called without any subcommands
2727
var rootCmd = &cobra.Command{
2828
Use: "githelper",
29-
Version: "v0.1.0",
29+
Version: "v0.1.1",
3030
Short: "A cli tool to help you manage git in multiple repositories",
3131
Long: `git-helper is a cli tool to help you manage git in multiple repositories
3232

cmd/status.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ var statusCmd = &cobra.Command{
2828
Long: `Print the working tree status of each repository. Equivalent to do git status`,
2929
Run: func(cmd *cobra.Command, args []string) {
3030
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
31-
repoNames := githelper.GetFromJsonReturnArray(json_file, "Name")
31+
myRepos := githelper.MyRepos{}
32+
repoNames := myRepos.GithubGetRepoNames(json_file)
3233
githelper.GitStatus(repoNames)
3334
},
3435
}

examples/json-files/new_repos.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[
22
{
33
"name": "testing1",
4-
"default_branch": "master",
5-
"master_branch": "master",
4+
"default_branch": "main",
5+
"master_branch": "main",
66
"organization": {
77
"name": "MyOrg"
88
},

githelper/github.go

+45-34
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,8 @@ import (
2626
"golang.org/x/oauth2"
2727
)
2828

29-
type jsonStructs interface {
30-
//githubWriteRepoInfo(myRepoInfo, []*github.Repository, string) error
31-
//githubWritePrInfo(string, *github.PullRequest, string) error
32-
GetGithubRepositoriesInfo(org string, f string) MyRepos
33-
GithubCreatePr(org string, r []string, f string) MyPrs
34-
}
35-
36-
type myReposJson struct {
37-
Name string `json:"name"`
38-
CloneURL string `json:"clone_url"`
39-
}
40-
4129
type MyRepos []*github.Repository
4230

43-
type prCreateInfo github.NewPullRequest
44-
45-
type MyPrs []*github.PullRequest
46-
47-
type MyPrsJson []prInfo
48-
type prInfo struct {
49-
Name string `json:"name"`
50-
Title string `json:"title,omitempty"`
51-
PrNumber int `json:"pr_number,omitempty"`
52-
Body string `json:"body,omitempty"`
53-
State string `json:"state,omitempty"`
54-
Base string `json:"base,omitempty"`
55-
Head string `json:"head,omitempty"`
56-
Url string `json:"url,omitempty"`
57-
}
58-
5931
// Authenticate with Github
6032
func githubInitClient() (*github.Client, context.Context) {
6133
gh_token := os.Getenv("GIT_ACCESS_TOKEN")
@@ -108,10 +80,49 @@ func (myRepos MyRepos) GetGithubRepositoriesInfo(org string) MyRepos {
10880
return myReposComplete
10981
}
11082

111-
func (myPrs MyPrs) GithubCreatePr(org string, repos []string, f string, reviewers []string) MyPrsJson {
83+
func (myRepos MyRepos) GithubGetOrg(f string) string {
84+
var org string
85+
myRepos = myRepos.fromJsontoSliceOfStructs(f)
86+
for _, repo := range myRepos {
87+
org = repo.GetOrganization().GetLogin()
88+
}
89+
return org
90+
}
91+
92+
func (myRepos MyRepos) GithubGetRepoNames(f string) []string {
93+
myRepos = myRepos.fromJsontoSliceOfStructs(f)
94+
var repoNames []string
95+
for _, repo := range myRepos {
96+
repoNames = append(repoNames, repo.GetName())
97+
}
98+
return repoNames
99+
}
100+
101+
func (myRepos MyRepos) GithubGetCloneUrls(f string) []string {
102+
myRepos = myRepos.fromJsontoSliceOfStructs(f)
103+
var repoUrls []string
104+
for _, repo := range myRepos {
105+
repoUrls = append(repoUrls, repo.GetCloneURL())
106+
}
107+
return repoUrls
108+
}
109+
110+
type prCreateInfo github.NewPullRequest
111+
type MyPrs []prInfo
112+
type prInfo struct {
113+
Name string `json:"name"`
114+
Title string `json:"title,omitempty"`
115+
PrNumber int `json:"pr_number,omitempty"`
116+
Body string `json:"body,omitempty"`
117+
State string `json:"state,omitempty"`
118+
Base string `json:"base,omitempty"`
119+
Head string `json:"head,omitempty"`
120+
Url string `json:"url,omitempty"`
121+
}
122+
123+
func (myPrs MyPrs) GithubCreatePr(org string, repos []string, f string, reviewers []string) MyPrs {
112124
prCreateInfoPointer := &prCreateInfo{}
113125
data := prCreateInfoPointer.fromJsontoStruct(f)
114-
myPrJson := MyPrsJson{}
115126
client, ctx := githubInitClient()
116127
pr_options := &github.NewPullRequest{
117128
Title: data.Title,
@@ -129,7 +140,7 @@ func (myPrs MyPrs) GithubCreatePr(org string, repos []string, f string, reviewer
129140
}
130141
fmt.Printf("PR created for repo: %s\n Url: %s\n", repo, pr_info.GetHTMLURL())
131142
my_pr_info := githubWritePrInfo(repo, pr_info)
132-
myPrJson = append(myPrJson, my_pr_info)
143+
myPrs = append(myPrs, my_pr_info)
133144
if len(reviewers) != 0 {
134145
reviewers := &github.ReviewersRequest{
135146
Reviewers: reviewers,
@@ -140,10 +151,10 @@ func (myPrs MyPrs) GithubCreatePr(org string, repos []string, f string, reviewer
140151
}
141152
}
142153
}
143-
return myPrJson
154+
return myPrs
144155
}
145156

146-
func (myPrs MyPrsJson) GithubEditPr(org string, repos []string, f string) {
157+
func (myPrs MyPrs) GithubEditPr(org string, repos []string, f string) {
147158
myPrs = myPrs.fromJsontoSliceOfStructs(f)
148159
client, ctx := githubInitClient()
149160
for i, pr := range myPrs {
@@ -165,7 +176,7 @@ func (myPrs MyPrsJson) GithubEditPr(org string, repos []string, f string) {
165176
}
166177
}
167178

168-
func (myPrs MyPrsJson) GithubMergePr(org string, repos []string, f string) {
179+
func (myPrs MyPrs) GithubMergePr(org string, repos []string, f string) {
169180
myPrs = myPrs.fromJsontoSliceOfStructs(f)
170181
client, ctx := githubInitClient()
171182
for i, pr := range myPrs {

githelper/utils.go

+2-20
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ import (
2222
"io/ioutil"
2323
"log"
2424
"os"
25-
"reflect"
2625
)
2726

28-
func WritePrsToJson(data MyPrsJson, json_file string) error {
27+
func WritePrsToJson(data MyPrs, json_file string) error {
2928
file, _ := json.MarshalIndent(data, "", " ")
3029
err := ioutil.WriteFile(getEnvValue("WORKDING_DIR")+json_file, file, 0644)
3130
return err
@@ -37,23 +36,6 @@ func WriteReposToJson(data MyRepos, json_file string) error {
3736
return err
3837
}
3938

40-
func GetFromJsonReturnArray(f string, d string) []string {
41-
// Open our jsonFile
42-
jsonFile, err := ioutil.ReadFile(f)
43-
// if we os.Open returns an error then handle it
44-
CheckIfError(err)
45-
var m []myReposJson
46-
var data []string
47-
err = json.Unmarshal([]byte(jsonFile), &m)
48-
CheckIfError(err)
49-
for _, val := range m {
50-
r := reflect.ValueOf(val)
51-
f := reflect.Indirect(r).FieldByName(d)
52-
data = append(data, f.String())
53-
}
54-
return data
55-
}
56-
5739
func (data *prCreateInfo) fromJsontoStruct(f string) *prCreateInfo {
5840
jsonFile, err := ioutil.ReadFile(f)
5941
CheckIfError(err)
@@ -62,7 +44,7 @@ func (data *prCreateInfo) fromJsontoStruct(f string) *prCreateInfo {
6244
return data
6345
}
6446

65-
func (data MyPrsJson) fromJsontoSliceOfStructs(f string) MyPrsJson {
47+
func (data MyPrs) fromJsontoSliceOfStructs(f string) MyPrs {
6648
jsonFile, err := ioutil.ReadFile(f)
6749
CheckIfError(err)
6850
err = json.Unmarshal([]byte(jsonFile), &data)

0 commit comments

Comments
 (0)