Skip to content

Commit

Permalink
Adopt memorystore for tests (#745)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanzei committed Mar 5, 2024
1 parent 475c110 commit aa1faef
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 140 deletions.
44 changes: 22 additions & 22 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ go 1.21
require (
github.com/Masterminds/sprig/v3 v3.2.2
github.com/google/go-github/v41 v41.0.0
github.com/gorilla/mux v1.8.0
github.com/mattermost/mattermost/server/public v0.0.12-0.20231212135924-685c08e7fb3a
github.com/gorilla/mux v1.8.1
github.com/mattermost/mattermost/server/public v0.0.15
github.com/microcosm-cc/bluemonday v1.0.19
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.4
golang.org/x/oauth2 v0.7.0
golang.org/x/oauth2 v0.17.0
)

require (
Expand All @@ -20,17 +20,17 @@ require (
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dyatlov/go-opengraph/opengraph v0.0.0-20220524092352-606d7b1e5f8a // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
github.com/go-asn1-ber/asn1-ber v1.5.4 // indirect
github.com/go-asn1-ber/asn1-ber v1.5.5 // indirect
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect
github.com/hashicorp/go-plugin v1.4.10 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/hashicorp/go-hclog v1.6.2 // indirect
github.com/hashicorp/go-plugin v1.6.0 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/huandu/xstrings v1.3.1 // indirect
github.com/imdario/mergo v0.3.11 // indirect
Expand All @@ -39,7 +39,7 @@ require (
github.com/mattermost/ldap v0.0.0-20231116144001-0f480c025956 // indirect
github.com/mattermost/logr/v2 v2.0.21 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
Expand All @@ -55,24 +55,24 @@ require (
github.com/shurcooL/graphql v0.0.0-20220606043923-3cf50f8a0a29 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/tidwall/gjson v1.14.3 // indirect
github.com/stretchr/objx v0.5.1 // indirect
github.com/tidwall/gjson v1.17.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tinylib/msgp v1.1.8 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tinylib/msgp v1.1.9 // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/wiggin77/merror v1.0.5 // indirect
github.com/wiggin77/srslog v1.0.1 // indirect
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/crypto v0.20.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/grpc v1.56.3 // indirect
google.golang.org/protobuf v1.31.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/grpc v1.62.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
105 changes: 53 additions & 52 deletions go.sum

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions server/plugin/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func (p *Plugin) connectUserToGitHub(c *Context, w http.ResponseWriter, r *http.
PrivateAllowed: privateAllowed,
}

_, err := p.client.KV.Set(githubOauthKey+state.Token, state, pluginapi.SetExpiry(TokenTTL))
_, err := p.store.Set(githubOauthKey+state.Token, state, pluginapi.SetExpiry(TokenTTL))
if err != nil {
http.Error(w, "error setting stored state", http.StatusBadRequest)
return
Expand Down Expand Up @@ -352,7 +352,7 @@ func (p *Plugin) completeConnectUserToGitHub(c *Context, w http.ResponseWriter,
stateToken := r.URL.Query().Get("state")

var state OAuthState
err := p.client.KV.Get(githubOauthKey+stateToken, &state)
err := p.store.Get(githubOauthKey+stateToken, &state)
if err != nil {
c.Log.Warnf("Failed to get state token", "error", err.Error())

Expand All @@ -361,7 +361,7 @@ func (p *Plugin) completeConnectUserToGitHub(c *Context, w http.ResponseWriter,
return
}

err = p.client.KV.Delete(githubOauthKey + stateToken)
err = p.store.Delete(githubOauthKey + stateToken)
if err != nil {
c.Log.WithError(err).Warnf("Failed to delete state token")

Expand Down Expand Up @@ -620,7 +620,7 @@ func (p *Plugin) getConnected(c *Context, w http.ResponseWriter, r *http.Request
privateRepoStoreKey := info.UserID + githubPrivateRepoKey
if config.EnablePrivateRepo && !info.AllowedPrivateRepos {
var val []byte
err := p.client.KV.Get(privateRepoStoreKey, &val)
err := p.store.Get(privateRepoStoreKey, &val)
if err != nil {
c.Log.WithError(err).Warnf("Unable to get private repo key value")
return
Expand All @@ -634,7 +634,7 @@ func (p *Plugin) getConnected(c *Context, w http.ResponseWriter, r *http.Request
} else {
p.CreateBotDMPost(info.UserID, fmt.Sprintf(message, "`/github connect private`."), "")
}
_, err := p.client.KV.Set(privateRepoStoreKey, []byte("1"))
_, err := p.store.Set(privateRepoStoreKey, []byte("1"))
if err != nil {
c.Log.WithError(err).Warnf("Unable to set private repo key value")
}
Expand Down
36 changes: 18 additions & 18 deletions server/plugin/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (p *Plugin) postCommandResponse(args *model.CommandArgs, text string) {

func (p *Plugin) getMutedUsernames(userInfo *GitHubUserInfo) []string {
var mutedUsernameBytes []byte
err := p.client.KV.Get(userInfo.UserID+"-muted-users", &mutedUsernameBytes)
err := p.store.Get(userInfo.UserID+"-muted-users", &mutedUsernameBytes)
if err != nil {
return nil
}
Expand Down Expand Up @@ -184,7 +184,7 @@ func (p *Plugin) handleMuteAdd(args *model.CommandArgs, username string, userInf
mutedUsers = username
}

_, err := p.client.KV.Set(userInfo.UserID+"-muted-users", []byte(mutedUsers))
_, err := p.store.Set(userInfo.UserID+"-muted-users", []byte(mutedUsers))
if err != nil {
return "Error occurred saving list of muted users"
}
Expand All @@ -197,7 +197,7 @@ func (p *Plugin) handleUnmute(args *model.CommandArgs, username string, userInfo
userToMute := []string{username}
newMutedList := arrayDifference(mutedUsernames, userToMute)

_, err := p.client.KV.Set(userInfo.UserID+"-muted-users", []byte(strings.Join(newMutedList, ",")))
_, err := p.store.Set(userInfo.UserID+"-muted-users", []byte(strings.Join(newMutedList, ",")))
if err != nil {
return "Error occurred unmuting users"
}
Expand All @@ -206,7 +206,7 @@ func (p *Plugin) handleUnmute(args *model.CommandArgs, username string, userInfo
}

func (p *Plugin) handleUnmuteAll(args *model.CommandArgs, userInfo *GitHubUserInfo) string {
_, err := p.client.KV.Set(userInfo.UserID+"-muted-users", []byte(""))
_, err := p.store.Set(userInfo.UserID+"-muted-users", []byte(""))
if err != nil {
return "Error occurred unmuting users"
}
Expand Down Expand Up @@ -318,9 +318,9 @@ func (p *Plugin) createPost(channelID, userID, message string) error {
Message: message,
}

if _, appErr := p.API.CreatePost(post); appErr != nil {
p.API.LogWarn("Error while creating post", "Post", post, "Error", appErr.Error())
return appErr
if err := p.client.Post.CreatePost(post); err != nil {
p.client.Log.Warn("Error while creating post", "post", post, "error", err.Error())
return err
}

return nil
Expand All @@ -345,7 +345,7 @@ func (p *Plugin) checkIfConfiguredWebhookExists(ctx context.Context, githubClien
}

if err != nil {
p.API.LogWarn("Not able to get the list of webhooks", "Owner", owner, "Repo", repo, "Error", err.Error())
p.client.Log.Warn("Not able to get the list of webhooks", "Owner", owner, "Repo", repo, "error", err.Error())
return found, err
}

Expand Down Expand Up @@ -423,9 +423,9 @@ func (p *Plugin) handleSubscribesAdd(_ *plugin.Context, args *model.CommandArgs,

ctx := context.Background()
githubClient := p.githubConnectUser(ctx, userInfo)
user, appErr := p.API.GetUser(args.UserId)
if appErr != nil {
return errors.Wrap(appErr, "failed to get the user").Error()
user, err := p.client.User.Get(args.UserId)
if err != nil {
return errors.Wrap(err, "failed to get the user").Error()
}

owner, repo := parseOwnerAndRepo(parameters[0], baseURL)
Expand Down Expand Up @@ -492,7 +492,7 @@ func (p *Plugin) handleSubscribesAdd(_ *plugin.Context, args *model.CommandArgs,
}

if err = p.createPost(args.ChannelId, p.BotUserID, msg); err != nil {
return fmt.Sprintf("%s\nError creating the public post: %s", msg, appErr.Error())
return fmt.Sprintf("%s\nError creating the public post: %s", msg, err.Error())
}

found, err := p.checkIfConfiguredWebhookExists(ctx, githubClient, repo, owner)
Expand Down Expand Up @@ -546,15 +546,15 @@ func (p *Plugin) handleUnsubscribe(_ *plugin.Context, args *model.CommandArgs, p
owner = strings.ToLower(owner)
repo = strings.ToLower(repo)
if err := p.Unsubscribe(args.ChannelId, repo, owner); err != nil {
p.API.LogWarn("Failed to unsubscribe", "repo", repo, "error", err.Error())
p.client.Log.Warn("Failed to unsubscribe", "repo", repo, "error", err.Error())
return "Encountered an error trying to unsubscribe. Please try again."
}

baseURL := config.getBaseURL()
user, appErr := p.API.GetUser(args.UserId)
if appErr != nil {
p.API.LogWarn("Error while fetching user details", "Error", appErr.Error())
return fmt.Sprintf("error while fetching user details: %s", appErr.Error())
user, err := p.client.User.Get(args.UserId)
if err != nil {
p.client.Log.Warn("Error while fetching user details", "error", err.Error())
return fmt.Sprintf("error while fetching user details: %s", err.Error())
}

unsubscribeMessage := ""
Expand Down Expand Up @@ -663,7 +663,7 @@ func (p *Plugin) handleSettings(_ *plugin.Context, _ *model.CommandArgs, paramet
"error", err.Error())
}
} else {
err := p.client.KV.Delete(userInfo.GitHubUsername + githubUsernameKey)
err := p.store.Delete(userInfo.GitHubUsername + githubUsernameKey)
if err != nil {
p.client.Log.Warn("Failed to delete GitHub to userID mapping",
"userID", userInfo.UserID,
Expand Down
5 changes: 1 addition & 4 deletions server/plugin/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/pkg/errors"

"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/pluginapi"
"github.com/mattermost/mattermost/server/public/pluginapi/experimental/telemetry"
)

Expand Down Expand Up @@ -193,9 +192,7 @@ func (p *Plugin) setConfiguration(configuration *Configuration) {

// OnConfigurationChange is invoked when configuration changes may have been made.
func (p *Plugin) OnConfigurationChange() error {
if p.client == nil {
p.client = pluginapi.NewClient(p.API, p.Driver)
}
p.ensurePluginAPIClient()

var configuration = new(Configuration)

Expand Down
2 changes: 1 addition & 1 deletion server/plugin/graphql/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewClient(logger pluginapi.LogService, token oauth2.Token, username, orgNam
} else {
baseURL, err := url.Parse(enterpriseBaseURL)
if err != nil {
logger.Debug("Not able to parse the URL", "Error", err.Error())
logger.Debug("Not able to parse the URL", "error", err.Error())
return nil
}

Expand Down
8 changes: 4 additions & 4 deletions server/plugin/mm_34646_token_refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (p *Plugin) forceResetAllMM34646() error {

time.Sleep(delayToStart)
var data []byte
err := p.client.KV.Get(mm34646DoneKey, &data)
err := p.store.Get(mm34646DoneKey, &data)
if err != nil {
return errors.Wrap(err, "failed check whether MM-34646 refresh is already done")
}
Expand All @@ -38,14 +38,14 @@ func (p *Plugin) forceResetAllMM34646() error {

for page := 0; ; page++ {
var keys []string
keys, err = p.client.KV.ListKeys(page, pageSize)
keys, err = p.store.ListKeys(page, pageSize)
if err != nil {
return err
}

for _, key := range keys {
var tryInfo GitHubUserInfo
err = p.client.KV.Get(key, &tryInfo)
err = p.store.Get(key, &tryInfo)
if err != nil {
p.client.Log.Warn("failed to inspect key", "key", key, "error",
err.Error())
Expand Down Expand Up @@ -82,7 +82,7 @@ func (p *Plugin) forceResetAllMM34646() error {
}
}

_, err = p.client.KV.Set(mm34646DoneKey, []byte("done"))
_, err = p.store.Set(mm34646DoneKey, []byte("done"))
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit aa1faef

Please sign in to comment.