diff --git a/server/api.go b/server/api.go index 3a1f7c10..bf9b57bb 100644 --- a/server/api.go +++ b/server/api.go @@ -172,7 +172,11 @@ func (p *Plugin) completeConnectUserToGitlab(w http.ResponseWriter, r *http.Requ } if err := p.storeGitlabToUserIDMapping(userInfo.GitlabUsername, userID); err != nil { - p.API.LogError("can't store user id mapping", "err", err.Error()) + p.API.LogError("can't store GitLab to user id mapping", "err", err.Error()) + } + + if err := p.storeGitlabIDToUserIDMapping(userInfo.GitlabUsername, userInfo.GitlabUserID); err != nil { + p.API.LogError("can't store GitLab to GitLab id mapping", "err", err.Error()) } // Post intro post diff --git a/server/command.go b/server/command.go index 4cd22c44..55487f6c 100644 --- a/server/command.go +++ b/server/command.go @@ -208,14 +208,16 @@ func (p *Plugin) ExecuteCommand(_ *plugin.Context, args *model.CommandArgs) (*mo case SettingNotifications: if value { if err := p.storeGitlabToUserIDMapping(info.GitlabUsername, info.UserID); err != nil { - p.API.LogError("can't store GitLab user id mapping", "err", err.Error()) + p.API.LogError("can't store GitLab to user id mapping", "err", err.Error()) return p.getCommandResponse(args, "Unknown error please retry or ask to an administrator to look at logs"), nil } - } else { - if err := p.API.KVDelete(info.GitlabUsername + GitlabUsernameKey); err != nil { - p.API.LogError("can't delete GitLab username in kvstore", "err", err.Error()) + if err := p.storeGitlabIDToUserIDMapping(info.GitlabUsername, info.GitlabUserID); err != nil { + p.API.LogError("can't store GitLab to GitLab id mapping", "err", err.Error()) return p.getCommandResponse(args, "Unknown error please retry or ask to an administrator to look at logs"), nil } + } else if err := p.deleteGitlabToUserIDMapping(info.GitlabUsername); err != nil { + p.API.LogError("can't delete GitLab username in kvstore", "err", err.Error()) + return p.getCommandResponse(args, "Unknown error please retry or ask to an administrator to look at logs"), nil } info.Settings.Notifications = value case SettingReminders: diff --git a/server/plugin.go b/server/plugin.go index 1aba89c7..35d46320 100644 --- a/server/plugin.go +++ b/server/plugin.go @@ -140,6 +140,13 @@ func (p *Plugin) storeGitlabUserInfo(info *gitlab.UserInfo) error { return nil } +func (p *Plugin) deleteGitlabUserInfo(userID string) error { + if err := p.API.KVDelete(userID + GitlabTokenKey); err != nil { + return fmt.Errorf("encountered error deleting GitLab user info: %w", err) + } + return nil +} + func (p *Plugin) getGitlabUserInfoByMattermostID(userID string) (*gitlab.UserInfo, *APIErrorResponse) { config := p.getConfiguration() @@ -164,10 +171,28 @@ func (p *Plugin) getGitlabUserInfoByMattermostID(userID string) (*gitlab.UserInf func (p *Plugin) storeGitlabToUserIDMapping(gitlabUsername, userID string) error { if err := p.API.KVSet(gitlabUsername+GitlabUsernameKey, []byte(userID)); err != nil { - return fmt.Errorf("encountered error saving GitLab username mapping") + return fmt.Errorf("encountered error saving GitLab username mapping: %w", err) } - if err := p.API.KVSet(userID+GitlabIDUsernameKey, []byte(gitlabUsername)); err != nil { - return fmt.Errorf("encountered error saving GitLab id mapping") + return nil +} + +func (p *Plugin) storeGitlabIDToUserIDMapping(gitlabUsername string, gitlabID int) error { + if err := p.API.KVSet(fmt.Sprintf("%d%s", gitlabID, GitlabIDUsernameKey), []byte(gitlabUsername)); err != nil { + return fmt.Errorf("encountered error saving GitLab id mapping: %w", err) + } + return nil +} + +func (p *Plugin) deleteGitlabToUserIDMapping(gitlabUsername string) error { + if err := p.API.KVDelete(gitlabUsername + GitlabUsernameKey); err != nil { + return fmt.Errorf("encountered error deleting GitLab username mapping: %w", err) + } + return nil +} + +func (p *Plugin) deleteGitlabIDToUserIDMapping(gitlabID int) error { + if err := p.API.KVDelete(fmt.Sprintf("%d%s", gitlabID, GitlabIDUsernameKey)); err != nil { + return fmt.Errorf("encountered error deleting GitLab id mapping: %w", err) } return nil } @@ -198,14 +223,14 @@ func (p *Plugin) disconnectGitlabAccount(userID string) { return } - if err := p.API.KVDelete(userID + GitlabTokenKey); err != nil { - p.API.LogError("can't delete token in store", "err", err.DetailedError, "userId", userID) + if err := p.deleteGitlabUserInfo(userID); err != nil { + p.API.LogError("can't delete token in store", "err", err.Error, "userId", userID) } - if err := p.API.KVDelete(userInfo.GitlabUsername + GitlabUsernameKey); err != nil { - p.API.LogError("can't delete username in store", "err", err.DetailedError, "username", userInfo.GitlabUsername) + if err := p.deleteGitlabToUserIDMapping(userInfo.GitlabUsername); err != nil { + p.API.LogError("can't delete username in store", "err", err.Error, "username", userInfo.GitlabUsername) } - if err := p.API.KVDelete(fmt.Sprintf("%d%s", userInfo.GitlabUserID, GitlabIDUsernameKey)); err != nil { - p.API.LogError("can't delete user id in sotre", "err", err.DetailedError, "id", userInfo.GitlabUserID) + if err := p.deleteGitlabIDToUserIDMapping(userInfo.GitlabUserID); err != nil { + p.API.LogError("can't delete user id in store", "err", err.Error, "id", userInfo.GitlabUserID) } if user, err := p.API.GetUser(userID); err == nil && user.Props != nil && len(user.Props["git_user"]) > 0 { diff --git a/server/webhook/push.go b/server/webhook/push.go index 8397de6a..934e1113 100644 --- a/server/webhook/push.go +++ b/server/webhook/push.go @@ -38,7 +38,7 @@ func (w *webhook) handleDMPush(event *gitlab.PushEvent) ([]*HandleWebhook, error } func (w *webhook) handleChannelPush(event *gitlab.PushEvent) ([]*HandleWebhook, error) { - senderGitlabUsername := event.UserName + senderGitlabUsername := event.UserUsername repo := event.Project res := []*HandleWebhook{} diff --git a/server/webhook/tag.go b/server/webhook/tag.go index 28ac67f9..c565251e 100644 --- a/server/webhook/tag.go +++ b/server/webhook/tag.go @@ -40,7 +40,7 @@ func (w *webhook) handleDMTag(event *gitlab.TagEvent) ([]*HandleWebhook, error) } func (w *webhook) handleChannelTag(event *gitlab.TagEvent) ([]*HandleWebhook, error) { - senderGitlabUsername := event.UserName + senderGitlabUsername := w.gitlabRetreiver.GetUsernameByID(event.UserID) repo := event.Project tagNames := strings.Split(event.Ref, "/") tagName := tagNames[len(tagNames)-1]