From c1cfc890f875bf5629d0d0c2d8e8937ef283f651 Mon Sep 17 00:00:00 2001 From: 6543 <6543@noreply.gitea.io> Date: Fri, 31 Jan 2020 05:17:06 +0000 Subject: [PATCH] Refactor RepoWatch (#241) Merge branch 'master' into refactor-repoWatch Add ListMilestoneOption to ListRepoMilestones (#244) use StateType use PageSize adjut test since gitea bug got fixed (#gitea/10047) add TestMilestones add optional ListMilestoneOption format Add ListOptions struct (#249) same struct as in models/list_options.go add mising license header add ListOptions struct Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/249 Reviewed-by: Andrew Thornton Reviewed-by: lafriks [README] add import path (#239) add import path add import path to readme Changelog v0.11.0 (#235) Co-authored-by: techknowlogick Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/235 Reviewed-by: lafriks Reviewed-by: Andrew Thornton Reviewed-by: John Olheiser Add TestMyUser (#237) Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/237 Reviewed-by: lafriks Reviewed-by: Andrew Thornton Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/239 Reviewed-by: Lunny Xiao Reviewed-by: John Olheiser Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/244 Reviewed-by: Lunny Xiao Reviewed-by: Andrew Thornton Merge branch 'master' into refactor-repoWatch Add ListOptions struct (#249) same struct as in models/list_options.go add mising license header add ListOptions struct Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/249 Reviewed-by: Andrew Thornton Reviewed-by: lafriks add TEST fix GetMyWatchedRepos [README] add import path (#239) add import path add import path to readme Changelog v0.11.0 (#235) Co-authored-by: techknowlogick Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/235 Reviewed-by: lafriks Reviewed-by: Andrew Thornton Reviewed-by: John Olheiser Add TestMyUser (#237) Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/237 Reviewed-by: lafriks Reviewed-by: Andrew Thornton Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/239 Reviewed-by: Lunny Xiao Reviewed-by: John Olheiser refactor RepoWatch * add CheckRepoWatch * add GetMyWatchedRepos * minimize func_options on GetWatchedRepos * WatchRepo return nil/error * UnWatchRepo return nil/error Co-authored-by: Lunny Xiao Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/241 Reviewed-by: Andrew Thornton Reviewed-by: Andrew Thornton Reviewed-by: Lunny Xiao --- gitea/repo_watch.go | 54 ++++++++++++++++++++++++++++++++-------- gitea/repo_watch_test.go | 52 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 11 deletions(-) create mode 100644 gitea/repo_watch_test.go diff --git a/gitea/repo_watch.go b/gitea/repo_watch.go index 1005f9f..c8ed3a1 100644 --- a/gitea/repo_watch.go +++ b/gitea/repo_watch.go @@ -21,21 +21,53 @@ type WatchInfo struct { } // GetWatchedRepos list all the watched repos of user -func (c *Client) GetWatchedRepos(user, pass string) ([]*Repository, error) { +func (c *Client) GetWatchedRepos(user string) ([]*Repository, error) { repos := make([]*Repository, 0, 10) - return repos, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/subscriptions", user), - http.Header{"Authorization": []string{"Basic " + BasicAuthEncode(user, pass)}}, nil, &repos) + return repos, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/subscriptions", user), nil, nil, &repos) +} + +// GetMyWatchedRepos list repositories watched by the authenticated user +func (c *Client) GetMyWatchedRepos() ([]*Repository, error) { + repos := make([]*Repository, 0, 10) + return repos, c.getParsedResponse("GET", fmt.Sprintf("/user/subscriptions"), nil, nil, &repos) +} + +// CheckRepoWatch check if the current user is watching a repo +func (c *Client) CheckRepoWatch(repoUser, repoName string) (bool, error) { + status, err := c.getStatusCode("GET", fmt.Sprintf("/repos/%s/%s/subscription", repoUser, repoName), nil, nil) + if err != nil { + return false, err + } + switch status { + case http.StatusNotFound: + return false, nil + case http.StatusOK: + return true, nil + default: + return false, fmt.Errorf("unexpected Status: %d", status) + } } // WatchRepo start to watch a repository -func (c *Client) WatchRepo(user, pass, repoUser, repoName string) (*WatchInfo, error) { - i := new(WatchInfo) - return i, c.getParsedResponse("PUT", fmt.Sprintf("/repos/%s/%s/subscription", repoUser, repoName), - http.Header{"Authorization": []string{"Basic " + BasicAuthEncode(user, pass)}}, nil, i) +func (c *Client) WatchRepo(repoUser, repoName string) error { + status, err := c.getStatusCode("PUT", fmt.Sprintf("/repos/%s/%s/subscription", repoUser, repoName), nil, nil) + if err != nil { + return err + } + if status == http.StatusOK { + return nil + } + return fmt.Errorf("unexpected Status: %d", status) } -// UnWatchRepo start to watch a repository -func (c *Client) UnWatchRepo(user, pass, repoUser, repoName string) (int, error) { - return c.getStatusCode("DELETE", fmt.Sprintf("/repos/%s/%s/subscription", repoUser, repoName), - http.Header{"Authorization": []string{"Basic " + BasicAuthEncode(user, pass)}}, nil) +// UnWatchRepo stop to watch a repository +func (c *Client) UnWatchRepo(repoUser, repoName string) error { + status, err := c.getStatusCode("DELETE", fmt.Sprintf("/repos/%s/%s/subscription", repoUser, repoName), nil, nil) + if err != nil { + return err + } + if status == http.StatusNoContent { + return nil + } + return fmt.Errorf("unexpected Status: %d", status) } diff --git a/gitea/repo_watch_test.go b/gitea/repo_watch_test.go new file mode 100644 index 0000000..f237f79 --- /dev/null +++ b/gitea/repo_watch_test.go @@ -0,0 +1,52 @@ +// 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 ( + "log" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestRepoWatch(t *testing.T) { + log.Printf("== TestRepoWatch ==") + c := newTestClient() + rawVersion, err := c.ServerVersion() + assert.NoError(t, err) + assert.True(t, true, rawVersion != "") + + repo1, _ := createTestRepo(t, "TestRepoWatch_1", c) + repo2, _ := createTestRepo(t, "TestRepoWatch_2", c) + assert.NotEqual(t, repo1, repo2) + + //GetWatchedRepos + wl, err := c.GetWatchedRepos("test01") + assert.NoError(t, err) + assert.NotNil(t, wl) + maxcount := len(wl) + + //GetMyWatchedRepos + wl, err = c.GetMyWatchedRepos() + assert.NoError(t, err) + assert.Len(t, wl, maxcount) + + //CheckRepoWatch + isWatching, err := c.CheckRepoWatch(repo1.Owner.UserName, repo1.Name) + assert.NoError(t, err) + assert.True(t, isWatching) + + //UnWatchRepo + err = c.UnWatchRepo(repo1.Owner.UserName, repo1.Name) + assert.NoError(t, err) + isWatching, _ = c.CheckRepoWatch(repo1.Owner.UserName, repo1.Name) + assert.False(t, isWatching) + + //WatchRepo + err = c.WatchRepo(repo1.Owner.UserName, repo1.Name) + assert.NoError(t, err) + isWatching, _ = c.CheckRepoWatch(repo1.Owner.UserName, repo1.Name) + assert.True(t, isWatching) +}