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 authored 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.