From 8534910a495ee7bce013872d97cc9fb847bb416b Mon Sep 17 00:00:00 2001 From: felipem1210 Date: Thu, 22 Dec 2022 15:18:13 +0100 Subject: [PATCH] feat: adds new feature to assign repos to teams on repo creation --- README.md | 4 ++-- cmd/repository.go | 10 ++++++---- cmd/root.go | 2 +- examples/json-files/new_repos.json | 4 ++++ githelper/github.go | 18 ++++++++++++++++++ 5 files changed, 31 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index dd3061b..15772e8 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Change the version for the [version](https://github.com/felipem1210/git-helper/t ### Linux amd64 ```sh -export GITHELPER_VERSION=0.2.1 +export GITHELPER_VERSION=0.3.0 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 chmod +x /usr/local/bin/git-helper @@ -22,7 +22,7 @@ chmod +x /usr/local/bin/git-helper ### MacOS amd64 ```sh -export GITHELPER_VERSION=0.2.1 +export GITHELPER_VERSION=0.3.0 curl -L "https://github.com/felipem1210/git-helper/releases/download/v${GITHELPER_VERSION}/git-helper_${GITHELPER_VERSION}_darwin_amd64.tar.gz" |tar xzv -C /tmp sudo mv /tmp/git-helper /usr/local/bin/git-helper chmod +x /usr/local/bin/git-helper diff --git a/cmd/repository.go b/cmd/repository.go index ca319e8..02ff405 100644 --- a/cmd/repository.go +++ b/cmd/repository.go @@ -1,6 +1,5 @@ /* Copyright © 2022 NAME HERE - */ package cmd @@ -20,11 +19,14 @@ var repoCmd = &cobra.Command{ new_repo_json_file, _ := cmd.Flags().GetString("new-repo-json-file") repo_info_json_file, _ := cmd.Flags().GetString("repo-info-json-file") create, _ := cmd.Flags().GetBool("create") - + team, _ := cmd.Flags().GetString("team") if provider == "github" { if create { myRepos := githelper.MyRepos{} myRepos = myRepos.GithubCreateRepos(new_repo_json_file) + if team != "" { + myRepos.GithubAssignTeamToRepo(new_repo_json_file, team) + } err := githelper.WriteReposToJson(myRepos, repo_info_json_file) if err != nil { githelper.CheckIfError(err) @@ -39,8 +41,8 @@ var repoCmd = &cobra.Command{ func init() { rootCmd.AddCommand(repoCmd) repoCmd.PersistentFlags().String("new-repo-json-file", "new_repos.json", "The json file needed to create new repositories.") - repoCmd.PersistentFlags().StringP("provider", "p", "", "A provider to choose, options: gitub, gitlab") - repoCmd.MarkPersistentFlagRequired("provider") + repoCmd.PersistentFlags().StringP("provider", "p", "github", "A provider to choose, options: gitub, gitlab") + repoCmd.PersistentFlags().String("team", "", "A single team to assign to the repository with admin permission. Use the team slug") repoCmd.PersistentFlags().BoolP("create", "c", false, "Create the repositories from repo-info-json-file") repoCmd.PersistentFlags().String("repo-info-json-file", "repos_info.json", "The name of the json file with info of the repos of the Github Org. It is read for each git local actions.") } diff --git a/cmd/root.go b/cmd/root.go index 8ab5679..1183b2b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -26,7 +26,7 @@ import ( // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "githelper", - Version: "v0.2.1", + Version: "v0.3.0", 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 diff --git a/examples/json-files/new_repos.json b/examples/json-files/new_repos.json index 4e05eb1..10c0cc1 100644 --- a/examples/json-files/new_repos.json +++ b/examples/json-files/new_repos.json @@ -10,6 +10,8 @@ "allow_squash_merge": false, "allow_merge_commit": true, "allow_auto_merge": false, + "allow_forking": false, + "allow_update_branch": true, "delete_branch_on_merge": true, "private": true, "auto_init": true @@ -25,6 +27,8 @@ "allow_squash_merge": false, "allow_merge_commit": true, "allow_auto_merge": false, + "allow_forking": false, + "allow_update_branch": true, "delete_branch_on_merge": true, "private": true, "auto_init": true diff --git a/githelper/github.go b/githelper/github.go index 3995602..b9f2022 100644 --- a/githelper/github.go +++ b/githelper/github.go @@ -47,6 +47,7 @@ func (myRepos MyRepos) GithubCreateRepos(f string) MyRepos { client, ctx := githubInitClient() for _, repo := range myRepos { repo_options := &github.Repository{ + AllowForking: repo.AllowForking, Name: repo.Name, DefaultBranch: repo.DefaultBranch, MasterBranch: repo.MasterBranch, @@ -55,6 +56,7 @@ func (myRepos MyRepos) GithubCreateRepos(f string) MyRepos { AllowSquashMerge: repo.AllowSquashMerge, AllowMergeCommit: repo.AllowMergeCommit, AllowAutoMerge: repo.AllowAutoMerge, + AllowUpdateBranch: repo.AllowUpdateBranch, DeleteBranchOnMerge: repo.DeleteBranchOnMerge, Private: repo.Private, AutoInit: repo.AutoInit, @@ -69,6 +71,22 @@ func (myRepos MyRepos) GithubCreateRepos(f string) MyRepos { return myReposComplete } +// Assigns a github team to a repository +func (myRepos MyRepos) GithubAssignTeamToRepo(f string, team string) { + myRepos = myRepos.fromJsontoSliceOfStructs(f) + client, ctx := githubInitClient() + for _, repo := range myRepos { + org := *repo.GetOrganization().Name + color.Green("Assigning team %s to repo %s\n", team, repo.GetName()) + _, err := client.Teams.AddTeamRepoBySlug(ctx, org, team, org, repo.GetName(), &github.TeamAddTeamRepoOptions{ + Permission: "admin", + }) + if err != nil { + CheckIfError(err) + } + } +} + func (myRepos MyRepos) GetGithubRepositoriesInfo(org string) MyRepos { var myReposComplete MyRepos client, ctx := githubInitClient()