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

feat(review-apps) wording changes #922

Merged
merged 5 commits into from
Mar 30, 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
26 changes: 20 additions & 6 deletions cmd/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"

"net/http"
"net/url"
"os"
Expand All @@ -11,6 +12,7 @@ import (
"time"

"github.com/stvp/rollbar"
"github.com/urfave/cli/v2"
"gopkg.in/errgo.v1"

"github.com/Scalingo/cli/config"
Expand Down Expand Up @@ -56,7 +58,21 @@ func (r *ReportError) Report() {
//rollbar.ErrorWithStack(rollbar.ERR, r.Error, errgorollbar.BuildStack(r.Error), fields...)
}

func errorQuitWithHelpMessage(err error, ctxCli *cli.Context, command string) {
displayError(err)
fmt.Print("\n")
_ = cli.ShowCommandHelp(ctxCli, command)
guillaume-sc marked this conversation as resolved.
Show resolved Hide resolved

os.Exit(1)
}

func errorQuit(err error) {
displayError(err)

os.Exit(1)
}

func displayError(err error) {
currentUser, autherr := config.C.CurrentUser()
if autherr != nil {
debug.Println("Fail to get current user")
Expand All @@ -70,15 +86,13 @@ func errorQuit(err error) {
if httpclient.IsRequestFailedError(rootError) {
displayRequestFailedError(rootError, currentUser, autherr, err)
} else if _, ok := rootError.(config.UnknownRegionError); ok {
displayError(rootError)
displayScalingoError(rootError)
} else {
displayError(err)
displayScalingoError(err)
}

os.Exit(1)
}

func displayError(err error) {
func displayScalingoError(err error) {
io.Error("An error occurred:")
debug.Println(errgo.Details(err))
message := err.Error()
Expand Down Expand Up @@ -122,7 +136,7 @@ func displayRequestFailedError(rootError error, currentUser *scalingo.User, auth
}
return
}
displayError(err)
displayScalingoError(err)
}

func newReportError(currentUser *scalingo.User, err error) *ReportError {
Expand Down
29 changes: 19 additions & 10 deletions cmd/integration_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,38 +141,42 @@ List of available integrations:
autoDeploy := c.Bool("auto-deploy")
noAutoDeploy := c.Bool("no-auto-deploy")
if autoDeploy && noAutoDeploy {
errorQuit(errors.New("cannot define both auto-deploy and no-auto-deploy"))
errorQuitWithHelpMessage(errors.New("cannot define both auto-deploy and no-auto-deploy"), c, "integration-link-create")
}

deployReviewApps := c.Bool("deploy-review-apps")
noDeployReviewApps := c.Bool("no-deploy-review-apps")
if deployReviewApps && noDeployReviewApps {
errorQuit(errors.New("cannot define both deploy-review-apps and no-deploy-review-apps"))
errorQuitWithHelpMessage(errors.New("cannot define both deploy-review-apps and no-deploy-review-apps"), c, "integration-link-create")
}

destroyOnClose := c.Bool("destroy-on-close")
noDestroyOnClose := c.Bool("no-destroy-on-close")
if destroyOnClose && noDestroyOnClose {
errorQuit(errors.New("cannot define both destroy-on-close and no-destroy-on-close"))
errorQuitWithHelpMessage(errors.New("cannot define both destroy-on-close and no-destroy-on-close"), c, "integration-link-create")
}
hoursBeforeDestroyOnClose := c.Uint("hours-before-destroy-on-close")

destroyOnStale := c.Bool("destroy-on-stale")
noDestroyOnStale := c.Bool("no-destroy-on-stale")
if destroyOnStale && noDestroyOnStale {
errorQuit(errors.New("cannot define both destroy-on-stale and no-destroy-on-stale"))
errorQuitWithHelpMessage(errors.New("cannot define both destroy-on-stale and no-destroy-on-stale"), c, "integration-link-create")
}
hoursBeforeDestroyOnStale := c.Uint("hours-before-destroy-on-stale")

allowReviewAppsFromForks := c.Bool("allow-review-apps-from-forks")
noAllowReviewAppsFromForks := c.Bool("no-allow-review-apps-from-forks")

if allowReviewAppsFromForks && noAllowReviewAppsFromForks {
errorQuit(errors.New("cannot define both allow-review-apps-from-forks and no-allow-review-apps-from-forks"))
errorQuitWithHelpMessage(errors.New("cannot define both allow-review-apps-from-forks and no-allow-review-apps-from-forks"), c, "integration-link-create")
}

awareOfSecurityRisks := c.Bool("aware-of-security-risks")

if awareOfSecurityRisks && !c.IsSet("allow-review-apps-from-forks") {
errorQuitWithHelpMessage(errors.New("flag --aware-of-security-risks must be used in conjunction with --allow-review-apps-from-forks"), c, "integration-link-create")
}

if deployReviewApps && allowReviewAppsFromForks && !awareOfSecurityRisks {
allowReviewAppsFromForks, err = askForConfirmationToAllowReviewAppsFromForks()
if err != nil {
Expand Down Expand Up @@ -251,6 +255,7 @@ List of available integrations:
"scalingo --app my-app integration-link-update --auto-deploy --branch test --deploy-review-apps",
"scalingo --app my-app integration-link-update --destroy-on-close --hours-before-destroy-on-close 1",
"scalingo --app my-app integration-link-update --destroy-on-stale --hours-before-destroy-on-stale 2",
"scalingo --app my-app integration-link-update --allow-review-apps-from-forks --aware-of-security-risks",
},
SeeAlso: []string{"integration-link", "integration-link-create", "integration-link-delete", "integration-link-manual-deploy", "integration-link-manual-review-app"},
}.Render(),
Expand All @@ -262,33 +267,37 @@ List of available integrations:
autoDeploy := c.Bool("auto-deploy")
noAutoDeploy := c.Bool("no-auto-deploy")
if autoDeploy && noAutoDeploy {
errorQuit(errors.New("cannot define both auto-deploy and no-auto-deploy"))
errorQuitWithHelpMessage(errors.New("cannot define both auto-deploy and no-auto-deploy"), c, "integration-link-update")
}
deployReviewApps := c.Bool("deploy-review-apps")
noDeployReviewApps := c.Bool("no-deploy-review-apps")
if deployReviewApps && noDeployReviewApps {
errorQuit(errors.New("cannot define both deploy-review-apps and no-deploy-review-apps"))
errorQuitWithHelpMessage(errors.New("cannot define both deploy-review-apps and no-deploy-review-apps"), c, "integration-link-update")
}
destroyOnClose := c.Bool("destroy-on-close")
noDestroyOnClose := c.Bool("no-destroy-on-close")
if destroyOnClose && noDestroyOnClose {
errorQuit(errors.New("cannot define both destroy-on-close and no-destroy-on-close"))
errorQuitWithHelpMessage(errors.New("cannot define both destroy-on-close and no-destroy-on-close"), c, "integration-link-update")
}
destroyOnStale := c.Bool("destroy-on-stale")
noDestroyOnStale := c.Bool("no-destroy-on-stale")
if destroyOnStale && noDestroyOnStale {
errorQuit(errors.New("cannot define both destroy-on-stale and no-destroy-on-stale"))
errorQuitWithHelpMessage(errors.New("cannot define both destroy-on-stale and no-destroy-on-stale"), c, "integration-link-update")
}

allowReviewAppsFromForks := c.Bool("allow-review-apps-from-forks")
noAllowReviewAppsFromForks := c.Bool("no-allow-review-apps-from-forks")

if allowReviewAppsFromForks && noAllowReviewAppsFromForks {
errorQuit(errors.New("cannot define both allow-review-apps-from-forks and no-allow-review-apps-from-forks"))
errorQuitWithHelpMessage(errors.New("cannot define both allow-review-apps-from-forks and no-allow-review-apps-from-forks"), c, "integration-link-update")
}

awareOfSecurityRisks := c.Bool("aware-of-security-risks")

if awareOfSecurityRisks && !c.IsSet("allow-review-apps-from-forks") {
errorQuitWithHelpMessage(errors.New("flag --aware-of-security-risks must be used in conjunction with --allow-review-apps-from-forks"), c, "integration-link-update")
}

currentApp := detect.CurrentApp(c)
params := integrationlink.CheckAndFillParams(c)

Expand Down
151 changes: 68 additions & 83 deletions integrationlink/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package integrationlink

import (
"context"
"fmt"

"gopkg.in/errgo.v1"
"os"
"text/template"

"github.com/fatih/color"

Expand All @@ -16,10 +15,43 @@ import (
"github.com/Scalingo/go-utils/errors/v2"
)

const integrationSettingsTemplateText = `
{{- title "Application:"}} {{ .AppName }} ({{ .AppID }})
{{title "Integration:"}} {{ .SCMType }} ({{ .IntegrationID }})
{{title "Linker:"}} {{.LinkerUsername}}

{{title "Repository:"}} {{ .RepositoryOwner }}/{{ .RepositoryName }}
{{title "Automatic deployment:"}} {{ enabledIcon .AutoDeploy }}{{ if .AutoDeploy }}, {{ .Branch }}{{ end }}
{{title "Automatic deployment of review apps:"}} {{ enabledIcon .AutoDeployReviewApps }}
{{- if .AutoDeployReviewApps }}
{{title "Allow creation from forks:"}} {{ enabledIcon .ReviewAppsFromForks }}
{{title "Automatic destroy on close:"}} {{ enabledIcon .DestroyOnClose }}{{ if .DestroyOnClose }}, {{ if eq .HoursBeforeDestroyOnClose 0 -}} instantly {{- else -}} {{ .HoursBeforeDestroyOnClose }}h{{ end }}{{ end }}
{{title "Automatic destroy on stale:"}} {{ enabledIcon .DestroyOnStale }}{{ if .DestroyOnStale }}, {{ if eq .HoursBeforeDestroyOnStale 0 -}} instantly {{- else -}} {{ .HoursBeforeDestroyOnStale }}h{{ end }}{{ end }}
{{- end}}
`

type IntegrationSettings struct {
AppName, AppID, SCMType, IntegrationID, LinkerUsername, RepositoryOwner, RepositoryName, Branch string
AutoDeploy, AutoDeployReviewApps, ReviewAppsFromForks, DestroyOnClose, DestroyOnStale bool
HoursBeforeDestroyOnClose, HoursBeforeDestroyOnStale uint
}

func title(textToFormat string) string {
return color.New(color.FgYellow).Sprint(textToFormat)
}

func enabledIcon(enabled bool) string {
if enabled {
return color.GreenString(utils.Success)
}

return color.RedString(utils.Error)
}

func Show(ctx context.Context, app string) error {
c, err := config.ScalingoClient(ctx)
if err != nil {
return errgo.Notef(err, "fail to get Scalingo client")
return errors.Notef(ctx, err, "fail to get Scalingo client")
}

repoLink, err := c.SCMRepoLinkShow(ctx, app)
Expand All @@ -28,89 +60,42 @@ func Show(ctx context.Context, app string) error {
return nil
}
if err != nil {
return errgo.Notef(err, "fail to get integration link for this app")
return errors.Notef(ctx, err, "fail to get integration link for this app")
}

fmt.Printf("%s: %s (%s)\n",
color.New(color.FgYellow).Sprint("Application"),
app, repoLink.AppID,
)
fmt.Printf("%s: %s (%s)\n",
color.New(color.FgYellow).Sprint("Integration"),
scalingo.SCMTypeDisplay[repoLink.SCMType], repoLink.AuthIntegrationUUID,
)
fmt.Printf("%s: %s\n",
color.New(color.FgYellow).Sprint("Linker"),
repoLink.Linker.Username,
)
fmt.Println()

fmt.Printf("%s: %s/%s\n",
color.New(color.FgYellow).Sprint("Repository"),
repoLink.Owner, repoLink.Repo,
)
var autoDeploy string
if repoLink.AutoDeployEnabled {
autoDeploy = fmt.Sprintf("%s %s", color.GreenString(utils.Success), repoLink.Branch)
} else {
autoDeploy = color.RedString(utils.Error)
templater, err := template.New("integrationSettings").
Funcs(template.FuncMap{
"enabledIcon": enabledIcon,
"title": title,
}).
Parse(integrationSettingsTemplateText)

if err != nil {
return errors.Notef(ctx, err, "invalid integration settings template")
}
fmt.Printf("%s: %s\n",
color.New(color.FgYellow).Sprint("Auto Deploy"),
autoDeploy,
)

var reviewAppsDeploy string
if repoLink.DeployReviewAppsEnabled {
reviewAppsDeploy = color.GreenString(utils.Success)
} else {
reviewAppsDeploy = color.RedString(utils.Error)

settings := IntegrationSettings{
AppName: app,
AppID: repoLink.AppID,
SCMType: scalingo.SCMTypeDisplay[repoLink.SCMType],
IntegrationID: repoLink.AuthIntegrationUUID,
LinkerUsername: repoLink.Linker.Username,
RepositoryOwner: repoLink.Owner,
RepositoryName: repoLink.Repo,
Branch: repoLink.Branch,
AutoDeploy: repoLink.AutoDeployEnabled,
AutoDeployReviewApps: repoLink.DeployReviewAppsEnabled,
ReviewAppsFromForks: repoLink.AutomaticCreationFromForksAllowed,
DestroyOnClose: repoLink.DeleteOnCloseEnabled,
HoursBeforeDestroyOnClose: repoLink.HoursBeforeDeleteOnClose,
DestroyOnStale: repoLink.DeleteStaleEnabled,
HoursBeforeDestroyOnStale: repoLink.HoursBeforeDeleteStale,
}
fmt.Printf("%s: %v\n",
color.New(color.FgYellow).Sprint("Review Apps Deploy"),
reviewAppsDeploy,
)
if repoLink.DeployReviewAppsEnabled {
var deleteOnClose string
if repoLink.DeleteOnCloseEnabled {
if repoLink.HoursBeforeDeleteOnClose == 0 {
deleteOnClose = "instantly"
} else {
deleteOnClose = fmt.Sprintf("after %dh", repoLink.HoursBeforeDeleteOnClose)
}
} else {
deleteOnClose = color.RedString(utils.Error)
}
fmt.Printf("\t%s: %s\n",
color.New(color.FgYellow).Sprint("Destroy on Close"),
deleteOnClose,
)

var deleteOnStale string
if repoLink.DeleteStaleEnabled {
if repoLink.HoursBeforeDeleteStale == 0 {
deleteOnStale = "instantly"
} else {
deleteOnStale = fmt.Sprintf("after %dh", repoLink.HoursBeforeDeleteStale)
}
} else {
deleteOnStale = color.RedString(utils.Error)
}
fmt.Printf("\t%s: %s\n",
color.New(color.FgYellow).Sprint("Destroy on Stale"),
deleteOnStale,
)

var forksAllowed string
if repoLink.AutomaticCreationFromForksAllowed {
forksAllowed = color.GreenString(utils.Success)
} else {
forksAllowed = color.RedString(utils.Error)
}
fmt.Printf("\t%s: %s\n",
color.New(color.FgYellow).Sprint("Automatic creation from forks"),
forksAllowed,
)

err = templater.Execute(os.Stdout, settings)

if err != nil {
return errors.Notef(ctx, err, "failed rendering integration settings")
}

return nil
Expand Down