2020-02-07 13:53:15 +00:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gitea
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-05-27 13:48:09 +00:00
|
|
|
"net/http"
|
2020-02-07 13:53:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetIssueSubscribers get list of users who subscribed on an issue
|
2020-09-16 02:01:41 +00:00
|
|
|
func (c *Client) GetIssueSubscribers(owner, repo string, index int64) ([]*User, *Response, error) {
|
2021-08-16 12:46:15 +00:00
|
|
|
if err := escapeValidatePathSegments(&owner, &repo); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-02-07 13:53:15 +00:00
|
|
|
subscribers := make([]*User, 0, 10)
|
2020-09-16 02:01:41 +00:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/subscriptions", owner, repo, index), nil, nil, &subscribers)
|
|
|
|
return subscribers, resp, err
|
2020-02-07 13:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddIssueSubscription Subscribe user to issue
|
2020-09-16 02:01:41 +00:00
|
|
|
func (c *Client) AddIssueSubscription(owner, repo string, index int64, user string) (*Response, error) {
|
2021-08-16 12:46:15 +00:00
|
|
|
if err := escapeValidatePathSegments(&owner, &repo, &user); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-16 02:01:41 +00:00
|
|
|
status, resp, err := c.getStatusCode("PUT", fmt.Sprintf("/repos/%s/%s/issues/%d/subscriptions/%s", owner, repo, index, user), nil, nil)
|
2020-05-27 13:48:09 +00:00
|
|
|
if err != nil {
|
2020-09-16 02:01:41 +00:00
|
|
|
return resp, err
|
2020-05-27 13:48:09 +00:00
|
|
|
}
|
|
|
|
if status == http.StatusCreated {
|
2020-09-16 02:01:41 +00:00
|
|
|
return resp, nil
|
2020-05-27 13:48:09 +00:00
|
|
|
}
|
|
|
|
if status == http.StatusOK {
|
2020-09-16 02:01:41 +00:00
|
|
|
return resp, fmt.Errorf("already subscribed")
|
2020-05-27 13:48:09 +00:00
|
|
|
}
|
2020-09-16 02:01:41 +00:00
|
|
|
return resp, fmt.Errorf("unexpected Status: %d", status)
|
2020-02-07 13:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteIssueSubscription unsubscribe user from issue
|
2020-09-16 02:01:41 +00:00
|
|
|
func (c *Client) DeleteIssueSubscription(owner, repo string, index int64, user string) (*Response, error) {
|
2021-08-16 12:46:15 +00:00
|
|
|
if err := escapeValidatePathSegments(&owner, &repo, &user); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-16 02:01:41 +00:00
|
|
|
status, resp, err := c.getStatusCode("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/subscriptions/%s", owner, repo, index, user), nil, nil)
|
2020-05-27 13:48:09 +00:00
|
|
|
if err != nil {
|
2020-09-16 02:01:41 +00:00
|
|
|
return resp, err
|
2020-05-27 13:48:09 +00:00
|
|
|
}
|
|
|
|
if status == http.StatusCreated {
|
2020-09-16 02:01:41 +00:00
|
|
|
return resp, nil
|
2020-05-27 13:48:09 +00:00
|
|
|
}
|
|
|
|
if status == http.StatusOK {
|
2020-09-16 02:01:41 +00:00
|
|
|
return resp, fmt.Errorf("already unsubscribed")
|
2020-05-27 13:48:09 +00:00
|
|
|
}
|
2020-09-16 02:01:41 +00:00
|
|
|
return resp, fmt.Errorf("unexpected Status: %d", status)
|
2020-05-27 13:48:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckIssueSubscription check if current user is subscribed to an issue
|
2020-09-16 02:01:41 +00:00
|
|
|
func (c *Client) CheckIssueSubscription(owner, repo string, index int64) (*WatchInfo, *Response, error) {
|
2021-08-16 12:46:15 +00:00
|
|
|
if err := escapeValidatePathSegments(&owner, &repo); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-11-14 10:31:01 +00:00
|
|
|
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
2020-09-16 02:01:41 +00:00
|
|
|
return nil, nil, err
|
2020-05-27 13:48:09 +00:00
|
|
|
}
|
|
|
|
wi := new(WatchInfo)
|
2020-09-16 02:01:41 +00:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/subscriptions/check", owner, repo, index), nil, nil, wi)
|
|
|
|
return wi, resp, err
|
2020-02-07 13:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IssueSubscribe subscribe current user to an issue
|
2020-09-16 02:01:41 +00:00
|
|
|
func (c *Client) IssueSubscribe(owner, repo string, index int64) (*Response, error) {
|
|
|
|
u, _, err := c.GetMyUserInfo()
|
2020-02-07 13:53:15 +00:00
|
|
|
if err != nil {
|
2020-09-16 02:01:41 +00:00
|
|
|
return nil, err
|
2020-02-07 13:53:15 +00:00
|
|
|
}
|
|
|
|
return c.AddIssueSubscription(owner, repo, index, u.UserName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IssueUnSubscribe unsubscribe current user from an issue
|
2020-09-16 02:01:41 +00:00
|
|
|
func (c *Client) IssueUnSubscribe(owner, repo string, index int64) (*Response, error) {
|
|
|
|
u, _, err := c.GetMyUserInfo()
|
2020-02-07 13:53:15 +00:00
|
|
|
if err != nil {
|
2020-09-16 02:01:41 +00:00
|
|
|
return nil, err
|
2020-02-07 13:53:15 +00:00
|
|
|
}
|
|
|
|
return c.DeleteIssueSubscription(owner, repo, index, u.UserName)
|
|
|
|
}
|