Skip to content

Commit 7556d96

Browse files
Automatically register user to the default git integration (#229)
* Started working on adding git PAT to cli * Call register * fix * Revert "fix" This reverts commit e5fbb3a. * fix * Eliminate events being sent conditionally
1 parent c235562 commit 7556d96

File tree

6 files changed

+141
-58
lines changed

6 files changed

+141
-58
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION=v0.0.212
1+
VERSION=v0.0.213
22

33
OUT_DIR=dist
44
YEAR?=$(shell date +"%Y")

cmd/commands/common.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,38 @@ func ensureGitToken(cmd *cobra.Command, cloneOpts *git.CloneOptions) error {
254254
return nil
255255
}
256256

257+
func ensureGitPAT(cmd *cobra.Command, opts *RuntimeInstallOptions) error {
258+
var err error
259+
if !store.Get().Silent {
260+
return getGitPATFromUserInput(cmd, opts)
261+
}
262+
263+
opts.GitIntegrationRegistrationOpts.Token, err = cmd.Flags().GetString("git-token")
264+
return err
265+
}
266+
267+
func getGitPATFromUserInput(cmd *cobra.Command, opts *RuntimeInstallOptions) error {
268+
gitPATPrompt := promptui.Prompt{
269+
Label: "Enter your Personal Git Access Token (leave blank to use system token. Can be changed later)",
270+
Mask: '*',
271+
}
272+
273+
gitPAT, err := gitPATPrompt.Run()
274+
if err != nil {
275+
return fmt.Errorf("prompt error: %w", err)
276+
}
277+
278+
if gitPAT == "" {
279+
gitPAT, err = cmd.Flags().GetString("git-token")
280+
if err != nil {
281+
return fmt.Errorf("%w", err)
282+
}
283+
}
284+
opts.GitIntegrationRegistrationOpts.Token = gitPAT
285+
286+
return nil
287+
}
288+
257289
func getGitTokenFromUserInput(cmd *cobra.Command) error {
258290
gitTokenPrompt := promptui.Prompt{
259291
Label: "Git provider api token",

cmd/commands/runtime.go

Lines changed: 102 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,23 @@ import (
6969

7070
type (
7171
RuntimeInstallOptions struct {
72-
RuntimeName string
73-
RuntimeToken string
74-
RuntimeStoreIV string
75-
IngressHost string
76-
IngressClass string
77-
IngressController string
78-
Insecure bool
79-
InstallDemoResources bool
80-
Version *semver.Version
81-
GsCloneOpts *git.CloneOptions
82-
InsCloneOpts *git.CloneOptions
83-
GitIntegrationOpts *apmodel.AddGitIntegrationArgs
84-
KubeFactory kube.Factory
85-
CommonConfig *runtime.CommonConfig
86-
versionStr string
87-
kubeContext string
72+
RuntimeName string
73+
RuntimeToken string
74+
RuntimeStoreIV string
75+
IngressHost string
76+
IngressClass string
77+
IngressController string
78+
Insecure bool
79+
InstallDemoResources bool
80+
Version *semver.Version
81+
GsCloneOpts *git.CloneOptions
82+
InsCloneOpts *git.CloneOptions
83+
GitIntegrationCreationOpts *apmodel.AddGitIntegrationArgs
84+
GitIntegrationRegistrationOpts *apmodel.RegisterToGitIntegrationArgs
85+
KubeFactory kube.Factory
86+
CommonConfig *runtime.CommonConfig
87+
versionStr string
88+
kubeContext string
8889
}
8990
RuntimeUninstallOptions struct {
9091
RuntimeName string
@@ -149,11 +150,14 @@ func NewRuntimeCommand() *cobra.Command {
149150

150151
func NewRuntimeInstallCommand() *cobra.Command {
151152
var (
152-
gitIntegrationOpts = apmodel.AddGitIntegrationArgs{
153+
gitIntegrationCreationOpts = apmodel.AddGitIntegrationArgs{
153154
SharingPolicy: apmodel.SharingPolicyAllUsersInAccount,
154155
}
155-
installationOpts = RuntimeInstallOptions{GitIntegrationOpts: &gitIntegrationOpts}
156-
finalParameters map[string]string
156+
installationOpts = RuntimeInstallOptions{
157+
GitIntegrationCreationOpts: &gitIntegrationCreationOpts,
158+
GitIntegrationRegistrationOpts: &apmodel.RegisterToGitIntegrationArgs{},
159+
}
160+
finalParameters map[string]string
157161
)
158162

159163
cmd := &cobra.Command{
@@ -191,7 +195,7 @@ func NewRuntimeInstallCommand() *cobra.Command {
191195
"Runtime name": installationOpts.RuntimeName,
192196
"Repository URL": installationOpts.InsCloneOpts.Repo,
193197
"Ingress host": installationOpts.IngressHost,
194-
"Ingress class": installationOpts.IngressClass,
198+
"Ingress class": installationOpts.IngressClass,
195199
"Installing demo resources": strconv.FormatBool(installationOpts.InstallDemoResources),
196200
}
197201

@@ -213,7 +217,7 @@ func NewRuntimeInstallCommand() *cobra.Command {
213217
cmd.Flags().StringVar(&installationOpts.versionStr, "version", "", "The runtime version to install (default: latest)")
214218
cmd.Flags().BoolVar(&installationOpts.InstallDemoResources, "demo-resources", true, "Installs demo resources (default: true)")
215219
cmd.Flags().DurationVar(&store.Get().WaitTimeout, "wait-timeout", store.Get().WaitTimeout, "How long to wait for the runtime components to be ready")
216-
cmd.Flags().StringVar(&gitIntegrationOpts.APIURL, "provider-api-url", "", "Git provider API url")
220+
cmd.Flags().StringVar(&gitIntegrationCreationOpts.APIURL, "provider-api-url", "", "Git provider API url")
217221
cmd.Flags().BoolVar(&store.Get().BypassIngressClassCheck, "bypass-ingress-class-check", false, "Disables the ingress class check during pre-installation")
218222

219223
installationOpts.InsCloneOpts = apu.AddCloneFlags(cmd, &apu.CloneFlagsOptions{
@@ -235,16 +239,12 @@ func NewRuntimeInstallCommand() *cobra.Command {
235239
func runtimeInstallCommandPreRunHandler(cmd *cobra.Command, opts *RuntimeInstallOptions) error {
236240
handleCliStep(reporter.InstallPhasePreCheckStart, "Starting pre checks", nil, false)
237241

238-
if opts.versionStr != "" {
239-
version, err := semver.NewVersion(opts.versionStr)
240-
handleCliStep(reporter.InstallStepPreCheckValidateRuntimeVersion, "Validating runtime version", err, false)
241-
if err != nil {
242-
return err
243-
}
244-
opts.Version = version
242+
err := getVersionIfExists(opts)
243+
handleCliStep(reporter.InstallStepPreCheckValidateRuntimeVersion, "Validating runtime version", err, false)
244+
if err != nil {
245+
return err
245246
}
246247

247-
var err error
248248
if opts.RuntimeName == "" {
249249
if !store.Get().Silent {
250250
err = getRuntimeNameFromUserInput(&opts.RuntimeName)
@@ -257,19 +257,11 @@ func runtimeInstallCommandPreRunHandler(cmd *cobra.Command, opts *RuntimeInstall
257257
return err
258258
}
259259

260-
isValid, err := IsValidName(opts.RuntimeName)
261-
var errMsg string
260+
err = validateRuntimeName(opts.RuntimeName)
261+
handleCliStep(reporter.InstallStepPreCheckRuntimeNameValidation, "Validating runtime name", err, false)
262262
if err != nil {
263-
errMsg = "failed to check the validity of the runtime name"
264-
} else if !isValid {
265-
errMsg = "runtime name cannot have any uppercase letters, must start with a character, end with character or number, and be shorter than 63 chars"
266-
}
267-
268-
if errMsg != "" {
269-
handleCliStep(reporter.InstallStepPreCheckRuntimeNameValidation, "Validating runtime name", fmt.Errorf(errMsg), false)
270-
log.G(cmd.Context()).Fatal(errMsg)
263+
log.G(cmd.Context()).Fatal(fmt.Errorf("%w", err))
271264
}
272-
handleCliStep(reporter.InstallStepPreCheckRuntimeNameValidation, "Validating runtime name", err, false)
273265

274266
err = getKubeContextNameFromUserSelect(cmd, &opts.kubeContext)
275267
handleCliStep(reporter.InstallStepPreCheckGetKubeContext, "Getting kube context name", err, false)
@@ -301,6 +293,12 @@ func runtimeInstallCommandPreRunHandler(cmd *cobra.Command, opts *RuntimeInstall
301293
return err
302294
}
303295

296+
err = ensureGitPAT(cmd, opts)
297+
handleCliStep(reporter.InstallStepPreCheckEnsureGitPAT, "Getting git personal access token", err, false)
298+
if err != nil {
299+
return err
300+
}
301+
304302
err = askUserIfToInstallDemoResources(cmd, &opts.InstallDemoResources)
305303
handleCliStep(reporter.InstallStepPreCheckShouldInstallDemoResources, "Asking user is demo resources should be installed", err, false)
306304
if err != nil {
@@ -650,12 +648,18 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
650648
return util.DecorateErrorWithDocsLink(fmt.Errorf("failed to complete installation: %w", timeoutErr))
651649
}
652650

653-
gitIntgErr := addDefaultGitIntegration(ctx, opts.RuntimeName, opts.GitIntegrationOpts)
651+
gitIntgErr := addDefaultGitIntegration(ctx, opts.RuntimeName, opts.GitIntegrationCreationOpts)
654652
handleCliStep(reporter.InstallStepCreateDefaultGitIntegration, "Creating a default git integration", gitIntgErr, true)
655653
if gitIntgErr != nil {
656654
return util.DecorateErrorWithDocsLink(fmt.Errorf("failed to create default git integration: %w", gitIntgErr))
657655
}
658656

657+
gitIntgErr = registerUserToGitIntegration(ctx, opts.RuntimeName, opts.GitIntegrationRegistrationOpts)
658+
handleCliStep(reporter.InstallStepRegisterToDefaultGitIntegration, "Registering user to the default git integration", gitIntgErr, true)
659+
if gitIntgErr != nil {
660+
return util.DecorateErrorWithDocsLink(fmt.Errorf("failed to register user to the default git integration: %w", gitIntgErr))
661+
}
662+
659663
installationSuccessMsg := fmt.Sprintf("Runtime '%s' installed successfully", opts.RuntimeName)
660664
summaryArr = append(summaryArr, summaryLog{installationSuccessMsg, Info})
661665
log.G(ctx).Infof(installationSuccessMsg)
@@ -678,6 +682,18 @@ func addDefaultGitIntegration(ctx context.Context, runtime string, opts *apmodel
678682
return nil
679683
}
680684

685+
func registerUserToGitIntegration(ctx context.Context, runtime string, opts *apmodel.RegisterToGitIntegrationArgs) error {
686+
appProxyClient, err := cfConfig.NewClient().AppProxy(ctx, runtime, store.Get().InsecureIngressHost)
687+
if err != nil {
688+
return fmt.Errorf("failed to build app-proxy client: %w", err)
689+
}
690+
if err := RunGitIntegrationRegisterCommand(ctx, appProxyClient, opts); err != nil {
691+
return err
692+
}
693+
694+
return nil
695+
}
696+
681697
func installComponents(ctx context.Context, opts *RuntimeInstallOptions, rt *runtime.Runtime) error {
682698
var err error
683699
if opts.IngressHost != "" {
@@ -1016,18 +1032,19 @@ func RunRuntimeUninstall(ctx context.Context, opts *RuntimeUninstallOptions) err
10161032
handleCliStep(reporter.UninstallPhaseStart, "Uninstall phase started", nil, false)
10171033

10181034
// check whether the runtime exists
1035+
var err error
10191036
if !opts.SkipChecks {
1020-
_, err := cfConfig.NewClient().V2().Runtime().Get(ctx, opts.RuntimeName)
1021-
handleCliStep(reporter.UninstallStepCheckRuntimeExists, "Checking if runtime exists", err, true)
1022-
if err != nil {
1023-
summaryArr = append(summaryArr, summaryLog{"you can attempt to uninstall again with the \"--skip-checks\" flag", Info})
1024-
return err
1025-
}
1037+
_, err = cfConfig.NewClient().V2().Runtime().Get(ctx, opts.RuntimeName)
1038+
}
1039+
handleCliStep(reporter.UninstallStepCheckRuntimeExists, "Checking if runtime exists", err, true)
1040+
if err != nil {
1041+
summaryArr = append(summaryArr, summaryLog{"you can attempt to uninstall again with the \"--skip-checks\" flag", Info})
1042+
return err
10261043
}
10271044

10281045
log.G(ctx).Infof("Uninstalling runtime '%s'", opts.RuntimeName)
10291046

1030-
err := apcmd.RunRepoUninstall(ctx, &apcmd.RepoUninstallOptions{
1047+
err = apcmd.RunRepoUninstall(ctx, &apcmd.RepoUninstallOptions{
10311048
Namespace: opts.RuntimeName,
10321049
Timeout: opts.Timeout,
10331050
CloneOptions: opts.CloneOpts,
@@ -1202,12 +1219,14 @@ func RunRuntimeUpgrade(ctx context.Context, opts *RuntimeUpgradeOptions) error {
12021219
infoStr := fmt.Sprintf("Creating app '%s'", component.Name)
12031220
log.G(ctx).Infof(infoStr)
12041221
err = component.CreateApp(ctx, nil, opts.CloneOpts, opts.RuntimeName, store.Get().CFComponentType, "", "")
1205-
handleCliStep(reporter.UpgradeStepCreateApp, infoStr, err, false)
12061222
if err != nil {
1207-
return fmt.Errorf("failed to create '%s' application: %w", component.Name, err)
1223+
err = fmt.Errorf("failed to create '%s' application: %w", component.Name, err)
1224+
break
12081225
}
12091226
}
12101227

1228+
handleCliStep(reporter.UpgradeStepCreateApps, "Creating apps", err, false)
1229+
12111230
return nil
12121231
}
12131232

@@ -1668,17 +1687,21 @@ func createCodefreshArgoDashboardAgent(ctx context.Context, path string, cloneOp
16681687
func ensureGitIntegrationOpts(opts *RuntimeInstallOptions) error {
16691688
var err error
16701689
if opts.InsCloneOpts.Provider == "" {
1671-
if opts.GitIntegrationOpts.Provider, err = inferProviderFromCloneURL(opts.InsCloneOpts.URL()); err != nil {
1690+
if opts.GitIntegrationCreationOpts.Provider, err = inferProviderFromCloneURL(opts.InsCloneOpts.URL()); err != nil {
16721691
return err
16731692
}
16741693
}
16751694

1676-
if opts.GitIntegrationOpts.APIURL == "" {
1677-
if opts.GitIntegrationOpts.APIURL, err = inferAPIURLForGitProvider(opts.GitIntegrationOpts.Provider); err != nil {
1695+
if opts.GitIntegrationCreationOpts.APIURL == "" {
1696+
if opts.GitIntegrationCreationOpts.APIURL, err = inferAPIURLForGitProvider(opts.GitIntegrationCreationOpts.Provider); err != nil {
16781697
return err
16791698
}
16801699
}
16811700

1701+
if opts.GitIntegrationRegistrationOpts.Token == "" {
1702+
return fmt.Errorf("git personal access token is missing")
1703+
}
1704+
16821705
return nil
16831706
}
16841707

@@ -1788,3 +1811,29 @@ func createAnalyticsReporter(ctx context.Context, flow reporter.FlowType) {
17881811

17891812
reporter.Init(user, flow)
17901813
}
1814+
1815+
func validateRuntimeName(runtime string) error {
1816+
var err error
1817+
isValid, err := IsValidName(runtime)
1818+
if err != nil {
1819+
err = fmt.Errorf("failed to check the validity of the runtime name: %w", err)
1820+
} else if !isValid {
1821+
err = fmt.Errorf("runtime name cannot have any uppercase letters, must start with a character, end with character or number, and be shorter than 63 chars")
1822+
}
1823+
1824+
return err
1825+
}
1826+
1827+
func getVersionIfExists(opts *RuntimeInstallOptions) error {
1828+
if opts.versionStr != "" {
1829+
log.G().Infof("vesionStr: %s", opts.versionStr)
1830+
version, err := semver.NewVersion(opts.versionStr)
1831+
handleCliStep(reporter.InstallStepPreCheckValidateRuntimeVersion, "Validating runtime version", err, false)
1832+
if err != nil {
1833+
return err
1834+
}
1835+
opts.Version = version
1836+
log.G().Infof("opts.Version: %s", opts.Version)
1837+
}
1838+
return nil
1839+
}

docs/releases/release_notes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ cf version
2323

2424
```bash
2525
# download and extract the binary
26-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.212/cf-linux-amd64.tar.gz | tar zx
26+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.213/cf-linux-amd64.tar.gz | tar zx
2727

2828
# move the binary to your $PATH
2929
mv ./cf-linux-amd64 /usr/local/bin/cf
@@ -36,7 +36,7 @@ cf version
3636

3737
```bash
3838
# download and extract the binary
39-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.212/cf-darwin-amd64.tar.gz | tar zx
39+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.213/cf-darwin-amd64.tar.gz | tar zx
4040

4141
# move the binary to your $PATH
4242
mv ./cf-darwin-amd64 /usr/local/bin/cf

manifests/runtime.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
namespace: '{{ namespace }}'
66
spec:
77
defVersion: 1.0.0
8-
version: 0.0.212
8+
version: 0.0.213
99
bootstrapSpecifier: github.com/codefresh-io/cli-v2/manifests/argo-cd
1010
components:
1111
- name: events

pkg/reporter/reporter.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ const (
6666
InstallStepPreCheckEnsureIngressClass CliStep = "install.pre-check.step.ensure-ingress-class"
6767
InstallStepPreCheckEnsureRuntimeRepo CliStep = "install.pre-check.step.ensure-runtime-repo"
6868
InstallStepPreCheckEnsureGitToken CliStep = "install.pre-check.step.ensure-git-token"
69+
InstallStepPreCheckEnsureGitPAT CliStep = "install.pre-check.step.ensure-git-personal-access-token"
6970
InstallStepPreCheckEnsureIngressHost CliStep = "install.pre-check.step.ensure-ingress-host"
7071
InstallStepPreCheckShouldInstallDemoResources CliStep = "install.pre-check.step.should-install-demo-resources"
7172
InstallPhasePreCheckFinish CliStep = "install.pre-check.phase.finish"
@@ -89,6 +90,7 @@ const (
8990
InstallStepCreateMarketplaceGitsource CliStep = "install.run.step.create-marketplace-gitsource"
9091
InstallStepCompleteRuntimeInstallation CliStep = "install.run.step.complete-runtime-installation"
9192
InstallStepCreateDefaultGitIntegration CliStep = "install.run.step.create-default-git-integration"
93+
InstallStepRegisterToDefaultGitIntegration CliStep = "install.run.step.register-to-default-git-integration"
9294
InstallPhaseFinish CliStep = "install.run.phase.finish"
9395

9496
// Uninstall
@@ -118,7 +120,7 @@ const (
118120
UpgradeStepLoadRuntimeDefinition CliStep = "upgrade.run.step.load-runtime-definition"
119121
UpgradeStepUpgradeRuntime CliStep = "upgrade.run.step.upgrade-runtime"
120122
UpgradeStepPushRuntimeDefinition CliStep = "upgrade.run.step.push-runtime-definition"
121-
UpgradeStepCreateApp CliStep = "upgrade.run.step.create-app"
123+
UpgradeStepCreateApps CliStep = "upgrade.run.step.create-apps"
122124
UpgradePhaseFinish CliStep = "upgrade.run.phase.finish"
123125

124126
// General

0 commit comments

Comments
 (0)