Skip to content

Commit

Permalink
[GH-184]: Use UserUsername in Link, Fixes GitLab id storage (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
n-thumann authored Dec 5, 2020
1 parent 9ebca52 commit de46ece
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 16 deletions.
6 changes: 5 additions & 1 deletion server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
43 changes: 34 additions & 9 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion server/webhook/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down
2 changes: 1 addition & 1 deletion server/webhook/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

This comment has been minimized.

Copy link
@apollo13

apollo13 Jul 28, 2021

Contributor

@n-thumann that seems to only work for users connected to gitlab in mattermost. I think this could also use UserUsername once the underlying go library is updated. See #254 for details.

repo := event.Project
tagNames := strings.Split(event.Ref, "/")
tagName := tagNames[len(tagNames)-1]
Expand Down

0 comments on commit de46ece

Please sign in to comment.