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

[MM-53016] Pass timeout to outgoing request to MM server #476

Merged
merged 7 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion apps/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package apps
import (
"encoding/json"

"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost/server/public/model"
)

// App describes an App installed on a Mattermost instance. App should be
Expand Down
17 changes: 9 additions & 8 deletions apps/appclient/mattermost_client.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package appclient

import (
"context"
"fmt"
"net/http"

"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost/server/public/model"
"github.com/pkg/errors"

"github.com/mattermost/mattermost-plugin-apps/apps"
Expand Down Expand Up @@ -187,8 +188,8 @@ func (c *Client) Call(creq apps.CallRequest) (*apps.CallResponse, error) {
return cresp, nil
}

func (c *Client) CreatePost(post *model.Post) (*model.Post, error) {
createdPost, res, err := c.Client4.CreatePost(post)
func (c *Client) CreatePost(ctx context.Context, post *model.Post) (*model.Post, error) {
createdPost, res, err := c.Client4.CreatePost(ctx, post)
if err != nil {
return nil, err
}
Expand All @@ -200,18 +201,18 @@ func (c *Client) CreatePost(post *model.Post) (*model.Post, error) {
return createdPost, nil
}

func (c *Client) DM(userID string, format string, args ...interface{}) (*model.Post, error) {
return c.DMPost(userID, &model.Post{
func (c *Client) DM(ctx context.Context, userID string, format string, args ...interface{}) (*model.Post, error) {
return c.DMPost(ctx, userID, &model.Post{
Message: fmt.Sprintf(format, args...),
})
}

func (c *Client) DMPost(userID string, post *model.Post) (*model.Post, error) {
func (c *Client) DMPost(ctx context.Context, userID string, post *model.Post) (*model.Post, error) {
if c.userID == "" {
return nil, errors.New("empty sender user_id, perhaps Call does not expand acting_user")
}

channel, res, err := c.CreateDirectChannel(c.userID, userID)
channel, res, err := c.CreateDirectChannel(ctx, c.userID, userID)
if err != nil {
return nil, errors.Wrap(err, "failed to get direct channel")
}
Expand All @@ -221,5 +222,5 @@ func (c *Client) DMPost(userID string, post *model.Post) (*model.Post, error) {
}

post.ChannelId = channel.Id
return c.CreatePost(post)
return c.CreatePost(ctx, post)
}
2 changes: 1 addition & 1 deletion apps/appclient/mattermost_client_pp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/pkg/errors"

"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost/server/public/model"

"github.com/mattermost/mattermost-plugin-apps/apps"
appspath "github.com/mattermost/mattermost-plugin-apps/apps/path"
Expand Down
2 changes: 1 addition & 1 deletion apps/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"sort"
"strings"

"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost/server/public/model"

"github.com/mattermost/mattermost-plugin-apps/utils"
)
Expand Down
2 changes: 1 addition & 1 deletion apps/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/pkg/errors"

"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost/server/public/model"

"github.com/mattermost/mattermost-plugin-apps/utils"
)
Expand Down
2 changes: 1 addition & 1 deletion apps/goapp/call_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/stretchr/testify/require"

"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost/server/public/model"

"github.com/mattermost/mattermost-plugin-apps/apps"
)
Expand Down
2 changes: 1 addition & 1 deletion build/custom.mk
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ifdef MM_RUDDER_PLUGINS_PROD
RUDDER_WRITE_KEY = $(MM_RUDDER_PLUGINS_PROD)
endif

LDFLAGS += -X "github.com/mattermost/mattermost-plugin-api/experimental/telemetry.rudderWriteKey=$(RUDDER_WRITE_KEY)"
LDFLAGS += -X "github.com/mattermost/mattermost/server/public/pluginapi/experimental/telemetry.rudderWriteKey=$(RUDDER_WRITE_KEY)"

BUILD_DATE = $(shell date -u)
BUILD_HASH = $(shell git rev-parse HEAD)
Expand Down
2 changes: 1 addition & 1 deletion build/manifest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"os"

"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost/server/public/model"
"github.com/pkg/errors"
)

Expand Down
43 changes: 25 additions & 18 deletions build/pluginctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
package main

import (
"context"
"errors"
"fmt"
"log"
"net"
"os"
"time"

"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost/server/public/model"
)

const commandTimeout = 120 * time.Second

const helpText = `
Usage:
pluginctl deploy <plugin id> <bundle path>
Expand All @@ -33,7 +37,10 @@ func pluginctl() error {
return errors.New("invalid number of arguments")
}

client, err := getClient()
ctx, cancel := context.WithTimeout(context.Background(), commandTimeout)
defer cancel()

client, err := getClient(ctx)
if err != nil {
return err
}
Expand All @@ -43,19 +50,19 @@ func pluginctl() error {
if len(os.Args) < 4 {
return errors.New("invalid number of arguments")
}
return deploy(client, os.Args[2], os.Args[3])
return deploy(ctx, client, os.Args[2], os.Args[3])
case "disable":
return disablePlugin(client, os.Args[2])
return disablePlugin(ctx, client, os.Args[2])
case "enable":
return enablePlugin(client, os.Args[2])
return enablePlugin(ctx, client, os.Args[2])
case "reset":
return resetPlugin(client, os.Args[2])
return resetPlugin(ctx, client, os.Args[2])
default:
return errors.New("invalid second argument")
}
}

func getClient() (*model.Client4, error) {
func getClient(ctx context.Context) (*model.Client4, error) {
socketPath := os.Getenv("MM_LOCALSOCKETPATH")
if socketPath == "" {
socketPath = model.LocalModeSocketPath
Expand Down Expand Up @@ -91,7 +98,7 @@ func getClient() (*model.Client4, error) {
if adminUsername != "" && adminPassword != "" {
client := model.NewAPIv4Client(siteURL)
log.Printf("Authenticating as %s against %s.", adminUsername, siteURL)
_, _, err := client.Login(adminUsername, adminPassword)
_, _, err := client.Login(ctx, adminUsername, adminPassword)
if err != nil {
return nil, fmt.Errorf("failed to login as %s: %w", adminUsername, err)
}
Expand All @@ -113,21 +120,21 @@ func getUnixClient(socketPath string) (*model.Client4, bool) {

// deploy attempts to upload and enable a plugin via the Client4 API.
// It will fail if plugin uploads are disabled.
func deploy(client *model.Client4, pluginID, bundlePath string) error {
func deploy(ctx context.Context, client *model.Client4, pluginID, bundlePath string) error {
pluginBundle, err := os.Open(bundlePath)
if err != nil {
return fmt.Errorf("failed to open %s: %w", bundlePath, err)
}
defer pluginBundle.Close()

log.Print("Uploading plugin via API.")
_, _, err = client.UploadPluginForced(pluginBundle)
_, _, err = client.UploadPluginForced(ctx, pluginBundle)
if err != nil {
return fmt.Errorf("failed to upload plugin bundle: %s", err.Error())
}

log.Print("Enabling plugin.")
_, err = client.EnablePlugin(pluginID)
_, err = client.EnablePlugin(ctx, pluginID)
if err != nil {
return fmt.Errorf("failed to enable plugin: %s", err.Error())
}
Expand All @@ -136,9 +143,9 @@ func deploy(client *model.Client4, pluginID, bundlePath string) error {
}

// disablePlugin attempts to disable the plugin via the Client4 API.
func disablePlugin(client *model.Client4, pluginID string) error {
func disablePlugin(ctx context.Context, client *model.Client4, pluginID string) error {
log.Print("Disabling plugin.")
_, err := client.DisablePlugin(pluginID)
_, err := client.DisablePlugin(ctx, pluginID)
if err != nil {
return fmt.Errorf("failed to disable plugin: %w", err)
}
Expand All @@ -147,9 +154,9 @@ func disablePlugin(client *model.Client4, pluginID string) error {
}

// enablePlugin attempts to enable the plugin via the Client4 API.
func enablePlugin(client *model.Client4, pluginID string) error {
func enablePlugin(ctx context.Context, client *model.Client4, pluginID string) error {
log.Print("Enabling plugin.")
_, err := client.EnablePlugin(pluginID)
_, err := client.EnablePlugin(ctx, pluginID)
if err != nil {
return fmt.Errorf("failed to enable plugin: %w", err)
}
Expand All @@ -158,13 +165,13 @@ func enablePlugin(client *model.Client4, pluginID string) error {
}

// resetPlugin attempts to reset the plugin via the Client4 API.
func resetPlugin(client *model.Client4, pluginID string) error {
err := disablePlugin(client, pluginID)
func resetPlugin(ctx context.Context, client *model.Client4, pluginID string) error {
err := disablePlugin(ctx, client, pluginID)
if err != nil {
return err
}

err = enablePlugin(client, pluginID)
err = enablePlugin(ctx, client, pluginID)
if err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/appsctl/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"time"

"github.com/spf13/cobra"
"go.uber.org/zap/zapcore"

Expand All @@ -15,6 +17,10 @@ var (

var log = utils.MustMakeCommandLogger(zapcore.InfoLevel)

const (
commandTimeout = 2 * time.Minute
)

var (
dockerRegistry string
executeRoleName string
Expand Down
7 changes: 4 additions & 3 deletions cmd/appsctl/mattermost.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package main

import (
"context"
"net/http"
"os"
"strings"
Expand Down Expand Up @@ -46,19 +47,19 @@ func updateMattermost(appClient *appclient.Client, m apps.Manifest, deployType a
return nil
}

func installPlugin(appClient *appclient.Client, bundlePath string) (*apps.Manifest, error) {
func installPlugin(ctx context.Context, appClient *appclient.Client, bundlePath string) (*apps.Manifest, error) {
f, err := os.Open(bundlePath)
if err != nil {
return nil, errors.Wrap(err, "failed to open the plugin bundle")
}
defer f.Close()

pluginManifest, _, err := appClient.UploadPluginForced(f)
pluginManifest, _, err := appClient.UploadPluginForced(ctx, f)
if err != nil {
return nil, errors.Wrap(err, "failed to upload the plugin to Mattermost")
}

_, err = appClient.EnablePlugin(pluginManifest.Id)
_, err = appClient.EnablePlugin(ctx, pluginManifest.Id)
if err != nil {
return nil, errors.Wrap(err, "failed to enable plugin on Mattermost")
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/appsctl/plugin.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -33,7 +34,10 @@ var pluginDeployCmd = &cobra.Command{
return err
}

m, err := installPlugin(appClient, bundlePath)
ctx, cancel := context.WithTimeout(context.Background(), commandTimeout)
defer cancel()

m, err := installPlugin(ctx, appClient, bundlePath)
if err != nil {
return err
}
Expand Down
Loading
Loading