-
Notifications
You must be signed in to change notification settings - Fork 39
feat(create): fetch remote manifest after linking app during create #585
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
Changes from 27 commits
0d8bfd3
da2eb75
e57f7d5
aceb7a4
906346a
8128d5e
315c9bb
3177aa1
6d6749a
42782a3
3d642ca
f984c33
3396d78
bdbf756
1538faf
040a3c8
1092da0
aecb626
c289a09
19c2fac
2283fbc
236702a
8639a29
2939882
19cfb59
22a96a9
43e5d3d
9ad0d79
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ import ( | |
|
|
||
| "github.com/slackapi/slack-cli/cmd/app" | ||
| "github.com/slackapi/slack-cli/internal/iostreams" | ||
| "github.com/slackapi/slack-cli/internal/manifest" | ||
| "github.com/slackapi/slack-cli/internal/pkg/create" | ||
| "github.com/slackapi/slack-cli/internal/shared" | ||
| "github.com/slackapi/slack-cli/internal/shared/types" | ||
|
|
@@ -229,10 +230,34 @@ func runCreateCommand(clients *shared.ClientFactory, cmd *cobra.Command, args [] | |
| defer func() { | ||
| _ = os.Chdir(originalDir) | ||
| }() | ||
|
|
||
| linkedApp := &types.App{} | ||
| if err := app.LinkExistingApp(ctx, clients, linkedApp); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if linkedApp.AppID != "" { | ||
| auth, err := clients.Auth().AuthWithTeamID(ctx, linkedApp.TeamID) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| fetchErr := manifest.SetManifestLocal(ctx, clients, auth.Token, linkedApp.AppID, absProjectPath) | ||
| if fetchErr != nil { | ||
| clients.IO.PrintWarning(ctx, "%s", style.Sectionf(style.TextSection{ | ||
| Text: "Could not fetch the remote app manifest", | ||
| Secondary: []string{ | ||
| fetchErr.Error(), | ||
| "The template manifest was kept unchanged", | ||
| }, | ||
| })) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm I'm hesitant because the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👁️🗨️ thought: I'm worried I'd miss warnings in CI and overwrite an existing app manifest for "A001" with these commands if the manifest step of "create" fails: $ slack create my-app -t slack-samples/bolt-js-blank-template -a A001 -E deployed
$ slack deployRecommending to save the outputs of |
||
| } else { | ||
| clients.IO.PrintInfo(ctx, false, "%s", style.Sectionf(style.TextSection{ | ||
| Emoji: "pencil2", | ||
| Text: "Manifest", | ||
| Secondary: []string{"Written to manifest.json from remote app settings"}, | ||
| })) | ||
| } | ||
| } | ||
| clients.IO.PrintInfo(ctx, false, "%s", style.Sectionf(style.TextSection{ | ||
| Emoji: "house", | ||
| Text: "App", | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧪 todo: Let's add unit tests alongside internal packages for more strict checks! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // Copyright 2022-2026 Salesforce, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package manifest | ||
|
|
||
| import ( | ||
| "context" | ||
| "path/filepath" | ||
|
|
||
| "github.com/slackapi/slack-cli/internal/goutils" | ||
| "github.com/slackapi/slack-cli/internal/shared" | ||
| "github.com/spf13/afero" | ||
| ) | ||
|
|
||
| // SetManifestLocal fetches the app manifest from remote settings and writes it to the project. | ||
| func SetManifestLocal(ctx context.Context, clients *shared.ClientFactory, token, appID, projectPath string) error { | ||
| slackYaml, err := clients.AppClient().Manifest.GetManifestRemote(ctx, token, appID) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| manifestJSON, err := goutils.JSONMarshalUnescapedIndent(slackYaml.AppManifest) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| manifestPath := filepath.Join(projectPath, "manifest.json") | ||
| if err := afero.WriteFile(clients.Fs, manifestPath, []byte(manifestJSON), 0644); err != nil { | ||
| return err | ||
| } | ||
| hash, err := clients.Config.ProjectConfig.Cache().NewManifestHash(ctx, slackYaml.AppManifest) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return clients.Config.ProjectConfig.Cache().SetManifestHash(ctx, appID, hash) | ||
|
Comment on lines
+40
to
+44
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏆 praise: Thanks for including this! 🦔 thought: I'm now curious if this would be better as part of the |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| // Copyright 2022-2026 Salesforce, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package manifest | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/slackapi/slack-cli/internal/app" | ||
| "github.com/slackapi/slack-cli/internal/cache" | ||
| "github.com/slackapi/slack-cli/internal/config" | ||
| "github.com/slackapi/slack-cli/internal/goutils" | ||
| "github.com/slackapi/slack-cli/internal/hooks" | ||
| "github.com/slackapi/slack-cli/internal/shared" | ||
| "github.com/slackapi/slack-cli/internal/shared/types" | ||
| "github.com/slackapi/slack-cli/internal/slackcontext" | ||
| "github.com/slackapi/slack-cli/internal/slackerror" | ||
| "github.com/spf13/afero" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/mock" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func Test_SetManifestLocal(t *testing.T) { | ||
| testManifest := types.SlackYaml{ | ||
| AppManifest: types.AppManifest{ | ||
| DisplayInformation: types.DisplayInformation{Name: "Test App"}, | ||
| }, | ||
| } | ||
|
|
||
| tests := map[string]struct { | ||
| setupMocks func(*shared.ClientsMock, *shared.ClientFactory) | ||
| expectError bool | ||
| assertFile bool | ||
| }{ | ||
| "writes manifest and saves hash on success": { | ||
| setupMocks: func(clientsMock *shared.ClientsMock, clients *shared.ClientFactory) { | ||
| manifestMock := &app.ManifestMockObject{} | ||
| manifestMock.On("GetManifestRemote", mock.Anything, "xoxp-token", "A001"). | ||
| Return(testManifest, nil) | ||
| clients.AppClient().Manifest = manifestMock | ||
|
|
||
| cc := cache.NewCacheMock() | ||
| cc.On("NewManifestHash", mock.Anything, testManifest.AppManifest). | ||
| Return(cache.Hash("abc123"), nil) | ||
| cc.On("SetManifestHash", mock.Anything, "A001", cache.Hash("abc123")). | ||
| Return(nil) | ||
|
|
||
| proj := config.NewProjectConfigMock() | ||
| proj.On("Cache").Return(cc) | ||
| clients.Config.ProjectConfig = proj | ||
| }, | ||
| expectError: false, | ||
| assertFile: true, | ||
| }, | ||
| "returns error when GetManifestRemote fails": { | ||
| setupMocks: func(clientsMock *shared.ClientsMock, clients *shared.ClientFactory) { | ||
| manifestMock := &app.ManifestMockObject{} | ||
| manifestMock.On("GetManifestRemote", mock.Anything, mock.Anything, mock.Anything). | ||
| Return(types.SlackYaml{}, slackerror.New("api error")) | ||
| clients.AppClient().Manifest = manifestMock | ||
| }, | ||
| expectError: true, | ||
| assertFile: false, | ||
| }, | ||
| "returns error when file write fails": { | ||
| setupMocks: func(clientsMock *shared.ClientsMock, clients *shared.ClientFactory) { | ||
| manifestMock := &app.ManifestMockObject{} | ||
| manifestMock.On("GetManifestRemote", mock.Anything, mock.Anything, mock.Anything). | ||
| Return(testManifest, nil) | ||
| clients.AppClient().Manifest = manifestMock | ||
|
|
||
| clients.Fs = afero.NewReadOnlyFs(&afero.MemMapFs{}) | ||
| }, | ||
| expectError: true, | ||
| assertFile: false, | ||
| }, | ||
| "returns error when SetManifestHash fails": { | ||
| setupMocks: func(clientsMock *shared.ClientsMock, clients *shared.ClientFactory) { | ||
| manifestMock := &app.ManifestMockObject{} | ||
| manifestMock.On("GetManifestRemote", mock.Anything, mock.Anything, mock.Anything). | ||
| Return(testManifest, nil) | ||
| clients.AppClient().Manifest = manifestMock | ||
|
|
||
| cc := cache.NewCacheMock() | ||
| cc.On("NewManifestHash", mock.Anything, mock.Anything). | ||
| Return(cache.Hash("abc123"), nil) | ||
| cc.On("SetManifestHash", mock.Anything, mock.Anything, mock.Anything). | ||
| Return(slackerror.New("cache write error")) | ||
|
|
||
| proj := config.NewProjectConfigMock() | ||
| proj.On("Cache").Return(cc) | ||
| clients.Config.ProjectConfig = proj | ||
| }, | ||
| expectError: true, | ||
| assertFile: true, | ||
| }, | ||
| } | ||
|
|
||
| for name, tc := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| ctx := slackcontext.MockContext(t.Context()) | ||
| clientsMock := shared.NewClientsMock() | ||
| clientsMock.AddDefaultMocks() | ||
| clients := shared.NewClientFactory(clientsMock.MockClientFactory(), func(c *shared.ClientFactory) { | ||
| c.SDKConfig = hooks.NewSDKConfigMock() | ||
| }) | ||
|
|
||
| tc.setupMocks(clientsMock, clients) | ||
|
|
||
| err := SetManifestLocal(ctx, clients, "xoxp-token", "A001", "/project") | ||
|
|
||
| if tc.expectError { | ||
| assert.Error(t, err) | ||
| } else { | ||
| assert.NoError(t, err) | ||
| } | ||
|
|
||
| if tc.assertFile { | ||
| content, readErr := afero.ReadFile(clients.Fs, "/project/manifest.json") | ||
| require.NoError(t, readErr) | ||
|
|
||
| expected, _ := goutils.JSONMarshalUnescapedIndent(testManifest.AppManifest) | ||
| assert.Equal(t, expected, string(content)) | ||
| } | ||
| }) | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
left a comment about this silent fail 🪂 #585 (comment) @zimeg
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@srtaalej I'm still hesitant to passthrough with an unexpected manifest. I'll leave a review comment but am curious if earlier thinking still stands?