Skip to content

Commit

Permalink
Merge pull request #5 from felipem1210/feat/pr-without-org-arg
Browse files Browse the repository at this point in the history
feat/pr-without-org-arg
  • Loading branch information
felipem1210 authored Jun 24, 2022
2 parents 3d9be4e + c83a78a commit c5c6c7c
Show file tree
Hide file tree
Showing 17 changed files with 84 additions and 84 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Change the version for the [version](https://github.com/felipem1210/git-helper/t
### Linux amd64

```sh
export GITHELPER_VERSION=0.1.0
export GITHELPER_VERSION=0.1.1
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
sudo mv /tmp/git-helper /usr/local/bin/git-helper
```
Expand All @@ -26,13 +26,13 @@ sudo mv /tmp/git-helper /usr/local/bin/git-helper
export WORKING_DIR=$(pwd)
```

Define `GIT_ACCESS_USER` and `GIT_ACCESS_TOKEN` with your Github Username and Token
* Define `GIT_ACCESS_USER` and `GIT_ACCESS_TOKEN` with your Github Username and Token

## Git integration

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

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

## Github integration

Expand Down
3 changes: 2 additions & 1 deletion cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ var addCmd = &cobra.Command{
Long: `Add all content to each repository, equivalent to make git add -A.`,
Run: func(cmd *cobra.Command, args []string) {
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
repoNames := githelper.GetFromJsonReturnArray(json_file, "Name")
myRepos := githelper.MyRepos{}
repoNames := myRepos.GithubGetRepoNames(json_file)
githelper.GitAdd(repoNames)
},
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ var checkoutCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
branch, _ := cmd.Flags().GetString("branch")
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
repoNames := githelper.GetFromJsonReturnArray(json_file, "Name")
myRepos := githelper.MyRepos{}
repoNames := myRepos.GithubGetRepoNames(json_file)
githelper.GitCheckout(repoNames, branch)
},
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ var cloneCmd = &cobra.Command{
org, _ := cmd.Flags().GetString("org")
if provider == "github" && json_file != "" {
myRepos := githelper.MyRepos{}
repoNames = myRepos.GithubGetRepoNames(json_file)
repoUrls = myRepos.GithubGetCloneUrls(json_file)
if _, err := os.Stat(json_file); errors.Is(err, os.ErrNotExist) {
myRepos = myRepos.GetGithubRepositoriesInfo(org)
err := githelper.WriteReposToJson(myRepos, json_file)
Expand All @@ -47,10 +49,7 @@ var cloneCmd = &cobra.Command{
color.Green("The json file %s with repo info was written sucessfully", json_file)
}
}
repoNames = githelper.GetFromJsonReturnArray(json_file, "Name")
repoUrls = githelper.GetFromJsonReturnArray(json_file, "CloneURL")
}

githelper.GitClone(repoNames, repoUrls)
},
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ var commitCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
message, _ := cmd.Flags().GetString("message")
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
repoNames := githelper.GetFromJsonReturnArray(json_file, "Name")
myRepos := githelper.MyRepos{}
repoNames := myRepos.GithubGetRepoNames(json_file)
githelper.GitCommit(repoNames, message)
},
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/createBranch.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ var createBranchCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
branch, _ := cmd.Flags().GetString("branch")
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
repoNames := githelper.GetFromJsonReturnArray(json_file, "Name")
myRepos := githelper.MyRepos{}
repoNames := myRepos.GithubGetRepoNames(json_file)
githelper.GitCreateBranch(repoNames, branch)
},
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ var fetchCmd = &cobra.Command{
Long: `Do a git fetch of all branches on each repo. Equivalent to do: git fetch --all.`,
Run: func(cmd *cobra.Command, args []string) {
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
repoNames := githelper.GetFromJsonReturnArray(json_file, "Name")
myRepos := githelper.MyRepos{}
repoNames := myRepos.GithubGetRepoNames(json_file)
githelper.GitFetch(repoNames)
},
}
Expand Down
17 changes: 7 additions & 10 deletions cmd/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,28 @@ var prCmd = &cobra.Command{
repos_json_file, _ := cmd.Flags().GetString("repo-info-json-file")
new_pr_json_file, _ := cmd.Flags().GetString("new-pr-json-file")
pr_info_json_file, _ := cmd.Flags().GetString("pr-info-json-file")
org, _ := cmd.Flags().GetString("org")
reviewers, _ := cmd.Flags().GetStringSlice("reviewers")
create, _ := cmd.Flags().GetBool("create")
update, _ := cmd.Flags().GetBool("update")
merge, _ := cmd.Flags().GetBool("merge")

repoNames := githelper.GetFromJsonReturnArray(repos_json_file, "Name")
repos := githelper.MyRepos{}
repoNames := repos.GithubGetRepoNames(repos_json_file)
org := repos.GithubGetOrg(repos_json_file)

myPrs := githelper.MyPrs{}
myPrsJson := githelper.MyPrsJson{}
if create {
myPrsJson = myPrs.GithubCreatePr(org, repoNames, new_pr_json_file, reviewers)
err := githelper.WritePrsToJson(myPrsJson, pr_info_json_file)
myPrs = myPrs.GithubCreatePr(org, repoNames, new_pr_json_file, reviewers)
err := githelper.WritePrsToJson(myPrs, pr_info_json_file)
if err != nil {
githelper.CheckIfError(err)
} else {
color.Green("The json file %s with pr info was written sucessfully", pr_info_json_file)
}
} else if update {
myPrsJson.GithubEditPr(org, repoNames, pr_info_json_file)
myPrs.GithubEditPr(org, repoNames, pr_info_json_file)
} else if merge {
myPrsJson.GithubMergePr(org, repoNames, pr_info_json_file)
myPrs.GithubMergePr(org, repoNames, pr_info_json_file)
}
},
}
Expand All @@ -66,7 +66,4 @@ func init() {
prCmd.PersistentFlags().BoolP("update", "u", false, "The json file with info of the PRs created.")
prCmd.PersistentFlags().BoolP("merge", "m", false, "The json file with info of the PRs created.")
prCmd.Flags().StringSliceVarP(&reviewers, "reviewers", "r", []string{}, "List of usernames of reviewers for the pull request")
prCmd.PersistentFlags().StringP("org", "o", "", "The github Organization to work with")
prCmd.MarkPersistentFlagRequired("org")

}
3 changes: 2 additions & 1 deletion cmd/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ var pullCmd = &cobra.Command{
Long: `Pull changes of a branch from remote. It will make the pull of current branch`,
Run: func(cmd *cobra.Command, args []string) {
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
repoNames := githelper.GetFromJsonReturnArray(json_file, "Name")
myRepos := githelper.MyRepos{}
repoNames := myRepos.GithubGetRepoNames(json_file)
githelper.GitPull(repoNames)
},
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ var pushCmd = &cobra.Command{
Long: `Push changes of a branch from remote. It will make the Push of current branch.`,
Run: func(cmd *cobra.Command, args []string) {
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
repoNames := githelper.GetFromJsonReturnArray(json_file, "Name")
myRepos := githelper.MyRepos{}
repoNames := myRepos.GithubGetRepoNames(json_file)
githelper.GitPush(repoNames)
},
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/rebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ If you have conflicts you can pass the argument with "continue" or "abort" optio
fmt.Println("rebase called")
base_branch, _ := cmd.Flags().GetString("base-branch")
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
repoNames := githelper.GetFromJsonReturnArray(json_file, "Name")
myRepos := githelper.MyRepos{}
repoNames := myRepos.GithubGetRepoNames(json_file)
githelper.GitRebase(repoNames, base_branch)
},
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ var resetCmd = &cobra.Command{
Long: `Hard Reset to HEAD in all repositories.`,
Run: func(cmd *cobra.Command, args []string) {
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
repoNames := githelper.GetFromJsonReturnArray(json_file, "Name")
myRepos := githelper.MyRepos{}
repoNames := myRepos.GithubGetRepoNames(json_file)
githelper.GitReset(repoNames)
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "githelper",
Version: "v0.1.0",
Version: "v0.1.1",
Short: "A cli tool to help you manage git in multiple repositories",
Long: `git-helper is a cli tool to help you manage git in multiple repositories
Expand Down
3 changes: 2 additions & 1 deletion cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ var statusCmd = &cobra.Command{
Long: `Print the working tree status of each repository. Equivalent to do git status`,
Run: func(cmd *cobra.Command, args []string) {
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
repoNames := githelper.GetFromJsonReturnArray(json_file, "Name")
myRepos := githelper.MyRepos{}
repoNames := myRepos.GithubGetRepoNames(json_file)
githelper.GitStatus(repoNames)
},
}
Expand Down
4 changes: 2 additions & 2 deletions examples/json-files/new_repos.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[
{
"name": "testing1",
"default_branch": "master",
"master_branch": "master",
"default_branch": "main",
"master_branch": "main",
"organization": {
"name": "MyOrg"
},
Expand Down
79 changes: 45 additions & 34 deletions githelper/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,8 @@ import (
"golang.org/x/oauth2"
)

type jsonStructs interface {
//githubWriteRepoInfo(myRepoInfo, []*github.Repository, string) error
//githubWritePrInfo(string, *github.PullRequest, string) error
GetGithubRepositoriesInfo(org string, f string) MyRepos
GithubCreatePr(org string, r []string, f string) MyPrs
}

type myReposJson struct {
Name string `json:"name"`
CloneURL string `json:"clone_url"`
}

type MyRepos []*github.Repository

type prCreateInfo github.NewPullRequest

type MyPrs []*github.PullRequest

type MyPrsJson []prInfo
type prInfo struct {
Name string `json:"name"`
Title string `json:"title,omitempty"`
PrNumber int `json:"pr_number,omitempty"`
Body string `json:"body,omitempty"`
State string `json:"state,omitempty"`
Base string `json:"base,omitempty"`
Head string `json:"head,omitempty"`
Url string `json:"url,omitempty"`
}

// Authenticate with Github
func githubInitClient() (*github.Client, context.Context) {
gh_token := os.Getenv("GIT_ACCESS_TOKEN")
Expand Down Expand Up @@ -108,10 +80,49 @@ func (myRepos MyRepos) GetGithubRepositoriesInfo(org string) MyRepos {
return myReposComplete
}

func (myPrs MyPrs) GithubCreatePr(org string, repos []string, f string, reviewers []string) MyPrsJson {
func (myRepos MyRepos) GithubGetOrg(f string) string {
var org string
myRepos = myRepos.fromJsontoSliceOfStructs(f)
for _, repo := range myRepos {
org = repo.GetOrganization().GetLogin()
}
return org
}

func (myRepos MyRepos) GithubGetRepoNames(f string) []string {
myRepos = myRepos.fromJsontoSliceOfStructs(f)
var repoNames []string
for _, repo := range myRepos {
repoNames = append(repoNames, repo.GetName())
}
return repoNames
}

func (myRepos MyRepos) GithubGetCloneUrls(f string) []string {
myRepos = myRepos.fromJsontoSliceOfStructs(f)
var repoUrls []string
for _, repo := range myRepos {
repoUrls = append(repoUrls, repo.GetCloneURL())
}
return repoUrls
}

type prCreateInfo github.NewPullRequest
type MyPrs []prInfo
type prInfo struct {
Name string `json:"name"`
Title string `json:"title,omitempty"`
PrNumber int `json:"pr_number,omitempty"`
Body string `json:"body,omitempty"`
State string `json:"state,omitempty"`
Base string `json:"base,omitempty"`
Head string `json:"head,omitempty"`
Url string `json:"url,omitempty"`
}

func (myPrs MyPrs) GithubCreatePr(org string, repos []string, f string, reviewers []string) MyPrs {
prCreateInfoPointer := &prCreateInfo{}
data := prCreateInfoPointer.fromJsontoStruct(f)
myPrJson := MyPrsJson{}
client, ctx := githubInitClient()
pr_options := &github.NewPullRequest{
Title: data.Title,
Expand All @@ -129,7 +140,7 @@ func (myPrs MyPrs) GithubCreatePr(org string, repos []string, f string, reviewer
}
fmt.Printf("PR created for repo: %s\n Url: %s\n", repo, pr_info.GetHTMLURL())
my_pr_info := githubWritePrInfo(repo, pr_info)
myPrJson = append(myPrJson, my_pr_info)
myPrs = append(myPrs, my_pr_info)
if len(reviewers) != 0 {
reviewers := &github.ReviewersRequest{
Reviewers: reviewers,
Expand All @@ -140,10 +151,10 @@ func (myPrs MyPrs) GithubCreatePr(org string, repos []string, f string, reviewer
}
}
}
return myPrJson
return myPrs
}

func (myPrs MyPrsJson) GithubEditPr(org string, repos []string, f string) {
func (myPrs MyPrs) GithubEditPr(org string, repos []string, f string) {
myPrs = myPrs.fromJsontoSliceOfStructs(f)
client, ctx := githubInitClient()
for i, pr := range myPrs {
Expand All @@ -165,7 +176,7 @@ func (myPrs MyPrsJson) GithubEditPr(org string, repos []string, f string) {
}
}

func (myPrs MyPrsJson) GithubMergePr(org string, repos []string, f string) {
func (myPrs MyPrs) GithubMergePr(org string, repos []string, f string) {
myPrs = myPrs.fromJsontoSliceOfStructs(f)
client, ctx := githubInitClient()
for i, pr := range myPrs {
Expand Down
22 changes: 2 additions & 20 deletions githelper/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ import (
"io/ioutil"
"log"
"os"
"reflect"
)

func WritePrsToJson(data MyPrsJson, json_file string) error {
func WritePrsToJson(data MyPrs, json_file string) error {
file, _ := json.MarshalIndent(data, "", " ")
err := ioutil.WriteFile(getEnvValue("WORKDING_DIR")+json_file, file, 0644)
return err
Expand All @@ -37,23 +36,6 @@ func WriteReposToJson(data MyRepos, json_file string) error {
return err
}

func GetFromJsonReturnArray(f string, d string) []string {
// Open our jsonFile
jsonFile, err := ioutil.ReadFile(f)
// if we os.Open returns an error then handle it
CheckIfError(err)
var m []myReposJson
var data []string
err = json.Unmarshal([]byte(jsonFile), &m)
CheckIfError(err)
for _, val := range m {
r := reflect.ValueOf(val)
f := reflect.Indirect(r).FieldByName(d)
data = append(data, f.String())
}
return data
}

func (data *prCreateInfo) fromJsontoStruct(f string) *prCreateInfo {
jsonFile, err := ioutil.ReadFile(f)
CheckIfError(err)
Expand All @@ -62,7 +44,7 @@ func (data *prCreateInfo) fromJsontoStruct(f string) *prCreateInfo {
return data
}

func (data MyPrsJson) fromJsontoSliceOfStructs(f string) MyPrsJson {
func (data MyPrs) fromJsontoSliceOfStructs(f string) MyPrs {
jsonFile, err := ioutil.ReadFile(f)
CheckIfError(err)
err = json.Unmarshal([]byte(jsonFile), &data)
Expand Down

0 comments on commit c5c6c7c

Please sign in to comment.