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 <art27@cantab.net>
Reviewed-by: lafriks <lafriks@noreply.gitea.io>

[README] add import path (#239)

add import path

add import path to readme

Changelog v0.11.0 (#235)

Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/235
Reviewed-by: lafriks <lafriks@noreply.gitea.io>
Reviewed-by: Andrew Thornton <art27@cantab.net>
Reviewed-by: John Olheiser <john.olheiser@gmail.com>

Add TestMyUser (#237)

Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/237
Reviewed-by: lafriks <lafriks@noreply.gitea.io>
Reviewed-by: Andrew Thornton <art27@cantab.net>

Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/239
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: John Olheiser <john.olheiser@gmail.com>

Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/244
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: Andrew Thornton <art27@cantab.net>

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 <art27@cantab.net>
Reviewed-by: lafriks <lafriks@noreply.gitea.io>

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 <techknowlogick@gitea.io>
Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/235
Reviewed-by: lafriks <lafriks@noreply.gitea.io>
Reviewed-by: Andrew Thornton <art27@cantab.net>
Reviewed-by: John Olheiser <john.olheiser@gmail.com>

Add TestMyUser (#237)

Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/237
Reviewed-by: lafriks <lafriks@noreply.gitea.io>
Reviewed-by: Andrew Thornton <art27@cantab.net>

Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/239
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: John Olheiser <john.olheiser@gmail.com>

refactor RepoWatch
 * add CheckRepoWatch
 * add GetMyWatchedRepos
 * minimize func_options on GetWatchedRepos
 * WatchRepo return nil/error
 * UnWatchRepo return nil/error

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/241
Reviewed-by: Andrew Thornton <art27@cantab.net>
Reviewed-by: Andrew Thornton <art27@cantab.net>
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
6543 2020-01-31 05:17:06 +00:00 committed by Lunny Xiao
parent d96f9692df
commit c1cfc890f8
2 changed files with 95 additions and 11 deletions

View file

@ -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)
}

52
gitea/repo_watch_test.go Normal file
View file

@ -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)
}