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: add an option to allow review app creation from forks #882

Merged
merged 21 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
c6a741d
feat: added the option to enable or disable review app creation from …
yohann-bacha Feb 3, 2023
f8a2765
fix: changed the option name
yohann-bacha Feb 6, 2023
678c261
Rename the flag for setting the parameter
yohann-bacha Feb 10, 2023
9f67acb
fix: help message for interactive creation
yohann-bacha Feb 21, 2023
7b9f785
feat(review-apps): security warning
yohann-bacha Feb 22, 2023
0bd2178
feat(review-apps): interactive security warning when creating and upd…
yohann-bacha Feb 22, 2023
c67487b
fix(review-apps): linting issues
yohann-bacha Feb 22, 2023
2e3cf1d
fix: apply suggestions from code review
yohann-bacha Feb 23, 2023
bfbf686
fix: applied reviews
yohann-bacha Feb 23, 2023
66972e0
fix(review-apps): when interactive creating, display review apps secu…
yohann-bacha Feb 23, 2023
fc566d7
feat(review-apps): giving some context to errors
yohann-bacha Feb 23, 2023
763a3e4
feat(review-apps): errors have even more context in the integration-l…
yohann-bacha Feb 23, 2023
94c4574
feat(review-apps): more information on the warning message
yohann-bacha Feb 23, 2023
c0d3f61
feat(review-apps): applying pr returns
yohann-bacha Feb 23, 2023
8def4b9
fix(review-apps): trying to fix the linter error
yohann-bacha Feb 23, 2023
60f78fa
fix(review-apps): trying to fix the linter error again
yohann-bacha Feb 23, 2023
8bac5fc
fix: do not prompt when review apps are not deployed
yohann-bacha Feb 27, 2023
6d032af
feat(review-apps): added changelog entry
yohann-bacha Feb 27, 2023
ee8190d
fix(review-apps): change forks allowed on update depending on user aw…
yohann-bacha Feb 27, 2023
0878f3d
fix(review-apps): refactoring the prompt message
yohann-bacha Feb 27, 2023
7d372a7
Improve warning message and prompt
aurelien-reeves-scalingo Feb 28, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* feat(deployments): add Image Size to the list of deployments
* fix(backups): backup flag is not required
* build(publish): replace `rm-dist` with `clean`
* feat(review-apps): permit automatic review app deployment from forks

### 1.27.2

Expand Down
118 changes: 93 additions & 25 deletions cmd/integration_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"strconv"

"gopkg.in/errgo.v1"

"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"

Expand All @@ -22,6 +24,8 @@ import (
)

var (
reviewAppsFromForksSecurityWarning = "Only allow automatic review apps deployments from forks if you trust the owners of those forks, as this could lead to security issues. More info here: https://doc.scalingo.com/platform/app/review-apps#addons-collaborators-and-environment-variables"

integrationLinkShowCommand = cli.Command{
Name: "integration-link",
Category: "Integration Link",
Expand Down Expand Up @@ -57,11 +61,14 @@ var (
&appFlag,
&cli.StringFlag{Name: "branch", Usage: "Branch used in auto deploy"},
&cli.BoolFlag{Name: "auto-deploy", Usage: "Enable auto deploy of application after each branch change"},
&cli.BoolFlag{Name: "deploy-review-apps", Usage: "Enable auto deploy of review app when new pull request is opened"},
&cli.BoolFlag{Name: "deploy-review-apps", Usage: "Enable auto deploy of review apps when new pull request is opened"},
&cli.BoolFlag{Name: "destroy-on-close", Usage: "Auto destroy review apps when pull request is closed"},
&cli.BoolFlag{Name: "no-auto-deploy", Usage: "Enable auto deploy of application after each branch change"},
&cli.BoolFlag{Name: "no-deploy-review-apps", Usage: "Enable auto deploy of review app when new pull request is opened"},
&cli.BoolFlag{Name: "no-auto-deploy", Usage: "Disable auto deploy of application after each branch change"},
&cli.BoolFlag{Name: "no-deploy-review-apps", Usage: "Disable auto deploy of review app when new pull request is opened"},
&cli.BoolFlag{Name: "no-destroy-on-close", Usage: "Auto destroy review apps when pull request is closed"},
&cli.BoolFlag{Name: "allow-review-apps-from-forks", Usage: "Enable auto deploy of review apps when new pull request is opened from a fork"},
&cli.BoolFlag{Name: "aware-of-security-risks", Usage: "Bypass the security warning about allowing automatic review app creation from forks"},
&cli.BoolFlag{Name: "no-allow-review-apps-from-forks", Usage: "Disable auto deploy of review apps when new pull request is opened from a fork"},
&cli.UintFlag{Name: "hours-before-destroy-on-close", Usage: "Time delay before auto destroying a review app when pull request is closed"},
&cli.BoolFlag{Name: "destroy-on-stale", Usage: "Auto destroy review apps when no deploy/commits has happened"},
&cli.BoolFlag{Name: "no-destroy-on-stale", Usage: "Auto destroy review apps when no deploy/commits has happened"},
Expand All @@ -76,11 +83,11 @@ List of available integrations:
- github => GitHub.com
- github-enterprise => GitHub Enterprise (private instance)
- gitlab => GitLab.com
- gitlab-self-hosted => GitLab Self-hosted (private instance)
- gitlab-self-hosted => GitLab Self-hosted (private instance)
`,
Examples: []string{
"scalingo --app my-app integration-link-create https://gitlab.com/gitlab-org/gitlab-ce",
"scalingo --app my-app integration-link-create --branch master --auto-deploy https://ghe.example.org/test/frontend-app",
"scalingo --app my-app integration-link-create --branch master --auto-deploy --deploy-review-apps --no-allow-review-apps-from-forks https://ghe.example.org/test/frontend-app",
},
SeeAlso: []string{"integration-link", "integration-link-update", "integration-link-delete", "integration-link-manual-deploy", "integration-link-manual-review-app"},
}.Render(),
Expand All @@ -94,7 +101,7 @@ List of available integrations:
integrationURL := c.Args().First()
integrationURLParsed, err := url.Parse(integrationURL)
if err != nil {
errorQuit(err)
errorQuit(errgo.Notef(err, "error parsing the repository url"))
}
// If the customer forgot to specify the scheme, we automatically prefix with https://
if integrationURLParsed.Scheme == "" {
Expand Down Expand Up @@ -157,14 +164,31 @@ List of available integrations:
}
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"))
}

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

if deployReviewApps && allowReviewAppsFromForks && !awareOfSecurityRisks {
allowReviewAppsFromForks, err = askForConfirmationToAllowReviewAppsFromForks()
if err != nil {
errorQuit(err)
}
}

params = scalingo.SCMRepoLinkCreateParams{
Branch: &branch,
AutoDeployEnabled: &autoDeploy,
DeployReviewAppsEnabled: &deployReviewApps,
DestroyOnCloseEnabled: &destroyOnClose,
HoursBeforeDeleteOnClose: &hoursBeforeDestroyOnClose,
DestroyStaleEnabled: &destroyOnStale,
HoursBeforeDeleteStale: &hoursBeforeDestroyOnStale,
Branch: &branch,
AutoDeployEnabled: &autoDeploy,
DeployReviewAppsEnabled: &deployReviewApps,
DestroyOnCloseEnabled: &destroyOnClose,
HoursBeforeDeleteOnClose: &hoursBeforeDestroyOnClose,
DestroyStaleEnabled: &destroyOnStale,
HoursBeforeDeleteStale: &hoursBeforeDestroyOnStale,
AutomaticCreationFromForksAllowed: &allowReviewAppsFromForks,
}
}

Expand Down Expand Up @@ -206,9 +230,12 @@ List of available integrations:
&appFlag,
&cli.StringFlag{Name: "branch", Usage: "Branch used in auto deploy"},
&cli.BoolFlag{Name: "auto-deploy", Usage: "Enable auto deploy of application after each branch change"},
&cli.BoolFlag{Name: "no-auto-deploy", Usage: "Enable auto deploy of application after each branch change"},
&cli.BoolFlag{Name: "no-auto-deploy", Usage: "Disable auto deploy of application after each branch change"},
&cli.BoolFlag{Name: "deploy-review-apps", Usage: "Enable auto deploy of review app when new pull request is opened"},
&cli.BoolFlag{Name: "no-deploy-review-apps", Usage: "Enable auto deploy of review app when new pull request is opened"},
&cli.BoolFlag{Name: "no-deploy-review-apps", Usage: "Disable auto deploy of review app when new pull request is opened"},
&cli.BoolFlag{Name: "allow-review-apps-from-forks", Usage: "Enable auto deploy of review apps when new pull request is opened from a fork"},
&cli.BoolFlag{Name: "aware-of-security-risks", Usage: "Bypass the security warning about allowing automatic review app creation from forks"},
&cli.BoolFlag{Name: "no-allow-review-apps-from-forks", Usage: "Disable auto deploy of review apps when new pull request is opened from a fork"},
&cli.BoolFlag{Name: "destroy-on-close", Usage: "Auto destroy review apps when pull request is closed"},
&cli.BoolFlag{Name: "no-destroy-on-close", Usage: "Auto destroy review apps when pull request is closed"},
&cli.UintFlag{Name: "hours-before-destroy-on-close", Usage: "Time delay before auto destroying a review app when pull request is closed"},
Expand Down Expand Up @@ -253,13 +280,28 @@ List of available integrations:
errorQuit(errors.New("cannot define both destroy-on-stale and no-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"))
}

aurelien-reeves-scalingo marked this conversation as resolved.
Show resolved Hide resolved
awareOfSecurityRisks := c.Bool("aware-of-security-risks")

currentApp := detect.CurrentApp(c)
params, err := integrationlink.CheckAndFillParams(c, currentApp)
if err != nil {
errorQuit(err)
params := integrationlink.CheckAndFillParams(c)

if allowReviewAppsFromForks && !awareOfSecurityRisks {
stillAllowed, err := askForConfirmationToAllowReviewAppsFromForks()
if err != nil {
errorQuit(err)
}

params.AutomaticCreationFromForksAllowed = &stillAllowed
}

err = integrationlink.Update(c.Context, currentApp, *params)
err := integrationlink.Update(c.Context, currentApp, *params)
if err != nil {
errorQuit(err)
}
Expand Down Expand Up @@ -351,6 +393,7 @@ List of available integrations:

currentApp := detect.CurrentApp(c)
pullRequestID := c.Args().First()

err := integrationlink.ManualReviewApp(c.Context, currentApp, pullRequestID)
if err != nil {
errorQuit(err)
Expand Down Expand Up @@ -388,7 +431,7 @@ func interactiveCreate() (scalingo.SCMRepoLinkCreateParams, error) {
}{}
err := survey.Ask(qs, &answers)
if err != nil {
return params, err
return params, errgo.Notef(err, "error enquiring about branch and automatic review apps deployment")
}

t := true
Expand All @@ -408,7 +451,7 @@ func interactiveCreate() (scalingo.SCMRepoLinkCreateParams, error) {
Default: destroyOnClose,
}, &destroyOnClose, nil)
if err != nil {
return params, err
return params, errgo.Notef(err, "error enquiring about destroy on close")
}
params.DestroyOnCloseEnabled = &destroyOnClose
if destroyOnClose {
Expand All @@ -418,7 +461,7 @@ func interactiveCreate() (scalingo.SCMRepoLinkCreateParams, error) {
Default: "0",
}, &answerHoursBeforeDestroyOnClose, survey.WithValidator(validateHoursBeforeDelete))
if err != nil {
return params, err
return params, errgo.Notef(err, "error enquiring about review apps destroy delay")
}
hoursBeforeDestroyOnClose64, _ := strconv.ParseUint(answerHoursBeforeDestroyOnClose, 10, 32)
hoursBeforeDestroyOnClose := uint(hoursBeforeDestroyOnClose64)
Expand All @@ -431,7 +474,7 @@ func interactiveCreate() (scalingo.SCMRepoLinkCreateParams, error) {
Default: destroyOnStale,
}, &destroyOnStale, nil)
if err != nil {
return params, err
return params, errgo.Notef(err, "error enquiring about stale review apps destroy")
}
params.DestroyStaleEnabled = &destroyOnStale
if destroyOnStale {
Expand All @@ -441,12 +484,19 @@ func interactiveCreate() (scalingo.SCMRepoLinkCreateParams, error) {
Default: "0",
}, &answerHoursBeforeDestroyOnStale, survey.WithValidator(validateHoursBeforeDelete))
if err != nil {
return params, err
return params, errgo.Notef(err, "error enquiring about stale review apps destroy")
}
hoursBeforeDestroyOnStale64, _ := strconv.ParseUint(answerHoursBeforeDestroyOnStale, 10, 32)
hoursBeforeDestroyOnStale := uint(hoursBeforeDestroyOnStale64)
params.HoursBeforeDeleteStale = &hoursBeforeDestroyOnStale
}

forksAllowed, err := askForConfirmationToAllowReviewAppsFromForks()
if err != nil {
return params, errgo.Notef(err, "error enquiring about automatic review apps creation from forks")
}
params.AutomaticCreationFromForksAllowed = &forksAllowed

return params, nil
}

Expand All @@ -457,10 +507,28 @@ func validateHoursBeforeDelete(ans interface{}) error {
}
i, err := strconv.ParseInt(str, 10, 32)
if err != nil {
return err
return errgo.Notef(err, "error parsing hours")
}
if i < 0 {
return errors.New("must be positive")
}
return nil
}

func askForConfirmationToAllowReviewAppsFromForks() (bool, error) {
fmt.Println()
io.Warning(reviewAppsFromForksSecurityWarning)
fmt.Println()

var confirmed bool
err := survey.AskOne(&survey.Confirm{
Message: "Allow automatic creation of review apps from forks?",
Default: false,
}, &confirmed, nil)

if err != nil {
return false, err
}

return confirmed, nil
}
31 changes: 22 additions & 9 deletions integrationlink/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ import (
"github.com/Scalingo/go-scalingo/v6"
)

func CheckAndFillParams(c *cli.Context, app string) (*scalingo.SCMRepoLinkUpdateParams, error) {
func CheckAndFillParams(c *cli.Context) *scalingo.SCMRepoLinkUpdateParams {
paramsChecker := newParamsChecker(c)
params := &scalingo.SCMRepoLinkUpdateParams{
Branch: paramsChecker.lookupBranch(),
AutoDeployEnabled: paramsChecker.lookupAutoDeploy(),
DeployReviewAppsEnabled: paramsChecker.lookupDeployReviewApps(),
DestroyOnCloseEnabled: paramsChecker.lookupDestroyOnClose(),
HoursBeforeDeleteOnClose: paramsChecker.lookupHoursBeforeDestroyOnClose(),
DestroyStaleEnabled: paramsChecker.lookupDestroyOnStale(),
HoursBeforeDeleteStale: paramsChecker.lookupHoursBeforeDestroyOnStale(),
Branch: paramsChecker.lookupBranch(),
AutoDeployEnabled: paramsChecker.lookupAutoDeploy(),
DeployReviewAppsEnabled: paramsChecker.lookupDeployReviewApps(),
DestroyOnCloseEnabled: paramsChecker.lookupDestroyOnClose(),
HoursBeforeDeleteOnClose: paramsChecker.lookupHoursBeforeDestroyOnClose(),
DestroyStaleEnabled: paramsChecker.lookupDestroyOnStale(),
HoursBeforeDeleteStale: paramsChecker.lookupHoursBeforeDestroyOnStale(),
AutomaticCreationFromForksAllowed: paramsChecker.lookupAllowReviewAppsFromForks(),
}

return params, nil
return params
}

type paramsChecker struct {
Expand Down Expand Up @@ -95,6 +96,18 @@ func (p *paramsChecker) lookupDestroyOnStale() *bool {
return nil
}

func (p *paramsChecker) lookupAllowReviewAppsFromForks() *bool {
if p.ctx.IsSet("allow-review-apps-from-forks") {
t := true
return &t
}
if p.ctx.IsSet("no-allow-review-apps-from-forks") {
f := false
return &f
}
return nil
}

func (p *paramsChecker) lookupHoursBeforeDestroyOnStale() *uint {
if !p.ctx.IsSet("hours-before-destroy-on-stale") {
return nil
Expand Down
11 changes: 11 additions & 0 deletions integrationlink/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ func Show(ctx context.Context, app string) error {
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,
)
}

return nil
Expand Down