2019-04-25 13:06:53 -04:00
|
|
|
// Copyright 2017 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"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// WatchInfo represents an API watch status of one repository
|
|
|
|
type WatchInfo struct {
|
|
|
|
Subscribed bool `json:"subscribed"`
|
|
|
|
Ignored bool `json:"ignored"`
|
|
|
|
Reason interface{} `json:"reason"`
|
|
|
|
CreatedAt time.Time `json:"created_at"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
RepositoryURL string `json:"repository_url"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetWatchedRepos list all the watched repos of user
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) GetWatchedRepos(user string) ([]*Repository, *Response, error) {
|
2019-04-25 13:06:53 -04:00
|
|
|
repos := make([]*Repository, 0, 10)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/users/%s/subscriptions", user), nil, nil, &repos)
|
|
|
|
return repos, resp, err
|
2020-05-27 09:48:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetMyWatchedRepos list repositories watched by the authenticated user
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) GetMyWatchedRepos() ([]*Repository, *Response, error) {
|
2020-05-27 09:48:09 -04:00
|
|
|
repos := make([]*Repository, 0, 10)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/user/subscriptions"), nil, nil, &repos)
|
|
|
|
return repos, resp, err
|
2020-05-27 09:48:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckRepoWatch check if the current user is watching a repo
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) CheckRepoWatch(repoUser, repoName string) (bool, *Response, error) {
|
|
|
|
status, resp, err := c.getStatusCode("GET", fmt.Sprintf("/repos/%s/%s/subscription", repoUser, repoName), nil, nil)
|
2020-05-27 09:48:09 -04:00
|
|
|
if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return false, resp, err
|
2020-05-27 09:48:09 -04:00
|
|
|
}
|
|
|
|
switch status {
|
|
|
|
case http.StatusNotFound:
|
2020-09-15 22:01:41 -04:00
|
|
|
return false, resp, nil
|
2020-05-27 09:48:09 -04:00
|
|
|
case http.StatusOK:
|
2020-09-15 22:01:41 -04:00
|
|
|
return true, resp, nil
|
2020-05-27 09:48:09 -04:00
|
|
|
default:
|
2020-09-15 22:01:41 -04:00
|
|
|
return false, resp, fmt.Errorf("unexpected Status: %d", status)
|
2020-05-27 09:48:09 -04:00
|
|
|
}
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// WatchRepo start to watch a repository
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) WatchRepo(repoUser, repoName string) (*Response, error) {
|
|
|
|
status, resp, err := c.getStatusCode("PUT", fmt.Sprintf("/repos/%s/%s/subscription", repoUser, repoName), nil, nil)
|
2020-05-27 09:48:09 -04:00
|
|
|
if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return resp, err
|
2020-05-27 09:48:09 -04:00
|
|
|
}
|
|
|
|
if status == http.StatusOK {
|
2020-09-15 22:01:41 -04:00
|
|
|
return resp, nil
|
2020-05-27 09:48:09 -04:00
|
|
|
}
|
2020-09-15 22:01:41 -04:00
|
|
|
return resp, fmt.Errorf("unexpected Status: %d", status)
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
2020-05-27 09:48:09 -04:00
|
|
|
// UnWatchRepo stop to watch a repository
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) UnWatchRepo(repoUser, repoName string) (*Response, error) {
|
|
|
|
status, resp, err := c.getStatusCode("DELETE", fmt.Sprintf("/repos/%s/%s/subscription", repoUser, repoName), nil, nil)
|
2020-05-27 09:48:09 -04:00
|
|
|
if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return resp, err
|
2020-05-27 09:48:09 -04:00
|
|
|
}
|
|
|
|
if status == http.StatusNoContent {
|
2020-09-15 22:01:41 -04:00
|
|
|
return resp, nil
|
2020-05-27 09:48:09 -04:00
|
|
|
}
|
2020-09-15 22:01:41 -04:00
|
|
|
return resp, fmt.Errorf("unexpected Status: %d", status)
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|