Skip to content

Commit

Permalink
KV example app, and a test for prefixes (#356)
Browse files Browse the repository at this point in the history
* wip

* wip: goapp builds

* more wip

* wip

* fixed examples README port ref

* removed a couple accidental changes

* wip - switching to a different task

* use functional options for better readability of the app

* a few fixes

* notify command

* tests pass

* style + comments + minor

* Eliminated Context.ActingUserID altogether

* wip stash

* wip /info works again, with IDs only

* wip example-expand user action seems to work (happy)

* Fixed expand level none parsing

* moved/rewrote all REST API tests

* goapp, and use it in REST API tests

* Makefile adjustment

* space

* moved code around

* Update apps/goapp/bindable.go

Co-authored-by: Michael Kochell <6913320+mickmister@users.noreply.github.com>

* PR feedback

* PR feedback: comment

* PR feedback: remove ServiceNow reference

* PR feedback: strings

* PR feedback: experimental

* fixed a test

* remamed invoke_call.go back to call.go to see the diff better

* make all works, moved a bunch of test stubs to restapitest

* ci to use go 1.18.2

* up golangci-lint to 1.46.1

* Updated OAuth2 REST tests

* add test for AppsMetadata returned by proxy

* bindings test

* passes rest test again

* more generic bindings test

* more bindings-related cleanup

* wip

* more bindings

* finished bindings test

* reenabled other tests

* beginning of notify test

* more refactoring

* touches

* more touches

* notify tests

* Update server/appservices/subscriptions.go

Co-authored-by: Michael Kochell <6913320+mickmister@users.noreply.github.com>

* added test for SubjectUserLeftTeam

* notify all subjects, happy, no expand

* lint

* removed scratchpad lines

* PR feedback: typo

* Update server/proxy/invoke_oauth2.go

Co-authored-by: Michael Kochell <6913320+mickmister@users.noreply.github.com>

* Update server/proxy/service.go

Co-authored-by: Michael Kochell <6913320+mickmister@users.noreply.github.com>

* test user_created with expand and all client combinations

* attempted to add test-rest-api to coverage

* Revert "attempted to add test-rest-api to coverage"

This reverts commit fcbcb4e.

* bot_joined_channel

* lint

* comments

* bot_left_channel + refactoring

* bot_joined/left_team

* lint

* go 1.18.3

* more ci go version

* updated mmserver from 6.6 to current

* more tests

* fixed E2E docker image back to 6.6

* fixed E2E docker image back to 6.6 once more

* lowered required server to 6.6 fot the E2E tests to pass

* restructuring tests/precision

* added bot_left_channel, more restructuring/fixes

* style

* saved a second off the test by using cached values for comparisons, where possible

* All tests pass

* type in cicrleci config

* Update server/httpin/service.go

Co-authored-by: Michael Kochell <6913320+mickmister@users.noreply.github.com>

* Update server/proxy/invoke_bindings.go

Co-authored-by: Daniel Espino García <larkox@gmail.com>

* Update server/proxy/invoke_bindings.go

Co-authored-by: Daniel Espino García <larkox@gmail.com>

* style

* fixed static and added a test

* comment

* Uninstall cleans out subscriptions

* removed old notfy tests

* added uninstall test

* Finished uninstall test

* lint

* i18n

* PR feedback and fixes

* PR feedback + TestRESTAPI/echo/email_expansion_is_controlled_by_server_settings

* renamed call.go -> invoke_call.go for consistency

* PR feedback: added comments

* fixed tests

* Examples: lifecycle, webhooks

* Example: OAuth2

* serverless example + small fixes for static

* fixed bindings test

* wip

* fixed the go.mod mess

* PR feedback: comments and strins

* set content-type json

* fixed the test app (#353)

* Update mattermost-server to 7.2, improve circle-ci caching (#354)

* fixed the test app

* Updated to server v7.2

* go mod tidy

* try release 7.1

* try 1

* try 2

* try 3

* try 4

* try 5

* try 6

* kv goapp example

* Added a REST API test for KV prefixes

* touches on kv example

Co-authored-by: Michael Kochell <6913320+mickmister@users.noreply.github.com>
Co-authored-by: Daniel Espino García <larkox@gmail.com>
  • Loading branch information
3 people committed Sep 29, 2022
1 parent b8285d1 commit da13e76
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 269 deletions.
2 changes: 2 additions & 0 deletions assets/i18n/active.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
"field.include_plugins.description": "include compatible Mattermost plugins in the output.",
"field.include_plugins.label": "include-plugins",
"field.kv.action.modal_label": "Action to take",
"field.kv.base64.description": "base64 encoded keys to use in other `debug kv` commands.",
"field.kv.base64.label": "base64",
"field.kv.base64key.description": "base64-encoded key, see output of `debug kv list`.",
"field.kv.base64key.hint": "[ base64-encoded key ]",
"field.kv.base64key.label": "base64_key",
Expand Down
237 changes: 0 additions & 237 deletions examples/goapp/expand/notify.gogo

This file was deleted.

130 changes: 130 additions & 0 deletions examples/goapp/kv/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package main

import (
"embed"

"github.com/mattermost/mattermost-plugin-apps/apps"
"github.com/mattermost/mattermost-plugin-apps/apps/goapp"
)

// static is preloaded with the contents of the ./static directory.
//go:embed static
var static embed.FS

// main starts the app, as a standalone HTTP server. Use $PORT and $ROOT_URL to
// customize.
func main() {
// Create the app, add `send` to the app's command and the channel header.
goapp.MakeAppOrPanic(
apps.Manifest{
AppID: "example-kv",
Version: "v1.2.0",
DisplayName: "Example of using the KV store",
Icon: "icon.png",
HomepageURL: "https://github.com/mattermost/mattermost-plugin-apps/examples/go/goapp",
RequestedPermissions: []apps.Permission{
apps.PermissionActAsBot,
apps.PermissionActAsUser,
},
},
goapp.WithStatic(static),
goapp.WithCommand(get, set),
).RunHTTP()
}

var set = goapp.MakeBindableFormOrPanic("set",
apps.Form{
Title: "Store a value in the KV store",
Icon: "icon.png",
Fields: []apps.Field{
{
Name: "prefix",
Description: "The namespace prefix to use, just 2 charachters, don't even ask why...",
TextMaxLength: 2,
},
{
Name: "key",
Description: "The key (id) to use",
TextMaxLength: 28,
},
{
Name: "value",
Description: "The value to store, as text",
},
{
Name: "as_bot",
Description: "Act as the app's bot, as opposed to the acting user",
Type: apps.FieldTypeBool,
},
},
Submit: &apps.Call{
Expand: &apps.Expand{
ActingUser: apps.ExpandID.Required(),
ActingUserAccessToken: apps.ExpandAll.Required(),
},
},
},
func(creq goapp.CallRequest) apps.CallResponse {
prefix := creq.GetValue("prefix", "")
key := creq.GetValue("key", "")
value := creq.GetValue("value", "")

client := creq.AsBot()
asBot, _ := creq.BoolValue("as_bot")
if !asBot {
client = creq.AsActingUser()
}
changed, err := client.KVSet(prefix, key, value)
if err != nil {
return apps.NewTextResponse("Error: %v", err)
}
return apps.NewTextResponse("Stored a value in the KV store: prefix: %q, key: %q, value: %q, changed: %v", prefix, key, value, changed)
},
)

var get = goapp.MakeBindableFormOrPanic("get",
apps.Form{
Title: "Get a value from the KV store",
Icon: "icon.png",
Fields: []apps.Field{
{
Name: "prefix",
Description: "The namespace prefix to use, just 2 charachters, don't even ask why...",
TextMaxLength: 2,
},
{
Name: "key",
Description: "The key (id) to use",
TextMaxLength: 28,
},
{
Name: "as_bot",
Description: "Act as the app's bot, as opposed to the acting user",
Type: apps.FieldTypeBool,
},
},
Submit: &apps.Call{
Expand: &apps.Expand{
ActingUser: apps.ExpandID.Required(),
ActingUserAccessToken: apps.ExpandAll.Required(),
},
},
},
func(creq goapp.CallRequest) apps.CallResponse {
prefix := creq.GetValue("prefix", "")
key := creq.GetValue("key", "")

client := creq.AsBot()
asBot, _ := creq.BoolValue("as_bot")
if !asBot {
client = creq.AsActingUser()
}

var value interface{}
err := client.KVGet(prefix, key, &value)
if err != nil {
return apps.NewTextResponse("Error: %v", err)
}
return apps.NewTextResponse("Read a value from the KV store: prefix: %q, key: %q, value: %#v", prefix, key, value)
},
)
Binary file added examples/goapp/kv/static/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions server/builtin/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,21 @@ func (a *builtinApp) debugBase64KeyField(loc *i18n.Localizer) apps.Field {
ID: "field.kv.base64key.hint",
Other: "[ base64-encoded key ]",
}),
}
}

func (a *builtinApp) debugBase64Field(loc *i18n.Localizer) apps.Field {
return apps.Field{
Name: fBase64,
Type: apps.FieldTypeBool,
Label: a.conf.I18N().LocalizeDefaultMessage(loc, &i18n.Message{
ID: "field.kv.base64.label",
Other: "base64",
}),
Description: a.conf.I18N().LocalizeDefaultMessage(loc, &i18n.Message{
ID: "field.kv.base64.description",
Other: "base64 encoded keys to use in other `debug kv` commands.",
}),
Value: true,
}
}
Loading

0 comments on commit da13e76

Please sign in to comment.