Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaning up #1

Merged
merged 5 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,21 @@ Organization level variables can be created from a `csv` file using `--from-file
* If specifying a Source Organization (`--source-organization`) to retrieve secrets and create under a new Org, the `--source-token` is required.

```sh
$ gh seva secrets create -h
Create Actions, Dependabot, and/or Codespaces secrets for an organization and/or repositories from a file.
$ gh seva variables create -h

Create Organization Actions variables for a specified organization or organization and repositories level variables from a file.

Usage:
seva secrets create <organization> [flags]
seva variables create <organization> [flags]

Flags:
-d, --debug To debug logging
-f, --from-file string Path and Name of CSV file to create webhooks from (required)
--hostname string GitHub Enterprise Server hostname (default "github.com")
-t, --token string GitHub personal access token for organization to write to (default "gh auth token")
-d, --debug To debug logging
-f, --from-file string Path and Name of CSV file to create variables from
--hostname string GitHub Enterprise Server hostname (default "github.com")
--source-hostname string GitHub Enterprise Server hostname where variables are copied from (default "github.com")
-o, --source-organization string Name of the Source Organization to copy variables from (Requires --source-token)
-s, --source-token string GitHub personal access token for Source Organization (Required for --source-organization)
-t, --token string GitHub personal access token for organization to write to (default "gh auth token")

Global Flags:
--help Show help for command
Expand Down
21 changes: 11 additions & 10 deletions cmd/secrets/create/create.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package secrets
package createsecrets

import (
"bytes"
Expand All @@ -12,6 +12,7 @@ import (
"github.com/cli/go-gh/pkg/auth"
"github.com/katiem0/gh-seva/internal/data"
"github.com/katiem0/gh-seva/internal/log"
"github.com/katiem0/gh-seva/internal/utils"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -80,21 +81,21 @@ func NewCmdCreate() *cobra.Command {

owner := args[0]

return runCmdCreate(owner, &cmdFlags, data.NewAPIGetter(gqlClient, restClient))
return runCmdCreate(owner, &cmdFlags, utils.NewAPIGetter(gqlClient, restClient))
},
}

// Configure flags for command
createCmd.PersistentFlags().StringVarP(&cmdFlags.token, "token", "t", "", `GitHub personal access token for organization to write to (default "gh auth token")`)
createCmd.PersistentFlags().StringVarP(&cmdFlags.hostname, "hostname", "", "github.com", "GitHub Enterprise Server hostname")
createCmd.Flags().StringVarP(&cmdFlags.fileName, "from-file", "f", "", "Path and Name of CSV file to create webhooks from (required)")
createCmd.Flags().StringVarP(&cmdFlags.fileName, "from-file", "f", "", "Path and Name of CSV file to create secrets from (required)")
createCmd.PersistentFlags().BoolVarP(&cmdFlags.debug, "debug", "d", false, "To debug logging")
createCmd.MarkFlagRequired("from-file")

return &createCmd
}

func runCmdCreate(owner string, cmdFlags *cmdFlags, g *data.APIGetter) error {
func runCmdCreate(owner string, cmdFlags *cmdFlags, g *utils.APIGetter) error {
var secretData [][]string
var importSecretList []data.ImportedSecret
if len(cmdFlags.fileName) > 0 {
Expand Down Expand Up @@ -136,7 +137,7 @@ func runCmdCreate(owner string, cmdFlags *cmdFlags, g *data.APIGetter) error {
return err
}
zap.S().Debugf("Creating Organization Actions Secret Data for %s", importSecret.Name)
orgSecretObject := data.CreateOrgSecretData(importSecret, responsePublicKey.KeyID, encryptedSecret)
orgSecretObject := utils.CreateOrgSecretData(importSecret, responsePublicKey.KeyID, encryptedSecret)
createSecret, err := json.Marshal(orgSecretObject)

if err != nil {
Expand Down Expand Up @@ -165,7 +166,7 @@ func runCmdCreate(owner string, cmdFlags *cmdFlags, g *data.APIGetter) error {
if err != nil {
return err
}
orgSecretObject := data.CreateOrgSecretData(importSecret, responsePublicKey.KeyID, encryptedSecret)
orgSecretObject := utils.CreateOrgSecretData(importSecret, responsePublicKey.KeyID, encryptedSecret)
createSecret, err := json.Marshal(orgSecretObject)

if err != nil {
Expand Down Expand Up @@ -194,7 +195,7 @@ func runCmdCreate(owner string, cmdFlags *cmdFlags, g *data.APIGetter) error {
if err != nil {
return err
}
orgSecretObject := data.CreateOrgDependabotSecretData(importSecret, responsePublicKey.KeyID, encryptedSecret)
orgSecretObject := utils.CreateOrgDependabotSecretData(importSecret, responsePublicKey.KeyID, encryptedSecret)
createSecret, err := json.Marshal(orgSecretObject)

if err != nil {
Expand Down Expand Up @@ -231,7 +232,7 @@ func runCmdCreate(owner string, cmdFlags *cmdFlags, g *data.APIGetter) error {
return err
}
zap.S().Debugf("Creating Repository Actions Secret Data for %s", importSecret.Name)
repoSecretObject := data.CreateRepoSecretData(responsePublicKey.KeyID, encryptedSecret)
repoSecretObject := utils.CreateRepoSecretData(responsePublicKey.KeyID, encryptedSecret)
createSecret, err := json.Marshal(repoSecretObject)

if err != nil {
Expand Down Expand Up @@ -260,7 +261,7 @@ func runCmdCreate(owner string, cmdFlags *cmdFlags, g *data.APIGetter) error {
if err != nil {
return err
}
repoSecretObject := data.CreateRepoSecretData(responsePublicKey.KeyID, encryptedSecret)
repoSecretObject := utils.CreateRepoSecretData(responsePublicKey.KeyID, encryptedSecret)
createSecret, err := json.Marshal(repoSecretObject)

if err != nil {
Expand Down Expand Up @@ -289,7 +290,7 @@ func runCmdCreate(owner string, cmdFlags *cmdFlags, g *data.APIGetter) error {
if err != nil {
return err
}
repoSecretObject := data.CreateRepoSecretData(responsePublicKey.KeyID, encryptedSecret)
repoSecretObject := utils.CreateRepoSecretData(responsePublicKey.KeyID, encryptedSecret)
createSecret, err := json.Marshal(repoSecretObject)

if err != nil {
Expand Down
20 changes: 14 additions & 6 deletions cmd/secrets/export/export.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package secrets
package exportsecrets

import (
"encoding/csv"
Expand All @@ -16,6 +16,7 @@ import (
"github.com/cli/go-gh/pkg/auth"
"github.com/katiem0/gh-seva/internal/data"
"github.com/katiem0/gh-seva/internal/log"
"github.com/katiem0/gh-seva/internal/utils"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -96,7 +97,7 @@ func NewCmdExport() *cobra.Command {
return err
}

return runCmdExport(owner, repos, &cmdFlags, data.NewAPIGetter(gqlClient, restClient), reportWriter)
return runCmdExport(owner, repos, &cmdFlags, utils.NewAPIGetter(gqlClient, restClient), reportWriter)
},
}

Expand All @@ -115,7 +116,7 @@ func NewCmdExport() *cobra.Command {
return &exportCmd
}

func runCmdExport(owner string, repos []string, cmdFlags *cmdFlags, g *data.APIGetter, reportWriter io.Writer) error {
func runCmdExport(owner string, repos []string, cmdFlags *cmdFlags, g *utils.APIGetter, reportWriter io.Writer) error {
var reposCursor *string
var allRepos []data.RepoInfo

Expand Down Expand Up @@ -189,7 +190,7 @@ func runCmdExport(owner string, repos []string, cmdFlags *cmdFlags, g *data.APIG
if err != nil {
zap.S().Error("Error raised in writing output", zap.Error(err))
}
var responseOObject data.ScopedSecretsResponse
var responseOObject data.ScopedResponse
err = json.Unmarshal(scoped_repo, &responseOObject)
if err != nil {
return err
Expand Down Expand Up @@ -256,6 +257,7 @@ func runCmdExport(owner string, repos []string, cmdFlags *cmdFlags, g *data.APIG

// Writing to CSV Org level Dependabot secrets
if len(repos) == 0 && (cmdFlags.app == "all" || cmdFlags.app == "dependabot") {
zap.S().Debugf("Gathering Dependabot Secrets for %s", owner)

orgDepSecrets, err := g.GetOrgDependabotSecrets(owner)
if err != nil {
Expand All @@ -274,7 +276,7 @@ func runCmdExport(owner string, repos []string, cmdFlags *cmdFlags, g *data.APIG
if err != nil {
return err
}
var rDepResponseObject data.ScopedSecretsResponse
var rDepResponseObject data.ScopedResponse
err = json.Unmarshal(scoped_repo, &rDepResponseObject)
if err != nil {
return err
Expand Down Expand Up @@ -341,6 +343,7 @@ func runCmdExport(owner string, repos []string, cmdFlags *cmdFlags, g *data.APIG

// Writing to CSV Org level Codespaces secrets
if len(repos) == 0 && (cmdFlags.app == "all" || cmdFlags.app == "codespaces") {
zap.S().Debugf("Gathering Codespaces Secrets for %s", owner)

orgCodeSecrets, err := g.GetOrgCodespacesSecrets(owner)
if err != nil {
Expand All @@ -359,7 +362,7 @@ func runCmdExport(owner string, repos []string, cmdFlags *cmdFlags, g *data.APIG
if err != nil {
return err
}
var rCodeResponseObject data.ScopedSecretsResponse
var rCodeResponseObject data.ScopedResponse
err = json.Unmarshal(scoped_repo, &rCodeResponseObject)
if err != nil {
return err
Expand Down Expand Up @@ -426,8 +429,11 @@ func runCmdExport(owner string, repos []string, cmdFlags *cmdFlags, g *data.APIG

// Writing to CSV repository level Secrets
for _, singleRepo := range allRepos {
zap.S().Debugf("Gathering Secrets for repo %s", singleRepo.Name)

// Writing to CSV repository level Actions secrets
if cmdFlags.app == "all" || cmdFlags.app == "actions" {
zap.S().Debugf("Gathering Actions Secrets for repo %s", singleRepo.Name)
repoActionSecretsList, err := g.GetRepoActionSecrets(owner, singleRepo.Name)
if err != nil {
return err
Expand All @@ -454,6 +460,7 @@ func runCmdExport(owner string, repos []string, cmdFlags *cmdFlags, g *data.APIG
}
// Writing to CSV repository level Dependabot secrets
if cmdFlags.app == "all" || cmdFlags.app == "dependabot" {
zap.S().Debugf("Gathering Dependabot Secrets for repo %s", singleRepo.Name)
repoDepSecretsList, err := g.GetRepoDependabotSecrets(owner, singleRepo.Name)
if err != nil {
return err
Expand All @@ -480,6 +487,7 @@ func runCmdExport(owner string, repos []string, cmdFlags *cmdFlags, g *data.APIG
}
// Writing to CSV repository level Codespaces secrets
if cmdFlags.app == "all" || cmdFlags.app == "codespaces" {
zap.S().Debugf("Gathering Codespaces Secrets for repo %s", singleRepo.Name)
repoCodeSecretsList, err := g.GetRepoCodespacesSecrets(owner, singleRepo.Name)
if err != nil {
zap.S().Error("Error raised in writing output", zap.Error(err))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/cli/go-gh/pkg/auth"
"github.com/katiem0/gh-seva/internal/data"
"github.com/katiem0/gh-seva/internal/log"
"github.com/katiem0/gh-seva/internal/utils"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -94,7 +95,7 @@ func NewCmdCreate() *cobra.Command {

owner := args[0]

return runCmdCreate(owner, &cmdFlags, data.NewAPIGetter(gqlClient, restClient))
return runCmdCreate(owner, &cmdFlags, utils.NewAPIGetter(gqlClient, restClient))
},
}

Expand All @@ -110,15 +111,15 @@ func NewCmdCreate() *cobra.Command {
return &createCmd
}

func runCmdCreate(owner string, cmdFlags *cmdFlags, g *data.APIGetter) error {
func runCmdCreate(owner string, cmdFlags *cmdFlags, g *utils.APIGetter) error {
var variableData [][]string
var variablesList []data.ImportedVariable

if len(cmdFlags.fileName) > 0 {
f, err := os.Open(cmdFlags.fileName)
zap.S().Debugf("Opening up file %s", cmdFlags.fileName)
if err != nil {
zap.S().Errorf("Error arose opening webhooks csv file")
zap.S().Errorf("Error arose opening variables csv file")
}
// remember to close the file at the end of the program
defer f.Close()
Expand All @@ -137,7 +138,7 @@ func runCmdCreate(owner string, cmdFlags *cmdFlags, g *data.APIGetter) error {

if variable.Level == "Organization" {
zap.S().Debugf("Gathering Organization level variable %s", variable.Name)
importOrgVar := data.CreateOrgVariableData(variable)
importOrgVar := utils.CreateOrgVariableData(variable)
createVariable, err := json.Marshal(importOrgVar)

if err != nil {
Expand All @@ -153,7 +154,7 @@ func runCmdCreate(owner string, cmdFlags *cmdFlags, g *data.APIGetter) error {
} else if variable.Level == "Repository" {
repoName := variable.SelectedRepos[0]
zap.S().Debugf("Gathering Repository level variable %s", variable.Name)
importRepoVar := data.CreateRepoVariableData(variable)
importRepoVar := utils.CreateRepoVariableData(variable)
createVariable, err := json.Marshal(importRepoVar)

if err != nil {
Expand Down Expand Up @@ -194,7 +195,7 @@ func runCmdCreate(owner string, cmdFlags *cmdFlags, g *data.APIGetter) error {

zap.S().Debugf("Gathering variables %s", cmdFlags.sourceOrg)

variableResponse, err := data.GetSourceOrganizationVariables(cmdFlags.sourceOrg, data.NewSourceAPIGetter(restSourceClient))
variableResponse, err := utils.GetSourceOrganizationVariables(cmdFlags.sourceOrg, utils.NewSourceAPIGetter(restSourceClient))
if err != nil {
return err
}
Expand All @@ -207,12 +208,12 @@ func runCmdCreate(owner string, cmdFlags *cmdFlags, g *data.APIGetter) error {
if variable.Visibility == "selected" {
zap.S().Debugf("Creating Scoped Variables under %s", owner)
var orgVariable data.CreateOrgVariable
scoped_repo, err := data.GetScopedSourceOrgActionVariables(cmdFlags.sourceOrg, variable.Name, data.NewSourceAPIGetter(restSourceClient))
scoped_repo, err := utils.GetScopedSourceOrgActionVariables(cmdFlags.sourceOrg, variable.Name, utils.NewSourceAPIGetter(restSourceClient))
if err != nil {
zap.S().Error("Error raised in writing output", zap.Error(err))
}

var responseOObject data.ScopedVariableResponse
var responseOObject data.ScopedResponse
err = json.Unmarshal(scoped_repo, &responseOObject)
if err != nil {
return err
Expand All @@ -238,7 +239,7 @@ func runCmdCreate(owner string, cmdFlags *cmdFlags, g *data.APIGetter) error {
zap.S().Errorf("Error arose creating variable with %s", variable.Name)
}
} else {
orgVariable := data.CreateOrgSourceVariableData(variable)
orgVariable := utils.CreateOrgSourceVariableData(variable)
createOrgVariable, err := json.Marshal(orgVariable)

if err != nil {
Expand Down
Loading