Skip to content

Commit

Permalink
Merge pull request #9 from felipem1210/feat/adds-team-repo-assignment
Browse files Browse the repository at this point in the history
Feat/adds-team-repo-assignment
  • Loading branch information
felipem1210 authored Dec 22, 2022
2 parents 09d2a9d + 8534910 commit 5336798
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
4 changes: 2 additions & 2 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.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
Expand All @@ -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
Expand Down
10 changes: 6 additions & 4 deletions cmd/repository.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

Expand All @@ -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)
Expand All @@ -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.")
}
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.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
Expand Down
4 changes: 4 additions & 0 deletions examples/json-files/new_repos.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
18 changes: 18 additions & 0 deletions githelper/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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()
Expand Down

0 comments on commit 5336798

Please sign in to comment.