Skip to content
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
96 changes: 43 additions & 53 deletions internal/prompts/app_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (

"github.com/slackapi/slack-cli/internal/api"
"github.com/slackapi/slack-cli/internal/cmdutil"
"github.com/slackapi/slack-cli/internal/config"
"github.com/slackapi/slack-cli/internal/experiment"
"github.com/slackapi/slack-cli/internal/iostreams"
authpkg "github.com/slackapi/slack-cli/internal/pkg/auth"
"github.com/slackapi/slack-cli/internal/shared"
Expand Down Expand Up @@ -476,10 +474,6 @@ func AppSelectPrompt(
filtered[id] = app
}
}
manifestSource, err := clients.Config.ProjectConfig.GetManifestSource(ctx)
if err != nil {
return SelectedApp{}, err
}
type Selection struct {
app SelectedApp
label string
Expand Down Expand Up @@ -524,58 +518,54 @@ func AppSelectPrompt(
return SelectedApp{}, slackerror.New(slackerror.ErrInstallationRequired)
}
case ShowAllApps, ShowInstalledAndNewApps:
isManifestSourceLocal := manifestSource.Equals(config.ManifestSourceLocal)
isBoltInstallEnabled := clients.Config.WithExperimentOn(experiment.BoltInstall)
if isManifestSourceLocal || isBoltInstallEnabled {
option := Selection{
label: style.Secondary("Create a new app"),
}
options = append(options, option)
switch {
case types.IsAppID(clients.Config.AppFlag):
// Skip to match the app ID later
case teamFlag != "":
// Check for an existing app ID
selections := []SelectedApp{}
for _, app := range filtered {
switch teamFlag {
case app.App.TeamID, app.App.TeamDomain:
switch {
case environment.Equals(ShowAllEnvironments):
selections = append(selections, app)
case environment.Equals(ShowHostedOnly) && !app.App.IsDev:
selections = append(selections, app)
case environment.Equals(ShowLocalOnly) && app.App.IsDev:
selections = append(selections, app)
}
option := Selection{
label: style.Secondary("Create a new app"),
}
options = append(options, option)
switch {
case types.IsAppID(clients.Config.AppFlag):
// Skip to match the app ID later
case teamFlag != "":
// Check for an existing app ID
selections := []SelectedApp{}
for _, app := range filtered {
switch teamFlag {
case app.App.TeamID, app.App.TeamDomain:
switch {
case environment.Equals(ShowAllEnvironments):
selections = append(selections, app)
case environment.Equals(ShowHostedOnly) && !app.App.IsDev:
selections = append(selections, app)
case environment.Equals(ShowLocalOnly) && app.App.IsDev:
selections = append(selections, app)
}
}
switch len(selections) {
case 0:
// Skip to create a new app
case 1:
return selections[0], nil
default:
return SelectedApp{}, slackerror.New(slackerror.ErrAppFound).
WithMessage("Multiple apps exist for the provided team")
}
// Create a new app if none exists
auths, err := getAuths(ctx, clients)
if err != nil {
return SelectedApp{}, err
}
for _, auth := range auths {
switch teamFlag {
case auth.TeamID, auth.TeamDomain:
app := SelectedApp{
App: types.NewApp(),
Auth: auth,
}
return app, nil
}
switch len(selections) {
case 0:
// Skip to create a new app
case 1:
return selections[0], nil
default:
return SelectedApp{}, slackerror.New(slackerror.ErrAppFound).
WithMessage("Multiple apps exist for the provided team")
}
// Create a new app if none exists
auths, err := getAuths(ctx, clients)
if err != nil {
return SelectedApp{}, err
}
for _, auth := range auths {
switch teamFlag {
case auth.TeamID, auth.TeamDomain:
app := SelectedApp{
App: types.NewApp(),
Auth: auth,
}
return app, nil
}
return SelectedApp{}, slackerror.New(slackerror.ErrTeamNotFound)
}
return SelectedApp{}, slackerror.New(slackerror.ErrTeamNotFound)
}
}
labels := []string{}
Expand Down
27 changes: 3 additions & 24 deletions internal/prompts/app_select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"time"

"github.com/slackapi/slack-cli/internal/api"
"github.com/slackapi/slack-cli/internal/config"
"github.com/slackapi/slack-cli/internal/hooks"
"github.com/slackapi/slack-cli/internal/iostreams"
"github.com/slackapi/slack-cli/internal/shared"
Expand Down Expand Up @@ -655,7 +654,6 @@ func TestPrompt_AppSelectPrompt(t *testing.T) {
mockFlagApp string
mockFlagTeam string
mockFlagToken string
mockManifestSource config.ManifestSource
appPromptConfigEnvironment AppEnvironmentType
appPromptConfigOptions []string
appPromptConfigStatus AppInstallStatus
Expand Down Expand Up @@ -702,7 +700,6 @@ func TestPrompt_AppSelectPrompt(t *testing.T) {
UserID: team2UserID,
},
},
mockManifestSource: config.ManifestSourceLocal,
appPromptConfigEnvironment: ShowAllEnvironments,
appPromptConfigOptions: []string{
"A1 team1 T1",
Expand All @@ -727,7 +724,6 @@ func TestPrompt_AppSelectPrompt(t *testing.T) {
"returns new application if selected": {
mockAuths: fakeAuthsByTeamDomainSlice,
mockAppsDeployed: []types.App{},
mockManifestSource: config.ManifestSourceLocal,
appPromptConfigEnvironment: ShowHostedOnly,
appPromptConfigOptions: []string{
"Create a new app",
Expand All @@ -746,7 +742,6 @@ func TestPrompt_AppSelectPrompt(t *testing.T) {
"errors if installation required and no apps saved": {
mockAuths: fakeAuthsByTeamDomainSlice,
mockAppsDeployed: []types.App{},
mockManifestSource: config.ManifestSourceLocal,
appPromptConfigEnvironment: ShowHostedOnly,
appPromptConfigStatus: ShowInstalledAppsOnly,
expectedError: slackerror.New(slackerror.ErrInstallationRequired),
Expand All @@ -772,7 +767,6 @@ func TestPrompt_AppSelectPrompt(t *testing.T) {
appPromptResponseOption: "Create a new app",
teamPromptResponseFlag: true,
teamPromptResponseOption: team1TeamDomain,
mockManifestSource: config.ManifestSourceLocal,
expectedError: slackerror.New(slackerror.ErrAppExists).
WithDetails(slackerror.ErrorDetails{{
Message: `The app "A1" already exists for team "team1" (T1)`,
Expand All @@ -783,15 +777,14 @@ func TestPrompt_AppSelectPrompt(t *testing.T) {
mockAuths: fakeAuthsByTeamDomainSlice,
mockFlagApp: "deployed",
mockFlagTeam: team1TeamID,
mockManifestSource: config.ManifestSourceLocal,
appPromptConfigEnvironment: ShowHostedOnly,
appPromptConfigStatus: ShowInstalledAndNewApps,
expectedSelection: SelectedApp{
App: types.NewApp(),
Auth: fakeAuthsByTeamDomain[team1TeamDomain],
},
},
"selects existing application for app environment flag and team id flag if app saved": {
"returns existing application for app environment flag and team id flag if app saved": {
mockAuths: fakeAuthsByTeamDomainSlice,
mockAppsDeployed: []types.App{
{
Expand All @@ -802,7 +795,6 @@ func TestPrompt_AppSelectPrompt(t *testing.T) {
},
mockFlagApp: "deployed",
mockFlagTeam: team2TeamID,
mockManifestSource: config.ManifestSourceLocal,
appPromptConfigEnvironment: ShowHostedOnly,
appPromptConfigStatus: ShowAllApps,
expectedSelection: SelectedApp{
Expand Down Expand Up @@ -851,6 +843,7 @@ func TestPrompt_AppSelectPrompt(t *testing.T) {
"A1 team1 T1",
"A2 team2 T2",
},
appPromptConfigStatus: ShowInstalledAndUninstalledApps,
appPromptResponsePrompt: true,
appPromptResponseIndex: 1,
expectedSelection: SelectedApp{
Expand Down Expand Up @@ -1049,7 +1042,7 @@ func TestPrompt_AppSelectPrompt(t *testing.T) {
mockFlagTeam: "TNOTFOUND",
appPromptConfigEnvironment: ShowAllEnvironments,
appPromptConfigStatus: ShowAllApps,
expectedError: slackerror.New(slackerror.ErrAppNotFound),
expectedError: slackerror.New(slackerror.ErrTeamNotFound),
},
"errors if deployed app environment flag for local app prompt": {
mockFlagApp: "deploy",
Expand All @@ -1064,22 +1057,19 @@ func TestPrompt_AppSelectPrompt(t *testing.T) {
"errors if deployed app environment flag and team id flag for local app prompt": {
mockFlagApp: "deployed",
mockFlagTeam: team1TeamID,
mockManifestSource: config.ManifestSourceLocal,
appPromptConfigEnvironment: ShowLocalOnly,
appPromptConfigStatus: ShowInstalledAndNewApps,
expectedError: slackerror.New(slackerror.ErrDeployedAppNotSupported),
},
"errors if local app environment flag and team id flag for hosted app prompt": {
mockFlagApp: "local",
mockFlagTeam: team1TeamID,
mockManifestSource: config.ManifestSourceLocal,
appPromptConfigEnvironment: ShowHostedOnly,
appPromptConfigStatus: ShowInstalledAndNewApps,
expectedError: slackerror.New(slackerror.ErrLocalAppNotSupported),
},
"errors if team id flag does not have authorization": {
mockFlagTeam: team1TeamID,
mockManifestSource: config.ManifestSourceLocal,
appPromptConfigEnvironment: ShowHostedOnly,
appPromptConfigStatus: ShowInstalledAndNewApps,
expectedError: slackerror.New(slackerror.ErrTeamNotFound),
Expand Down Expand Up @@ -1128,7 +1118,6 @@ func TestPrompt_AppSelectPrompt(t *testing.T) {
mockAuthWithTeamIDTeamID: team1TeamID,
mockFlagTeam: team1TeamID,
mockFlagToken: fakeAuthsByTeamDomain[team1TeamDomain].Token,
mockManifestSource: config.ManifestSourceLocal,
appPromptConfigStatus: ShowInstalledAndNewApps,
appPromptConfigEnvironment: ShowHostedOnly,
expectedSelection: SelectedApp{
Expand All @@ -1145,7 +1134,6 @@ func TestPrompt_AppSelectPrompt(t *testing.T) {
mockAuthWithTeamIDTeamID: team1TeamID,
mockFlagTeam: team1TeamID,
mockFlagToken: fakeAuthsByTeamDomain[team1TeamDomain].Token,
mockManifestSource: config.ManifestSourceLocal,
appPromptConfigStatus: ShowAllApps,
appPromptConfigEnvironment: ShowLocalOnly,
expectedSelection: SelectedApp{
Expand Down Expand Up @@ -1361,16 +1349,7 @@ func TestPrompt_AppSelectPrompt(t *testing.T) {
nil,
)
clientsMock.AddDefaultMocks()
projectConfigMock := config.NewProjectConfigMock()
projectConfigMock.On(
"GetManifestSource",
mock.Anything,
).Return(
tt.mockManifestSource,
nil,
)
clientsMock.Config.AppFlag = tt.mockFlagApp
clientsMock.Config.ProjectConfig = projectConfigMock
clientsMock.Config.TeamFlag = tt.mockFlagTeam
clientsMock.Config.TokenFlag = tt.mockFlagToken
clients := shared.NewClientFactory(clientsMock.MockClientFactory())
Expand Down
Loading