Skip to content

Commit

Permalink
[MM-537]: Added message to delete webhook after subscription deletion (
Browse files Browse the repository at this point in the history
…#538)

* [MM-537]: Added message to delete webhook after subscription deletion

* handling for groups webhook

* fixed groups subscription delete msg

* fixed lint

* fixed lint build
  • Loading branch information
Kshitij-Katiyar authored Feb 11, 2025
1 parent fa5857b commit 0f886e5
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
64 changes: 62 additions & 2 deletions server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ func parseTriggers(triggersCsv string) *gitlab.AddWebhookOptions {
}
}

func (p *Plugin) subscriptionDelete(_ *gitlab.UserInfo, config *configuration, fullPath, channelID string) (string, error) {
func (p *Plugin) subscriptionDelete(userInfo *gitlab.UserInfo, config *configuration, fullPath, channelID string) (string, error) {
normalizedPath := normalizePath(fullPath, config.GitlabURL)
deleted, updatedSubscriptions, err := p.Unsubscribe(channelID, normalizedPath)
if err != nil {
Expand All @@ -607,7 +607,67 @@ func (p *Plugin) subscriptionDelete(_ *gitlab.UserInfo, config *configuration, f

p.sendChannelSubscriptionsUpdated(updatedSubscriptions, channelID)

return fmt.Sprintf("Successfully deleted subscription for %s.", normalizedPath), nil
baseURL := config.GitlabURL
if !strings.HasSuffix(baseURL, "/") {
baseURL += "/"
}

owner := strings.Split(normalizedPath, "/")[0]
remainingPath := strings.Split(normalizedPath, "/")[1:]

ctx, cancel := context.WithTimeout(context.Background(), webhookTimeout)
defer cancel()

var project *gitlabLib.Project
var getProjectError error
err = p.useGitlabClient(userInfo, func(info *gitlab.UserInfo, token *oauth2.Token) error {
//nolint:govet // Ignore variable shadowing warning
resp, err := p.GitlabClient.GetProject(ctx, info, token, owner, strings.Join(remainingPath, "/"))
if err != nil {
getProjectError = err
} else {
project = resp
}
return nil
})
if project == nil || err != nil {
if err != nil {
p.client.Log.Warn("Can't get group in subscription delete", "err", err.Error(), "group", normalizedPath)
}
}

var webhookMsg string
if getProjectError == nil && project != nil {
webhookMsg = fmt.Sprintf("\n Please delete the [webhook](%s) for this subscription unless it's required for other subscriptions.", fmt.Sprintf("%s%s/-/hooks", baseURL, normalizedPath))
} else {
var group *gitlabLib.Group
var getGroupError error
err = p.useGitlabClient(userInfo, func(info *gitlab.UserInfo, token *oauth2.Token) error {
//nolint:govet // Ignore variable shadowing warning
resp, err := p.GitlabClient.GetGroup(ctx, info, token, owner, strings.Join(remainingPath, "/"))
if err != nil {
getGroupError = err
} else {
group = resp
}
return nil
})
if group == nil || err != nil {
if err != nil {
p.client.Log.Warn("Can't get project in subscription delete", "err", err.Error(), "project", normalizedPath)
}
}
if getGroupError == nil && group != nil {
webhookMsg = fmt.Sprintf("\n Please delete the [webhook](%s) for this subscription unless it's required for other subscriptions.", fmt.Sprintf("%sgroups/%s/-/hooks", baseURL, normalizedPath))
} else {
webhookMsg = "\n Please delete the webhook for this subscription unless it's required for other subscriptions."
}
}

unsubscribeMessage := fmt.Sprintf("Successfully deleted subscription for %s.", fmt.Sprintf("[%s](%s)", normalizedPath, baseURL+normalizedPath))
unsubscribeMessage += webhookMsg

return unsubscribeMessage, nil
}

// subscriptionsListCommand list GitLab subscriptions in a channel
Expand Down
20 changes: 20 additions & 0 deletions server/gitlab/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,26 @@ func (g *gitlab) GetProject(ctx context.Context, user *UserInfo, token *oauth2.T
return project, nil
}

func (g *gitlab) GetGroup(ctx context.Context, user *UserInfo, token *oauth2.Token, group, subgroup string) (*internGitlab.Group, error) {
client, err := g.GitlabConnect(*token)
if err != nil {
return nil, err
}

groupData, resp, err := client.Groups.GetGroup(group,
&internGitlab.GetGroupOptions{},
internGitlab.WithContext(ctx),
)
if respErr := checkResponse(resp); respErr != nil {
return nil, respErr
}
if err != nil {
return nil, err
}

return groupData, nil
}

func (g *gitlab) GetLHSData(ctx context.Context, user *UserInfo, token *oauth2.Token) (*LHSContent, error) {
client, err := g.GitlabConnect(*token)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions server/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Gitlab interface {
GetMergeRequestByID(ctx context.Context, user *UserInfo, owner, repo string, mergeRequestID int, token *oauth2.Token) (*MergeRequest, error)
GetUserDetails(ctx context.Context, user *UserInfo, token *oauth2.Token) (*internGitlab.User, error)
GetProject(ctx context.Context, user *UserInfo, token *oauth2.Token, owner, repo string) (*internGitlab.Project, error)
GetGroup(ctx context.Context, user *UserInfo, token *oauth2.Token, owner, repo string) (*internGitlab.Group, error)
GetYourPrDetails(ctx context.Context, log logger.Logger, user *UserInfo, token *oauth2.Token, prList []*PRDetails) ([]*PRDetails, error)
GetReviews(ctx context.Context, user *UserInfo, client *internGitlab.Client) ([]*internGitlab.MergeRequest, error)
GetYourAssignedPrs(ctx context.Context, user *UserInfo, client *internGitlab.Client) ([]*internGitlab.MergeRequest, error)
Expand Down
9 changes: 9 additions & 0 deletions server/gitlab/mocks/mock_gitlab.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0f886e5

Please sign in to comment.