Add gitea-vet #312
|
@ -29,7 +29,7 @@ steps:
|
|||
- echo "[server]" >> /tmp/conf/app.ini
|
||||
- echo "ROOT_URL = http://gitea:3000" >> /tmp/conf/app.ini
|
||||
- gitea migrate -c /tmp/conf/app.ini
|
||||
- gitea admin create-user --username=test01 --password=test01 --email=test01@gitea.io --admin=true --must-change-password=false --access-token -c /tmp/conf/app.ini
|
||||
- gitea admin user create --username=test01 --password=test01 --email=test01@gitea.io --admin=true --must-change-password=false --access-token -c /tmp/conf/app.ini
|
||||
- gitea web -c /tmp/conf/app.ini
|
||||
|
||||
- name: testing
|
||||
|
|
55
CHANGELOG.md
55
CHANGELOG.md
|
@ -1,5 +1,60 @@
|
|||
# Changelog
|
||||
|
||||
## [v0.13.1](https://gitea.com/gitea/go-sdk/releases/tag/v0.13.1) - 2020-09-29
|
||||
|
||||
* FEATURES
|
||||
* Add GetReleaseByTag (#427) (#430)
|
||||
* BUGFIXES
|
||||
* Return http Response on NotFound too (#428) (#429)
|
||||
* ENHANCEMENTS
|
||||
* Update Struct NotificationSubject (#424) (#425)
|
||||
* Add Debug Mode (#422) (#423)
|
||||
|
||||
|
||||
## [v0.13.0](https://gitea.com/gitea/go-sdk/releases/tag/v0.13.0) - 2020-09-15
|
||||
|
||||
* BREAKING
|
||||
* Check Gitea Version Requirement (#419)
|
||||
* All Function return http responce (#416)
|
||||
* Remove opts from ListPullReviewComments (#411)
|
||||
* Use enum AccessMode for OrgTeam and Collaborator functions (#408)
|
||||
* CreateOrgOption rename UserName to Name (#386)
|
||||
* EditMilestoneOption also use StateType (#350)
|
||||
* Refactor RepoSearch to be easy usable (#346)
|
||||
* FEATURES
|
||||
* Milestone Functions accept name to identify (#418)
|
||||
* Make http requests with context (#417)
|
||||
* Add GetGlobalAttachmentSettings (#414)
|
||||
* Add GetArchive (#413)
|
||||
* Add GetRepoLanguages + TESTs (#412)
|
||||
* Add CreateBranch (#407)
|
||||
* Add Admin CronTask functions (#406)
|
||||
* Add GetGlobalAPISettings Function (#404)
|
||||
* Add Get Diff and Patch endpoints for pull requests (#398)
|
||||
* Add Validate func for Create/Edit Options (#370)
|
||||
* Add Function to get GetGlobalSettings and GetSettingAllowedReactions (#359)
|
||||
* ENHANCEMENTS
|
||||
* TrackedTime API >= 1.11.x needed (#415)
|
||||
* Update Milestone struct (#410)
|
||||
* Add Fallback for GetPullRequestDiff/Patch (#399)
|
||||
* DeleteToken Accept Names too (#394)
|
||||
* Update ListMilestoneOption struct (#393)
|
||||
* Migration Api Changed (#392)
|
||||
* Refactor Visibletype Orgs (#382)
|
||||
* Extend Notification Functions (#381)
|
||||
* Update GetGlobalSettings Functions (#376)
|
||||
* Allow Creating Closed Milestones (#373)
|
||||
* CreateLabel correct Color if needed for old versions (#365)
|
||||
* Issue/Pull add IsLocked Property (#357)
|
||||
* Update EditPullRequestOption Add Base (#353)
|
||||
* File Create/Update/Delete detect DefaultBranch if Branch not set for old Versions (#352)
|
||||
* Improve Error Handling (#351)
|
||||
|
||||
## [v0.12.2](https://gitea.com/gitea/go-sdk/releases/tag/v0.12.2) - 2020-09-05
|
||||
|
||||
* ENHANCEMENTS
|
||||
* Extend Notification Functions (#381) (#385)
|
||||
|
||||
## [v0.12.1](https://gitea.com/gitea/go-sdk/pulls?q=&type=all&state=closed&milestone=1268) - 2020-07-09
|
||||
|
||||
* ENHANCEMENTS
|
||||
|
|
30
MAINTAINERS
30
MAINTAINERS
|
@ -1,30 +0,0 @@
|
|||
Alexey Makhov <amakhov@avito.ru> (@makhov)
|
||||
Andrey Nering <andrey.nering@gmail.com> (@andreynering)
|
||||
Bo-Yi Wu <appleboy.tw@gmail.com> (@appleboy)
|
||||
Ethan Koenig <ethantkoenig@gmail.com> (@ethantkoenig)
|
||||
Kees de Vries <bouwko@gmail.com> (@Bwko)
|
||||
Kim Carlbäcker <kim.carlbacker@gmail.com> (@bkcsoft)
|
||||
LefsFlare <nobody@nobody.tld> (@LefsFlarey)
|
||||
Lunny Xiao <xiaolunwen@gmail.com> (@lunny)
|
||||
Matthias Loibl <mail@matthiasloibl.com> (@metalmatze)
|
||||
Morgan Bazalgette <the@howl.moe> (@thehowl)
|
||||
Rachid Zarouali <nobody@nobody.tld> (@xinity)
|
||||
Rémy Boulanouar <admin@dblk.org> (@DblK)
|
||||
Sandro Santilli <strk@kbt.io> (@strk)
|
||||
Thibault Meyer <meyer.thibault@gmail.com> (@0xbaadf00d)
|
||||
Thomas Boerger <thomas@webhippie.de> (@tboerger)
|
||||
Patrick G <geek1011@outlook.com> (@geek1011)
|
||||
Antoine Girard <sapk@sapk.fr> (@sapk)
|
||||
Lauris Bukšis-Haberkorns <lauris@nix.lv> (@lafriks)
|
||||
Jonas Östanbäck <jonas.ostanback@gmail.com> (@cez81)
|
||||
David Schneiderbauer <dschneiderbauer@gmail.com> (@daviian)
|
||||
Peter Žeby <morlinest@gmail.com> (@morlinest)
|
||||
Matti Ranta <matti@mdranta.net> (@techknowlogick)
|
||||
Jonas Franz <info@jonasfranz.software> (@jonasfranz)
|
||||
Alexey Terentyev <axifnx@gmail.com> (@axifive)
|
||||
Lanre Adelowo <yo@lanre.wtf> (@adelowo)
|
||||
Konrad Langenberg <k@knt.li> (@kolaente)
|
||||
He-Long Zhang <outman99@hotmail.com> (@BetaCat0)
|
||||
Andrew Thornton <art27@cantab.net> (@zeripath)
|
||||
John Olheiser <john.olheiser@gmail.com> (@jolheiser)
|
||||
Richard Mahn <rich.mahn@unfoldingword.org> (@richmahn)
|
33
Makefile
33
Makefile
|
@ -8,6 +8,29 @@ GITEA_SDK_TEST_PASSWORD ?= test01
|
|||
|
||||
PACKAGE := code.gitea.io/sdk/gitea
|
||||
|
||||
GITEA_DL := https://dl.gitea.io/gitea/master/gitea-master-
|
||||
UNAME_S := $(shell uname -s)
|
||||
ifeq ($(UNAME_S),Linux)
|
||||
GITEA_DL := $(GITEA_DL)linux-
|
||||
|
||||
UNAME_P := $(shell uname -p)
|
||||
ifeq ($(UNAME_P),unknown)
|
||||
GITEA_DL := $(GITEA_DL)amd64
|
||||
endif
|
||||
ifeq ($(UNAME_P),x86_64)
|
||||
GITEA_DL := $(GITEA_DL)amd64
|
||||
endif
|
||||
ifneq ($(filter %86,$(UNAME_P)),)
|
||||
GITEA_DL := $(GITEA_DL)386
|
||||
endif
|
||||
ifneq ($(filter arm%,$(UNAME_P)),)
|
||||
GITEA_DL := $(GITEA_DL)arm-5
|
||||
endif
|
||||
endif
|
||||
ifeq ($(UNAME_S),Darwin)
|
||||
GITEA_DL := $(GITEA_DL)darwin-10.6-amd64
|
||||
endif
|
||||
|
||||
.PHONY: all
|
||||
all: clean test build
|
||||
|
||||
|
@ -27,7 +50,7 @@ help:
|
|||
.PHONY: clean
|
||||
clean:
|
||||
rm -r -f test
|
||||
$(GO) clean -i ./...
|
||||
cd gitea && $(GO) clean -i ./...
|
||||
|
||||
.PHONY: fmt
|
||||
fmt:
|
||||
|
@ -64,7 +87,7 @@ test:
|
|||
test-instance:
|
||||
rm -f -r ${WORK_DIR}/test 2> /dev/null; \
|
||||
mkdir -p ${WORK_DIR}/test/conf/ ${WORK_DIR}/test/data/
|
||||
wget "https://dl.gitea.io/gitea/master/gitea-master-linux-amd64" -O ${WORK_DIR}/test/gitea-master; \
|
||||
wget ${GITEA_DL} -O ${WORK_DIR}/test/gitea-master; \
|
||||
chmod +x ${WORK_DIR}/test/gitea-master; \
|
||||
echo "[security]" > ${WORK_DIR}/test/conf/app.ini; \
|
||||
echo "INTERNAL_TOKEN = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE1NTg4MzY4ODB9.LoKQyK5TN_0kMJFVHWUW0uDAyoGjDP6Mkup4ps2VJN4" >> ${WORK_DIR}/test/conf/app.ini; \
|
||||
|
@ -75,10 +98,10 @@ test-instance:
|
|||
echo "DB_TYPE = sqlite3" >> ${WORK_DIR}/test/conf/app.ini; \
|
||||
echo "[repository]" >> ${WORK_DIR}/test/conf/app.ini; \
|
||||
echo "ROOT = ${WORK_DIR}/test/data/" >> ${WORK_DIR}/test/conf/app.ini; \
|
||||
echo "[server]" >> /tmp/conf/app.ini; \
|
||||
echo "ROOT_URL = ${GITEA_SDK_TEST_URL}" >> /tmp/conf/app.ini; \
|
||||
echo "[server]" >> ${WORK_DIR}/test/conf/app.ini; \
|
||||
echo "ROOT_URL = ${GITEA_SDK_TEST_URL}" >> ${WORK_DIR}/test/conf/app.ini; \
|
||||
${WORK_DIR}/test/gitea-master migrate -c ${WORK_DIR}/test/conf/app.ini; \
|
||||
${WORK_DIR}/test/gitea-master admin create-user --username=${GITEA_SDK_TEST_USERNAME} --password=${GITEA_SDK_TEST_PASSWORD} --email=test01@gitea.io --admin=true --must-change-password=false --access-token -c ${WORK_DIR}/test/conf/app.ini; \
|
||||
${WORK_DIR}/test/gitea-master admin user create --username=${GITEA_SDK_TEST_USERNAME} --password=${GITEA_SDK_TEST_PASSWORD} --email=test01@gitea.io --admin=true --must-change-password=false --access-token -c ${WORK_DIR}/test/conf/app.ini; \
|
||||
${WORK_DIR}/test/gitea-master web -c ${WORK_DIR}/test/conf/app.ini
|
||||
|
||||
.PHONY: bench
|
||||
|
|
|
@ -10,6 +10,10 @@ This project acts as a client SDK implementation written in Go to interact with
|
|||
import "code.gitea.io/sdk/gitea"
|
||||
```
|
||||
|
||||
## Version Requirements
|
||||
* go >= 1.13
|
||||
* gitea >= 1.11
|
||||
|
||||
## Contributing
|
||||
|
||||
Fork -> Patch -> Push -> Pull Request
|
||||
|
|
88
docs/migrate-v0.12-to-v0.13.md
Normal file
88
docs/migrate-v0.12-to-v0.13.md
Normal file
|
@ -0,0 +1,88 @@
|
|||
# Migration Guide: v0.12 to v0.13
|
||||
|
||||
v0.13.0 introduces a number of breaking changes, throu it should not be hard to migrate.
|
||||
Just follow this guid and if issues still ocure ask for help on discord or
|
||||
feel free to create an issue.
|
||||
|
||||
<!-- toc -->
|
||||
|
||||
- [EditMilestoneOption use StateType (#350)](#EditMilestoneOption-use-StateType)
|
||||
- [RepoSearch Options Struct was rewritten (#346)](#RepoSearch-Options-Struct-was-rewritten)
|
||||
- [Variable Renames (#386)](#Variable-Renames)
|
||||
- [Change Type of Permission Field (#408)](#Change-Type-of-Permission-Field)
|
||||
- [All Function return http responce (#416)](#All-Function-return-http-responce)
|
||||
- [NewClient has new Option Interface (#417)](#NewClient-has-new-Option-Interface)
|
||||
|
||||
<!-- tocstop -->
|
||||
|
||||
## EditMilestoneOption use StateType
|
||||
|
||||
Instead of a raw string StateType is now used for State too.
|
||||
just replace old strings with new enum.
|
||||
|
||||
|
||||
Pulls:
|
||||
- [#350 EditMilestoneOption also use StateType](https://gitea.com/gitea/go-sdk/pulls/350)
|
||||
|
||||
|
||||
## RepoSearch Options Struct was rewritten
|
||||
|
||||
Since the API itself is ugly and there was no nameconvention whats o ever.
|
||||
You easely can pass the wrong options and dont get the result you want.
|
||||
|
||||
Now it is rewritten and translated for the API.
|
||||
The easyest way to migrate is to look at who this function is used and rewritten that code block.
|
||||
|
||||
If there is a special edgecase you have you can pass a `RawQuery` to the API endpoint.
|
||||
|
||||
Pulls:
|
||||
- [#346 Refactor RepoSearch to be easy usable](https://gitea.com/gitea/go-sdk/pulls/346)
|
||||
|
||||
|
||||
## Variable Renames
|
||||
|
||||
Some names of strcut options have been renamed to describe there function/usecase more precisely.
|
||||
if you use `CreateOrgOption` somewhere just rename `UserName` to `Name`.
|
||||
|
||||
Pulls:
|
||||
- [#386 CreateOrgOption rename UserName to Name](https://gitea.com/gitea/go-sdk/pulls/386)
|
||||
|
||||
## Change Type of Permission Field
|
||||
|
||||
The following functions are affected: ListOrgTeams, ListMyTeams, GetTeam, CreateTeam, EditTeam and AddCollaborator
|
||||
|
||||
The `Permission` field has changed type from `string` to `AccessMode`,
|
||||
which represent the raw strings you must use before.
|
||||
Just replace the string with the AccessMode equivalent.
|
||||
|
||||
Pulls:
|
||||
- [#408 Use enum AccessMode for OrgTeam and Collaborator functions](https://gitea.com/gitea/go-sdk/pulls/408)
|
||||
|
||||
|
||||
## All Function return http responce
|
||||
|
||||
All functions got one new return (`Responce`)!
|
||||
If you just like to migrate, add `_,` before the error return.
|
||||
|
||||
example:
|
||||
```diff
|
||||
- user, err := c.GetMyUserInfo()
|
||||
+ user, _, err := c.GetMyUserInfo()
|
||||
```
|
||||
|
||||
If you like to check responce if an error ocure, make sure responce is not nil!
|
||||
If an error ocure before an http request (e.g. gitea is to old), it will be nil.
|
||||
|
||||
Pulls:
|
||||
- [#416 All Function return http responce](https://gitea.com/gitea/go-sdk/pulls/416)
|
||||
|
||||
|
||||
## NewClient has new Option Interface
|
||||
|
||||
function `NewClient` use functional options now.
|
||||
If you simply like to migrate replace `client := NewClient(giteaUrl, token)` with `client, _ := NewClient(giteaURL, SetToken(token))`.
|
||||
|
||||
If you like tu utilize them, currently there are: SetContext, SetBasicAuth, SetOTP, SetToken, SetHTTPClient, SetSudo
|
||||
|
||||
Pulls:
|
||||
- [#417 Make http requests with context](https://gitea.com/gitea/go-sdk/pulls/417)
|
|
@ -32,7 +32,7 @@ func TestLabels(t *testing.T) {
|
|||
assert.EqualValues(t, "empty name not allowed", err.Error())
|
||||
createOpts.Name = "label one"
|
||||
|
||||
label1, err := c.CreateLabel(repo.Owner.UserName, repo.Name, createOpts)
|
||||
label1, _, err := c.CreateLabel(repo.Owner.UserName, repo.Name, createOpts)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, createOpts.Name, label1.Name)
|
||||
assert.EqualValues(t, createOpts.Color, label1.Color)
|
||||
|
|
44
gitea/admin_cron.go
Normal file
44
gitea/admin_cron.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
// 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"
|
||||
"time"
|
||||
)
|
||||
|
||||
// CronTask represents a Cron task
|
||||
type CronTask struct {
|
||||
Name string `json:"name"`
|
||||
Schedule string `json:"schedule"`
|
||||
Next time.Time `json:"next"`
|
||||
Prev time.Time `json:"prev"`
|
||||
ExecTimes int64 `json:"exec_times"`
|
||||
}
|
||||
|
||||
// ListCronTaskOptions list options for ListCronTasks
|
||||
type ListCronTaskOptions struct {
|
||||
ListOptions
|
||||
}
|
||||
|
||||
// ListCronTasks list available cron tasks
|
||||
func (c *Client) ListCronTasks(opt ListCronTaskOptions) ([]*CronTask, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_13_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
opt.setDefaults()
|
||||
ct := make([]*CronTask, 0, opt.PageSize)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/admin/cron?%s", opt.getURLQuery().Encode()), jsonHeader, nil, &ct)
|
||||
return ct, resp, err
|
||||
}
|
||||
|
||||
// RunCronTasks run a cron task
|
||||
func (c *Client) RunCronTasks(task string) (*Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_13_0); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, resp, err := c.getResponse("POST", fmt.Sprintf("/admin/cron/%s", task), jsonHeader, nil)
|
||||
return resp, err
|
||||
}
|
|
@ -17,19 +17,20 @@ type AdminListOrgsOptions struct {
|
|||
}
|
||||
|
||||
// AdminListOrgs lists all orgs
|
||||
func (c *Client) AdminListOrgs(opt AdminListOrgsOptions) ([]*Organization, error) {
|
||||
func (c *Client) AdminListOrgs(opt AdminListOrgsOptions) ([]*Organization, *Response, error) {
|
||||
opt.setDefaults()
|
||||
orgs := make([]*Organization, 0, opt.PageSize)
|
||||
return orgs, c.getParsedResponse("GET", fmt.Sprintf("/admin/orgs?%s", opt.getURLQuery().Encode()), nil, nil, &orgs)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/admin/orgs?%s", opt.getURLQuery().Encode()), nil, nil, &orgs)
|
||||
return orgs, resp, err
|
||||
}
|
||||
|
||||
// AdminCreateOrg create an organization
|
||||
func (c *Client) AdminCreateOrg(user string, opt CreateOrgOption) (*Organization, error) {
|
||||
func (c *Client) AdminCreateOrg(user string, opt CreateOrgOption) (*Organization, *Response, error) {
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
org := new(Organization)
|
||||
return org, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/orgs", user),
|
||||
jsonHeader, bytes.NewReader(body), org)
|
||||
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/orgs", user), jsonHeader, bytes.NewReader(body), org)
|
||||
return org, resp, err
|
||||
}
|
||||
|
|
|
@ -11,12 +11,12 @@ import (
|
|||
)
|
||||
|
||||
// AdminCreateRepo create a repo
|
||||
func (c *Client) AdminCreateRepo(user string, opt CreateRepoOption) (*Repository, error) {
|
||||
func (c *Client) AdminCreateRepo(user string, opt CreateRepoOption) (*Repository, *Response, error) {
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
repo := new(Repository)
|
||||
return repo, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/repos", user),
|
||||
jsonHeader, bytes.NewReader(body), repo)
|
||||
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/repos", user), jsonHeader, bytes.NewReader(body), repo)
|
||||
return repo, resp, err
|
||||
}
|
||||
|
|
|
@ -14,12 +14,12 @@ import (
|
|||
func TestAdminOrg(t *testing.T) {
|
||||
log.Println("== TestAdminOrg ==")
|
||||
c := newTestClient()
|
||||
user, err := c.GetMyUserInfo()
|
||||
user, _, err := c.GetMyUserInfo()
|
||||
assert.NoError(t, err)
|
||||
|
||||
orgName := "NewTestOrg"
|
||||
newOrg, err := c.AdminCreateOrg(user.UserName, CreateOrgOption{
|
||||
UserName: orgName,
|
||||
newOrg, _, err := c.AdminCreateOrg(user.UserName, CreateOrgOption{
|
||||
Name: orgName,
|
||||
FullName: orgName + " FullName",
|
||||
Description: "test adminCreateOrg",
|
||||
Visibility: VisibleTypePublic,
|
||||
|
@ -28,11 +28,22 @@ func TestAdminOrg(t *testing.T) {
|
|||
assert.NotEmpty(t, newOrg)
|
||||
assert.EqualValues(t, orgName, newOrg.UserName)
|
||||
|
||||
orgs, err := c.AdminListOrgs(AdminListOrgsOptions{})
|
||||
orgs, _, err := c.AdminListOrgs(AdminListOrgsOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, orgs, 1)
|
||||
assert.EqualValues(t, newOrg.ID, orgs[0].ID)
|
||||
|
||||
err = c.DeleteOrg(orgName)
|
||||
_, err = c.DeleteOrg(orgName)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestAdminCronTasks(t *testing.T) {
|
||||
log.Println("== TestAdminCronTasks ==")
|
||||
c := newTestClient()
|
||||
|
||||
tasks, _, err := c.ListCronTasks(ListCronTaskOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, tasks, 16)
|
||||
_, err = c.RunCronTasks(tasks[0].Name)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
|
|
@ -17,10 +17,11 @@ type AdminListUsersOptions struct {
|
|||
}
|
||||
|
||||
// AdminListUsers lists all users
|
||||
func (c *Client) AdminListUsers(opt AdminListUsersOptions) ([]*User, error) {
|
||||
func (c *Client) AdminListUsers(opt AdminListUsersOptions) ([]*User, *Response, error) {
|
||||
opt.setDefaults()
|
||||
users := make([]*User, 0, opt.PageSize)
|
||||
return users, c.getParsedResponse("GET", fmt.Sprintf("/admin/users?%s", opt.getURLQuery().Encode()), nil, nil, &users)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/admin/users?%s", opt.getURLQuery().Encode()), nil, nil, &users)
|
||||
return users, resp, err
|
||||
}
|
||||
|
||||
// CreateUserOption create user options
|
||||
|
@ -47,16 +48,17 @@ func (opt CreateUserOption) Validate() error {
|
|||
}
|
||||
|
||||
// AdminCreateUser create a user
|
||||
func (c *Client) AdminCreateUser(opt CreateUserOption) (*User, error) {
|
||||
func (c *Client) AdminCreateUser(opt CreateUserOption) (*User, *Response, error) {
|
||||
if err := opt.Validate(); err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
user := new(User)
|
||||
return user, c.getParsedResponse("POST", "/admin/users", jsonHeader, bytes.NewReader(body), user)
|
||||
resp, err := c.getParsedResponse("POST", "/admin/users", jsonHeader, bytes.NewReader(body), user)
|
||||
return user, resp, err
|
||||
}
|
||||
|
||||
// EditUserOption edit user options
|
||||
|
@ -79,33 +81,34 @@ type EditUserOption struct {
|
|||
}
|
||||
|
||||
// AdminEditUser modify user informations
|
||||
func (c *Client) AdminEditUser(user string, opt EditUserOption) error {
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = c.getResponse("PATCH", fmt.Sprintf("/admin/users/%s", user), jsonHeader, bytes.NewReader(body))
|
||||
return err
|
||||
}
|
||||
|
||||
// AdminDeleteUser delete one user according name
|
||||
func (c *Client) AdminDeleteUser(user string) error {
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/admin/users/%s", user), nil, nil)
|
||||
return err
|
||||
}
|
||||
|
||||
// AdminCreateUserPublicKey adds a public key for the user
|
||||
func (c *Client) AdminCreateUserPublicKey(user string, opt CreateKeyOption) (*PublicKey, error) {
|
||||
func (c *Client) AdminEditUser(user string, opt EditUserOption) (*Response, error) {
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, resp, err := c.getResponse("PATCH", fmt.Sprintf("/admin/users/%s", user), jsonHeader, bytes.NewReader(body))
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// AdminDeleteUser delete one user according name
|
||||
func (c *Client) AdminDeleteUser(user string) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/admin/users/%s", user), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// AdminCreateUserPublicKey adds a public key for the user
|
||||
func (c *Client) AdminCreateUserPublicKey(user string, opt CreateKeyOption) (*PublicKey, *Response, error) {
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
key := new(PublicKey)
|
||||
return key, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/keys", user), jsonHeader, bytes.NewReader(body), key)
|
||||
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/keys", user), jsonHeader, bytes.NewReader(body), key)
|
||||
return key, resp, err
|
||||
}
|
||||
|
||||
// AdminDeleteUserPublicKey deletes a user's public key
|
||||
func (c *Client) AdminDeleteUserPublicKey(user string, keyID int) error {
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/admin/users/%s/keys/%d", user, keyID), nil, nil)
|
||||
return err
|
||||
func (c *Client) AdminDeleteUserPublicKey(user string, keyID int) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/admin/users/%s/keys/%d", user, keyID), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
|
|
@ -30,47 +30,47 @@ type ListReleaseAttachmentsOptions struct {
|
|||
}
|
||||
|
||||
// ListReleaseAttachments list release's attachments
|
||||
func (c *Client) ListReleaseAttachments(user, repo string, release int64, opt ListReleaseAttachmentsOptions) ([]*Attachment, error) {
|
||||
func (c *Client) ListReleaseAttachments(user, repo string, release int64, opt ListReleaseAttachmentsOptions) ([]*Attachment, *Response, error) {
|
||||
opt.setDefaults()
|
||||
attachments := make([]*Attachment, 0, opt.PageSize)
|
||||
err := c.getParsedResponse("GET",
|
||||
resp, err := c.getParsedResponse("GET",
|
||||
fmt.Sprintf("/repos/%s/%s/releases/%d/assets?%s", user, repo, release, opt.getURLQuery().Encode()),
|
||||
nil, nil, &attachments)
|
||||
return attachments, err
|
||||
return attachments, resp, err
|
||||
}
|
||||
|
||||
// GetReleaseAttachment returns the requested attachment
|
||||
func (c *Client) GetReleaseAttachment(user, repo string, release int64, id int64) (*Attachment, error) {
|
||||
func (c *Client) GetReleaseAttachment(user, repo string, release int64, id int64) (*Attachment, *Response, error) {
|
||||
a := new(Attachment)
|
||||
err := c.getParsedResponse("GET",
|
||||
resp, err := c.getParsedResponse("GET",
|
||||
fmt.Sprintf("/repos/%s/%s/releases/%d/assets/%d", user, repo, release, id),
|
||||
nil, nil, &a)
|
||||
return a, err
|
||||
return a, resp, err
|
||||
}
|
||||
|
||||
// CreateReleaseAttachment creates an attachment for the given release
|
||||
func (c *Client) CreateReleaseAttachment(user, repo string, release int64, file io.Reader, filename string) (*Attachment, error) {
|
||||
func (c *Client) CreateReleaseAttachment(user, repo string, release int64, file io.Reader, filename string) (*Attachment, *Response, error) {
|
||||
// Write file to body
|
||||
body := new(bytes.Buffer)
|
||||
writer := multipart.NewWriter(body)
|
||||
part, err := writer.CreateFormFile("attachment", filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if _, err = io.Copy(part, file); err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
if err = writer.Close(); err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// Send request
|
||||
attachment := new(Attachment)
|
||||
err = c.getParsedResponse("POST",
|
||||
resp, err := c.getParsedResponse("POST",
|
||||
fmt.Sprintf("/repos/%s/%s/releases/%d/assets", user, repo, release),
|
||||
http.Header{"Content-Type": {writer.FormDataContentType()}}, body, &attachment)
|
||||
return attachment, err
|
||||
return attachment, resp, err
|
||||
}
|
||||
|
||||
// EditAttachmentOptions options for editing attachments
|
||||
|
@ -79,17 +79,18 @@ type EditAttachmentOptions struct {
|
|||
}
|
||||
|
||||
// EditReleaseAttachment updates the given attachment with the given options
|
||||
func (c *Client) EditReleaseAttachment(user, repo string, release int64, attachment int64, form EditAttachmentOptions) (*Attachment, error) {
|
||||
func (c *Client) EditReleaseAttachment(user, repo string, release int64, attachment int64, form EditAttachmentOptions) (*Attachment, *Response, error) {
|
||||
body, err := json.Marshal(&form)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
attach := new(Attachment)
|
||||
return attach, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/releases/%d/assets/%d", user, repo, release, attachment), jsonHeader, bytes.NewReader(body), attach)
|
||||
resp, err := c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/releases/%d/assets/%d", user, repo, release, attachment), jsonHeader, bytes.NewReader(body), attach)
|
||||
return attach, resp, err
|
||||
}
|
||||
|
||||
// DeleteReleaseAttachment deletes the given attachment including the uploaded file
|
||||
func (c *Client) DeleteReleaseAttachment(user, repo string, release int64, id int64) error {
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/releases/%d/assets/%d", user, repo, release, id), nil, nil)
|
||||
return err
|
||||
func (c *Client) DeleteReleaseAttachment(user, repo string, release int64, id int64) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/releases/%d/assets/%d", user, repo, release, id), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
|
180
gitea/client.go
180
gitea/client.go
|
@ -6,6 +6,7 @@
|
|||
package gitea
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
@ -22,60 +23,152 @@ var jsonHeader = http.Header{"content-type": []string{"application/json"}}
|
|||
|
||||
// Version return the library version
|
||||
func Version() string {
|
||||
return "0.13.0"
|
||||
return "0.14.0"
|
||||
}
|
||||
|
||||
// Client represents a Gitea API client.
|
||||
type Client struct {
|
||||
url string
|
||||
accessToken string
|
||||
username string
|
||||
password string
|
||||
otp string
|
||||
sudo string
|
||||
client *http.Client
|
||||
serverVersion *version.Version
|
||||
versionLock sync.RWMutex
|
||||
url string
|
||||
accessToken string
|
||||
username string
|
||||
password string
|
||||
otp string
|
||||
sudo string
|
||||
debug bool
|
||||
client *http.Client
|
||||
ctx context.Context
|
||||
serverVersion *version.Version
|
||||
getVersionOnce sync.Once
|
||||
}
|
||||
|
||||
// Response represents the gitea response
|
||||
type Response struct {
|
||||
*http.Response
|
||||
}
|
||||
|
||||
// NewClient initializes and returns a API client.
|
||||
func NewClient(url, token string) *Client {
|
||||
return &Client{
|
||||
url: strings.TrimSuffix(url, "/"),
|
||||
accessToken: token,
|
||||
client: &http.Client{},
|
||||
func NewClient(url string, options ...func(*Client)) (*Client, error) {
|
||||
client := &Client{
|
||||
url: strings.TrimSuffix(url, "/"),
|
||||
client: &http.Client{},
|
||||
ctx: context.Background(),
|
||||
}
|
||||
for _, opt := range options {
|
||||
opt(client)
|
||||
}
|
||||
if err := client.checkServerVersionGreaterThanOrEqual(version1_11_0); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// NewClientWithHTTP creates an API client with a custom http client
|
||||
// Deprecated use SetHTTPClient option
|
||||
func NewClientWithHTTP(url string, httpClient *http.Client) *Client {
|
||||
client := NewClient(url, "")
|
||||
client.client = httpClient
|
||||
client, _ := NewClient(url, SetHTTPClient(httpClient))
|
||||
return client
|
||||
}
|
||||
|
||||
// SetBasicAuth sets basicauth
|
||||
// SetHTTPClient is an option for NewClient to set custom http client
|
||||
func SetHTTPClient(httpClient *http.Client) func(client *Client) {
|
||||
return func(client *Client) {
|
||||
client.client = httpClient
|
||||
}
|
||||
}
|
||||
|
||||
// SetToken is an option for NewClient to set token
|
||||
func SetToken(token string) func(client *Client) {
|
||||
return func(client *Client) {
|
||||
client.accessToken = token
|
||||
}
|
||||
}
|
||||
|
||||
// SetBasicAuth is an option for NewClient to set username and password
|
||||
func SetBasicAuth(username, password string) func(client *Client) {
|
||||
return func(client *Client) {
|
||||
client.SetBasicAuth(username, password)
|
||||
}
|
||||
}
|
||||
|
||||
// SetBasicAuth sets username and password
|
||||
func (c *Client) SetBasicAuth(username, password string) {
|
||||
c.username, c.password = username, password
|
||||
}
|
||||
|
||||
// SetOTP is an option for NewClient to set OTP for 2FA
|
||||
func SetOTP(otp string) func(client *Client) {
|
||||
return func(client *Client) {
|
||||
client.SetOTP(otp)
|
||||
}
|
||||
}
|
||||
|
||||
// SetOTP sets OTP for 2FA
|
||||
func (c *Client) SetOTP(otp string) {
|
||||
c.otp = otp
|
||||
}
|
||||
|
||||
// SetContext is an option for NewClient to set context
|
||||
func SetContext(ctx context.Context) func(client *Client) {
|
||||
return func(client *Client) {
|
||||
client.SetContext(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// SetContext set context witch is used for http requests
|
||||
func (c *Client) SetContext(ctx context.Context) {
|
||||
c.ctx = ctx
|
||||
}
|
||||
|
||||
// SetHTTPClient replaces default http.Client with user given one.
|
||||
func (c *Client) SetHTTPClient(client *http.Client) {
|
||||
c.client = client
|
||||
}
|
||||
|
||||
// SetSudo is an option for NewClient to set sudo header
|
||||
func SetSudo(sudo string) func(client *Client) {
|
||||
return func(client *Client) {
|
||||
client.SetSudo(sudo)
|
||||
}
|
||||
}
|
||||
|
||||
// SetSudo sets username to impersonate.
|
||||
func (c *Client) SetSudo(sudo string) {
|
||||
c.sudo = sudo
|
||||
}
|
||||
|
||||
func (c *Client) doRequest(method, path string, header http.Header, body io.Reader) (*http.Response, error) {
|
||||
req, err := http.NewRequest(method, c.url+"/api/v1"+path, body)
|
||||
// SetDebugMode is an option for NewClient to enable debug mode
|
||||
func SetDebugMode() func(client *Client) {
|
||||
return func(client *Client) {
|
||||
client.debug = true
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) getWebResponse(method, path string, body io.Reader) ([]byte, *Response, error) {
|
||||
if c.debug {
|
||||
fmt.Printf("%s: %s\nBody: %v\n", method, c.url+path, body)
|
||||
}
|
||||
req, err := http.NewRequestWithContext(c.ctx, method, c.url+path, body)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
data, err := ioutil.ReadAll(resp.Body)
|
||||
if c.debug {
|
||||
fmt.Printf("Response: %v\n\n", resp)
|
||||
}
|
||||
return data, &Response{resp}, nil
|
||||
}
|
||||
|
||||
func (c *Client) doRequest(method, path string, header http.Header, body io.Reader) (*Response, error) {
|
||||
if c.debug {
|
||||
fmt.Printf("%s: %s\nHeader: %v\nBody: %s\n", method, c.url+"/api/v1"+path, header, body)
|
||||
}
|
||||
req, err := http.NewRequestWithContext(c.ctx, method, c.url+"/api/v1"+path, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -95,30 +188,37 @@ func (c *Client) doRequest(method, path string, header http.Header, body io.Read
|
|||
req.Header[k] = v
|
||||
}
|
||||
|
||||
return c.client.Do(req)
|
||||
}
|
||||
|
||||
func (c *Client) getResponse(method, path string, header http.Header, body io.Reader) ([]byte, error) {
|
||||
resp, err := c.doRequest(method, path, header, body)
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if c.debug {
|
||||
fmt.Printf("Response: %v\n\n", resp)
|
||||
}
|
||||
return &Response{resp}, nil
|
||||
}
|
||||
|
||||
func (c *Client) getResponse(method, path string, header http.Header, body io.Reader) ([]byte, *Response, error) {
|
||||
resp, err := c.doRequest(method, path, header, body)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
data, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
switch resp.StatusCode {
|
||||
case 403:
|
||||
return nil, errors.New("403 Forbidden")
|
||||
return data, resp, errors.New("403 Forbidden")
|
||||
case 404:
|
||||
return nil, errors.New("404 Not Found")
|
||||
return data, resp, errors.New("404 Not Found")
|
||||
case 409:
|
||||
return nil, errors.New("409 Conflict")
|
||||
return data, resp, errors.New("409 Conflict")
|
||||
case 422:
|
||||
return nil, fmt.Errorf("422 Unprocessable Entity: %s", string(data))
|
||||
return data, resp, fmt.Errorf("422 Unprocessable Entity: %s", string(data))
|
||||
}
|
||||
|
||||
if resp.StatusCode/100 != 2 {
|
||||
|
@ -126,28 +226,28 @@ func (c *Client) getResponse(method, path string, header http.Header, body io.Re
|
|||
if err = json.Unmarshal(data, &errMap); err != nil {
|
||||
// when the JSON can't be parsed, data was probably empty or a plain string,
|
||||
// so we try to return a helpful error anyway
|
||||
return nil, fmt.Errorf("Unknown API Error: %d\nRequest: '%s' with '%s' method '%s' header and '%s' body", resp.StatusCode, path, method, header, string(data))
|
||||
return data, resp, fmt.Errorf("Unknown API Error: %d\nRequest: '%s' with '%s' method '%s' header and '%s' body", resp.StatusCode, path, method, header, string(data))
|
||||
}
|
||||
return nil, errors.New(errMap["message"].(string))
|
||||
return data, resp, errors.New(errMap["message"].(string))
|
||||
}
|
||||
|
||||
return data, nil
|
||||
return data, resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) getParsedResponse(method, path string, header http.Header, body io.Reader, obj interface{}) error {
|
||||
data, err := c.getResponse(method, path, header, body)
|
||||
func (c *Client) getParsedResponse(method, path string, header http.Header, body io.Reader, obj interface{}) (*Response, error) {
|
||||
data, resp, err := c.getResponse(method, path, header, body)
|
||||
if err != nil {
|
||||
return err
|
||||
return resp, err
|
||||
}
|
||||
return json.Unmarshal(data, obj)
|
||||
return resp, json.Unmarshal(data, obj)
|
||||
}
|
||||
|
||||
func (c *Client) getStatusCode(method, path string, header http.Header, body io.Reader) (int, error) {
|
||||
func (c *Client) getStatusCode(method, path string, header http.Header, body io.Reader) (int, *Response, error) {
|
||||
resp, err := c.doRequest(method, path, header, body)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
return -1, resp, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
return resp.StatusCode, nil
|
||||
return resp.StatusCode, resp, nil
|
||||
}
|
||||
|
|
|
@ -16,12 +16,13 @@ type ListForksOptions struct {
|
|||
}
|
||||
|
||||
// ListForks list a repository's forks
|
||||
func (c *Client) ListForks(user string, repo string, opt ListForksOptions) ([]*Repository, error) {
|
||||
func (c *Client) ListForks(user string, repo string, opt ListForksOptions) ([]*Repository, *Response, error) {
|
||||
opt.setDefaults()
|
||||
forks := make([]*Repository, opt.PageSize)
|
||||
return forks, c.getParsedResponse("GET",
|
||||
resp, err := c.getParsedResponse("GET",
|
||||
fmt.Sprintf("/repos/%s/%s/forks?%s", user, repo, opt.getURLQuery().Encode()),
|
||||
nil, nil, &forks)
|
||||
return forks, resp, err
|
||||
}
|
||||
|
||||
// CreateForkOption options for creating a fork
|
||||
|
@ -31,11 +32,12 @@ type CreateForkOption struct {
|
|||
}
|
||||
|
||||
// CreateFork create a fork of a repository
|
||||
func (c *Client) CreateFork(user, repo string, form CreateForkOption) (*Repository, error) {
|
||||
func (c *Client) CreateFork(user, repo string, form CreateForkOption) (*Repository, *Response, error) {
|
||||
body, err := json.Marshal(form)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
fork := new(Repository)
|
||||
return fork, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/forks", user, repo), jsonHeader, bytes.NewReader(body), &fork)
|
||||
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/forks", user, repo), jsonHeader, bytes.NewReader(body), &fork)
|
||||
return fork, resp, err
|
||||
}
|
||||
|
|
|
@ -18,7 +18,8 @@ type GitBlobResponse struct {
|
|||
}
|
||||
|
||||
// GetBlob get the blob of a repository file
|
||||
func (c *Client) GetBlob(user, repo, sha string) (*GitBlobResponse, error) {
|
||||
func (c *Client) GetBlob(user, repo, sha string) (*GitBlobResponse, *Response, error) {
|
||||
blob := new(GitBlobResponse)
|
||||
return blob, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/git/blobs/%s", user, repo, sha), nil, nil, blob)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/git/blobs/%s", user, repo, sha), nil, nil, blob)
|
||||
return blob, resp, err
|
||||
}
|
||||
|
|
|
@ -23,16 +23,18 @@ type ListRepoGitHooksOptions struct {
|
|||
}
|
||||
|
||||
// ListRepoGitHooks list all the Git hooks of one repository
|
||||
func (c *Client) ListRepoGitHooks(user, repo string, opt ListRepoGitHooksOptions) ([]*GitHook, error) {
|
||||
func (c *Client) ListRepoGitHooks(user, repo string, opt ListRepoGitHooksOptions) ([]*GitHook, *Response, error) {
|
||||
opt.setDefaults()
|
||||
hooks := make([]*GitHook, 0, opt.PageSize)
|
||||
return hooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks/git?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &hooks)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks/git?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &hooks)
|
||||
return hooks, resp, err
|
||||
}
|
||||
|
||||
// GetRepoGitHook get a Git hook of a repository
|
||||
func (c *Client) GetRepoGitHook(user, repo, id string) (*GitHook, error) {
|
||||
func (c *Client) GetRepoGitHook(user, repo, id string) (*GitHook, *Response, error) {
|
||||
h := new(GitHook)
|
||||
return h, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks/git/%s", user, repo, id), nil, nil, h)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks/git/%s", user, repo, id), nil, nil, h)
|
||||
return h, resp, err
|
||||
}
|
||||
|
||||
// EditGitHookOption options when modifying one Git hook
|
||||
|
@ -41,17 +43,17 @@ type EditGitHookOption struct {
|
|||
}
|
||||
|
||||
// EditRepoGitHook modify one Git hook of a repository
|
||||
func (c *Client) EditRepoGitHook(user, repo, id string, opt EditGitHookOption) error {
|
||||
func (c *Client) EditRepoGitHook(user, repo, id string, opt EditGitHookOption) (*Response, error) {
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
_, err = c.getResponse("PATCH", fmt.Sprintf("/repos/%s/%s/hooks/git/%s", user, repo, id), jsonHeader, bytes.NewReader(body))
|
||||
return err
|
||||
_, resp, err := c.getResponse("PATCH", fmt.Sprintf("/repos/%s/%s/hooks/git/%s", user, repo, id), jsonHeader, bytes.NewReader(body))
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// DeleteRepoGitHook delete one Git hook from a repository
|
||||
func (c *Client) DeleteRepoGitHook(user, repo, id string) error {
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/hooks/git/%s", user, repo, id), nil, nil)
|
||||
return err
|
||||
func (c *Client) DeleteRepoGitHook(user, repo, id string) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/hooks/git/%s", user, repo, id), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
module code.gitea.io/sdk/gitea
|
||||
|
||||
go 1.12
|
||||
go 1.13
|
||||
|
||||
require (
|
||||
github.com/hashicorp/go-version v1.2.0
|
||||
github.com/hashicorp/go-version v1.2.1
|
||||
github.com/stretchr/testify v1.4.0
|
||||
)
|
||||
|
|
|
@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
|
|||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E=
|
||||
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
|
||||
github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pBJ3TkI=
|
||||
github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
|
|
20
gitea/helper.go
Normal file
20
gitea/helper.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
// 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
|
||||
|
||||
// OptionalBool convert a bool to a bool reference
|
||||
func OptionalBool(v bool) *bool {
|
||||
return &v
|
||||
}
|
||||
|
||||
// OptionalString convert a string to a string reference
|
||||
func OptionalString(v string) *string {
|
||||
return &v
|
||||
}
|
||||
|
||||
// OptionalInt64 convert a int64 to a int64 reference
|
||||
func OptionalInt64(v int64) *int64 {
|
||||
return &v
|
||||
}
|
|
@ -30,29 +30,33 @@ type ListHooksOptions struct {
|
|||
}
|
||||
|
||||
// ListOrgHooks list all the hooks of one organization
|
||||
func (c *Client) ListOrgHooks(org string, opt ListHooksOptions) ([]*Hook, error) {
|
||||
func (c *Client) ListOrgHooks(org string, opt ListHooksOptions) ([]*Hook, *Response, error) {
|
||||
opt.setDefaults()
|
||||
hooks := make([]*Hook, 0, opt.PageSize)
|
||||
return hooks, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/hooks?%s", org, opt.getURLQuery().Encode()), nil, nil, &hooks)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/hooks?%s", org, opt.getURLQuery().Encode()), nil, nil, &hooks)
|
||||
return hooks, resp, err
|
||||
}
|
||||
|
||||
// ListRepoHooks list all the hooks of one repository
|
||||
func (c *Client) ListRepoHooks(user, repo string, opt ListHooksOptions) ([]*Hook, error) {
|
||||
func (c *Client) ListRepoHooks(user, repo string, opt ListHooksOptions) ([]*Hook, *Response, error) {
|
||||
opt.setDefaults()
|
||||
hooks := make([]*Hook, 0, opt.PageSize)
|
||||
return hooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &hooks)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &hooks)
|
||||
return hooks, resp, err
|
||||
}
|
||||
|
||||
// GetOrgHook get a hook of an organization
|
||||
func (c *Client) GetOrgHook(org string, id int64) (*Hook, error) {
|
||||
func (c *Client) GetOrgHook(org string, id int64) (*Hook, *Response, error) {
|
||||
h := new(Hook)
|
||||
return h, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/hooks/%d", org, id), nil, nil, h)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/hooks/%d", org, id), nil, nil, h)
|
||||
return h, resp, err
|
||||
}
|
||||
|
||||
// GetRepoHook get a hook of a repository
|
||||
func (c *Client) GetRepoHook(user, repo string, id int64) (*Hook, error) {
|
||||
func (c *Client) GetRepoHook(user, repo string, id int64) (*Hook, *Response, error) {
|
||||
h := new(Hook)
|
||||
return h, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), nil, nil, h)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), nil, nil, h)
|
||||
return h, resp, err
|
||||
}
|
||||
|
||||
// CreateHookOption options when create a hook
|
||||
|
@ -73,26 +77,28 @@ func (opt CreateHookOption) Validate() error {
|
|||
}
|
||||
|
||||
// CreateOrgHook create one hook for an organization, with options
|
||||
func (c *Client) CreateOrgHook(org string, opt CreateHookOption) (*Hook, error) {
|
||||
func (c *Client) CreateOrgHook(org string, opt CreateHookOption) (*Hook, *Response, error) {
|
||||
if err := opt.Validate(); err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
h := new(Hook)
|
||||
return h, c.getParsedResponse("POST", fmt.Sprintf("/orgs/%s/hooks", org), jsonHeader, bytes.NewReader(body), h)
|
||||
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/orgs/%s/hooks", org), jsonHeader, bytes.NewReader(body), h)
|
||||
return h, resp, err
|
||||
}
|
||||
|
||||
// CreateRepoHook create one hook for a repository, with options
|
||||
func (c *Client) CreateRepoHook(user, repo string, opt CreateHookOption) (*Hook, error) {
|
||||
func (c *Client) CreateRepoHook(user, repo string, opt CreateHookOption) (*Hook, *Response, error) {
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
h := new(Hook)
|
||||
return h, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), jsonHeader, bytes.NewReader(body), h)
|
||||
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), jsonHeader, bytes.NewReader(body), h)
|
||||
return h, resp, err
|
||||
}
|
||||
|
||||
// EditHookOption options when modify one hook
|
||||
|
@ -104,33 +110,33 @@ type EditHookOption struct {
|
|||
}
|
||||
|
||||
// EditOrgHook modify one hook of an organization, with hook id and options
|
||||
func (c *Client) EditOrgHook(org string, id int64, opt EditHookOption) error {
|
||||
func (c *Client) EditOrgHook(org string, id int64, opt EditHookOption) (*Response, error) {
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
_, err = c.getResponse("PATCH", fmt.Sprintf("/orgs/%s/hooks/%d", org, id), jsonHeader, bytes.NewReader(body))
|
||||
return err
|
||||
_, resp, err := c.getResponse("PATCH", fmt.Sprintf("/orgs/%s/hooks/%d", org, id), jsonHeader, bytes.NewReader(body))
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// EditRepoHook modify one hook of a repository, with hook id and options
|
||||
func (c *Client) EditRepoHook(user, repo string, id int64, opt EditHookOption) error {
|
||||
func (c *Client) EditRepoHook(user, repo string, id int64, opt EditHookOption) (*Response, error) {
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
_, err = c.getResponse("PATCH", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), jsonHeader, bytes.NewReader(body))
|
||||
return err
|
||||
_, resp, err := c.getResponse("PATCH", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), jsonHeader, bytes.NewReader(body))
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// DeleteOrgHook delete one hook from an organization, with hook id
|
||||
func (c *Client) DeleteOrgHook(org string, id int64) error {
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/orgs/%s/hooks/%d", org, id), nil, nil)
|
||||
return err
|
||||
func (c *Client) DeleteOrgHook(org string, id int64) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/orgs/%s/hooks/%d", org, id), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// DeleteRepoHook delete one hook from a repository, with hook id
|
||||
func (c *Client) DeleteRepoHook(user, repo string, id int64) error {
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), nil, nil)
|
||||
return err
|
||||
func (c *Client) DeleteRepoHook(user, repo string, id int64) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
|
|
@ -114,49 +114,49 @@ func (opt *ListIssueOption) QueryEncode() string {
|
|||
}
|
||||
|
||||
// ListIssues returns all issues assigned the authenticated user
|
||||
func (c *Client) ListIssues(opt ListIssueOption) ([]*Issue, error) {
|
||||
func (c *Client) ListIssues(opt ListIssueOption) ([]*Issue, *Response, error) {
|
||||
opt.setDefaults()
|
||||
issues := make([]*Issue, 0, opt.PageSize)
|
||||
|
||||
link, _ := url.Parse("/repos/issues/search")
|
||||
link.RawQuery = opt.QueryEncode()
|
||||
err := c.getParsedResponse("GET", link.String(), jsonHeader, nil, &issues)
|
||||
if e := c.CheckServerVersionConstraint(">=1.12.0"); e != nil {
|
||||
resp, err := c.getParsedResponse("GET", link.String(), jsonHeader, nil, &issues)
|
||||
if e := c.checkServerVersionGreaterThanOrEqual(version1_12_0); e != nil {
|
||||
for i := 0; i < len(issues); i++ {
|
||||
if issues[i].Repository != nil {
|
||||
issues[i].Repository.Owner = strings.Split(issues[i].Repository.FullName, "/")[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
return issues, err
|
||||
return issues, resp, err
|
||||
}
|
||||
|
||||
// ListRepoIssues returns all issues for a given repository
|
||||
func (c *Client) ListRepoIssues(owner, repo string, opt ListIssueOption) ([]*Issue, error) {
|
||||
func (c *Client) ListRepoIssues(owner, repo string, opt ListIssueOption) ([]*Issue, *Response, error) {
|
||||
opt.setDefaults()
|
||||
issues := make([]*Issue, 0, opt.PageSize)
|
||||
|
||||
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/issues", owner, repo))
|
||||
link.RawQuery = opt.QueryEncode()
|
||||
err := c.getParsedResponse("GET", link.String(), jsonHeader, nil, &issues)
|
||||
if e := c.CheckServerVersionConstraint(">=1.12.0"); e != nil {
|
||||
resp, err := c.getParsedResponse("GET", link.String(), jsonHeader, nil, &issues)
|
||||
if e := c.checkServerVersionGreaterThanOrEqual(version1_12_0); e != nil {
|
||||
for i := 0; i < len(issues); i++ {
|
||||
if issues[i].Repository != nil {
|
||||
issues[i].Repository.Owner = strings.Split(issues[i].Repository.FullName, "/")[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
return issues, err
|
||||
return issues, resp, err
|
||||
}
|
||||
|
||||
// GetIssue returns a single issue for a given repository
|
||||
func (c *Client) GetIssue(owner, repo string, index int64) (*Issue, error) {
|
||||
func (c *Client) GetIssue(owner, repo string, index int64) (*Issue, *Response, error) {
|
||||
issue := new(Issue)
|
||||
err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index), nil, nil, issue)
|
||||
if e := c.CheckServerVersionConstraint(">=1.12.0"); e != nil && issue.Repository != nil {
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index), nil, nil, issue)
|
||||
if e := c.checkServerVersionGreaterThanOrEqual(version1_12_0); e != nil && issue.Repository != nil {
|
||||
issue.Repository.Owner = strings.Split(issue.Repository.FullName, "/")[0]
|
||||
}
|
||||
return issue, err
|
||||
return issue, resp, err
|
||||
}
|
||||
|
||||
// CreateIssueOption options to create one issue
|
||||
|
@ -183,17 +183,18 @@ func (opt CreateIssueOption) Validate() error {
|
|||
}
|
||||
|
||||
// CreateIssue create a new issue for a given repository
|
||||
func (c *Client) CreateIssue(owner, repo string, opt CreateIssueOption) (*Issue, error) {
|
||||
func (c *Client) CreateIssue(owner, repo string, opt CreateIssueOption) (*Issue, *Response, error) {
|
||||
if err := opt.Validate(); err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
issue := new(Issue)
|
||||
return issue, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues", owner, repo),
|
||||
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues", owner, repo),
|
||||
jsonHeader, bytes.NewReader(body), issue)
|
||||
return issue, resp, err
|
||||
}
|
||||
|
||||
// EditIssueOption options for editing an issue
|
||||
|
@ -216,15 +217,17 @@ func (opt EditIssueOption) Validate() error {
|
|||
}
|
||||
|
||||
// EditIssue modify an existing issue for a given repository
|
||||
func (c *Client) EditIssue(owner, repo string, index int64, opt EditIssueOption) (*Issue, error) {
|
||||
func (c *Client) EditIssue(owner, repo string, index int64, opt EditIssueOption) (*Issue, *Response, error) {
|
||||
if err := opt.Validate(); err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
issue := new(Issue)
|
||||
return issue, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index),
|
||||
resp, err := c.getParsedResponse("PATCH",
|
||||
fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index),
|
||||
jsonHeader, bytes.NewReader(body), issue)
|
||||
return issue, resp, err
|
||||
}
|
||||
|
|
|
@ -46,30 +46,33 @@ func (opt *ListIssueCommentOptions) QueryEncode() string {
|
|||
}
|
||||
|
||||
// ListIssueComments list comments on an issue.
|
||||
func (c *Client) ListIssueComments(owner, repo string, index int64, opt ListIssueCommentOptions) ([]*Comment, error) {
|
||||
func (c *Client) ListIssueComments(owner, repo string, index int64, opt ListIssueCommentOptions) ([]*Comment, *Response, error) {
|
||||
opt.setDefaults()
|
||||
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/issues/%d/comments", owner, repo, index))
|
||||
link.RawQuery = opt.QueryEncode()
|
||||
comments := make([]*Comment, 0, opt.PageSize)
|
||||
return comments, c.getParsedResponse("GET", link.String(), nil, nil, &comments)
|
||||
resp, err := c.getParsedResponse("GET", link.String(), nil, nil, &comments)
|
||||
return comments, resp, err
|
||||
}
|
||||
|
||||
// ListRepoIssueComments list comments for a given repo.
|
||||
func (c *Client) ListRepoIssueComments(owner, repo string, opt ListIssueCommentOptions) ([]*Comment, error) {
|
||||
func (c *Client) ListRepoIssueComments(owner, repo string, opt ListIssueCommentOptions) ([]*Comment, *Response, error) {
|
||||
opt.setDefaults()
|
||||
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/issues/comments", owner, repo))
|
||||
link.RawQuery = opt.QueryEncode()
|
||||
comments := make([]*Comment, 0, opt.PageSize)
|
||||
return comments, c.getParsedResponse("GET", link.String(), nil, nil, &comments)
|
||||
resp, err := c.getParsedResponse("GET", link.String(), nil, nil, &comments)
|
||||
return comments, resp, err
|
||||
}
|
||||
|
||||
// GetIssueComment get a comment for a given repo by id.
|
||||
func (c *Client) GetIssueComment(owner, repo string, id int64) (*Comment, error) {
|
||||
func (c *Client) GetIssueComment(owner, repo string, id int64) (*Comment, *Response, error) {
|
||||
comment := new(Comment)
|
||||
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
||||
return comment, err
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return comment, nil, err
|
||||
}
|
||||
return comment, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/comments/%d", owner, repo, id), nil, nil, &comment)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/comments/%d", owner, repo, id), nil, nil, &comment)
|
||||
return comment, resp, err
|
||||
}
|
||||
|
||||
// CreateIssueCommentOption options for creating a comment on an issue
|
||||
|
@ -86,16 +89,17 @@ func (opt CreateIssueCommentOption) Validate() error {
|
|||
}
|
||||
|
||||
// CreateIssueComment create comment on an issue.
|
||||
func (c *Client) CreateIssueComment(owner, repo string, index int64, opt CreateIssueCommentOption) (*Comment, error) {
|
||||
func (c *Client) CreateIssueComment(owner, repo string, index int64, opt CreateIssueCommentOption) (*Comment, *Response, error) {
|
||||
if err := opt.Validate(); err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
comment := new(Comment)
|
||||
return comment, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues/%d/comments", owner, repo, index), jsonHeader, bytes.NewReader(body), comment)
|
||||
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues/%d/comments", owner, repo, index), jsonHeader, bytes.NewReader(body), comment)
|
||||
return comment, resp, err
|
||||
}
|
||||
|
||||
// EditIssueCommentOption options for editing a comment
|
||||
|
@ -112,20 +116,21 @@ func (opt EditIssueCommentOption) Validate() error {
|
|||
}
|
||||
|
||||
// EditIssueComment edits an issue comment.
|
||||
func (c *Client) EditIssueComment(owner, repo string, commentID int64, opt EditIssueCommentOption) (*Comment, error) {
|
||||
func (c *Client) EditIssueComment(owner, repo string, commentID int64, opt EditIssueCommentOption) (*Comment, *Response, error) {
|
||||
if err := opt.Validate(); err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
comment := new(Comment)
|
||||
return comment, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/issues/comments/%d", owner, repo, commentID), jsonHeader, bytes.NewReader(body), comment)
|
||||
resp, err := c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/issues/comments/%d", owner, repo, commentID), jsonHeader, bytes.NewReader(body), comment)
|
||||
return comment, resp, err
|
||||
}
|
||||
|
||||
// DeleteIssueComment deletes an issue comment.
|
||||
func (c *Client) DeleteIssueComment(owner, repo string, commentID int64) error {
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/comments/%d", owner, repo, commentID), nil, nil)
|
||||
return err
|
||||
func (c *Client) DeleteIssueComment(owner, repo string, commentID int64) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/comments/%d", owner, repo, commentID), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
|
|
@ -17,15 +17,15 @@ func TestIssueComment(t *testing.T) {
|
|||
|
||||
c := newTestClient()
|
||||
|
||||
user, err := c.GetMyUserInfo()
|
||||
user, _, err := c.GetMyUserInfo()
|
||||
|
||||
assert.NoError(t, err)
|
||||
repo, err := createTestRepo(t, "TestIssueCommentRepo", c)
|
||||
assert.NoError(t, err)
|
||||
issue1, err := c.CreateIssue(user.UserName, repo.Name, CreateIssueOption{Title: "issue1", Body: "body", Closed: false})
|
||||
issue1, _, err := c.CreateIssue(user.UserName, repo.Name, CreateIssueOption{Title: "issue1", Body: "body", Closed: false})
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 1, issue1.Index)
|
||||
issue2, err := c.CreateIssue(user.UserName, repo.Name, CreateIssueOption{Title: "issue1", Body: "body", Closed: false})
|
||||
issue2, _, err := c.CreateIssue(user.UserName, repo.Name, CreateIssueOption{Title: "issue1", Body: "body", Closed: false})
|
||||
assert.EqualValues(t, 2, issue2.Index)
|
||||
assert.NoError(t, err)
|
||||
tUser2 := createTestUser(t, "Commenter2", c)
|
||||
|
@ -33,7 +33,7 @@ func TestIssueComment(t *testing.T) {
|
|||
|
||||
createOne := func(u *User, issue int64, text string) {
|
||||
c.sudo = u.UserName
|
||||
comment, e := c.CreateIssueComment(user.UserName, repo.Name, issue, CreateIssueCommentOption{Body: text})
|
||||
comment, _, e := c.CreateIssueComment(user.UserName, repo.Name, issue, CreateIssueCommentOption{Body: text})
|
||||
c.sudo = ""
|
||||
assert.NoError(t, e)
|
||||
assert.NotEmpty(t, comment)
|
||||
|
@ -50,34 +50,36 @@ func TestIssueComment(t *testing.T) {
|
|||
createOne(tUser2, 2, "second")
|
||||
createOne(user, 2, "3")
|
||||
|
||||
assert.NoError(t, c.AdminDeleteUser(tUser3.UserName))
|
||||
_, err = c.AdminDeleteUser(tUser3.UserName)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// ListRepoIssueComments
|
||||
comments, err := c.ListRepoIssueComments(user.UserName, repo.Name, ListIssueCommentOptions{})
|
||||
comments, _, err := c.ListRepoIssueComments(user.UserName, repo.Name, ListIssueCommentOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, comments, 7)
|
||||
|
||||
// ListIssueComments
|
||||
comments, err = c.ListIssueComments(user.UserName, repo.Name, 2, ListIssueCommentOptions{})
|
||||
comments, _, err = c.ListIssueComments(user.UserName, repo.Name, 2, ListIssueCommentOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, comments, 3)
|
||||
|
||||
// GetIssueComment
|
||||
comment, err := c.GetIssueComment(user.UserName, repo.Name, comments[1].ID)
|
||||
comment, _, err := c.GetIssueComment(user.UserName, repo.Name, comments[1].ID)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, comment.Poster.ID, comments[1].Poster.ID)
|
||||
assert.EqualValues(t, comment.Body, comments[1].Body)
|
||||
assert.EqualValues(t, comment.Updated.Unix(), comments[1].Updated.Unix())
|
||||
|
||||
// EditIssueComment
|
||||
comment, err = c.EditIssueComment(user.UserName, repo.Name, comments[1].ID, EditIssueCommentOption{
|
||||
comment, _, err = c.EditIssueComment(user.UserName, repo.Name, comments[1].ID, EditIssueCommentOption{
|
||||
Body: "changed my mind",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, "changed my mind", comment.Body)
|
||||
|
||||
// DeleteIssueComment
|
||||
assert.NoError(t, c.DeleteIssueComment(user.UserName, repo.Name, comments[1].ID))
|
||||
_, err = c.GetIssueComment(user.UserName, repo.Name, comments[1].ID)
|
||||
_, err = c.DeleteIssueComment(user.UserName, repo.Name, comments[1].ID)
|
||||
assert.NoError(t, err)
|
||||
_, _, err = c.GetIssueComment(user.UserName, repo.Name, comments[1].ID)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
|
|
@ -28,16 +28,18 @@ type ListLabelsOptions struct {
|
|||
}
|
||||
|
||||
// ListRepoLabels list labels of one repository
|
||||
func (c *Client) ListRepoLabels(owner, repo string, opt ListLabelsOptions) ([]*Label, error) {
|
||||
func (c *Client) ListRepoLabels(owner, repo string, opt ListLabelsOptions) ([]*Label, *Response, error) {
|
||||
opt.setDefaults()
|
||||
labels := make([]*Label, 0, opt.PageSize)
|
||||
return labels, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/labels?%s", owner, repo, opt.getURLQuery().Encode()), nil, nil, &labels)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/labels?%s", owner, repo, opt.getURLQuery().Encode()), nil, nil, &labels)
|
||||
return labels, resp, err
|
||||
}
|
||||
|
||||
// GetRepoLabel get one label of repository by repo it
|
||||
func (c *Client) GetRepoLabel(owner, repo string, id int64) (*Label, error) {
|
||||
func (c *Client) GetRepoLabel(owner, repo string, id int64) (*Label, *Response, error) {
|
||||
label := new(Label)
|
||||
return label, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, id), nil, nil, label)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, id), nil, nil, label)
|
||||
return label, resp, err
|
||||
}
|
||||
|
||||
// CreateLabelOption options for creating a label
|
||||
|
@ -64,22 +66,24 @@ func (opt CreateLabelOption) Validate() error {
|
|||
}
|
||||
|
||||
// CreateLabel create one label of repository
|
||||
func (c *Client) CreateLabel(owner, repo string, opt CreateLabelOption) (*Label, error) {
|
||||
func (c *Client) CreateLabel(owner, repo string, opt CreateLabelOption) (*Label, *Response, error) {
|
||||
if err := opt.Validate(); err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
if len(opt.Color) == 6 {
|
||||
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
opt.Color = "#" + opt.Color
|
||||
}
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
label := new(Label)
|
||||
return label, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/labels", owner, repo),
|
||||
resp, err := c.getParsedResponse("POST",
|
||||
fmt.Sprintf("/repos/%s/%s/labels", owner, repo),
|
||||
jsonHeader, bytes.NewReader(body), label)
|
||||
return label, resp, err
|
||||
}
|
||||
|
||||
// EditLabelOption options for editing a label
|
||||
|
@ -109,28 +113,30 @@ func (opt EditLabelOption) Validate() error {
|
|||
}
|
||||
|
||||
// EditLabel modify one label with options
|
||||
func (c *Client) EditLabel(owner, repo string, id int64, opt EditLabelOption) (*Label, error) {
|
||||
func (c *Client) EditLabel(owner, repo string, id int64, opt EditLabelOption) (*Label, *Response, error) {
|
||||
if err := opt.Validate(); err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
label := new(Label)
|
||||
return label, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, id), jsonHeader, bytes.NewReader(body), label)
|
||||
resp, err := c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, id), jsonHeader, bytes.NewReader(body), label)
|
||||
return label, resp, err
|
||||
}
|
||||
|
||||
// DeleteLabel delete one label of repository by id
|
||||
func (c *Client) DeleteLabel(owner, repo string, id int64) error {
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, id), nil, nil)
|
||||
return err
|
||||
func (c *Client) DeleteLabel(owner, repo string, id int64) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, id), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// GetIssueLabels get labels of one issue via issue id
|
||||
func (c *Client) GetIssueLabels(owner, repo string, index int64, opts ListLabelsOptions) ([]*Label, error) {
|
||||
func (c *Client) GetIssueLabels(owner, repo string, index int64, opts ListLabelsOptions) ([]*Label, *Response, error) {
|
||||
labels := make([]*Label, 0, 5)
|
||||
return labels, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/labels?%s", owner, repo, index, opts.getURLQuery().Encode()), nil, nil, &labels)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/labels?%s", owner, repo, index, opts.getURLQuery().Encode()), nil, nil, &labels)
|
||||
return labels, resp, err
|
||||
}
|
||||
|
||||
// IssueLabelsOption a collection of labels
|
||||
|
@ -140,34 +146,36 @@ type IssueLabelsOption struct {
|
|||
}
|
||||
|
||||
// AddIssueLabels add one or more labels to one issue
|
||||
func (c *Client) AddIssueLabels(owner, repo string, index int64, opt IssueLabelsOption) ([]*Label, error) {
|
||||
func (c *Client) AddIssueLabels(owner, repo string, index int64, opt IssueLabelsOption) ([]*Label, *Response, error) {
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
var labels []*Label
|
||||
return labels, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), jsonHeader, bytes.NewReader(body), &labels)
|
||||
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), jsonHeader, bytes.NewReader(body), &labels)
|
||||
return labels, resp, err
|
||||
}
|
||||
|
||||
// ReplaceIssueLabels replace old labels of issue with new labels
|
||||
func (c *Client) ReplaceIssueLabels(owner, repo string, index int64, opt IssueLabelsOption) ([]*Label, error) {
|
||||
func (c *Client) ReplaceIssueLabels(owner, repo string, index int64, opt IssueLabelsOption) ([]*Label, *Response, error) {
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
var labels []*Label
|
||||
return labels, c.getParsedResponse("PUT", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), jsonHeader, bytes.NewReader(body), &labels)
|
||||
resp, err := c.getParsedResponse("PUT", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), jsonHeader, bytes.NewReader(body), &labels)
|
||||
return labels, resp, err
|
||||
}
|
||||
|
||||
// DeleteIssueLabel delete one label of one issue by issue id and label id
|
||||
// TODO: maybe we need delete by label name and issue id
|
||||
func (c *Client) DeleteIssueLabel(owner, repo string, index, label int64) error {
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/labels/%d", owner, repo, index, label), nil, nil)
|
||||
return err
|
||||
func (c *Client) DeleteIssueLabel(owner, repo string, index, label int64) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/labels/%d", owner, repo, index, label), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// ClearIssueLabels delete all the labels of one issue.
|
||||
func (c *Client) ClearIssueLabels(owner, repo string, index int64) error {
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), nil, nil)
|
||||
return err
|
||||
func (c *Client) ClearIssueLabels(owner, repo string, index int64) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
|
|
@ -21,6 +21,8 @@ type Milestone struct {
|
|||
State StateType `json:"state"`
|
||||
OpenIssues int `json:"open_issues"`
|
||||
ClosedIssues int `json:"closed_issues"`
|
||||
Created time.Time `json:"created_at"`
|
||||
Updated *time.Time `json:"updated_at"`
|
||||
Closed *time.Time `json:"closed_at"`
|
||||
Deadline *time.Time `json:"due_on"`
|
||||
}
|
||||
|
@ -30,6 +32,7 @@ type ListMilestoneOption struct {
|
|||
ListOptions
|
||||
// open, closed, all
|
||||
State StateType
|
||||
Name string
|
||||
}
|
||||
|
||||
// QueryEncode turns options into querystring argument
|
||||
|
@ -38,23 +41,40 @@ func (opt *ListMilestoneOption) QueryEncode() string {
|
|||
if opt.State != "" {
|
||||
query.Add("state", string(opt.State))
|
||||
}
|
||||
if len(opt.Name) != 0 {
|
||||
query.Add("name", opt.Name)
|
||||
}
|
||||
return query.Encode()
|
||||
}
|
||||
|
||||
// ListRepoMilestones list all the milestones of one repository
|
||||
func (c *Client) ListRepoMilestones(owner, repo string, opt ListMilestoneOption) ([]*Milestone, error) {
|
||||
func (c *Client) ListRepoMilestones(owner, repo string, opt ListMilestoneOption) ([]*Milestone, *Response, error) {
|
||||
opt.setDefaults()
|
||||
milestones := make([]*Milestone, 0, opt.PageSize)
|
||||
|
||||
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/milestones", owner, repo))
|
||||
link.RawQuery = opt.QueryEncode()
|
||||
return milestones, c.getParsedResponse("GET", link.String(), nil, nil, &milestones)
|
||||
resp, err := c.getParsedResponse("GET", link.String(), nil, nil, &milestones)
|
||||
return milestones, resp, err
|
||||
}
|
||||
|
||||
// GetMilestone get one milestone by repo name and milestone id
|
||||
func (c *Client) GetMilestone(owner, repo string, id int64) (*Milestone, error) {
|
||||
func (c *Client) GetMilestone(owner, repo string, id int64) (*Milestone, *Response, error) {
|
||||
milestone := new(Milestone)
|
||||
return milestone, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/milestones/%d", owner, repo, id), nil, nil, milestone)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/milestones/%d", owner, repo, id), nil, nil, milestone)
|
||||
return milestone, resp, err
|
||||
}
|
||||
|
||||
// GetMilestoneByName get one milestone by repo and milestone name
|
||||
func (c *Client) GetMilestoneByName(owner, repo string, name string) (*Milestone, *Response, error) {
|
||||
if c.CheckServerVersionConstraint(">=1.13") != nil {
|
||||
// backwards compatibility mode
|
||||
m, resp, err := c.resolveMilestoneByName(owner, repo, name)
|
||||
return m, resp, err
|
||||
}
|
||||
milestone := new(Milestone)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/milestones/%s", owner, repo, name), nil, nil, milestone)
|
||||
return milestone, resp, err
|
||||
}
|
||||
|
||||
// CreateMilestoneOption options for creating a milestone
|
||||
|
@ -74,16 +94,16 @@ func (opt CreateMilestoneOption) Validate() error {
|
|||
}
|
||||
|
||||
// CreateMilestone create one milestone with options
|
||||
func (c *Client) CreateMilestone(owner, repo string, opt CreateMilestoneOption) (*Milestone, error) {
|
||||
func (c *Client) CreateMilestone(owner, repo string, opt CreateMilestoneOption) (*Milestone, *Response, error) {
|
||||
if err := opt.Validate(); err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
milestone := new(Milestone)
|
||||
err = c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/milestones", owner, repo), jsonHeader, bytes.NewReader(body), milestone)
|
||||
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/milestones", owner, repo), jsonHeader, bytes.NewReader(body), milestone)
|
||||
|
||||
// make creating closed milestones need gitea >= v1.13.0
|
||||
// this make it backwards compatible
|
||||
|
@ -94,7 +114,7 @@ func (c *Client) CreateMilestone(owner, repo string, opt CreateMilestoneOption)
|
|||
})
|
||||
}
|
||||
|
||||
return milestone, err
|
||||
return milestone, resp, err
|
||||
}
|
||||
|
||||
// EditMilestoneOption options for editing a milestone
|
||||
|
@ -114,20 +134,80 @@ func (opt EditMilestoneOption) Validate() error {
|
|||
}
|
||||
|
||||
// EditMilestone modify milestone with options
|
||||
func (c *Client) EditMilestone(owner, repo string, id int64, opt EditMilestoneOption) (*Milestone, error) {
|
||||
func (c *Client) EditMilestone(owner, repo string, id int64, opt EditMilestoneOption) (*Milestone, *Response, error) {
|
||||
if err := opt.Validate(); err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
milestone := new(Milestone)
|
||||
return milestone, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/milestones/%d", owner, repo, id), jsonHeader, bytes.NewReader(body), milestone)
|
||||
resp, err := c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/milestones/%d", owner, repo, id), jsonHeader, bytes.NewReader(body), milestone)
|
||||
return milestone, resp, err
|
||||
}
|
||||
|
||||
// DeleteMilestone delete one milestone by milestone id
|
||||
func (c *Client) DeleteMilestone(owner, repo string, id int64) error {
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/milestones/%d", owner, repo, id), nil, nil)
|
||||
return err
|
||||
// EditMilestoneByName modify milestone with options
|
||||
func (c *Client) EditMilestoneByName(owner, repo string, name string, opt EditMilestoneOption) (*Milestone, *Response, error) {
|
||||
if c.CheckServerVersionConstraint(">=1.13") != nil {
|
||||
// backwards compatibility mode
|
||||
m, _, err := c.resolveMilestoneByName(owner, repo, name)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return c.EditMilestone(owner, repo, m.ID, opt)
|
||||
}
|
||||
if err := opt.Validate(); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
milestone := new(Milestone)
|
||||
resp, err := c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/milestones/%s", owner, repo, name), jsonHeader, bytes.NewReader(body), milestone)
|
||||
return milestone, resp, err
|
||||
}
|
||||
|
||||
// DeleteMilestone delete one milestone by id
|
||||
func (c *Client) DeleteMilestone(owner, repo string, id int64) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/milestones/%d", owner, repo, id), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// DeleteMilestoneByName delete one milestone by name
|
||||
func (c *Client) DeleteMilestoneByName(owner, repo string, name string) (*Response, error) {
|
||||
if c.CheckServerVersionConstraint(">=1.13") != nil {
|
||||
// backwards compatibility mode
|
||||
m, _, err := c.resolveMilestoneByName(owner, repo, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c.DeleteMilestone(owner, repo, m.ID)
|
||||
}
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/milestones/%s", owner, repo, name), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// resolveMilestoneByName is a fallback method to find milestone id by name
|
||||
func (c *Client) resolveMilestoneByName(owner, repo, name string) (*Milestone, *Response, error) {
|
||||
for i := 1; ; i++ {
|
||||
miles, resp, err := c.ListRepoMilestones(owner, repo, ListMilestoneOption{
|
||||
ListOptions: ListOptions{
|
||||
Page: i,
|
||||
},
|
||||
State: "all",
|
||||
})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if len(miles) == 0 {
|
||||
return nil, nil, fmt.Errorf("milestone '%s' do not exist", name)
|
||||
}
|
||||
for _, m := range miles {
|
||||
if strings.ToLower(strings.TrimSpace(m.Title)) == strings.ToLower(strings.TrimSpace(name)) {
|
||||
return m, resp, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,37 +23,53 @@ func TestMilestones(t *testing.T) {
|
|||
sClosed := StateClosed
|
||||
|
||||
// CreateMilestone 4x
|
||||
m1, err := c.CreateMilestone(repo.Owner.UserName, repo.Name, CreateMilestoneOption{Title: "v1.0", Description: "First Version", Deadline: &now})
|
||||
m1, _, err := c.CreateMilestone(repo.Owner.UserName, repo.Name, CreateMilestoneOption{Title: "v1.0", Description: "First Version", Deadline: &now})
|
||||
assert.NoError(t, err)
|
||||
_, err = c.CreateMilestone(repo.Owner.UserName, repo.Name, CreateMilestoneOption{Title: "v2.0", Description: "Second Version", Deadline: &future})
|
||||
_, _, err = c.CreateMilestone(repo.Owner.UserName, repo.Name, CreateMilestoneOption{Title: "v2.0", Description: "Second Version", Deadline: &future})
|
||||
assert.NoError(t, err)
|
||||
_, err = c.CreateMilestone(repo.Owner.UserName, repo.Name, CreateMilestoneOption{Title: "v3.0", Description: "Third Version", Deadline: nil})
|
||||
_, _, err = c.CreateMilestone(repo.Owner.UserName, repo.Name, CreateMilestoneOption{Title: "v3.0", Description: "Third Version", Deadline: nil})
|
||||
assert.NoError(t, err)
|
||||
m4, err := c.CreateMilestone(repo.Owner.UserName, repo.Name, CreateMilestoneOption{Title: "temp", Description: "part time milestone"})
|
||||
m4, _, err := c.CreateMilestone(repo.Owner.UserName, repo.Name, CreateMilestoneOption{Title: "temp", Description: "part time milestone"})
|
||||
assert.NoError(t, err)
|
||||
|
||||
// EditMilestone
|
||||
m1, err = c.EditMilestone(repo.Owner.UserName, repo.Name, m1.ID, EditMilestoneOption{Description: &closed, State: &sClosed})
|
||||
m1, _, err = c.EditMilestone(repo.Owner.UserName, repo.Name, m1.ID, EditMilestoneOption{Description: &closed, State: &sClosed})
|
||||
assert.NoError(t, err)
|
||||
|
||||
// DeleteMilestone
|
||||
assert.NoError(t, c.DeleteMilestone(repo.Owner.UserName, repo.Name, m4.ID))
|
||||
_, err = c.DeleteMilestone(repo.Owner.UserName, repo.Name, m4.ID)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// ListRepoMilestones
|
||||
ml, err := c.ListRepoMilestones(repo.Owner.UserName, repo.Name, ListMilestoneOption{})
|
||||
ml, _, err := c.ListRepoMilestones(repo.Owner.UserName, repo.Name, ListMilestoneOption{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, ml, 2)
|
||||
ml, err = c.ListRepoMilestones(repo.Owner.UserName, repo.Name, ListMilestoneOption{State: StateClosed})
|
||||
ml, _, err = c.ListRepoMilestones(repo.Owner.UserName, repo.Name, ListMilestoneOption{State: StateClosed})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, ml, 1)
|
||||
ml, err = c.ListRepoMilestones(repo.Owner.UserName, repo.Name, ListMilestoneOption{State: StateAll})
|
||||
ml, _, err = c.ListRepoMilestones(repo.Owner.UserName, repo.Name, ListMilestoneOption{State: StateAll})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, ml, 3)
|
||||
ml, _, err = c.ListRepoMilestones(repo.Owner.UserName, repo.Name, ListMilestoneOption{State: StateAll, Name: "V3.0"})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, ml, 1)
|
||||
assert.EqualValues(t, "v3.0", ml[0].Title)
|
||||
|
||||
// test fallback resolveMilestoneByName
|
||||
m, _, err := c.resolveMilestoneByName(repo.Owner.UserName, repo.Name, "V3.0")
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, ml[0].ID, m.ID)
|
||||
m, _, err = c.resolveMilestoneByName(repo.Owner.UserName, repo.Name, "NoEvidenceOfExist")
|
||||
assert.Error(t, err)
|
||||
assert.EqualValues(t, "milestone 'NoEvidenceOfExist' do not exist", err.Error())
|
||||
|
||||
// GetMilestone
|
||||
_, err = c.GetMilestone(repo.Owner.UserName, repo.Name, m4.ID)
|
||||
_, _, err = c.GetMilestone(repo.Owner.UserName, repo.Name, m4.ID)
|
||||
assert.Error(t, err)
|
||||
m, err := c.GetMilestone(repo.Owner.UserName, repo.Name, m1.ID)
|
||||
m, _, err = c.GetMilestone(repo.Owner.UserName, repo.Name, m1.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, m1, m)
|
||||
m2, _, err := c.GetMilestoneByName(repo.Owner.UserName, repo.Name, m.Title)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, m, m2)
|
||||
}
|
||||
|
|
|
@ -19,21 +19,17 @@ type Reaction struct {
|
|||
}
|
||||
|
||||
// GetIssueReactions get a list reactions of an issue
|
||||
func (c *Client) GetIssueReactions(owner, repo string, index int64) ([]*Reaction, error) {
|
||||
if err := c.CheckServerVersionConstraint(">=1.11.0"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
func (c *Client) GetIssueReactions(owner, repo string, index int64) ([]*Reaction, *Response, error) {
|
||||
reactions := make([]*Reaction, 0, 10)
|
||||
return reactions, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/reactions", owner, repo, index), nil, nil, &reactions)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/reactions", owner, repo, index), nil, nil, &reactions)
|
||||
return reactions, resp, err
|
||||
}
|
||||
|
||||
// GetIssueCommentReactions get a list of reactions from a comment of an issue
|
||||
func (c *Client) GetIssueCommentReactions(owner, repo string, commentID int64) ([]*Reaction, error) {
|
||||
if err := c.CheckServerVersionConstraint(">=1.11.0"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
func (c *Client) GetIssueCommentReactions(owner, repo string, commentID int64) ([]*Reaction, *Response, error) {
|
||||
reactions := make([]*Reaction, 0, 10)
|
||||
return reactions, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/comments/%d/reactions", owner, repo, commentID), nil, nil, &reactions)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/comments/%d/reactions", owner, repo, commentID), nil, nil, &reactions)
|
||||
return reactions, resp, err
|
||||
}
|
||||
|
||||
// editReactionOption contain the reaction type
|
||||
|
@ -42,57 +38,49 @@ type editReactionOption struct {
|
|||
}
|
||||
|
||||
// PostIssueReaction add a reaction to an issue
|
||||
func (c *Client) PostIssueReaction(owner, repo string, index int64, reaction string) (*Reaction, error) {
|
||||
if err := c.CheckServerVersionConstraint(">=1.11.0"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
func (c *Client) PostIssueReaction(owner, repo string, index int64, reaction string) (*Reaction, *Response, error) {
|
||||
reactionResponse := new(Reaction)
|
||||
body, err := json.Marshal(&editReactionOption{Reaction: reaction})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
return reactionResponse, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues/%d/reactions", owner, repo, index),
|
||||
resp, err := c.getParsedResponse("POST",
|
||||
fmt.Sprintf("/repos/%s/%s/issues/%d/reactions", owner, repo, index),
|
||||
jsonHeader, bytes.NewReader(body), reactionResponse)
|
||||
return reactionResponse, resp, err
|
||||
}
|
||||
|
||||
// DeleteIssueReaction remove a reaction from an issue
|
||||
func (c *Client) DeleteIssueReaction(owner, repo string, index int64, reaction string) error {
|
||||
if err := c.CheckServerVersionConstraint(">=1.11.0"); err != nil {
|
||||
return err
|
||||
}
|
||||
func (c *Client) DeleteIssueReaction(owner, repo string, index int64, reaction string) (*Response, error) {
|
||||
body, err := json.Marshal(&editReactionOption{Reaction: reaction})
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
_, err = c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/reactions", owner, repo, index), jsonHeader, bytes.NewReader(body))
|
||||
return err
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/reactions", owner, repo, index), jsonHeader, bytes.NewReader(body))
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// PostIssueCommentReaction add a reaction to a comment of an issue
|
||||
func (c *Client) PostIssueCommentReaction(owner, repo string, commentID int64, reaction string) (*Reaction, error) {
|
||||
if err := c.CheckServerVersionConstraint(">=1.11.0"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
func (c *Client) PostIssueCommentReaction(owner, repo string, commentID int64, reaction string) (*Reaction, *Response, error) {
|
||||
reactionResponse := new(Reaction)
|
||||
body, err := json.Marshal(&editReactionOption{Reaction: reaction})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
return reactionResponse, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues/comments/%d/reactions", owner, repo, commentID),
|
||||
resp, err := c.getParsedResponse("POST",
|
||||
fmt.Sprintf("/repos/%s/%s/issues/comments/%d/reactions", owner, repo, commentID),
|
||||
jsonHeader, bytes.NewReader(body), reactionResponse)
|
||||
return reactionResponse, resp, err
|
||||
}
|
||||
|
||||
// DeleteIssueCommentReaction remove a reaction from a comment of an issue
|
||||
func (c *Client) DeleteIssueCommentReaction(owner, repo string, commentID int64, reaction string) error {
|
||||
if err := c.CheckServerVersionConstraint(">=1.11.0"); err != nil {
|
||||
return err
|
||||
}
|
||||
// swagger:operation DELETE /repos/{owner}/{repo}/issues/comments/{id}/reactions issue issueDeleteCommentReaction
|
||||
func (c *Client) DeleteIssueCommentReaction(owner, repo string, commentID int64, reaction string) (*Response, error) {
|
||||
body, err := json.Marshal(&editReactionOption{Reaction: reaction})
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
_, err = c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/comments/%d/reactions", owner, repo, commentID),
|
||||
_, resp, err := c.getResponse("DELETE",
|
||||
fmt.Sprintf("/repos/%s/%s/issues/comments/%d/reactions", owner, repo, commentID),
|
||||
jsonHeader, bytes.NewReader(body))
|
||||
return err
|
||||
return resp, err
|
||||
}
|
||||
|
|
|
@ -16,27 +16,28 @@ type StopWatch struct {
|
|||
}
|
||||
|
||||
// GetMyStopwatches list all stopwatches
|
||||
func (c *Client) GetMyStopwatches() ([]*StopWatch, error) {
|
||||
func (c *Client) GetMyStopwatches() ([]*StopWatch, *Response, error) {
|
||||
stopwatches := make([]*StopWatch, 0, 1)
|
||||
return stopwatches, c.getParsedResponse("GET", "/user/stopwatches", nil, nil, &stopwatches)
|
||||
resp, err := c.getParsedResponse("GET", "/user/stopwatches", nil, nil, &stopwatches)
|
||||
return stopwatches, resp, err
|
||||
}
|
||||
|
||||
// DeleteIssueStopwatch delete / cancel a specific stopwatch
|
||||
func (c *Client) DeleteIssueStopwatch(owner, repo string, index int64) error {
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/stopwatch/delete", owner, repo, index), nil, nil)
|
||||
return err
|
||||
func (c *Client) DeleteIssueStopwatch(owner, repo string, index int64) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/stopwatch/delete", owner, repo, index), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// StartIssueStopWatch starts a stopwatch for an existing issue for a given
|
||||
// repository
|
||||
func (c *Client) StartIssueStopWatch(owner, repo string, index int64) error {
|
||||
_, err := c.getResponse("POST", fmt.Sprintf("/repos/%s/%s/issues/%d/stopwatch/start", owner, repo, index), nil, nil)
|
||||
return err
|
||||
func (c *Client) StartIssueStopWatch(owner, repo string, index int64) (*Response, error) {
|
||||
_, resp, err := c.getResponse("POST", fmt.Sprintf("/repos/%s/%s/issues/%d/stopwatch/start", owner, repo, index), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// StopIssueStopWatch stops an existing stopwatch for an issue in a given
|
||||
// repository
|
||||
func (c *Client) StopIssueStopWatch(owner, repo string, index int64) error {
|
||||
_, err := c.getResponse("POST", fmt.Sprintf("/repos/%s/%s/issues/%d/stopwatch/stop", owner, repo, index), nil, nil)
|
||||
return err
|
||||
func (c *Client) StopIssueStopWatch(owner, repo string, index int64) (*Response, error) {
|
||||
_, resp, err := c.getResponse("POST", fmt.Sprintf("/repos/%s/%s/issues/%d/stopwatch/stop", owner, repo, index), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
|
|
@ -10,73 +10,66 @@ import (
|
|||
)
|
||||
|
||||
// GetIssueSubscribers get list of users who subscribed on an issue
|
||||
func (c *Client) GetIssueSubscribers(owner, repo string, index int64) ([]*User, error) {
|
||||
if err := c.CheckServerVersionConstraint(">=1.11.0"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
func (c *Client) GetIssueSubscribers(owner, repo string, index int64) ([]*User, *Response, error) {
|
||||
subscribers := make([]*User, 0, 10)
|
||||
return subscribers, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/subscriptions", owner, repo, index), nil, nil, &subscribers)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/subscriptions", owner, repo, index), nil, nil, &subscribers)
|
||||
return subscribers, resp, err
|
||||
}
|
||||
|
||||
// AddIssueSubscription Subscribe user to issue
|
||||
func (c *Client) AddIssueSubscription(owner, repo string, index int64, user string) error {
|
||||
if err := c.CheckServerVersionConstraint(">=1.11.0"); err != nil {
|
||||
return err
|
||||
}
|
||||
status, err := c.getStatusCode("PUT", fmt.Sprintf("/repos/%s/%s/issues/%d/subscriptions/%s", owner, repo, index, user), nil, nil)
|
||||
func (c *Client) AddIssueSubscription(owner, repo string, index int64, user string) (*Response, error) {
|
||||
status, resp, err := c.getStatusCode("PUT", fmt.Sprintf("/repos/%s/%s/issues/%d/subscriptions/%s", owner, repo, index, user), nil, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
return resp, err
|
||||
}
|
||||
if status == http.StatusCreated {
|
||||
return nil
|
||||
return resp, nil
|
||||
}
|
||||
if status == http.StatusOK {
|
||||
return fmt.Errorf("already subscribed")
|
||||
return resp, fmt.Errorf("already subscribed")
|
||||
}
|
||||
return fmt.Errorf("unexpected Status: %d", status)
|
||||
return resp, fmt.Errorf("unexpected Status: %d", status)
|
||||
}
|
||||
|
||||
// DeleteIssueSubscription unsubscribe user from issue
|
||||
func (c *Client) DeleteIssueSubscription(owner, repo string, index int64, user string) error {
|
||||
if err := c.CheckServerVersionConstraint(">=1.11.0"); err != nil {
|
||||
return err
|
||||
}
|
||||
status, err := c.getStatusCode("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/subscriptions/%s", owner, repo, index, user), nil, nil)
|
||||
func (c *Client) DeleteIssueSubscription(owner, repo string, index int64, user string) (*Response, error) {
|
||||
status, resp, err := c.getStatusCode("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/subscriptions/%s", owner, repo, index, user), nil, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
return resp, err
|
||||
}
|
||||
if status == http.StatusCreated {
|
||||
return nil
|
||||
return resp, nil
|
||||
}
|
||||
if status == http.StatusOK {
|
||||
return fmt.Errorf("already unsubscribed")
|
||||
return resp, fmt.Errorf("already unsubscribed")
|
||||
}
|
||||
return fmt.Errorf("unexpected Status: %d", status)
|
||||
return resp, fmt.Errorf("unexpected Status: %d", status)
|
||||
}
|
||||
|
||||
// CheckIssueSubscription check if current user is subscribed to an issue
|
||||
func (c *Client) CheckIssueSubscription(owner, repo string, index int64) (*WatchInfo, error) {
|
||||
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
||||
return nil, err
|
||||
func (c *Client) CheckIssueSubscription(owner, repo string, index int64) (*WatchInfo, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
wi := new(WatchInfo)
|
||||
return wi, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/subscriptions/check", owner, repo, index), nil, nil, wi)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/subscriptions/check", owner, repo, index), nil, nil, wi)
|
||||
return wi, resp, err
|
||||
}
|
||||
|
||||
// IssueSubscribe subscribe current user to an issue
|
||||
func (c *Client) IssueSubscribe(owner, repo string, index int64) error {
|
||||
u, err := c.GetMyUserInfo()
|
||||
func (c *Client) IssueSubscribe(owner, repo string, index int64) (*Response, error) {
|
||||
u, _, err := c.GetMyUserInfo()
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
return c.AddIssueSubscription(owner, repo, index, u.UserName)
|
||||
}
|
||||
|
||||
// IssueUnSubscribe unsubscribe current user from an issue
|
||||
func (c *Client) IssueUnSubscribe(owner, repo string, index int64) error {
|
||||
u, err := c.GetMyUserInfo()
|
||||
func (c *Client) IssueUnSubscribe(owner, repo string, index int64) (*Response, error) {
|
||||
u, _, err := c.GetMyUserInfo()
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
return c.DeleteIssueSubscription(owner, repo, index, u.UserName)
|
||||
}
|
||||
|
|
|
@ -20,30 +20,33 @@ func TestIssueSubscription(t *testing.T) {
|
|||
repo, _ := createTestRepo(t, "IssueWatch", c)
|
||||
createTestIssue(t, c, repo.Name, "First Issue", "", nil, nil, 0, nil, false, false)
|
||||
|
||||
wi, err := c.CheckIssueSubscription(repo.Owner.UserName, repo.Name, 1)
|
||||
wi, _, err := c.CheckIssueSubscription(repo.Owner.UserName, repo.Name, 1)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, wi.Subscribed)
|
||||
|
||||
assert.NoError(t, c.UnWatchRepo(repo.Owner.UserName, repo.Name))
|
||||
wi, err = c.CheckIssueSubscription(repo.Owner.UserName, repo.Name, 1)
|
||||
_, err = c.UnWatchRepo(repo.Owner.UserName, repo.Name)
|
||||
assert.NoError(t, err)
|
||||
wi, _, err = c.CheckIssueSubscription(repo.Owner.UserName, repo.Name, 1)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, wi.Subscribed)
|
||||
|
||||
err = c.IssueSubscribe(repo.Owner.UserName, repo.Name, 1)
|
||||
_, err = c.IssueSubscribe(repo.Owner.UserName, repo.Name, 1)
|
||||
if assert.Error(t, err) {
|
||||
assert.EqualValues(t, "already subscribed", err.Error())
|
||||
}
|
||||
wi, err = c.CheckIssueSubscription(repo.Owner.UserName, repo.Name, 1)
|
||||
wi, _, err = c.CheckIssueSubscription(repo.Owner.UserName, repo.Name, 1)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, wi.Subscribed)
|
||||
|
||||
assert.NoError(t, c.IssueUnSubscribe(repo.Owner.UserName, repo.Name, 1))
|
||||
wi, err = c.CheckIssueSubscription(repo.Owner.UserName, repo.Name, 1)
|
||||
_, err = c.IssueUnSubscribe(repo.Owner.UserName, repo.Name, 1)
|
||||
assert.NoError(t, err)
|
||||
wi, _, err = c.CheckIssueSubscription(repo.Owner.UserName, repo.Name, 1)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, wi.Subscribed)
|
||||
|
||||
assert.NoError(t, c.WatchRepo(repo.Owner.UserName, repo.Name))
|
||||
wi, err = c.CheckIssueSubscription(repo.Owner.UserName, repo.Name, 1)
|
||||
_, err = c.WatchRepo(repo.Owner.UserName, repo.Name)
|
||||
assert.NoError(t, err)
|
||||
wi, _, err = c.CheckIssueSubscription(repo.Owner.UserName, repo.Name, 1)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, wi.Subscribed)
|
||||
}
|
||||
|
|
|
@ -27,14 +27,14 @@ func TestIssue(t *testing.T) {
|
|||
func createIssue(t *testing.T, c *Client) {
|
||||
log.Println("== TestCreateIssues ==")
|
||||
|
||||
user, err := c.GetMyUserInfo()
|
||||
user, _, err := c.GetMyUserInfo()
|
||||
assert.NoError(t, err)
|
||||
repo, _ := createTestRepo(t, "IssueTestsRepo", c)
|
||||
|
||||
nowTime := time.Now()
|
||||
mile, _ := c.CreateMilestone(user.UserName, repo.Name, CreateMilestoneOption{Title: "mile1"})
|
||||
label1, _ := c.CreateLabel(user.UserName, repo.Name, CreateLabelOption{Name: "Label1", Description: "a", Color: "#ee0701"})
|
||||
label2, _ := c.CreateLabel(user.UserName, repo.Name, CreateLabelOption{Name: "Label2", Description: "b", Color: "#128a0c"})
|
||||
mile, _, _ := c.CreateMilestone(user.UserName, repo.Name, CreateMilestoneOption{Title: "mile1"})
|
||||
label1, _, _ := c.CreateLabel(user.UserName, repo.Name, CreateLabelOption{Name: "Label1", Description: "a", Color: "#ee0701"})
|
||||
label2, _, _ := c.CreateLabel(user.UserName, repo.Name, CreateLabelOption{Name: "Label2", Description: "b", Color: "#128a0c"})
|
||||
|
||||
createTestIssue(t, c, repo.Name, "First Issue", "", nil, nil, 0, nil, false, false)
|
||||
createTestIssue(t, c, repo.Name, "Issue 2", "closed isn't it?", nil, nil, 0, nil, true, false)
|
||||
|
@ -49,28 +49,27 @@ func createIssue(t *testing.T, c *Client) {
|
|||
|
||||
func editIssues(t *testing.T, c *Client) {
|
||||
log.Println("== TestEditIssues ==")
|
||||
il, err := c.ListIssues(ListIssueOption{KeyWord: "soon"})
|
||||
il, _, err := c.ListIssues(ListIssueOption{KeyWord: "soon"})
|
||||
assert.NoError(t, err)
|
||||
issue, err := c.GetIssue(il[0].Poster.UserName, il[0].Repository.Name, il[0].Index)
|
||||
issue, _, err := c.GetIssue(il[0].Poster.UserName, il[0].Repository.Name, il[0].Index)
|
||||
assert.NoError(t, err)
|
||||
|
||||
body := "123 test and go"
|
||||
state := StateClosed
|
||||
issueNew, err := c.EditIssue(issue.Poster.UserName, issue.Repository.Name, issue.Index, EditIssueOption{
|
||||
issueNew, _, err := c.EditIssue(issue.Poster.UserName, issue.Repository.Name, issue.Index, EditIssueOption{
|
||||
Title: "Edited",
|
||||
Body: &body,
|
||||
Body: OptionalString("123 test and go"),
|
||||
State: &state,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, issue.ID, issueNew.ID)
|
||||
assert.EqualValues(t, body, issueNew.Body)
|
||||
assert.EqualValues(t, "123 test and go", issueNew.Body)
|
||||
assert.EqualValues(t, "Edited", issueNew.Title)
|
||||
}
|
||||
|
||||
func listIssues(t *testing.T, c *Client) {
|
||||
log.Println("== TestListIssues ==")
|
||||
|
||||
issues, err := c.ListRepoIssues("test01", "IssueTestsRepo", ListIssueOption{
|
||||
issues, _, err := c.ListRepoIssues("test01", "IssueTestsRepo", ListIssueOption{
|
||||
Labels: []string{"Label1", "Label2"},
|
||||
KeyWord: "",
|
||||
State: "all",
|
||||
|
@ -78,7 +77,7 @@ func listIssues(t *testing.T, c *Client) {
|
|||
assert.NoError(t, err)
|
||||
assert.Len(t, issues, 1)
|
||||
|
||||
issues, err = c.ListIssues(ListIssueOption{
|
||||
issues, _, err = c.ListIssues(ListIssueOption{
|
||||
Labels: []string{"Label2"},
|
||||
KeyWord: "Done",
|
||||
State: "all",
|
||||
|
@ -86,7 +85,7 @@ func listIssues(t *testing.T, c *Client) {
|
|||
assert.NoError(t, err)
|
||||
assert.Len(t, issues, 1)
|
||||
|
||||
issues, err = c.ListRepoIssues("test01", "IssueTestsRepo", ListIssueOption{
|
||||
issues, _, err = c.ListRepoIssues("test01", "IssueTestsRepo", ListIssueOption{
|
||||
Milestones: []string{"mile1"},
|
||||
State: "all",
|
||||
})
|
||||
|
@ -98,15 +97,15 @@ func listIssues(t *testing.T, c *Client) {
|
|||
}
|
||||
}
|
||||
|
||||
issues, err = c.ListRepoIssues("test01", "IssueTestsRepo", ListIssueOption{})
|
||||
issues, _, err = c.ListRepoIssues("test01", "IssueTestsRepo", ListIssueOption{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, issues, 3)
|
||||
}
|
||||
|
||||
func createTestIssue(t *testing.T, c *Client, repoName, title, body string, assignees []string, deadline *time.Time, milestone int64, labels []int64, closed, shouldFail bool) {
|
||||
user, err := c.GetMyUserInfo()
|
||||
user, _, err := c.GetMyUserInfo()
|
||||
assert.NoError(t, err)
|
||||
issue, e := c.CreateIssue(user.UserName, repoName, CreateIssueOption{
|
||||
issue, _, e := c.CreateIssue(user.UserName, repoName, CreateIssueOption{
|
||||
Title: title,
|
||||
Body: body,
|
||||
Assignees: assignees,
|
||||
|
|
|
@ -26,21 +26,24 @@ type TrackedTime struct {
|
|||
}
|
||||
|
||||
// GetUserTrackedTimes list tracked times of a user
|
||||
func (c *Client) GetUserTrackedTimes(owner, repo, user string) ([]*TrackedTime, error) {
|
||||
func (c *Client) GetUserTrackedTimes(owner, repo, user string) ([]*TrackedTime, *Response, error) {
|
||||
times := make([]*TrackedTime, 0, 10)
|
||||
return times, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/times/%s", owner, repo, user), nil, nil, ×)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/times/%s", owner, repo, user), nil, nil, ×)
|
||||
return times, resp, err
|
||||
}
|
||||
|
||||
// GetRepoTrackedTimes list tracked times of a repository
|
||||
func (c *Client) GetRepoTrackedTimes(owner, repo string) ([]*TrackedTime, error) {
|
||||
func (c *Client) GetRepoTrackedTimes(owner, repo string) ([]*TrackedTime, *Response, error) {
|
||||
times := make([]*TrackedTime, 0, 10)
|
||||
return times, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/times", owner, repo), nil, nil, ×)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/times", owner, repo), nil, nil, ×)
|
||||
return times, resp, err
|
||||
}
|
||||
|
||||
// GetMyTrackedTimes list tracked times of the current user
|
||||
func (c *Client) GetMyTrackedTimes() ([]*TrackedTime, error) {
|
||||
func (c *Client) GetMyTrackedTimes() ([]*TrackedTime, *Response, error) {
|
||||
times := make([]*TrackedTime, 0, 10)
|
||||
return times, c.getParsedResponse("GET", "/user/times", nil, nil, ×)
|
||||
resp, err := c.getParsedResponse("GET", "/user/times", nil, nil, ×)
|
||||
return times, resp, err
|
||||
}
|
||||
|
||||
// AddTimeOption options for adding time to an issue
|
||||
|
@ -62,17 +65,19 @@ func (opt AddTimeOption) Validate() error {
|
|||
}
|
||||
|
||||
// AddTime adds time to issue with the given index
|
||||
func (c *Client) AddTime(owner, repo string, index int64, opt AddTimeOption) (*TrackedTime, error) {
|
||||
func (c *Client) AddTime(owner, repo string, index int64, opt AddTimeOption) (*TrackedTime, *Response, error) {
|
||||
if err := opt.Validate(); err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
t := new(TrackedTime)
|
||||
return t, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues/%d/times", owner, repo, index),
|
||||
resp, err := c.getParsedResponse("POST",
|
||||
fmt.Sprintf("/repos/%s/%s/issues/%d/times", owner, repo, index),
|
||||
jsonHeader, bytes.NewReader(body), t)
|
||||
return t, resp, err
|
||||
}
|
||||
|
||||
// ListTrackedTimesOptions options for listing repository's tracked times
|
||||
|
@ -81,20 +86,21 @@ type ListTrackedTimesOptions struct {
|
|||
}
|
||||
|
||||
// ListTrackedTimes list tracked times of a single issue for a given repository
|
||||
func (c *Client) ListTrackedTimes(owner, repo string, index int64, opt ListTrackedTimesOptions) ([]*TrackedTime, error) {
|
||||
func (c *Client) ListTrackedTimes(owner, repo string, index int64, opt ListTrackedTimesOptions) ([]*TrackedTime, *Response, error) {
|
||||
opt.setDefaults()
|
||||
times := make([]*TrackedTime, 0, opt.PageSize)
|
||||
return times, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/times?%s", owner, repo, index, opt.getURLQuery().Encode()), nil, nil, ×)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/times?%s", owner, repo, index, opt.getURLQuery().Encode()), nil, nil, ×)
|
||||
return times, resp, err
|
||||
}
|
||||
|
||||
// ResetIssueTime reset tracked time of a single issue for a given repository
|
||||
func (c *Client) ResetIssueTime(owner, repo string, index int64) error {
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/times", owner, repo, index), nil, nil)
|
||||
return err
|
||||
func (c *Client) ResetIssueTime(owner, repo string, index int64) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/times", owner, repo, index), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// DeleteTime delete a specific tracked time by id of a single issue for a given repository
|
||||
func (c *Client) DeleteTime(owner, repo string, index, timeID int64) error {
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/times/%d", owner, repo, index, timeID), nil, nil)
|
||||
return err
|
||||
func (c *Client) DeleteTime(owner, repo string, index, timeID int64) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/times/%d", owner, repo, index, timeID), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
|
|
@ -46,7 +46,8 @@ func newTestClient() *Client {
|
|||
client.SetBasicAuth(getGiteaUsername(), getGiteaPassword())
|
||||
return client
|
||||
}
|
||||
return NewClient(getGiteaURL(), getGiteaToken())
|
||||
c, _ := NewClient(getGiteaURL(), SetToken(getGiteaToken()))
|
||||
return c
|
||||
}
|
||||
|
||||
func giteaMasterPath() string {
|
||||
|
|
|
@ -8,6 +8,12 @@ import (
|
|||
"fmt"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/go-version"
|
||||
)
|
||||
|
||||
var (
|
||||
version1_12_3, _ = version.NewVersion("1.12.3")
|
||||
)
|
||||
|
||||
// NotificationThread expose Notification on API
|
||||
|
@ -23,10 +29,11 @@ type NotificationThread struct {
|
|||
|
||||
// NotificationSubject contains the notification subject (Issue/Pull/Commit)
|
||||
type NotificationSubject struct {
|
||||
Title string `json:"title"`
|
||||
URL string `json:"url"`
|
||||
LatestCommentURL string `json:"latest_comment_url"`
|
||||
Type string `json:"type" binding:"In(Issue,Pull,Commit)"`
|
||||
Title string `json:"title"`
|
||||
URL string `json:"url"`
|
||||
LatestCommentURL string `json:"latest_comment_url"`
|
||||
Type string `json:"type"`
|
||||
State StateType `json:"state"`
|
||||
}
|
||||
|
||||
// NotifyStatus notification status type
|
||||
|
@ -74,7 +81,7 @@ func (opt *ListNotificationOptions) QueryEncode() string {
|
|||
// Validate the CreateUserOption struct
|
||||
func (opt ListNotificationOptions) Validate(c *Client) error {
|
||||
if len(opt.Status) != 0 {
|
||||
return c.CheckServerVersionConstraint(">=1.12.3")
|
||||
return c.checkServerVersionGreaterThanOrEqual(version1_12_3)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -97,98 +104,102 @@ func (opt *MarkNotificationOptions) QueryEncode() string {
|
|||
// Validate the CreateUserOption struct
|
||||
func (opt MarkNotificationOptions) Validate(c *Client) error {
|
||||
if len(opt.Status) != 0 || len(opt.ToStatus) != 0 {
|
||||
return c.CheckServerVersionConstraint(">=1.12.3")
|
||||
return c.checkServerVersionGreaterThanOrEqual(version1_12_3)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CheckNotifications list users's notification threads
|
||||
func (c *Client) CheckNotifications() (int64, error) {
|
||||
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
||||
return 0, err
|
||||
func (c *Client) CheckNotifications() (int64, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
new := struct {
|
||||
New int64 `json:"new"`
|
||||
}{}
|
||||
|
||||
return new.New, c.getParsedResponse("GET", "/notifications/new", jsonHeader, nil, &new)
|
||||
resp, err := c.getParsedResponse("GET", "/notifications/new", jsonHeader, nil, &new)
|
||||
return new.New, resp, err
|
||||
}
|
||||
|
||||
// GetNotification get notification thread by ID
|
||||
func (c *Client) GetNotification(id int64) (*NotificationThread, error) {
|
||||
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
||||
return nil, err
|
||||
func (c *Client) GetNotification(id int64) (*NotificationThread, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
thread := new(NotificationThread)
|
||||
return thread, c.getParsedResponse("GET", fmt.Sprintf("/notifications/threads/%d", id), nil, nil, thread)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/notifications/threads/%d", id), nil, nil, thread)
|
||||
return thread, resp, err
|
||||
}
|
||||
|
||||
// ReadNotification mark notification thread as read by ID
|
||||
// It optionally takes a second argument if status has to be set other than 'read'
|
||||
func (c *Client) ReadNotification(id int64, status ...NotifyStatus) error {
|
||||
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
||||
return err
|
||||
func (c *Client) ReadNotification(id int64, status ...NotifyStatus) (*Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
link := fmt.Sprintf("/notifications/threads/%d", id)
|
||||
if len(status) != 0 {
|
||||
link += fmt.Sprintf("?to-status=%s", status[0])
|
||||
}
|
||||
_, err := c.getResponse("PATCH", link, nil, nil)
|
||||
return err
|
||||
_, resp, err := c.getResponse("PATCH", link, nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// ListNotifications list users's notification threads
|
||||
func (c *Client) ListNotifications(opt ListNotificationOptions) ([]*NotificationThread, error) {
|
||||
if err := opt.Validate(c); err != nil {
|
||||
return nil, err
|
||||
func (c *Client) ListNotifications(opt ListNotificationOptions) ([]*NotificationThread, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
||||
return nil, err
|
||||
if err := opt.Validate(c); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
link, _ := url.Parse("/notifications")
|
||||
link.RawQuery = opt.QueryEncode()
|
||||
threads := make([]*NotificationThread, 0, 10)
|
||||
return threads, c.getParsedResponse("GET", link.String(), nil, nil, &threads)
|
||||
resp, err := c.getParsedResponse("GET", link.String(), nil, nil, &threads)
|
||||
return threads, resp, err
|
||||
}
|
||||
|
||||
// ReadNotifications mark notification threads as read
|
||||
func (c *Client) ReadNotifications(opt MarkNotificationOptions) error {
|
||||
if err := opt.Validate(c); err != nil {
|
||||
return err
|
||||
func (c *Client) ReadNotifications(opt MarkNotificationOptions) (*Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
||||
return err
|
||||
if err := opt.Validate(c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
link, _ := url.Parse("/notifications")
|
||||
link.RawQuery = opt.QueryEncode()
|
||||
_, err := c.getResponse("PUT", link.String(), nil, nil)
|
||||
return err
|
||||
_, resp, err := c.getResponse("PUT", link.String(), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// ListRepoNotifications list users's notification threads on a specific repo
|
||||
func (c *Client) ListRepoNotifications(owner, reponame string, opt ListNotificationOptions) ([]*NotificationThread, error) {
|
||||
if err := opt.Validate(c); err != nil {
|
||||
return nil, err
|
||||
func (c *Client) ListRepoNotifications(owner, reponame string, opt ListNotificationOptions) ([]*NotificationThread, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
||||
return nil, err
|
||||
if err := opt.Validate(c); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/notifications", owner, reponame))
|
||||
link.RawQuery = opt.QueryEncode()
|
||||
threads := make([]*NotificationThread, 0, 10)
|
||||
return threads, c.getParsedResponse("GET", link.String(), nil, nil, &threads)
|
||||
resp, err := c.getParsedResponse("GET", link.String(), nil, nil, &threads)
|
||||
return threads, resp, err
|
||||
}
|
||||
|
||||
// ReadRepoNotifications mark notification threads as read on a specific repo
|
||||
func (c *Client) ReadRepoNotifications(owner, reponame string, opt MarkNotificationOptions) error {
|
||||
if err := opt.Validate(c); err != nil {
|
||||
return err
|
||||
func (c *Client) ReadRepoNotifications(owner, reponame string, opt MarkNotificationOptions) (*Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
||||
return err
|
||||
if err := opt.Validate(c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/notifications", owner, reponame))
|
||||
link.RawQuery = opt.QueryEncode()
|
||||
_, err := c.getResponse("PUT", link.String(), nil, nil)
|
||||
return err
|
||||
_, resp, err := c.getResponse("PUT", link.String(), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ func TestNotifications(t *testing.T) {
|
|||
// init user2
|
||||
c := newTestClient()
|
||||
|
||||
user1, err := c.GetMyUserInfo()
|
||||
user1, _, err := c.GetMyUserInfo()
|
||||
assert.NoError(t, err)
|
||||
user2 := createTestUser(t, "notify2", c)
|
||||
|
||||
|
@ -30,30 +30,31 @@ func TestNotifications(t *testing.T) {
|
|||
c.sudo = user2.UserName
|
||||
repoB, err := createTestRepo(t, "TestNotifications_B", c)
|
||||
assert.NoError(t, err)
|
||||
err = c.WatchRepo(user1.UserName, repoA.Name)
|
||||
_, err = c.WatchRepo(user1.UserName, repoA.Name)
|
||||
c.sudo = ""
|
||||
assert.NoError(t, err)
|
||||
|
||||
c.sudo = user2.UserName
|
||||
assert.NoError(t, c.ReadNotifications(MarkNotificationOptions{}))
|
||||
count, err := c.CheckNotifications()
|
||||
_, err = c.ReadNotifications(MarkNotificationOptions{})
|
||||
assert.NoError(t, err)
|
||||
count, _, err := c.CheckNotifications()
|
||||
assert.EqualValues(t, 0, count)
|
||||
assert.NoError(t, err)
|
||||
c.sudo = ""
|
||||
_, err = c.CreateIssue(repoA.Owner.UserName, repoA.Name, CreateIssueOption{Title: "A Issue", Closed: false})
|
||||
_, _, err = c.CreateIssue(repoA.Owner.UserName, repoA.Name, CreateIssueOption{Title: "A Issue", Closed: false})
|
||||
assert.NoError(t, err)
|
||||
issue, err := c.CreateIssue(repoB.Owner.UserName, repoB.Name, CreateIssueOption{Title: "B Issue", Closed: false})
|
||||
issue, _, err := c.CreateIssue(repoB.Owner.UserName, repoB.Name, CreateIssueOption{Title: "B Issue", Closed: false})
|
||||
assert.NoError(t, err)
|
||||
time.Sleep(time.Second * 1)
|
||||
|
||||
// CheckNotifications of user2
|
||||
c.sudo = user2.UserName
|
||||
count, err = c.CheckNotifications()
|
||||
count, _, err = c.CheckNotifications()
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 2, count)
|
||||
|
||||
// ListNotifications
|
||||
nList, err := c.ListNotifications(ListNotificationOptions{})
|
||||
nList, _, err := c.ListNotifications(ListNotificationOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, nList, 2)
|
||||
for _, n := range nList {
|
||||
|
@ -69,55 +70,58 @@ func TestNotifications(t *testing.T) {
|
|||
}
|
||||
|
||||
// ListRepoNotifications
|
||||
nList, err = c.ListRepoNotifications(repoA.Owner.UserName, repoA.Name, ListNotificationOptions{})
|
||||
nList, _, err = c.ListRepoNotifications(repoA.Owner.UserName, repoA.Name, ListNotificationOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, nList, 1)
|
||||
assert.EqualValues(t, "A Issue", nList[0].Subject.Title)
|
||||
// ReadRepoNotifications
|
||||
err = c.ReadRepoNotifications(repoA.Owner.UserName, repoA.Name, MarkNotificationOptions{})
|
||||
_, err = c.ReadRepoNotifications(repoA.Owner.UserName, repoA.Name, MarkNotificationOptions{})
|
||||
assert.NoError(t, err)
|
||||
|
||||
// GetThread
|
||||
n, err := c.GetNotification(nList[0].ID)
|
||||
n, _, err := c.GetNotification(nList[0].ID)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, false, n.Unread)
|
||||
assert.EqualValues(t, "A Issue", n.Subject.Title)
|
||||
|
||||
// ReadNotifications
|
||||
err = c.ReadNotifications(MarkNotificationOptions{})
|
||||
_, err = c.ReadNotifications(MarkNotificationOptions{})
|
||||
assert.NoError(t, err)
|
||||
nList, err = c.ListNotifications(ListNotificationOptions{})
|
||||
nList, _, err = c.ListNotifications(ListNotificationOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, nList, 0)
|
||||
|
||||
// ReadThread
|
||||
iState := StateClosed
|
||||
c.sudo = ""
|
||||
_, err = c.EditIssue(repoB.Owner.UserName, repoB.Name, issue.Index, EditIssueOption{State: &iState})
|
||||
_, _, err = c.EditIssue(repoB.Owner.UserName, repoB.Name, issue.Index, EditIssueOption{State: &iState})
|
||||
assert.NoError(t, err)
|
||||
time.Sleep(time.Second * 1)
|
||||
|
||||
c.sudo = user2.UserName
|
||||
nList, err = c.ListNotifications(ListNotificationOptions{})
|
||||
nList, _, err = c.ListNotifications(ListNotificationOptions{})
|
||||
assert.NoError(t, err)
|
||||
count, err = c.CheckNotifications()
|
||||
count, _, err = c.CheckNotifications()
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 1, count)
|
||||
assert.Len(t, nList, 1)
|
||||
if len(nList) > 0 {
|
||||
assert.NoError(t, c.ReadNotification(nList[0].ID))
|
||||
_, err = c.ReadNotification(nList[0].ID)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
c.sudo = ""
|
||||
err = c.ReadNotifications(MarkNotificationOptions{})
|
||||
_, err = c.ReadNotifications(MarkNotificationOptions{})
|
||||
assert.NoError(t, err)
|
||||
nList, err = c.ListNotifications(ListNotificationOptions{Status: []NotifyStatus{NotifyStatusRead}})
|
||||
nList, _, err = c.ListNotifications(ListNotificationOptions{Status: []NotifyStatus{NotifyStatusRead}})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, nList, 4)
|
||||
|
||||
assert.NoError(t, c.ReadNotification(nList[2].ID, NotifyStatusPinned))
|
||||
assert.NoError(t, c.ReadNotification(nList[3].ID, NotifyStatusUnread))
|
||||
nList, err = c.ListNotifications(ListNotificationOptions{Status: []NotifyStatus{NotifyStatusPinned, NotifyStatusUnread}})
|
||||
_, err = c.ReadNotification(nList[2].ID, NotifyStatusPinned)
|
||||
assert.NoError(t, err)
|
||||
_, err = c.ReadNotification(nList[3].ID, NotifyStatusUnread)
|
||||
assert.NoError(t, err)
|
||||
nList, _, err = c.ListNotifications(ListNotificationOptions{Status: []NotifyStatus{NotifyStatusPinned, NotifyStatusUnread}})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, nList, 2)
|
||||
}
|
||||
|
|
|
@ -33,55 +33,59 @@ type CreateOauth2Option struct {
|
|||
}
|
||||
|
||||
// CreateOauth2 create an Oauth2 Application and returns a completed Oauth2 object.
|
||||
func (c *Client) CreateOauth2(opt CreateOauth2Option) (*Oauth2, error) {
|
||||
if e := c.CheckServerVersionConstraint(">=1.12.0"); e != nil {
|
||||
return nil, e
|
||||
func (c *Client) CreateOauth2(opt CreateOauth2Option) (*Oauth2, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
oauth := new(Oauth2)
|
||||
return oauth, c.getParsedResponse("POST", "/user/applications/oauth2", jsonHeader, bytes.NewReader(body), oauth)
|
||||
resp, err := c.getParsedResponse("POST", "/user/applications/oauth2", jsonHeader, bytes.NewReader(body), oauth)
|
||||
return oauth, resp, err
|
||||
}
|
||||
|
||||
// UpdateOauth2 a specific Oauth2 Application by ID and return a completed Oauth2 object.
|
||||
func (c *Client) UpdateOauth2(oauth2id int64, opt CreateOauth2Option) (*Oauth2, error) {
|
||||
if e := c.CheckServerVersionConstraint(">=1.12.0"); e != nil {
|
||||
return nil, e
|
||||
func (c *Client) UpdateOauth2(oauth2id int64, opt CreateOauth2Option) (*Oauth2, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
oauth := new(Oauth2)
|
||||
return oauth, c.getParsedResponse("PATCH", fmt.Sprintf("/user/applications/oauth2/%d", oauth2id), jsonHeader, bytes.NewReader(body), oauth)
|
||||
resp, err := c.getParsedResponse("PATCH", fmt.Sprintf("/user/applications/oauth2/%d", oauth2id), jsonHeader, bytes.NewReader(body), oauth)
|
||||
return oauth, resp, err
|
||||
}
|
||||
|
||||
// GetOauth2 a specific Oauth2 Application by ID.
|
||||
func (c *Client) GetOauth2(oauth2id int64) (*Oauth2, error) {
|
||||
if e := c.CheckServerVersionConstraint(">=1.12.0"); e != nil {
|
||||
return nil, e
|
||||
func (c *Client) GetOauth2(oauth2id int64) (*Oauth2, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
oauth2s := &Oauth2{}
|
||||
return oauth2s, c.getParsedResponse("GET", fmt.Sprintf("/user/applications/oauth2/%d", oauth2id), nil, nil, &oauth2s)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/user/applications/oauth2/%d", oauth2id), nil, nil, &oauth2s)
|
||||
return oauth2s, resp, err
|
||||
}
|
||||
|
||||
// ListOauth2 all of your Oauth2 Applications.
|
||||
func (c *Client) ListOauth2(opt ListOauth2Option) ([]*Oauth2, error) {
|
||||
if e := c.CheckServerVersionConstraint(">=1.12.0"); e != nil {
|
||||
return nil, e
|
||||
func (c *Client) ListOauth2(opt ListOauth2Option) ([]*Oauth2, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
opt.setDefaults()
|
||||
oauth2s := make([]*Oauth2, 0, opt.PageSize)
|
||||
return oauth2s, c.getParsedResponse("GET", fmt.Sprintf("/user/applications/oauth2?%s", opt.getURLQuery().Encode()), nil, nil, &oauth2s)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/user/applications/oauth2?%s", opt.getURLQuery().Encode()), nil, nil, &oauth2s)
|
||||
return oauth2s, resp, err
|
||||
}
|
||||
|
||||
// DeleteOauth2 delete an Oauth2 application by ID
|
||||
func (c *Client) DeleteOauth2(oauth2id int64) error {
|
||||
if e := c.CheckServerVersionConstraint(">=1.12.0"); e != nil {
|
||||
return e
|
||||
func (c *Client) DeleteOauth2(oauth2id int64) (*Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/user/applications/oauth2/%d", oauth2id), nil, nil)
|
||||
return err
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/user/applications/oauth2/%d", oauth2id), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
|
|
@ -18,26 +18,27 @@ func TestOauth2(t *testing.T) {
|
|||
user := createTestUser(t, "oauth2_user", c)
|
||||
c.SetSudo(user.UserName)
|
||||
|
||||
newApp, err := c.CreateOauth2(CreateOauth2Option{Name: "test", RedirectURIs: []string{"http://test/test"}})
|
||||
newApp, _, err := c.CreateOauth2(CreateOauth2Option{Name: "test", RedirectURIs: []string{"http://test/test"}})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, newApp)
|
||||
assert.EqualValues(t, "test", newApp.Name)
|
||||
|
||||
a, err := c.ListOauth2(ListOauth2Option{})
|
||||
a, _, err := c.ListOauth2(ListOauth2Option{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, a, 1)
|
||||
assert.EqualValues(t, newApp.Name, a[0].Name)
|
||||
|
||||
b, err := c.GetOauth2(newApp.ID)
|
||||
b, _, err := c.GetOauth2(newApp.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, newApp.Name, b.Name)
|
||||
|
||||
b, err = c.UpdateOauth2(newApp.ID, CreateOauth2Option{Name: newApp.Name, RedirectURIs: []string{"https://test/login"}})
|
||||
b, _, err = c.UpdateOauth2(newApp.ID, CreateOauth2Option{Name: newApp.Name, RedirectURIs: []string{"https://test/login"}})
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, newApp.Name, b.Name)
|
||||
assert.EqualValues(t, "https://test/login", b.RedirectURIs[0])
|
||||
assert.EqualValues(t, newApp.ID, b.ID)
|
||||
assert.NotEqual(t, newApp.ClientSecret, b.ClientSecret)
|
||||
|
||||
assert.NoError(t, c.DeleteOauth2(newApp.ID))
|
||||
_, err = c.DeleteOauth2(newApp.ID)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
|
44
gitea/org.go
44
gitea/org.go
|
@ -43,28 +43,31 @@ type ListOrgsOptions struct {
|
|||
}
|
||||
|
||||
// ListMyOrgs list all of current user's organizations
|
||||
func (c *Client) ListMyOrgs(opt ListOrgsOptions) ([]*Organization, error) {
|
||||
func (c *Client) ListMyOrgs(opt ListOrgsOptions) ([]*Organization, *Response, error) {
|
||||
opt.setDefaults()
|
||||
orgs := make([]*Organization, 0, opt.PageSize)
|
||||
return orgs, c.getParsedResponse("GET", fmt.Sprintf("/user/orgs?%s", opt.getURLQuery().Encode()), nil, nil, &orgs)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/user/orgs?%s", opt.getURLQuery().Encode()), nil, nil, &orgs)
|
||||
return orgs, resp, err
|
||||
}
|
||||
|
||||
// ListUserOrgs list all of some user's organizations
|
||||
func (c *Client) ListUserOrgs(user string, opt ListOrgsOptions) ([]*Organization, error) {
|
||||
func (c *Client) ListUserOrgs(user string, opt ListOrgsOptions) ([]*Organization, *Response, error) {
|
||||
opt.setDefaults()
|
||||
orgs := make([]*Organization, 0, opt.PageSize)
|
||||
return orgs, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/orgs?%s", user, opt.getURLQuery().Encode()), nil, nil, &orgs)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/users/%s/orgs?%s", user, opt.getURLQuery().Encode()), nil, nil, &orgs)
|
||||
return orgs, resp, err
|
||||
}
|
||||
|
||||
// GetOrg get one organization by name
|
||||
func (c *Client) GetOrg(orgname string) (*Organization, error) {
|
||||
func (c *Client) GetOrg(orgname string) (*Organization, *Response, error) {
|
||||
org := new(Organization)
|
||||
return org, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s", orgname), nil, nil, org)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s", orgname), nil, nil, org)
|
||||
return org, resp, err
|
||||
}
|
||||
|
||||
// CreateOrgOption options for creating an organization
|
||||
type CreateOrgOption struct {
|
||||
UserName string `json:"username"`
|
||||
Name string `json:"username"`
|
||||
FullName string `json:"full_name"`
|
||||
Description string `json:"description"`
|
||||
Website string `json:"website"`
|
||||
|
@ -79,7 +82,7 @@ func checkVisibilityOpt(v VisibleType) bool {
|
|||
|
||||
// Validate the CreateOrgOption struct
|
||||
func (opt CreateOrgOption) Validate() error {
|
||||
if len(opt.UserName) == 0 {
|
||||
if len(opt.Name) == 0 {
|
||||
return fmt.Errorf("empty org name")
|
||||
}
|
||||
if len(opt.Visibility) != 0 && !checkVisibilityOpt(opt.Visibility) {
|
||||
|
@ -89,16 +92,17 @@ func (opt CreateOrgOption) Validate() error {
|
|||
}
|
||||
|
||||
// CreateOrg creates an organization
|
||||
func (c *Client) CreateOrg(opt CreateOrgOption) (*Organization, error) {
|
||||
func (c *Client) CreateOrg(opt CreateOrgOption) (*Organization, *Response, error) {
|
||||
if err := opt.Validate(); err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
org := new(Organization)
|
||||
return org, c.getParsedResponse("POST", "/orgs", jsonHeader, bytes.NewReader(body), org)
|
||||
resp, err := c.getParsedResponse("POST", "/orgs", jsonHeader, bytes.NewReader(body), org)
|
||||
return org, resp, err
|
||||
}
|
||||
|
||||
// EditOrgOption options for editing an organization
|
||||
|
@ -119,20 +123,20 @@ func (opt EditOrgOption) Validate() error {
|
|||
}
|
||||
|
||||
// EditOrg modify one organization via options
|
||||
func (c *Client) EditOrg(orgname string, opt EditOrgOption) error {
|
||||
func (c *Client) EditOrg(orgname string, opt EditOrgOption) (*Response, error) {
|
||||
if err := opt.Validate(); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
_, err = c.getResponse("PATCH", fmt.Sprintf("/orgs/%s", orgname), jsonHeader, bytes.NewReader(body))
|
||||
return err
|
||||
_, resp, err := c.getResponse("PATCH", fmt.Sprintf("/orgs/%s", orgname), jsonHeader, bytes.NewReader(body))
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// DeleteOrg deletes an organization
|
||||
func (c *Client) DeleteOrg(orgname string) error {
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/orgs/%s", orgname), jsonHeader, nil)
|
||||
return err
|
||||
func (c *Client) DeleteOrg(orgname string) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/orgs/%s", orgname), jsonHeader, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
|
|
@ -11,9 +11,9 @@ import (
|
|||
)
|
||||
|
||||
// DeleteOrgMembership remove a member from an organization
|
||||
func (c *Client) DeleteOrgMembership(org, user string) error {
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/orgs/%s/members/%s", url.PathEscape(org), url.PathEscape(user)), nil, nil)
|
||||
return err
|
||||
func (c *Client) DeleteOrgMembership(org, user string) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/orgs/%s/members/%s", url.PathEscape(org), url.PathEscape(user)), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// ListOrgMembershipOption list OrgMembership options
|
||||
|
@ -22,77 +22,80 @@ type ListOrgMembershipOption struct {
|
|||
}
|
||||
|
||||
// ListOrgMembership list an organization's members
|
||||
func (c *Client) ListOrgMembership(org string, opt ListOrgMembershipOption) ([]*User, error) {
|
||||
func (c *Client) ListOrgMembership(org string, opt ListOrgMembershipOption) ([]*User, *Response, error) {
|
||||
opt.setDefaults()
|
||||
users := make([]*User, 0, opt.PageSize)
|
||||
|
||||
link, _ := url.Parse(fmt.Sprintf("/orgs/%s/members", url.PathEscape(org)))
|
||||
link.RawQuery = opt.getURLQuery().Encode()
|
||||
return users, c.getParsedResponse("GET", link.String(), jsonHeader, nil, &users)
|
||||
resp, err := c.getParsedResponse("GET", link.String(), jsonHeader, nil, &users)
|
||||
return users, resp, err
|
||||
}
|
||||
|
||||
// ListPublicOrgMembership list an organization's members
|
||||
func (c *Client) ListPublicOrgMembership(org string, opt ListOrgMembershipOption) ([]*User, error) {
|
||||
func (c *Client) ListPublicOrgMembership(org string, opt ListOrgMembershipOption) ([]*User, *Response, error) {
|
||||
opt.setDefaults()
|
||||
users := make([]*User, 0, opt.PageSize)
|
||||
|
||||
link, _ := url.Parse(fmt.Sprintf("/orgs/%s/public_members", url.PathEscape(org)))
|
||||
link.RawQuery = opt.getURLQuery().Encode()
|
||||
return users, c.getParsedResponse("GET", link.String(), jsonHeader, nil, &users)
|
||||
resp, err := c.getParsedResponse("GET", link.String(), jsonHeader, nil, &users)
|
||||
return users, resp, err
|
||||
}
|
||||
|
||||
// CheckOrgMembership Check if a user is a member of an organization
|
||||
func (c *Client) CheckOrgMembership(org, user string) (bool, error) {
|
||||
status, err := c.getStatusCode("GET", fmt.Sprintf("/orgs/%s/members/%s", url.PathEscape(org), url.PathEscape(user)), nil, nil)
|
||||
func (c *Client) CheckOrgMembership(org, user string) (bool, *Response, error) {
|
||||
status, resp, err := c.getStatusCode("GET", fmt.Sprintf("/orgs/%s/members/%s", url.PathEscape(org), url.PathEscape(user)), nil, nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, resp, err
|
||||
}
|
||||
switch status {
|
||||
case http.StatusNoContent:
|
||||
return true, nil
|
||||
return true, resp, nil
|
||||
case http.StatusNotFound:
|
||||
return false, nil
|
||||
return false, resp, nil
|
||||
default:
|
||||
return false, fmt.Errorf("unexpected Status: %d", status)
|
||||
return false, resp, fmt.Errorf("unexpected Status: %d", status)
|
||||
}
|
||||
}
|
||||
|
||||
// CheckPublicOrgMembership Check if a user is a member of an organization
|
||||
func (c *Client) CheckPublicOrgMembership(org, user string) (bool, error) {
|
||||
status, err := c.getStatusCode("GET", fmt.Sprintf("/orgs/%s/public_members/%s", url.PathEscape(org), url.PathEscape(user)), nil, nil)
|
||||
func (c *Client) CheckPublicOrgMembership(org, user string) (bool, *Response, error) {
|
||||
status, resp, err := c.getStatusCode("GET", fmt.Sprintf("/orgs/%s/public_members/%s", url.PathEscape(org), url.PathEscape(user)), nil, nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, resp, err
|
||||
}
|
||||
switch status {
|
||||
case http.StatusNoContent:
|
||||
return true, nil
|
||||
return true, resp, nil
|
||||
case http.StatusNotFound:
|
||||
return false, nil
|
||||
return false, resp, nil
|
||||
default:
|
||||
return false, fmt.Errorf("unexpected Status: %d", status)
|
||||
return false, resp, fmt.Errorf("unexpected Status: %d", status)
|
||||
}
|
||||
}
|
||||
|
||||
// SetPublicOrgMembership publicize/conceal a user's membership
|
||||
func (c *Client) SetPublicOrgMembership(org, user string, visible bool) error {
|
||||
func (c *Client) SetPublicOrgMembership(org, user string, visible bool) (*Response, error) {
|
||||
var (
|
||||
status int
|
||||
err error
|
||||
resp *Response
|
||||
)
|
||||
if visible {
|
||||
status, err = c.getStatusCode("PUT", fmt.Sprintf("/orgs/%s/public_members/%s", url.PathEscape(org), url.PathEscape(user)), nil, nil)
|
||||
status, resp, err = c.getStatusCode("PUT", fmt.Sprintf("/orgs/%s/public_members/%s", url.PathEscape(org), url.PathEscape(user)), nil, nil)
|
||||
} else {
|
||||
status, err = c.getStatusCode("DELETE", fmt.Sprintf("/orgs/%s/public_members/%s", url.PathEscape(org), url.PathEscape(user)), nil, nil)
|
||||
status, resp, err = c.getStatusCode("DELETE", fmt.Sprintf("/orgs/%s/public_members/%s", url.PathEscape(org), url.PathEscape(user)), nil, nil)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
return resp, err
|
||||
}
|
||||
switch status {
|
||||
case http.StatusNoContent:
|
||||
return nil
|
||||
return resp, nil
|
||||
case http.StatusNotFound:
|
||||
return fmt.Errorf("forbidden")
|
||||
return resp, fmt.Errorf("forbidden")
|
||||
default:
|
||||
return fmt.Errorf("unexpected Status: %d", status)
|
||||
return resp, fmt.Errorf("unexpected Status: %d", status)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,37 +23,41 @@ func TestOrgMembership(t *testing.T) {
|
|||
|
||||
user := createTestUser(t, "org_mem_user", c)
|
||||
c.SetSudo(user.UserName)
|
||||
newOrg, err := c.CreateOrg(CreateOrgOption{UserName: "MemberOrg"})
|
||||
newOrg, _, err := c.CreateOrg(CreateOrgOption{Name: "MemberOrg"})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, newOrg)
|
||||
|
||||
// Check func
|
||||
check, err := c.CheckPublicOrgMembership(newOrg.UserName, user.UserName)
|
||||
check, _, err := c.CheckPublicOrgMembership(newOrg.UserName, user.UserName)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, check)
|
||||
check, err = c.CheckOrgMembership(newOrg.UserName, user.UserName)
|
||||
check, _, err = c.CheckOrgMembership(newOrg.UserName, user.UserName)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, check)
|
||||
|
||||
err = c.SetPublicOrgMembership(newOrg.UserName, user.UserName, true)
|
||||
_, err = c.SetPublicOrgMembership(newOrg.UserName, user.UserName, true)
|
||||
assert.NoError(t, err)
|
||||
check, err = c.CheckPublicOrgMembership(newOrg.UserName, user.UserName)
|
||||
check, _, err = c.CheckPublicOrgMembership(newOrg.UserName, user.UserName)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, check)
|
||||
|
||||
u, err := c.ListOrgMembership(newOrg.UserName, ListOrgMembershipOption{})
|
||||
u, _, err := c.ListOrgMembership(newOrg.UserName, ListOrgMembershipOption{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, u, 1)
|
||||
assert.EqualValues(t, user.UserName, u[0].UserName)
|
||||
u, err = c.ListPublicOrgMembership(newOrg.UserName, ListOrgMembershipOption{})
|
||||
u, _, err = c.ListPublicOrgMembership(newOrg.UserName, ListOrgMembershipOption{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, u, 1)
|
||||
assert.EqualValues(t, user.UserName, u[0].UserName)
|
||||
|
||||
assert.Error(t, c.DeleteOrgMembership(newOrg.UserName, user.UserName))
|
||||
_, err = c.DeleteOrgMembership(newOrg.UserName, user.UserName)
|
||||
assert.Error(t, err)
|
||||
|
||||
c.sudo = ""
|
||||
assert.Error(t, c.AdminDeleteUser(user.UserName))
|
||||
assert.NoError(t, c.DeleteOrg(newOrg.UserName))
|
||||
assert.NoError(t, c.AdminDeleteUser(user.UserName))
|
||||
_, err = c.AdminDeleteUser(user.UserName)
|
||||
assert.Error(t, err)
|
||||
_, err = c.DeleteOrg(newOrg.UserName)
|
||||
assert.NoError(t, err)
|
||||
_, err = c.AdminDeleteUser(user.UserName)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
|
|
@ -12,12 +12,13 @@ import (
|
|||
|
||||
// Team represents a team in an organization
|
||||
type Team struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Organization *Organization `json:"organization"`
|
||||
// enum: none,read,write,admin,owner
|
||||
Permission string `json:"permission"`
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Organization *Organization `json:"organization"`
|
||||
Permission AccessMode `json:"permission"`
|
||||
CanCreateOrgRepo bool `json:"can_create_org_repo"`
|
||||
IncludesAllRepositories bool `json:"includes_all_repositories"`
|
||||
// example: ["repo.code","repo.issues","repo.ext_issues","repo.wiki","repo.pulls","repo.releases","repo.ext_wiki"]
|
||||
Units []string `json:"units"`
|
||||
}
|
||||
|
@ -28,69 +29,119 @@ type ListTeamsOptions struct {
|
|||
}
|
||||
|
||||
// ListOrgTeams lists all teams of an organization
|
||||
func (c *Client) ListOrgTeams(org string, opt ListTeamsOptions) ([]*Team, error) {
|
||||
func (c *Client) ListOrgTeams(org string, opt ListTeamsOptions) ([]*Team, *Response, error) {
|
||||
opt.setDefaults()
|
||||
teams := make([]*Team, 0, opt.PageSize)
|
||||
return teams, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/teams?%s", org, opt.getURLQuery().Encode()), nil, nil, &teams)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/teams?%s", org, opt.getURLQuery().Encode()), nil, nil, &teams)
|
||||
return teams, resp, err
|
||||
}
|
||||
|
||||
// ListMyTeams lists all the teams of the current user
|
||||
func (c *Client) ListMyTeams(opt *ListTeamsOptions) ([]*Team, error) {
|
||||
func (c *Client) ListMyTeams(opt *ListTeamsOptions) ([]*Team, *Response, error) {
|
||||
opt.setDefaults()
|
||||
teams := make([]*Team, 0, opt.PageSize)
|
||||
return teams, c.getParsedResponse("GET", fmt.Sprintf("/user/teams?%s", opt.getURLQuery().Encode()), nil, nil, &teams)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/user/teams?%s", opt.getURLQuery().Encode()), nil, nil, &teams)
|
||||
return teams, resp, err
|
||||
}
|
||||
|
||||
// GetTeam gets a team by ID
|
||||
func (c *Client) GetTeam(id int64) (*Team, error) {
|
||||
func (c *Client) GetTeam(id int64) (*Team, *Response, error) {
|
||||
t := new(Team)
|
||||
return t, c.getParsedResponse("GET", fmt.Sprintf("/teams/%d", id), nil, nil, t)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/teams/%d", id), nil, nil, t)
|
||||
return t, resp, err
|
||||
}
|
||||
|
||||
// CreateTeamOption options for creating a team
|
||||
type CreateTeamOption struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
// enum: read,write,admin
|
||||
Permission string `json:"permission"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Permission AccessMode `json:"permission"`
|
||||
CanCreateOrgRepo bool `json:"can_create_org_repo"`
|
||||
IncludesAllRepositories bool `json:"includes_all_repositories"`
|
||||
// example: ["repo.code","repo.issues","repo.ext_issues","repo.wiki","repo.pulls","repo.releases","repo.ext_wiki"]
|
||||
Units []string `json:"units"`
|
||||
}
|
||||
|
||||
// Validate the CreateTeamOption struct
|
||||
func (opt CreateTeamOption) Validate() error {
|
||||
if opt.Permission == AccessModeOwner {
|
||||
opt.Permission = AccessModeAdmin
|
||||
} else if opt.Permission != AccessModeRead && opt.Permission != AccessModeWrite && opt.Permission != AccessModeAdmin {
|
||||
return fmt.Errorf("permission mode invalid")
|
||||
}
|
||||
if len(opt.Name) == 0 {
|
||||
return fmt.Errorf("name required")
|
||||
}
|
||||
if len(opt.Name) > 30 {
|
||||
return fmt.Errorf("name to long")
|
||||
}
|
||||
if len(opt.Description) > 255 {
|
||||
return fmt.Errorf("description to long")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateTeam creates a team for an organization
|
||||
func (c *Client) CreateTeam(org string, opt CreateTeamOption) (*Team, error) {
|
||||
func (c *Client) CreateTeam(org string, opt CreateTeamOption) (*Team, *Response, error) {
|
||||
if err := opt.Validate(); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
t := new(Team)
|
||||
return t, c.getParsedResponse("POST", fmt.Sprintf("/orgs/%s/teams", org), jsonHeader, bytes.NewReader(body), t)
|
||||
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/orgs/%s/teams", org), jsonHeader, bytes.NewReader(body), t)
|
||||
return t, resp, err
|
||||
}
|
||||
|
||||
// EditTeamOption options for editing a team
|
||||
type EditTeamOption struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
// enum: read,write,admin
|
||||
Permission string `json:"permission"`
|
||||
Name string `json:"name"`
|
||||
Description *string `json:"description"`
|
||||
Permission AccessMode `json:"permission"`
|
||||
CanCreateOrgRepo *bool `json:"can_create_org_repo"`
|
||||
IncludesAllRepositories *bool `json:"includes_all_repositories"`
|
||||
// example: ["repo.code","repo.issues","repo.ext_issues","repo.wiki","repo.pulls","repo.releases","repo.ext_wiki"]
|
||||
Units []string `json:"units"`
|
||||
}
|
||||
|
||||
// Validate the EditTeamOption struct
|
||||
func (opt EditTeamOption) Validate() error {
|
||||
if opt.Permission == AccessModeOwner {
|
||||
opt.Permission = AccessModeAdmin
|
||||
} else if opt.Permission != AccessModeRead && opt.Permission != AccessModeWrite && opt.Permission != AccessModeAdmin {
|
||||
return fmt.Errorf("permission mode invalid")
|
||||
}
|
||||
if len(opt.Name) == 0 {
|
||||
return fmt.Errorf("name required")
|
||||
}
|
||||
if len(opt.Name) > 30 {
|
||||
return fmt.Errorf("name to long")
|
||||
}
|
||||
if opt.Description != nil && len(*opt.Description) > 255 {
|
||||
return fmt.Errorf("description to long")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditTeam edits a team of an organization
|
||||
func (c *Client) EditTeam(id int64, opt EditTeamOption) error {
|
||||
func (c *Client) EditTeam(id int64, opt EditTeamOption) (*Response, error) {
|
||||
if err := opt.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
_, err = c.getResponse("PATCH", fmt.Sprintf("/teams/%d", id), jsonHeader, bytes.NewReader(body))
|
||||
return err
|
||||
_, resp, err := c.getResponse("PATCH", fmt.Sprintf("/teams/%d", id), jsonHeader, bytes.NewReader(body))
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// DeleteTeam deletes a team of an organization
|
||||
func (c *Client) DeleteTeam(id int64) error {
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/teams/%d", id), nil, nil)
|
||||
return err
|
||||
func (c *Client) DeleteTeam(id int64) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/teams/%d", id), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// ListTeamMembersOptions options for listing team's members
|
||||
|
@ -99,28 +150,30 @@ type ListTeamMembersOptions struct {
|
|||
}
|
||||
|
||||
// ListTeamMembers lists all members of a team
|
||||
func (c *Client) ListTeamMembers(id int64, opt ListTeamMembersOptions) ([]*User, error) {
|
||||
func (c *Client) ListTeamMembers(id int64, opt ListTeamMembersOptions) ([]*User, *Response, error) {
|
||||
opt.setDefaults()
|
||||
members := make([]*User, 0, opt.PageSize)
|
||||
return members, c.getParsedResponse("GET", fmt.Sprintf("/teams/%d/members?%s", id, opt.getURLQuery().Encode()), nil, nil, &members)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/teams/%d/members?%s", id, opt.getURLQuery().Encode()), nil, nil, &members)
|
||||
return members, resp, err
|
||||
}
|
||||
|
||||
// GetTeamMember gets a member of a team
|
||||
func (c *Client) GetTeamMember(id int64, user string) (*User, error) {
|
||||
func (c *Client) GetTeamMember(id int64, user string) (*User, *Response, error) {
|
||||
m := new(User)
|
||||
return m, c.getParsedResponse("GET", fmt.Sprintf("/teams/%d/members/%s", id, user), nil, nil, m)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/teams/%d/members/%s", id, user), nil, nil, m)
|
||||
return m, resp, err
|
||||
}
|
||||
|
||||
// AddTeamMember adds a member to a team
|
||||
func (c *Client) AddTeamMember(id int64, user string) error {
|
||||
_, err := c.getResponse("PUT", fmt.Sprintf("/teams/%d/members/%s", id, user), nil, nil)
|
||||
return err
|
||||
func (c *Client) AddTeamMember(id int64, user string) (*Response, error) {
|
||||
_, resp, err := c.getResponse("PUT", fmt.Sprintf("/teams/%d/members/%s", id, user), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// RemoveTeamMember removes a member from a team
|
||||
func (c *Client) RemoveTeamMember(id int64, user string) error {
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/teams/%d/members/%s", id, user), nil, nil)
|
||||
return err
|
||||
func (c *Client) RemoveTeamMember(id int64, user string) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/teams/%d/members/%s", id, user), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// ListTeamRepositoriesOptions options for listing team's repositories
|
||||
|
@ -129,20 +182,21 @@ type ListTeamRepositoriesOptions struct {
|
|||
}
|
||||
|
||||
// ListTeamRepositories lists all repositories of a team
|
||||
func (c *Client) ListTeamRepositories(id int64, opt ListTeamRepositoriesOptions) ([]*Repository, error) {
|
||||
func (c *Client) ListTeamRepositories(id int64, opt ListTeamRepositoriesOptions) ([]*Repository, *Response, error) {
|
||||
opt.setDefaults()
|
||||
repos := make([]*Repository, 0, opt.PageSize)
|
||||
return repos, c.getParsedResponse("GET", fmt.Sprintf("/teams/%d/repos?%s", id, opt.getURLQuery().Encode()), nil, nil, &repos)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/teams/%d/repos?%s", id, opt.getURLQuery().Encode()), nil, nil, &repos)
|
||||
return repos, resp, err
|
||||
}
|
||||
|
||||
// AddTeamRepository adds a repository to a team
|
||||
func (c *Client) AddTeamRepository(id int64, org, repo string) error {
|
||||
_, err := c.getResponse("PUT", fmt.Sprintf("/teams/%d/repos/%s/%s", id, org, repo), nil, nil)
|
||||
return err
|
||||
func (c *Client) AddTeamRepository(id int64, org, repo string) (*Response, error) {
|
||||
_, resp, err := c.getResponse("PUT", fmt.Sprintf("/teams/%d/repos/%s/%s", id, org, repo), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// RemoveTeamRepository removes a repository from a team
|
||||
func (c *Client) RemoveTeamRepository(id int64, org, repo string) error {
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/teams/%d/repos/%s/%s", id, org, repo), nil, nil)
|
||||
return err
|
||||
func (c *Client) RemoveTeamRepository(id int64, org, repo string) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/teams/%d/repos/%s/%s", id, org, repo), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
|
|
@ -98,19 +98,21 @@ func (opt *ListPullRequestsOptions) QueryEncode() string {
|
|||
}
|
||||
|
||||
// ListRepoPullRequests list PRs of one repository
|
||||
func (c *Client) ListRepoPullRequests(owner, repo string, opt ListPullRequestsOptions) ([]*PullRequest, error) {
|
||||
func (c *Client) ListRepoPullRequests(owner, repo string, opt ListPullRequestsOptions) ([]*PullRequest, *Response, error) {
|
||||
opt.setDefaults()
|
||||
prs := make([]*PullRequest, 0, opt.PageSize)
|
||||
|
||||
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/pulls", owner, repo))
|
||||
link.RawQuery = opt.QueryEncode()
|
||||
return prs, c.getParsedResponse("GET", link.String(), jsonHeader, nil, &prs)
|
||||
resp, err := c.getParsedResponse("GET", link.String(), jsonHeader, nil, &prs)
|
||||
return prs, resp, err
|
||||
}
|
||||
|
||||
// GetPullRequest get information of one PR
|
||||
func (c *Client) GetPullRequest(owner, repo string, index int64) (*PullRequest, error) {
|
||||
func (c *Client) GetPullRequest(owner, repo string, index int64) (*PullRequest, *Response, error) {
|
||||
pr := new(PullRequest)
|
||||
return pr, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d", owner, repo, index), nil, nil, pr)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d", owner, repo, index), nil, nil, pr)
|
||||
return pr, resp, err
|
||||
}
|
||||
|
||||
// CreatePullRequestOption options when creating a pull request
|
||||
|
@ -127,14 +129,16 @@ type CreatePullRequestOption struct {
|
|||
}
|
||||
|
||||
// CreatePullRequest create pull request with options
|
||||
func (c *Client) CreatePullRequest(owner, repo string, opt CreatePullRequestOption) (*PullRequest, error) {
|
||||
func (c *Client) CreatePullRequest(owner, repo string, opt CreatePullRequestOption) (*PullRequest, *Response, error) {
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
pr := new(PullRequest)
|
||||
return pr, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/pulls", owner, repo),
|
||||
resp, err := c.getParsedResponse("POST",
|
||||
fmt.Sprintf("/repos/%s/%s/pulls", owner, repo),
|
||||
jsonHeader, bytes.NewReader(body), pr)
|
||||
return pr, resp, err
|
||||
}
|
||||
|
||||
// EditPullRequestOption options when modify pull request
|
||||
|
@ -156,7 +160,7 @@ func (opt EditPullRequestOption) Validate(c *Client) error {
|
|||
return fmt.Errorf("title is empty")
|
||||
}
|
||||
if len(opt.Base) != 0 {
|
||||
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return fmt.Errorf("can not change base gitea to old")
|
||||
}
|
||||
}
|
||||
|
@ -164,17 +168,19 @@ func (opt EditPullRequestOption) Validate(c *Client) error {
|
|||
}
|
||||
|
||||
// EditPullRequest modify pull request with PR id and options
|
||||
func (c *Client) EditPullRequest(owner, repo string, index int64, opt EditPullRequestOption) (*PullRequest, error) {
|
||||
func (c *Client) EditPullRequest(owner, repo string, index int64, opt EditPullRequestOption) (*PullRequest, *Response, error) {
|
||||
if err := opt.Validate(c); err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
pr := new(PullRequest)
|
||||
return pr, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/pulls/%d", owner, repo, index),
|
||||
resp, err := c.getParsedResponse("PATCH",
|
||||
fmt.Sprintf("/repos/%s/%s/pulls/%d", owner, repo, index),
|
||||
jsonHeader, bytes.NewReader(body), pr)
|
||||
return pr, resp, err
|
||||
}
|
||||
|
||||
// MergePullRequestOption options when merging a pull request
|
||||
|
@ -195,28 +201,53 @@ func (opt MergePullRequestOption) Validate(c *Client) error {
|
|||
}
|
||||
|
||||
// MergePullRequest merge a PR to repository by PR id
|
||||
func (c *Client) MergePullRequest(owner, repo string, index int64, opt MergePullRequestOption) (bool, error) {
|
||||
func (c *Client) MergePullRequest(owner, repo string, index int64, opt MergePullRequestOption) (bool, *Response, error) {
|
||||
if err := opt.Validate(c); err != nil {
|
||||
return false, err
|
||||
return false, nil, err
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, nil, err
|
||||
}
|
||||
status, err := c.getStatusCode("POST", fmt.Sprintf("/repos/%s/%s/pulls/%d/merge", owner, repo, index), jsonHeader, bytes.NewReader(body))
|
||||
status, resp, err := c.getStatusCode("POST", fmt.Sprintf("/repos/%s/%s/pulls/%d/merge", owner, repo, index), jsonHeader, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, resp, err
|
||||
}
|
||||
return status == 200, nil
|
||||
return status == 200, resp, nil
|
||||
}
|
||||
|
||||
// IsPullRequestMerged test if one PR is merged to one repository
|
||||
func (c *Client) IsPullRequestMerged(owner, repo string, index int64) (bool, error) {
|
||||
statusCode, err := c.getStatusCode("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d/merge", owner, repo, index), nil, nil)
|
||||
func (c *Client) IsPullRequestMerged(owner, repo string, index int64) (bool, *Response, error) {
|
||||
status, resp, err := c.getStatusCode("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d/merge", owner, repo, index), nil, nil)
|
||||
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, resp, err
|
||||
}
|
||||
|
||||
return statusCode == 204, nil
|
||||
return status == 204, resp, nil
|
||||
}
|
||||
|
||||
// getPullRequestDiffOrPatch gets the patch or diff file as bytes for a PR
|
||||
func (c *Client) getPullRequestDiffOrPatch(owner, repo, kind string, index int64) ([]byte, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_13_0); err != nil {
|
||||
r, _, err2 := c.GetRepo(owner, repo)
|
||||
if err2 != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if r.Private {
|
||||
return nil, nil, err
|
||||
}
|
||||
return c.getWebResponse("GET", fmt.Sprintf("/%s/%s/pulls/%d.%s", owner, repo, index, kind), nil)
|
||||
}
|
||||
return c.getResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d.%s", owner, repo, index, kind), nil, nil)
|
||||
}
|
||||
|
||||
// GetPullRequestPatch gets the .patch file as bytes for a PR
|
||||
func (c *Client) GetPullRequestPatch(owner, repo string, index int64) ([]byte, *Response, error) {
|
||||
return c.getPullRequestDiffOrPatch(owner, repo, "patch", index)
|
||||
}
|
||||
|
||||
// GetPullRequestDiff gets the .diff file as bytes for a PR
|
||||
func (c *Client) GetPullRequestDiff(owner, repo string, index int64) ([]byte, *Response, error) {
|
||||
return c.getPullRequestDiffOrPatch(owner, repo, "diff", index)
|
||||
}
|
||||
|
|
|
@ -131,9 +131,9 @@ func (opt CreatePullReviewComment) Validate() error {
|
|||
}
|
||||
|
||||
// ListPullReviews lists all reviews of a pull request
|
||||
func (c *Client) ListPullReviews(owner, repo string, index int64, opt ListPullReviewsOptions) ([]*PullReview, error) {
|
||||
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
||||
return nil, err
|
||||
func (c *Client) ListPullReviews(owner, repo string, index int64, opt ListPullReviewsOptions) ([]*PullReview, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
opt.setDefaults()
|
||||
rs := make([]*PullReview, 0, opt.PageSize)
|
||||
|
@ -141,80 +141,79 @@ func (c *Client) ListPullReviews(owner, repo string, index int64, opt ListPullRe
|
|||
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/pulls/%d/reviews", owner, repo, index))
|
||||
link.RawQuery = opt.ListOptions.getURLQuery().Encode()
|
||||
|
||||
return rs, c.getParsedResponse("GET", link.String(), jsonHeader, nil, &rs)
|
||||
resp, err := c.getParsedResponse("GET", link.String(), jsonHeader, nil, &rs)
|
||||
return rs, resp, err
|
||||
}
|
||||
|
||||
// GetPullReview gets a specific review of a pull request
|
||||
func (c *Client) GetPullReview(owner, repo string, index, id int64) (*PullReview, error) {
|
||||
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
||||
return nil, err
|
||||
func (c *Client) GetPullReview(owner, repo string, index, id int64) (*PullReview, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
r := new(PullReview)
|
||||
return r, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d/reviews/%d", owner, repo, index, id), jsonHeader, nil, &r)
|
||||
}
|
||||
|
||||
// ListPullReviewsCommentsOptions options for listing PullReviewsComments
|
||||
type ListPullReviewsCommentsOptions struct {
|
||||
ListOptions
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d/reviews/%d", owner, repo, index, id), jsonHeader, nil, &r)
|
||||
return r, resp, err
|
||||
}
|
||||
|
||||
// ListPullReviewComments lists all comments of a pull request review
|
||||
func (c *Client) ListPullReviewComments(owner, repo string, index, id int64, opt ListPullReviewsCommentsOptions) ([]*PullReviewComment, error) {
|
||||
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
||||
return nil, err
|
||||
func (c *Client) ListPullReviewComments(owner, repo string, index, id int64) ([]*PullReviewComment, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
opt.setDefaults()
|
||||
rcl := make([]*PullReviewComment, 0, opt.PageSize)
|
||||
|
||||
rcl := make([]*PullReviewComment, 0, 4)
|
||||
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/pulls/%d/reviews/%d/comments", owner, repo, index, id))
|
||||
link.RawQuery = opt.ListOptions.getURLQuery().Encode()
|
||||
|
||||
return rcl, c.getParsedResponse("GET", link.String(), jsonHeader, nil, &rcl)
|
||||
resp, err := c.getParsedResponse("GET", link.String(), jsonHeader, nil, &rcl)
|
||||
return rcl, resp, err
|
||||
}
|
||||
|
||||
// DeletePullReview delete a specific review from a pull request
|
||||
func (c *Client) DeletePullReview(owner, repo string, index, id int64) error {
|
||||
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
||||
return err
|
||||
func (c *Client) DeletePullReview(owner, repo string, index, id int64) (*Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/pulls/%d/reviews/%d", owner, repo, index, id), jsonHeader, nil)
|
||||
return err
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/pulls/%d/reviews/%d", owner, repo, index, id), jsonHeader, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// CreatePullReview create a review to an pull request
|
||||
func (c *Client) CreatePullReview(owner, repo string, index int64, opt CreatePullReviewOptions) (*PullReview, error) {
|
||||
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
||||
return nil, err
|
||||
func (c *Client) CreatePullReview(owner, repo string, index int64, opt CreatePullReviewOptions) (*PullReview, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if err := opt.Validate(); err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
r := new(PullReview)
|
||||
return r, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/pulls/%d/reviews", owner, repo, index),
|
||||
resp, err := c.getParsedResponse("POST",
|
||||
fmt.Sprintf("/repos/%s/%s/pulls/%d/reviews", owner, repo, index),
|
||||
jsonHeader, bytes.NewReader(body), r)
|
||||
return r, resp, err
|
||||
}
|
||||
|
||||
// SubmitPullReview submit a pending review to an pull request
|
||||
func (c *Client) SubmitPullReview(owner, repo string, index, id int64, opt SubmitPullReviewOptions) (*PullReview, error) {
|
||||
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
||||
return nil, err
|
||||
func (c *Client) SubmitPullReview(owner, repo string, index, id int64, opt SubmitPullReviewOptions) (*PullReview, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if err := opt.Validate(); err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
r := new(PullReview)
|
||||
return r, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/pulls/%d/reviews/%d", owner, repo, index, id),
|
||||
resp, err := c.getParsedResponse("POST",
|
||||
fmt.Sprintf("/repos/%s/%s/pulls/%d/reviews/%d", owner, repo, index, id),
|
||||
jsonHeader, bytes.NewReader(body), r)
|
||||
return r, resp, err
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ func TestPullReview(t *testing.T) {
|
|||
defer c.AdminDeleteUser(submitter.UserName)
|
||||
|
||||
// CreatePullReview
|
||||
r1, err := c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
|
||||
r1, _, err := c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
|
||||
State: ReviewStateComment,
|
||||
Body: "I'll have a look at it later",
|
||||
})
|
||||
|
@ -35,12 +35,12 @@ func TestPullReview(t *testing.T) {
|
|||
}
|
||||
|
||||
c.SetSudo(submitter.UserName)
|
||||
r2, err := c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
|
||||
r2, _, err := c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
|
||||
State: ReviewStateApproved,
|
||||
Body: "lgtm it myself",
|
||||
})
|
||||
assert.Error(t, err)
|
||||
r2, err = c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
|
||||
r2, _, err = c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
|
||||
State: ReviewStateComment,
|
||||
Body: "no seriously please have a look at it",
|
||||
})
|
||||
|
@ -48,7 +48,7 @@ func TestPullReview(t *testing.T) {
|
|||
assert.NotNil(t, r2)
|
||||
|
||||
c.SetSudo(reviewer.UserName)
|
||||
r3, err := c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
|
||||
r3, _, err := c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
|
||||
State: ReviewStateApproved,
|
||||
Body: "lgtm",
|
||||
Comments: []CreatePullReviewComment{{
|
||||
|
@ -63,7 +63,7 @@ func TestPullReview(t *testing.T) {
|
|||
|
||||
// ListPullReviews
|
||||
c.SetSudo("")
|
||||
rl, err := c.ListPullReviews(repo.Owner.UserName, repo.Name, pull.Index, ListPullReviewsOptions{})
|
||||
rl, _, err := c.ListPullReviews(repo.Owner.UserName, repo.Name, pull.Index, ListPullReviewsOptions{})
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
@ -76,20 +76,20 @@ func TestPullReview(t *testing.T) {
|
|||
}
|
||||
|
||||
// GetPullReview
|
||||
rNew, err := c.GetPullReview(repo.Owner.UserName, repo.Name, pull.Index, r3.ID)
|
||||
rNew, _, err := c.GetPullReview(repo.Owner.UserName, repo.Name, pull.Index, r3.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, r3, rNew)
|
||||
|
||||
// DeletePullReview
|
||||
c.SetSudo(submitter.UserName)
|
||||
err = c.DeletePullReview(repo.Owner.UserName, repo.Name, pull.Index, r2.ID)
|
||||
_, err = c.DeletePullReview(repo.Owner.UserName, repo.Name, pull.Index, r2.ID)
|
||||
assert.NoError(t, err)
|
||||
err = c.DeletePullReview(repo.Owner.UserName, repo.Name, pull.Index, r3.ID)
|
||||
_, err = c.DeletePullReview(repo.Owner.UserName, repo.Name, pull.Index, r3.ID)
|
||||
assert.Error(t, err)
|
||||
|
||||
// SubmitPullReview
|
||||
c.SetSudo("")
|
||||
r4, err := c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
|
||||
r4, _, err := c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
|
||||
Body: "...",
|
||||
Comments: []CreatePullReviewComment{{
|
||||
Path: "WOW-file",
|
||||
|
@ -99,7 +99,7 @@ func TestPullReview(t *testing.T) {
|
|||
},
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
r5, err := c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
|
||||
r5, _, err := c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
|
||||
Body: "...",
|
||||
Comments: []CreatePullReviewComment{{
|
||||
Path: "WOW-file",
|
||||
|
@ -111,7 +111,7 @@ func TestPullReview(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, r4.ID, r5.ID)
|
||||
|
||||
r, err := c.SubmitPullReview(repo.Owner.UserName, repo.Name, pull.Index, r4.ID, SubmitPullReviewOptions{
|
||||
r, _, err := c.SubmitPullReview(repo.Owner.UserName, repo.Name, pull.Index, r4.ID, SubmitPullReviewOptions{
|
||||
State: ReviewStateRequestChanges,
|
||||
Body: "one nit",
|
||||
})
|
||||
|
@ -120,11 +120,11 @@ func TestPullReview(t *testing.T) {
|
|||
assert.EqualValues(t, ReviewStateRequestChanges, r.State)
|
||||
|
||||
// ListPullReviewComments
|
||||
rcl, err := c.ListPullReviewComments(repo.Owner.UserName, repo.Name, pull.Index, r.ID, ListPullReviewsCommentsOptions{})
|
||||
rcl, _, err := c.ListPullReviewComments(repo.Owner.UserName, repo.Name, pull.Index, r.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, r.CodeCommentsCount, len(rcl))
|
||||
for _, rc := range rcl {
|
||||
//assert.EqualValues(t, pull.HTMLURL, rc.HTMLPullURL) https://github.com/go-gitea/gitea/issues/11499
|
||||
assert.EqualValues(t, pull.HTMLURL, rc.HTMLPullURL)
|
||||
if rc.LineNum == 3 {
|
||||
assert.EqualValues(t, "hehe and here it is", rc.Body)
|
||||
} else {
|
||||
|
@ -142,14 +142,15 @@ func preparePullReviewTest(t *testing.T, c *Client, repoName string) (*Repositor
|
|||
}
|
||||
|
||||
pullSubmitter := createTestUser(t, "pull_submitter", c)
|
||||
write := "write"
|
||||
c.AddCollaborator(repo.Owner.UserName, repo.Name, pullSubmitter.UserName, AddCollaboratorOption{
|
||||
write := AccessModeWrite
|
||||
_, err = c.AddCollaborator(repo.Owner.UserName, repo.Name, pullSubmitter.UserName, AddCollaboratorOption{
|
||||
Permission: &write,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
|
||||
c.SetSudo("pull_submitter")
|
||||
|
||||
newFile, err := c.CreateFile(repo.Owner.UserName, repo.Name, "WOW-file", CreateFileOptions{
|
||||
newFile, _, err := c.CreateFile(repo.Owner.UserName, repo.Name, "WOW-file", CreateFileOptions{
|
||||
Content: "QSBuZXcgRmlsZQoKYW5kIHNvbWUgbGluZXMK",
|
||||
FileOptions: FileOptions{
|
||||
Message: "creat a new file",
|
||||
|
@ -162,7 +163,7 @@ func preparePullReviewTest(t *testing.T, c *Client, repoName string) (*Repositor
|
|||
return nil, nil, nil, nil, false
|
||||
}
|
||||
|
||||
pull, err := c.CreatePullRequest(c.username, repoName, CreatePullRequestOption{
|
||||
pull, _, err := c.CreatePullRequest(c.username, repoName, CreatePullRequestOption{
|
||||
Base: "master",
|
||||
Head: "new_file",
|
||||
Title: "Creat a NewFile",
|
||||
|
@ -173,10 +174,11 @@ func preparePullReviewTest(t *testing.T, c *Client, repoName string) (*Repositor
|
|||
c.SetSudo("")
|
||||
|
||||
reviewer := createTestUser(t, "pull_reviewer", c)
|
||||
admin := "admin"
|
||||
c.AddCollaborator(repo.Owner.UserName, repo.Name, pullSubmitter.UserName, AddCollaboratorOption{
|
||||
admin := AccessModeAdmin
|
||||
_, err = c.AddCollaborator(repo.Owner.UserName, repo.Name, pullSubmitter.UserName, AddCollaboratorOption{
|
||||
Permission: &admin,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
|
||||
return repo, pull, pullSubmitter, reviewer, pull.Poster.ID == pullSubmitter.ID
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ import (
|
|||
func TestPull(t *testing.T) {
|
||||
log.Println("== TestPull ==")
|
||||
c := newTestClient()
|
||||
user, err := c.GetMyUserInfo()
|
||||
user, _, err := c.GetMyUserInfo()
|
||||
assert.NoError(t, err)
|
||||
|
||||
var repoName = "repo_pull_test"
|
||||
|
@ -24,11 +24,11 @@ func TestPull(t *testing.T) {
|
|||
}
|
||||
|
||||
// ListRepoPullRequests list PRs of one repository
|
||||
pulls, err := c.ListRepoPullRequests(user.UserName, repoName, ListPullRequestsOptions{})
|
||||
pulls, _, err := c.ListRepoPullRequests(user.UserName, repoName, ListPullRequestsOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, pulls, 0)
|
||||
|
||||
pullUpdateFile, err := c.CreatePullRequest(c.username, repoName, CreatePullRequestOption{
|
||||
pullUpdateFile, _, err := c.CreatePullRequest(c.username, repoName, CreatePullRequestOption{
|
||||
Base: "master",
|
||||
Head: forkOrg + ":overwrite_licence",
|
||||
Title: "overwrite a file",
|
||||
|
@ -36,7 +36,7 @@ func TestPull(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
assert.NotNil(t, pullUpdateFile)
|
||||
|
||||
pullNewFile, err := c.CreatePullRequest(c.username, repoName, CreatePullRequestOption{
|
||||
pullNewFile, _, err := c.CreatePullRequest(c.username, repoName, CreatePullRequestOption{
|
||||
Base: "master",
|
||||
Head: forkOrg + ":new_file",
|
||||
Title: "create a file",
|
||||
|
@ -44,7 +44,7 @@ func TestPull(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
assert.NotNil(t, pullNewFile)
|
||||
|
||||
pullConflict, err := c.CreatePullRequest(c.username, repoName, CreatePullRequestOption{
|
||||
pullConflict, _, err := c.CreatePullRequest(c.username, repoName, CreatePullRequestOption{
|
||||
Base: "master",
|
||||
Head: forkOrg + ":will_conflict",
|
||||
Title: "this pull will conflict",
|
||||
|
@ -52,26 +52,33 @@ func TestPull(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
assert.NotNil(t, pullConflict)
|
||||
|
||||
pulls, err = c.ListRepoPullRequests(user.UserName, repoName, ListPullRequestsOptions{})
|
||||
pulls, _, err = c.ListRepoPullRequests(user.UserName, repoName, ListPullRequestsOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, pulls, 3)
|
||||
|
||||
diff, _, err := c.GetPullRequestDiff(c.username, repoName, pullUpdateFile.Index)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, diff, 1310)
|
||||
patch, _, err := c.GetPullRequestPatch(c.username, repoName, pullUpdateFile.Index)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, len(patch) > len(diff))
|
||||
|
||||
// test Update pull
|
||||
pr, err := c.GetPullRequest(user.UserName, repoName, pullUpdateFile.Index)
|
||||
pr, _, err := c.GetPullRequest(user.UserName, repoName, pullUpdateFile.Index)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, pullUpdateFile.HasMerged)
|
||||
assert.True(t, pullUpdateFile.Mergeable)
|
||||
merged, err := c.MergePullRequest(user.UserName, repoName, pullUpdateFile.Index, MergePullRequestOption{
|
||||
merged, _, err := c.MergePullRequest(user.UserName, repoName, pullUpdateFile.Index, MergePullRequestOption{
|
||||
Style: MergeStyleSquash,
|
||||
Title: pullUpdateFile.Title,
|
||||
Message: "squash: " + pullUpdateFile.Title,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, merged)
|
||||
merged, err = c.IsPullRequestMerged(user.UserName, repoName, pullUpdateFile.Index)
|
||||
merged, _, err = c.IsPullRequestMerged(user.UserName, repoName, pullUpdateFile.Index)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, merged)
|
||||
pr, err = c.GetPullRequest(user.UserName, repoName, pullUpdateFile.Index)
|
||||
pr, _, err = c.GetPullRequest(user.UserName, repoName, pullUpdateFile.Index)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, pullUpdateFile.Head.Name, pr.Head.Name)
|
||||
assert.EqualValues(t, pullUpdateFile.Base.Name, pr.Base.Name)
|
||||
|
@ -80,34 +87,34 @@ func TestPull(t *testing.T) {
|
|||
assert.True(t, pr.HasMerged)
|
||||
|
||||
// test conflict pull
|
||||
pr, err = c.GetPullRequest(user.UserName, repoName, pullConflict.Index)
|
||||
pr, _, err = c.GetPullRequest(user.UserName, repoName, pullConflict.Index)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, pullConflict.HasMerged)
|
||||
assert.False(t, pullConflict.Mergeable)
|
||||
merged, err = c.MergePullRequest(user.UserName, repoName, pullConflict.Index, MergePullRequestOption{
|
||||
merged, _, err = c.MergePullRequest(user.UserName, repoName, pullConflict.Index, MergePullRequestOption{
|
||||
Style: MergeStyleMerge,
|
||||
Title: "pullConflict",
|
||||
Message: "pullConflict Msg",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, merged)
|
||||
merged, err = c.IsPullRequestMerged(user.UserName, repoName, pullConflict.Index)
|
||||
merged, _, err = c.IsPullRequestMerged(user.UserName, repoName, pullConflict.Index)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, merged)
|
||||
pr, err = c.GetPullRequest(user.UserName, repoName, pullConflict.Index)
|
||||
pr, _, err = c.GetPullRequest(user.UserName, repoName, pullConflict.Index)
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, pr.MergedCommitID)
|
||||
assert.False(t, pr.HasMerged)
|
||||
|
||||
state := StateClosed
|
||||
pr, err = c.EditPullRequest(user.UserName, repoName, pullConflict.Index, EditPullRequestOption{
|
||||
pr, _, err = c.EditPullRequest(user.UserName, repoName, pullConflict.Index, EditPullRequestOption{
|
||||
Title: "confl",
|
||||
State: &state,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, state, pr.State)
|
||||
|
||||
pulls, err = c.ListRepoPullRequests(user.UserName, repoName, ListPullRequestsOptions{
|
||||
pulls, _, err = c.ListRepoPullRequests(user.UserName, repoName, ListPullRequestsOptions{
|
||||
State: StateClosed,
|
||||
Sort: "leastupdate",
|
||||
})
|
||||
|
@ -116,26 +123,26 @@ func TestPull(t *testing.T) {
|
|||
}
|
||||
|
||||
func preparePullTest(t *testing.T, c *Client, repoName, forkOrg string) bool {
|
||||
_ = c.DeleteRepo(forkOrg, repoName)
|
||||
_ = c.DeleteRepo(c.username, repoName)
|
||||
_ = c.DeleteOrg(forkOrg)
|
||||
_, _ = c.DeleteRepo(forkOrg, repoName)
|
||||
_, _ = c.DeleteRepo(c.username, repoName)
|
||||
_, _ = c.DeleteOrg(forkOrg)
|
||||
|
||||
origRepo, err := createTestRepo(t, repoName, c)
|
||||
if !assert.NoError(t, err) {
|
||||
return false
|
||||
}
|
||||
org, err := c.CreateOrg(CreateOrgOption{UserName: forkOrg})
|
||||
org, _, err := c.CreateOrg(CreateOrgOption{Name: forkOrg})
|
||||
assert.NoError(t, err)
|
||||
forkRepo, err := c.CreateFork(origRepo.Owner.UserName, origRepo.Name, CreateForkOption{Organization: &org.UserName})
|
||||
forkRepo, _, err := c.CreateFork(origRepo.Owner.UserName, origRepo.Name, CreateForkOption{Organization: &org.UserName})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, forkRepo)
|
||||
|
||||
masterLicence, err := c.GetContents(forkRepo.Owner.UserName, forkRepo.Name, "master", "LICENSE")
|
||||
masterLicence, _, err := c.GetContents(forkRepo.Owner.UserName, forkRepo.Name, "master", "LICENSE")
|
||||
if !assert.NoError(t, err) || !assert.NotNil(t, masterLicence) {
|
||||
return false
|
||||
}
|
||||
|
||||
updatedFile, err := c.UpdateFile(forkRepo.Owner.UserName, forkRepo.Name, "LICENSE", UpdateFileOptions{
|
||||
updatedFile, _, err := c.UpdateFile(forkRepo.Owner.UserName, forkRepo.Name, "LICENSE", UpdateFileOptions{
|
||||
FileOptions: FileOptions{
|
||||
Message: "Overwrite",
|
||||
BranchName: "master",
|
||||
|
@ -148,7 +155,7 @@ func preparePullTest(t *testing.T, c *Client, repoName, forkOrg string) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
newFile, err := c.CreateFile(forkRepo.Owner.UserName, forkRepo.Name, "WOW-file", CreateFileOptions{
|
||||
newFile, _, err := c.CreateFile(forkRepo.Owner.UserName, forkRepo.Name, "WOW-file", CreateFileOptions{
|
||||
Content: "QSBuZXcgRmlsZQo=",
|
||||
FileOptions: FileOptions{
|
||||
Message: "creat a new file",
|
||||
|
@ -160,7 +167,7 @@ func preparePullTest(t *testing.T, c *Client, repoName, forkOrg string) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
conflictFile1, err := c.CreateFile(origRepo.Owner.UserName, origRepo.Name, "bad-file", CreateFileOptions{
|
||||
conflictFile1, _, err := c.CreateFile(origRepo.Owner.UserName, origRepo.Name, "bad-file", CreateFileOptions{
|
||||
Content: "U3RhcnQgQ29uZmxpY3QK",
|
||||
FileOptions: FileOptions{
|
||||
Message: "Start Conflict",
|
||||
|
@ -171,7 +178,7 @@ func preparePullTest(t *testing.T, c *Client, repoName, forkOrg string) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
conflictFile2, err := c.CreateFile(forkRepo.Owner.UserName, forkRepo.Name, "bad-file", CreateFileOptions{
|
||||
conflictFile2, _, err := c.CreateFile(forkRepo.Owner.UserName, forkRepo.Name, "bad-file", CreateFileOptions{
|
||||
Content: "V2lsbEhhdmUgQ29uZmxpY3QK",
|
||||
FileOptions: FileOptions{
|
||||
Message: "creat a new file witch will conflict",
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
@ -36,22 +37,34 @@ type ListReleasesOptions struct {
|
|||
}
|
||||
|
||||
// ListReleases list releases of a repository
|
||||
func (c *Client) ListReleases(user, repo string, opt ListReleasesOptions) ([]*Release, error) {
|
||||
func (c *Client) ListReleases(user, repo string, opt ListReleasesOptions) ([]*Release, *Response, error) {
|
||||
opt.setDefaults()
|
||||
releases := make([]*Release, 0, opt.PageSize)
|
||||
err := c.getParsedResponse("GET",
|
||||
resp, err := c.getParsedResponse("GET",
|
||||
fmt.Sprintf("/repos/%s/%s/releases?%s", user, repo, opt.getURLQuery().Encode()),
|
||||
nil, nil, &releases)
|
||||
return releases, err
|
||||
return releases, resp, err
|
||||
}
|
||||
|
||||
// GetRelease get a release of a repository
|
||||
func (c *Client) GetRelease(user, repo string, id int64) (*Release, error) {
|
||||
// GetRelease get a release of a repository by id
|
||||
func (c *Client) GetRelease(user, repo string, id int64) (*Release, *Response, error) {
|
||||
r := new(Release)
|
||||
err := c.getParsedResponse("GET",
|
||||
resp, err := c.getParsedResponse("GET",
|
||||
fmt.Sprintf("/repos/%s/%s/releases/%d", user, repo, id),
|
||||
jsonHeader, nil, &r)
|
||||
return r, resp, err
|
||||
}
|
||||
|
||||
// GetReleaseByTag get a release of a repository by tag
|
||||
func (c *Client) GetReleaseByTag(user, repo string, tag string) (*Release, *Response, error) {
|
||||
if c.checkServerVersionGreaterThanOrEqual(version1_13_0) != nil {
|
||||
return c.fallbackGetReleaseByTag(user, repo, tag)
|
||||
}
|
||||
r := new(Release)
|
||||
resp, err := c.getParsedResponse("GET",
|
||||
fmt.Sprintf("/repos/%s/%s/releases/tags/%s", user, repo, tag),
|
||||
nil, nil, &r)
|
||||
return r, err
|
||||
return r, resp, err
|
||||
}
|
||||
|
||||
// CreateReleaseOption options when creating a release
|
||||
|
@ -73,19 +86,19 @@ func (opt CreateReleaseOption) Validate() error {
|
|||
}
|
||||
|
||||
// CreateRelease create a release
|
||||
func (c *Client) CreateRelease(user, repo string, opt CreateReleaseOption) (*Release, error) {
|
||||
func (c *Client) CreateRelease(user, repo string, opt CreateReleaseOption) (*Release, *Response, error) {
|
||||
if err := opt.Validate(); err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
body, err := json.Marshal(opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
r := new(Release)
|
||||
err = c.getParsedResponse("POST",
|
||||
resp, err := c.getParsedResponse("POST",
|
||||
fmt.Sprintf("/repos/%s/%s/releases", user, repo),
|
||||
jsonHeader, bytes.NewReader(body), r)
|
||||
return r, err
|
||||
return r, resp, err
|
||||
}
|
||||
|
||||
// EditReleaseOption options when editing a release
|
||||
|
@ -99,22 +112,42 @@ type EditReleaseOption struct {
|
|||
}
|
||||
|
||||
// EditRelease edit a release
|
||||
func (c *Client) EditRelease(user, repo string, id int64, form EditReleaseOption) (*Release, error) {
|
||||
func (c *Client) EditRelease(user, repo string, id int64, form EditReleaseOption) (*Release, *Response, error) {
|
||||
body, err := json.Marshal(form)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
r := new(Release)
|
||||
err = c.getParsedResponse("PATCH",
|
||||
resp, err := c.getParsedResponse("PATCH",
|
||||
fmt.Sprintf("/repos/%s/%s/releases/%d", user, repo, id),
|
||||
jsonHeader, bytes.NewReader(body), r)
|
||||
return r, err
|
||||
return r, resp, err
|
||||
}
|
||||
|
||||
// DeleteRelease delete a release from a repository
|
||||
func (c *Client) DeleteRelease(user, repo string, id int64) error {
|
||||
_, err := c.getResponse("DELETE",
|
||||
func (c *Client) DeleteRelease(user, repo string, id int64) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE",
|
||||
fmt.Sprintf("/repos/%s/%s/releases/%d", user, repo, id),
|
||||
nil, nil)
|
||||
return err
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// fallbackGetReleaseByTag is fallback for old gitea installations ( < 1.13.0 )
|
||||
func (c *Client) fallbackGetReleaseByTag(user, repo string, tag string) (*Release, *Response, error) {
|
||||
for i := 1; ; i++ {
|
||||
rl, resp, err := c.ListReleases(user, repo, ListReleasesOptions{ListOptions{Page: i}})
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
}
|
||||
if len(rl) == 0 {
|
||||
return nil,
|
||||
&Response{&http.Response{StatusCode: 404}},
|
||||
fmt.Errorf("release with tag '%s' not found", tag)
|
||||
}
|
||||
for _, r := range rl {
|
||||
if r.TagName == tag {
|
||||
return r, resp, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
94
gitea/release_test.go
Normal file
94
gitea/release_test.go
Normal file
|
@ -0,0 +1,94 @@
|
|||
// 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"
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRelease(t *testing.T) {
|
||||
log.Println("== TestRelease ==")
|
||||
c := newTestClient()
|
||||
|
||||
repo, _ := createTestRepo(t, "ReleaseTests", c)
|
||||
|
||||
// ListReleases
|
||||
rl, _, err := c.ListReleases(repo.Owner.UserName, repo.Name, ListReleasesOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, rl, 0)
|
||||
|
||||
// CreateRelease
|
||||
r, _, err := c.CreateRelease(repo.Owner.UserName, repo.Name, CreateReleaseOption{
|
||||
TagName: "awesome",
|
||||
Target: "master",
|
||||
Title: "Release 1",
|
||||
Note: "yes it's awesome",
|
||||
IsDraft: true,
|
||||
IsPrerelease: true,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, "awesome", r.TagName)
|
||||
assert.EqualValues(t, true, r.IsPrerelease)
|
||||
assert.EqualValues(t, true, r.IsDraft)
|
||||
assert.EqualValues(t, "Release 1", r.Title)
|
||||
assert.EqualValues(t, fmt.Sprintf("%s/api/v1/repos/%s/releases/%d", c.url, repo.FullName, r.ID), r.URL)
|
||||
assert.EqualValues(t, "master", r.Target)
|
||||
assert.EqualValues(t, "yes it's awesome", r.Note)
|
||||
assert.EqualValues(t, c.username, r.Publisher.UserName)
|
||||
rl, _, _ = c.ListReleases(repo.Owner.UserName, repo.Name, ListReleasesOptions{})
|
||||
assert.Len(t, rl, 1)
|
||||
|
||||
// GetRelease
|
||||
r2, _, err := c.GetRelease(repo.Owner.UserName, repo.Name, r.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, r, r2)
|
||||
r2, _, err = c.GetReleaseByTag(repo.Owner.UserName, repo.Name, r.TagName)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, r, r2)
|
||||
// test fallback
|
||||
r2, _, err = c.fallbackGetReleaseByTag(repo.Owner.UserName, repo.Name, r.TagName)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, r, r2)
|
||||
|
||||
// EditRelease
|
||||
r2, _, err = c.EditRelease(repo.Owner.UserName, repo.Name, r.ID, EditReleaseOption{
|
||||
Title: "Release Awesome",
|
||||
Note: "",
|
||||
IsDraft: OptionalBool(false),
|
||||
IsPrerelease: OptionalBool(false),
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, r.Target, r2.Target)
|
||||
assert.EqualValues(t, false, r2.IsDraft)
|
||||
assert.EqualValues(t, false, r2.IsPrerelease)
|
||||
assert.EqualValues(t, r.Note, r2.Note)
|
||||
|
||||
// DeleteRelease
|
||||
_, err = c.DeleteRelease(repo.Owner.UserName, repo.Name, r.ID)
|
||||
assert.NoError(t, err)
|
||||
rl, _, _ = c.ListReleases(repo.Owner.UserName, repo.Name, ListReleasesOptions{})
|
||||
assert.Len(t, rl, 0)
|
||||
|
||||
// Test Response if try to get not existing release
|
||||
_, resp, err := c.GetRelease(repo.Owner.UserName, repo.Name, 1234)
|
||||
assert.Error(t, err)
|
||||
if assert.NotNil(t, resp) {
|
||||
assert.EqualValues(t, 404, resp.StatusCode)
|
||||
}
|
||||
_, resp, err = c.GetReleaseByTag(repo.Owner.UserName, repo.Name, "not_here")
|
||||
assert.Error(t, err)
|
||||
if assert.NotNil(t, resp) {
|
||||
assert.EqualValues(t, 404, resp.StatusCode)
|
||||
}
|
||||
_, resp, err = c.fallbackGetReleaseByTag(repo.Owner.UserName, repo.Name, "not_here")
|
||||
assert.Error(t, err)
|
||||
if assert.NotNil(t, resp) {
|
||||
assert.EqualValues(t, 404, resp.StatusCode)
|
||||
}
|
||||
}
|
164
gitea/repo.go
164
gitea/repo.go
|
@ -1,4 +1,5 @@
|
|||
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||||
// 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.
|
||||
|
||||
|
@ -72,23 +73,39 @@ const (
|
|||
RepoTypeMirror RepoType = "mirror"
|
||||
)
|
||||
|
||||
// TrustModel represent how git signatures are handled in a repository
|
||||
type TrustModel string
|
||||
|
||||
const (
|
||||
// TrustModelDefault use TM set by global config
|
||||
TrustModelDefault TrustModel = "default"
|
||||
// TrustModelCollaborator gpg signature has to be owned by a repo collaborator
|
||||
TrustModelCollaborator TrustModel = "collaborator"
|
||||
// TrustModelCommitter gpg signature has to match committer
|
||||
TrustModelCommitter TrustModel = "committer"
|
||||
// TrustModelCollaboratorCommitter gpg signature has to match committer and owned by a repo collaborator
|
||||
TrustModelCollaboratorCommitter TrustModel = "collaboratorcommitter"
|
||||
)
|
||||
|
||||
// ListReposOptions options for listing repositories
|
||||
type ListReposOptions struct {
|
||||
ListOptions
|
||||
}
|
||||
|
||||
// ListMyRepos lists all repositories for the authenticated user that has access to.
|
||||
func (c *Client) ListMyRepos(opt ListReposOptions) ([]*Repository, error) {
|
||||
func (c *Client) ListMyRepos(opt ListReposOptions) ([]*Repository, *Response, error) {
|
||||
opt.setDefaults()
|
||||
repos := make([]*Repository, 0, opt.PageSize)
|
||||
return repos, c.getParsedResponse("GET", fmt.Sprintf("/user/repos?%s", opt.getURLQuery().Encode()), nil, nil, &repos)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/user/repos?%s", opt.getURLQuery().Encode()), nil, nil, &repos)
|
||||
return repos, resp, err
|
||||
}
|
||||
|
||||
// ListUserRepos list all repositories of one user by user's name
|
||||
func (c *Client) ListUserRepos(user string, opt ListReposOptions) ([]*Repository, error) {
|
||||
func (c *Client) ListUserRepos(user string, opt ListReposOptions) ([]*Repository, *Response, error) {
|
||||
opt.setDefaults()
|
||||
repos := make([]*Repository, 0, opt.PageSize)
|
||||
return repos, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/repos?%s", user, opt.getURLQuery().Encode()), nil, nil, &repos)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/users/%s/repos?%s", user, opt.getURLQuery().Encode()), nil, nil, &repos)
|
||||
return repos, resp, err
|
||||
}
|
||||
|
||||
// ListOrgReposOptions options for a organization's repositories
|
||||
|
@ -97,10 +114,11 @@ type ListOrgReposOptions struct {
|
|||
}
|
||||
|
||||
// ListOrgRepos list all repositories of one organization by organization's name
|
||||
func (c *Client) ListOrgRepos(org string, opt ListOrgReposOptions) ([]*Repository, error) {
|
||||
func (c *Client) ListOrgRepos(org string, opt ListOrgReposOptions) ([]*Repository, *Response, error) {
|
||||
opt.setDefaults()
|
||||
repos := make([]*Repository, 0, opt.PageSize)
|
||||
return repos, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/repos?%s", org, opt.getURLQuery().Encode()), nil, nil, &repos)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/repos?%s", org, opt.getURLQuery().Encode()), nil, nil, &repos)
|
||||
return repos, resp, err
|
||||
}
|
||||
|
||||
// SearchRepoOptions options for searching repositories
|
||||
|
@ -209,9 +227,9 @@ type searchRepoResponse struct {
|
|||
}
|
||||
|
||||
// SearchRepos searches for repositories matching the given filters
|
||||
func (c *Client) SearchRepos(opt SearchRepoOptions) ([]*Repository, error) {
|
||||
func (c *Client) SearchRepos(opt SearchRepoOptions) ([]*Repository, *Response, error) {
|
||||
opt.setDefaults()
|
||||
resp := new(searchRepoResponse)
|
||||
repos := new(searchRepoResponse)
|
||||
|
||||
link, _ := url.Parse("/repos/search")
|
||||
|
||||
|
@ -220,17 +238,17 @@ func (c *Client) SearchRepos(opt SearchRepoOptions) ([]*Repository, error) {
|
|||
} else {
|
||||
link.RawQuery = opt.QueryEncode()
|
||||
// IsPrivate only works on gitea >= 1.12.0
|
||||
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil && opt.IsPrivate != nil {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil && opt.IsPrivate != nil {
|
||||
if *opt.IsPrivate {
|
||||
// private repos only not supported on gitea <= 1.11.x
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
link.Query().Add("private", "false")
|
||||
}
|
||||
}
|
||||
|
||||
err := c.getParsedResponse("GET", link.String(), nil, nil, &resp)
|
||||
return resp.Repos, err
|
||||
resp, err := c.getParsedResponse("GET", link.String(), nil, nil, &repos)
|
||||
return repos.Repos, resp, err
|
||||
}
|
||||
|
||||
// CreateRepoOption options when creating repository
|
||||
|
@ -245,6 +263,8 @@ type CreateRepoOption struct {
|
|||
IssueLabels string `json:"issue_labels"`
|
||||
// Whether the repository should be auto-intialized?
|
||||
AutoInit bool `json:"auto_init"`
|
||||
// Whether the repository is template
|
||||
Template bool `json:"template"`
|
||||
// Gitignores to use
|
||||
Gitignores string `json:"gitignores"`
|
||||
// License to use
|
||||
|
@ -253,46 +273,65 @@ type CreateRepoOption struct {
|
|||
Readme string `json:"readme"`
|
||||
// DefaultBranch of the repository (used when initializes and in template)
|
||||
DefaultBranch string `json:"default_branch"`
|
||||
// TrustModel of the repository
|
||||
TrustModel TrustModel `json:"trust_model"`
|
||||
}
|
||||
|
||||
// Validate the CreateRepoOption struct
|
||||
func (opt CreateRepoOption) Validate() error {
|
||||
func (opt CreateRepoOption) Validate(c *Client) error {
|
||||
if len(strings.TrimSpace(opt.Name)) == 0 {
|
||||
return fmt.Errorf("name is empty")
|
||||
}
|
||||
if len(opt.Name) > 100 {
|
||||
return fmt.Errorf("name has more than 100 chars")
|
||||
}
|
||||
if len(opt.Description) > 255 {
|
||||
return fmt.Errorf("name has more than 255 chars")
|
||||
}
|
||||
if len(opt.DefaultBranch) > 100 {
|
||||
return fmt.Errorf("name has more than 100 chars")
|
||||
}
|
||||
if len(opt.TrustModel) != 0 {
|
||||
if err := c.CheckServerVersionConstraint(">=1.13.0"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateRepo creates a repository for authenticated user.
|
||||
func (c *Client) CreateRepo(opt CreateRepoOption) (*Repository, error) {
|
||||
if err := opt.Validate(); err != nil {
|
||||
return nil, err
|
||||
func (c *Client) CreateRepo(opt CreateRepoOption) (*Repository, *Response, error) {
|
||||
if err := opt.Validate(c); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
repo := new(Repository)
|
||||
return repo, c.getParsedResponse("POST", "/user/repos", jsonHeader, bytes.NewReader(body), repo)
|
||||
resp, err := c.getParsedResponse("POST", "/user/repos", jsonHeader, bytes.NewReader(body), repo)
|
||||
return repo, resp, err
|
||||
}
|
||||
|
||||
// CreateOrgRepo creates an organization repository for authenticated user.
|
||||
func (c *Client) CreateOrgRepo(org string, opt CreateRepoOption) (*Repository, error) {
|
||||
if err := opt.Validate(); err != nil {
|
||||
return nil, err
|
||||
func (c *Client) CreateOrgRepo(org string, opt CreateRepoOption) (*Repository, *Response, error) {
|
||||
if err := opt.Validate(c); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
repo := new(Repository)
|
||||
return repo, c.getParsedResponse("POST", fmt.Sprintf("/org/%s/repos", org), jsonHeader, bytes.NewReader(body), repo)
|
||||
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/org/%s/repos", org), jsonHeader, bytes.NewReader(body), repo)
|
||||
return repo, resp, err
|
||||
}
|
||||
|
||||
// GetRepo returns information of a repository of given owner.
|
||||
func (c *Client) GetRepo(owner, reponame string) (*Repository, error) {
|
||||
func (c *Client) GetRepo(owner, reponame string) (*Repository, *Response, error) {
|
||||
repo := new(Repository)
|
||||
return repo, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s", owner, reponame), nil, nil, repo)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s", owner, reponame), nil, nil, repo)
|
||||
return repo, resp, err
|
||||
}
|
||||
|
||||
// EditRepoOption options when editing a repository's properties
|
||||
|
@ -330,49 +369,54 @@ type EditRepoOption struct {
|
|||
}
|
||||
|
||||
// EditRepo edit the properties of a repository
|
||||
func (c *Client) EditRepo(owner, reponame string, opt EditRepoOption) (*Repository, error) {
|
||||
func (c *Client) EditRepo(owner, reponame string, opt EditRepoOption) (*Repository, *Response, error) {
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
repo := new(Repository)
|
||||
return repo, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s", owner, reponame), jsonHeader, bytes.NewReader(body), repo)
|
||||
resp, err := c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s", owner, reponame), jsonHeader, bytes.NewReader(body), repo)
|
||||
return repo, resp, err
|
||||
}
|
||||
|
||||
// DeleteRepo deletes a repository of user or organization.
|
||||
func (c *Client) DeleteRepo(owner, repo string) error {
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s", owner, repo), nil, nil)
|
||||
return err
|
||||
}
|
||||
|
||||
// MigrateRepoOption options for migrating a repository from an external service
|
||||
type MigrateRepoOption struct {
|
||||
CloneAddr string `json:"clone_addr"`
|
||||
AuthUsername string `json:"auth_username"`
|
||||
AuthPassword string `json:"auth_password"`
|
||||
UID int `json:"uid"`
|
||||
RepoName string `json:"repo_name"`
|
||||
Mirror bool `json:"mirror"`
|
||||
Private bool `json:"private"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
// MigrateRepo migrates a repository from other Git hosting sources for the
|
||||
// authenticated user.
|
||||
//
|
||||
// To migrate a repository for a organization, the authenticated user must be a
|
||||
// owner of the specified organization.
|
||||
func (c *Client) MigrateRepo(opt MigrateRepoOption) (*Repository, error) {
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
repo := new(Repository)
|
||||
return repo, c.getParsedResponse("POST", "/repos/migrate", jsonHeader, bytes.NewReader(body), repo)
|
||||
func (c *Client) DeleteRepo(owner, repo string) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s", owner, repo), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// MirrorSync adds a mirrored repository to the mirror sync queue.
|
||||
func (c *Client) MirrorSync(owner, repo string) error {
|
||||
_, err := c.getResponse("POST", fmt.Sprintf("/repos/%s/%s/mirror-sync", owner, repo), nil, nil)
|
||||
return err
|
||||
func (c *Client) MirrorSync(owner, repo string) (*Response, error) {
|
||||
_, resp, err := c.getResponse("POST", fmt.Sprintf("/repos/%s/%s/mirror-sync", owner, repo), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// GetRepoLanguages return language stats of a repo
|
||||
func (c *Client) GetRepoLanguages(owner, repo string) (map[string]int64, *Response, error) {
|
||||
langMap := make(map[string]int64)
|
||||
|
||||
data, resp, err := c.getResponse("GET", fmt.Sprintf("/repos/%s/%s/languages", owner, repo), jsonHeader, nil)
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
}
|
||||
if err = json.Unmarshal(data, &langMap); err != nil {
|
||||
return nil, resp, err
|
||||
}
|
||||
return langMap, resp, nil
|
||||
}
|
||||
|
||||
// ArchiveType represent supported archive formats by gitea
|
||||
type ArchiveType string
|
||||
|
||||
const (
|
||||
// ZipArchive represent zip format
|
||||
ZipArchive ArchiveType = ".zip"
|
||||
// TarGZArchive represent tar.gz format
|
||||
TarGZArchive ArchiveType = ".tar.gz"
|
||||
)
|
||||
|
||||
// GetArchive get an archive of a repository by git reference
|
||||
// e.g.: ref -> master, 70b7c74b33, v1.2.1, ...
|
||||
func (c *Client) GetArchive(owner, repo, ref string, ext ArchiveType) ([]byte, *Response, error) {
|
||||
return c.getResponse("GET", fmt.Sprintf("/repos/%s/%s/archive/%s%s", owner, repo, url.PathEscape(ref), ext), nil, nil)
|
||||
}
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
package gitea
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
@ -63,29 +65,70 @@ type ListRepoBranchesOptions struct {
|
|||
}
|
||||
|
||||
// ListRepoBranches list all the branches of one repository
|
||||
func (c *Client) ListRepoBranches(user, repo string, opt ListRepoBranchesOptions) ([]*Branch, error) {
|
||||
func (c *Client) ListRepoBranches(user, repo string, opt ListRepoBranchesOptions) ([]*Branch, *Response, error) {
|
||||
opt.setDefaults()
|
||||
branches := make([]*Branch, 0, opt.PageSize)
|
||||
return branches, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/branches?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &branches)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/branches?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &branches)
|
||||
return branches, resp, err
|
||||
}
|
||||
|
||||
// GetRepoBranch get one branch's information of one repository
|
||||
func (c *Client) GetRepoBranch(user, repo, branch string) (*Branch, error) {
|
||||
func (c *Client) GetRepoBranch(user, repo, branch string) (*Branch, *Response, error) {
|
||||
b := new(Branch)
|
||||
if err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/branches/%s", user, repo, branch), nil, nil, &b); err != nil {
|
||||
return nil, err
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/branches/%s", user, repo, branch), nil, nil, &b)
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
}
|
||||
return b, nil
|
||||
return b, resp, nil
|
||||
}
|
||||
|
||||
// DeleteRepoBranch delete a branch in a repository
|
||||
func (c *Client) DeleteRepoBranch(user, repo, branch string) (bool, error) {
|
||||
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
||||
return false, err
|
||||
func (c *Client) DeleteRepoBranch(user, repo, branch string) (bool, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return false, nil, err
|
||||
}
|
||||
status, err := c.getStatusCode("DELETE", fmt.Sprintf("/repos/%s/%s/branches/%s", user, repo, branch), nil, nil)
|
||||
status, resp, err := c.getStatusCode("DELETE", fmt.Sprintf("/repos/%s/%s/branches/%s", user, repo, branch), nil, nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, resp, err
|
||||
}
|
||||
return status == 204, nil
|
||||
return status == 204, resp, nil
|
||||
}
|
||||
|
||||
// CreateBranchOption options when creating a branch in a repository
|
||||
type CreateBranchOption struct {
|
||||
// Name of the branch to create
|
||||
BranchName string `json:"new_branch_name"`
|
||||
// Name of the old branch to create from (optional)
|
||||
OldBranchName string `json:"old_branch_name"`
|
||||
}
|
||||
|
||||
// Validate the CreateBranchOption struct
|
||||
func (opt CreateBranchOption) Validate() error {
|
||||
if len(opt.BranchName) == 0 {
|
||||
return fmt.Errorf("BranchName is empty")
|
||||
}
|
||||
if len(opt.BranchName) > 100 {
|
||||
return fmt.Errorf("BranchName to long")
|
||||
}
|
||||
if len(opt.OldBranchName) > 100 {
|
||||
return fmt.Errorf("OldBranchName to long")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateBranch creates a branch for a user's repository
|
||||
func (c *Client) CreateBranch(owner, repo string, opt CreateBranchOption) (*Branch, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_13_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if err := opt.Validate(); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
branch := new(Branch)
|
||||
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/branches", owner, repo), jsonHeader, bytes.NewReader(body), branch)
|
||||
return branch, resp, err
|
||||
}
|
||||
|
|
|
@ -91,56 +91,60 @@ type ListBranchProtectionsOptions struct {
|
|||
}
|
||||
|
||||
// ListBranchProtections list branch protections for a repo
|
||||
func (c *Client) ListBranchProtections(owner, repo string, opt ListBranchProtectionsOptions) ([]*BranchProtection, error) {
|
||||
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
||||
return nil, err
|
||||
func (c *Client) ListBranchProtections(owner, repo string, opt ListBranchProtectionsOptions) ([]*BranchProtection, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
bps := make([]*BranchProtection, 0, 5)
|
||||
bps := make([]*BranchProtection, 0, opt.PageSize)
|
||||
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/branch_protections", owner, repo))
|
||||
link.RawQuery = opt.getURLQuery().Encode()
|
||||
return bps, c.getParsedResponse("GET", link.String(), jsonHeader, nil, &bps)
|
||||
resp, err := c.getParsedResponse("GET", link.String(), jsonHeader, nil, &bps)
|
||||
return bps, resp, err
|
||||
}
|
||||
|
||||
// GetBranchProtection gets a branch protection
|
||||
func (c *Client) GetBranchProtection(owner, repo, name string) (*BranchProtection, error) {
|
||||
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
||||
return nil, err
|
||||
func (c *Client) GetBranchProtection(owner, repo, name string) (*BranchProtection, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
bp := new(BranchProtection)
|
||||
return bp, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/branch_protections/%s", owner, repo, name), jsonHeader, nil, bp)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/branch_protections/%s", owner, repo, name), jsonHeader, nil, bp)
|
||||
return bp, resp, err
|
||||
}
|
||||
|
||||
// CreateBranchProtection creates a branch protection for a repo
|
||||
func (c *Client) CreateBranchProtection(owner, repo string, opt CreateBranchProtectionOption) (*BranchProtection, error) {
|
||||
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
||||
return nil, err
|
||||
func (c *Client) CreateBranchProtection(owner, repo string, opt CreateBranchProtectionOption) (*BranchProtection, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
bp := new(BranchProtection)
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
return bp, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/branch_protections", owner, repo), jsonHeader, bytes.NewReader(body), bp)
|
||||
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/branch_protections", owner, repo), jsonHeader, bytes.NewReader(body), bp)
|
||||
return bp, resp, err
|
||||
}
|
||||
|
||||
// EditBranchProtection edits a branch protection for a repo
|
||||
func (c *Client) EditBranchProtection(owner, repo, name string, opt EditBranchProtectionOption) (*BranchProtection, error) {
|
||||
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
||||
return nil, err
|
||||
func (c *Client) EditBranchProtection(owner, repo, name string, opt EditBranchProtectionOption) (*BranchProtection, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
bp := new(BranchProtection)
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
return bp, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/branch_protections/%s", owner, repo, name), jsonHeader, bytes.NewReader(body), bp)
|
||||
resp, err := c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/branch_protections/%s", owner, repo, name), jsonHeader, bytes.NewReader(body), bp)
|
||||
return bp, resp, err
|
||||
}
|
||||
|
||||
// DeleteBranchProtection deletes a branch protection for a repo
|
||||
func (c *Client) DeleteBranchProtection(owner, repo, name string) error {
|
||||
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
||||
return err
|
||||
func (c *Client) DeleteBranchProtection(owner, repo, name string) (*Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/branch_protections/%s", owner, repo, name), jsonHeader, nil)
|
||||
return err
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/branch_protections/%s", owner, repo, name), jsonHeader, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
|
|
@ -21,32 +21,39 @@ func TestRepoBranches(t *testing.T) {
|
|||
return
|
||||
}
|
||||
|
||||
bl, err := c.ListRepoBranches(repo.Owner.UserName, repo.Name, ListRepoBranchesOptions{})
|
||||
bl, _, err := c.ListRepoBranches(repo.Owner.UserName, repo.Name, ListRepoBranchesOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, bl, 3)
|
||||
assert.EqualValues(t, "feature", bl[0].Name)
|
||||
assert.EqualValues(t, "master", bl[1].Name)
|
||||
assert.EqualValues(t, "update", bl[2].Name)
|
||||
|
||||
b, err := c.GetRepoBranch(repo.Owner.UserName, repo.Name, "update")
|
||||
b, _, err := c.GetRepoBranch(repo.Owner.UserName, repo.Name, "update")
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, bl[2].Commit.ID, b.Commit.ID)
|
||||
assert.EqualValues(t, bl[2].Commit.Added, b.Commit.Added)
|
||||
|
||||
s, err := c.DeleteRepoBranch(repo.Owner.UserName, repo.Name, "master")
|
||||
s, _, err := c.DeleteRepoBranch(repo.Owner.UserName, repo.Name, "master")
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, s)
|
||||
s, err = c.DeleteRepoBranch(repo.Owner.UserName, repo.Name, "feature")
|
||||
s, _, err = c.DeleteRepoBranch(repo.Owner.UserName, repo.Name, "feature")
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, s)
|
||||
|
||||
bl, err = c.ListRepoBranches(repo.Owner.UserName, repo.Name, ListRepoBranchesOptions{})
|
||||
bl, _, err = c.ListRepoBranches(repo.Owner.UserName, repo.Name, ListRepoBranchesOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, bl, 2)
|
||||
|
||||
b, err = c.GetRepoBranch(repo.Owner.UserName, repo.Name, "feature")
|
||||
b, _, err = c.GetRepoBranch(repo.Owner.UserName, repo.Name, "feature")
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, b)
|
||||
|
||||
bNew, _, err := c.CreateBranch(repo.Owner.UserName, repo.Name, CreateBranchOption{BranchName: "NewBranch"})
|
||||
assert.NoError(t, err)
|
||||
|
||||
b, _, err = c.GetRepoBranch(repo.Owner.UserName, repo.Name, bNew.Name)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, bNew, b)
|
||||
}
|
||||
|
||||
func TestRepoBranchProtection(t *testing.T) {
|
||||
|
@ -61,12 +68,12 @@ func TestRepoBranchProtection(t *testing.T) {
|
|||
assert.NotNil(t, repo)
|
||||
|
||||
// ListBranchProtections
|
||||
bpl, err := c.ListBranchProtections(repo.Owner.UserName, repo.Name, ListBranchProtectionsOptions{})
|
||||
bpl, _, err := c.ListBranchProtections(repo.Owner.UserName, repo.Name, ListBranchProtectionsOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, bpl, 0)
|
||||
|
||||
// CreateBranchProtection
|
||||
bp, err := c.CreateBranchProtection(repo.Owner.UserName, repo.Name, CreateBranchProtectionOption{
|
||||
bp, _, err := c.CreateBranchProtection(repo.Owner.UserName, repo.Name, CreateBranchProtectionOption{
|
||||
BranchName: "master",
|
||||
EnablePush: true,
|
||||
EnablePushWhitelist: true,
|
||||
|
@ -82,7 +89,7 @@ func TestRepoBranchProtection(t *testing.T) {
|
|||
assert.EqualValues(t, true, bp.EnablePushWhitelist)
|
||||
assert.EqualValues(t, []string{"test01"}, bp.PushWhitelistUsernames)
|
||||
|
||||
bp, err = c.CreateBranchProtection(repo.Owner.UserName, repo.Name, CreateBranchProtectionOption{
|
||||
bp, _, err = c.CreateBranchProtection(repo.Owner.UserName, repo.Name, CreateBranchProtectionOption{
|
||||
BranchName: "update",
|
||||
EnablePush: false,
|
||||
EnableMergeWhitelist: true,
|
||||
|
@ -91,26 +98,22 @@ func TestRepoBranchProtection(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
assert.NotNil(t, bp)
|
||||
|
||||
bpl, err = c.ListBranchProtections(repo.Owner.UserName, repo.Name, ListBranchProtectionsOptions{})
|
||||
bpl, _, err = c.ListBranchProtections(repo.Owner.UserName, repo.Name, ListBranchProtectionsOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, bpl, 2)
|
||||
|
||||
// GetBranchProtection
|
||||
bp, err = c.GetBranchProtection(repo.Owner.UserName, repo.Name, bpl[0].BranchName)
|
||||
bp, _, err = c.GetBranchProtection(repo.Owner.UserName, repo.Name, bpl[0].BranchName)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, bpl[0], bp)
|
||||
|
||||
optTrue := true
|
||||
optFalse := false
|
||||
one := int64(1)
|
||||
|
||||
// EditBranchProtection
|
||||
bp, err = c.EditBranchProtection(repo.Owner.UserName, repo.Name, bpl[0].BranchName, EditBranchProtectionOption{
|
||||
EnablePush: &optFalse,
|
||||
EnablePushWhitelist: &optFalse,
|
||||
bp, _, err = c.EditBranchProtection(repo.Owner.UserName, repo.Name, bpl[0].BranchName, EditBranchProtectionOption{
|
||||
EnablePush: OptionalBool(false),
|
||||
EnablePushWhitelist: OptionalBool(false),
|
||||
PushWhitelistUsernames: nil,
|
||||
RequiredApprovals: &one,
|
||||
EnableApprovalsWhitelist: &optTrue,
|
||||
RequiredApprovals: OptionalInt64(1),
|
||||
EnableApprovalsWhitelist: OptionalBool(true),
|
||||
ApprovalsWhitelistUsernames: []string{"test01"},
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
|
@ -120,9 +123,9 @@ func TestRepoBranchProtection(t *testing.T) {
|
|||
assert.EqualValues(t, bpl[0].Created, bp.Created)
|
||||
|
||||
// DeleteBranchProtection
|
||||
err = c.DeleteBranchProtection(repo.Owner.UserName, repo.Name, bpl[1].BranchName)
|
||||
_, err = c.DeleteBranchProtection(repo.Owner.UserName, repo.Name, bpl[1].BranchName)
|
||||
assert.NoError(t, err)
|
||||
bpl, err = c.ListBranchProtections(repo.Owner.UserName, repo.Name, ListBranchProtectionsOptions{})
|
||||
bpl, _, err = c.ListBranchProtections(repo.Owner.UserName, repo.Name, ListBranchProtectionsOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, bpl, 1)
|
||||
}
|
||||
|
@ -133,12 +136,12 @@ func prepareBranchTest(t *testing.T, c *Client, repoName string) *Repository {
|
|||
return nil
|
||||
}
|
||||
|
||||
masterLicence, err := c.GetContents(origRepo.Owner.UserName, origRepo.Name, "master", "README.md")
|
||||
masterLicence, _, err := c.GetContents(origRepo.Owner.UserName, origRepo.Name, "master", "README.md")
|
||||
if !assert.NoError(t, err) || !assert.NotNil(t, masterLicence) {
|
||||
return nil
|
||||
}
|
||||
|
||||
updatedFile, err := c.UpdateFile(origRepo.Owner.UserName, origRepo.Name, "README.md", UpdateFileOptions{
|
||||
updatedFile, _, err := c.UpdateFile(origRepo.Owner.UserName, origRepo.Name, "README.md", UpdateFileOptions{
|
||||
FileOptions: FileOptions{
|
||||
Message: "update it",
|
||||
BranchName: "master",
|
||||
|
@ -151,7 +154,7 @@ func prepareBranchTest(t *testing.T, c *Client, repoName string) *Repository {
|
|||
return nil
|
||||
}
|
||||
|
||||
newFile, err := c.CreateFile(origRepo.Owner.UserName, origRepo.Name, "WOW-file", CreateFileOptions{
|
||||
newFile, _, err := c.CreateFile(origRepo.Owner.UserName, origRepo.Name, "WOW-file", CreateFileOptions{
|
||||
Content: "QSBuZXcgRmlsZQo=",
|
||||
FileOptions: FileOptions{
|
||||
Message: "creat a new file",
|
||||
|
|
|
@ -16,56 +16,82 @@ type ListCollaboratorsOptions struct {
|
|||
}
|
||||
|
||||
// ListCollaborators list a repository's collaborators
|
||||
func (c *Client) ListCollaborators(user, repo string, opt ListCollaboratorsOptions) ([]*User, error) {
|
||||
func (c *Client) ListCollaborators(user, repo string, opt ListCollaboratorsOptions) ([]*User, *Response, error) {
|
||||
opt.setDefaults()
|
||||
collaborators := make([]*User, 0, opt.PageSize)
|
||||
return collaborators, c.getParsedResponse("GET",
|
||||
resp, err := c.getParsedResponse("GET",
|
||||
fmt.Sprintf("/repos/%s/%s/collaborators?%s", user, repo, opt.getURLQuery().Encode()),
|
||||
nil, nil, &collaborators)
|
||||
return collaborators, resp, err
|
||||
}
|
||||
|
||||
// IsCollaborator check if a user is a collaborator of a repository
|
||||
func (c *Client) IsCollaborator(user, repo, collaborator string) (bool, error) {
|
||||
status, err := c.getStatusCode("GET", fmt.Sprintf("/repos/%s/%s/collaborators/%s", user, repo, collaborator), nil, nil)
|
||||
func (c *Client) IsCollaborator(user, repo, collaborator string) (bool, *Response, error) {
|
||||
status, resp, err := c.getStatusCode("GET", fmt.Sprintf("/repos/%s/%s/collaborators/%s", user, repo, collaborator), nil, nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, resp, err
|
||||
}
|
||||
if status == 204 {
|
||||
return true, nil
|
||||
return true, resp, nil
|
||||
}
|
||||
return false, nil
|
||||
return false, resp, nil
|
||||
}
|
||||
|
||||
// AddCollaboratorOption options when adding a user as a collaborator of a repository
|
||||
type AddCollaboratorOption struct {
|
||||
Permission *string `json:"permission"`
|
||||
Permission *AccessMode `json:"permission"`
|
||||
}
|
||||
|
||||
// AccessMode represent the grade of access you have to something
|
||||
type AccessMode string
|
||||
|
||||
const (
|
||||
// AccessModeNone no access
|
||||
AccessModeNone AccessMode = "none"
|
||||
// AccessModeRead read access
|
||||
AccessModeRead AccessMode = "read"
|
||||
// AccessModeWrite write access
|
||||
AccessModeWrite AccessMode = "write"
|
||||
// AccessModeAdmin admin access
|
||||
AccessModeAdmin AccessMode = "admin"
|
||||
// AccessModeOwner owner
|
||||
AccessModeOwner AccessMode = "owner"
|
||||
)
|
||||
|
||||
// Validate the AddCollaboratorOption struct
|
||||
func (opt AddCollaboratorOption) Validate() error {
|
||||
if opt.Permission != nil &&
|
||||
*opt.Permission != "read" && *opt.Permission != "write" && *opt.Permission != "admin" {
|
||||
return fmt.Errorf("permission mode invalid")
|
||||
if opt.Permission != nil {
|
||||
if *opt.Permission == AccessModeOwner {
|
||||
*opt.Permission = AccessModeAdmin
|
||||
return nil
|
||||
}
|
||||
if *opt.Permission == AccessModeNone {
|
||||
opt.Permission = nil
|
||||
return nil
|
||||
}
|
||||
if *opt.Permission != AccessModeRead && *opt.Permission != AccessModeWrite && *opt.Permission != AccessModeAdmin {
|
||||
return fmt.Errorf("permission mode invalid")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddCollaborator add some user as a collaborator of a repository
|
||||
func (c *Client) AddCollaborator(user, repo, collaborator string, opt AddCollaboratorOption) error {
|
||||
func (c *Client) AddCollaborator(user, repo, collaborator string, opt AddCollaboratorOption) (*Response, error) {
|
||||
if err := opt.Validate(); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
_, err = c.getResponse("PUT", fmt.Sprintf("/repos/%s/%s/collaborators/%s", user, repo, collaborator), jsonHeader, bytes.NewReader(body))
|
||||
return err
|
||||
_, resp, err := c.getResponse("PUT", fmt.Sprintf("/repos/%s/%s/collaborators/%s", user, repo, collaborator), jsonHeader, bytes.NewReader(body))
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// DeleteCollaborator remove a collaborator from a repository
|
||||
func (c *Client) DeleteCollaborator(user, repo, collaborator string) error {
|
||||
_, err := c.getResponse("DELETE",
|
||||
func (c *Client) DeleteCollaborator(user, repo, collaborator string) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE",
|
||||
fmt.Sprintf("/repos/%s/%s/collaborators/%s", user, repo, collaborator), nil, nil)
|
||||
return err
|
||||
return resp, err
|
||||
}
|
||||
|
|
|
@ -19,8 +19,9 @@ type Identity struct {
|
|||
|
||||
// CommitMeta contains meta information of a commit in terms of API.
|
||||
type CommitMeta struct {
|
||||
URL string `json:"url"`
|
||||
SHA string `json:"sha"`
|
||||
URL string `json:"url"`
|
||||
SHA string `json:"sha"`
|
||||
Created time.Time `json:"created"`
|
||||
}
|
||||
|
||||
// CommitUser contains information of a user in the context of a commit.
|
||||
|
@ -55,9 +56,10 @@ type CommitDateOptions struct {
|
|||
}
|
||||
|
||||
// GetSingleCommit returns a single commit
|
||||
func (c *Client) GetSingleCommit(user, repo, commitID string) (*Commit, error) {
|
||||
func (c *Client) GetSingleCommit(user, repo, commitID string) (*Commit, *Response, error) {
|
||||
commit := new(Commit)
|
||||
return commit, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/git/commits/%s", user, repo, commitID), nil, nil, &commit)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/git/commits/%s", user, repo, commitID), nil, nil, &commit)
|
||||
return commit, resp, err
|
||||
}
|
||||
|
||||
// ListCommitOptions list commit options
|
||||
|
@ -77,10 +79,11 @@ func (opt *ListCommitOptions) QueryEncode() string {
|
|||
}
|
||||
|
||||
// ListRepoCommits return list of commits from a repo
|
||||
func (c *Client) ListRepoCommits(user, repo string, opt ListCommitOptions) ([]*Commit, error) {
|
||||
func (c *Client) ListRepoCommits(user, repo string, opt ListCommitOptions) ([]*Commit, *Response, error) {
|
||||
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/commits", user, repo))
|
||||
opt.setDefaults()
|
||||
commits := make([]*Commit, 0, opt.PageSize)
|
||||
link.RawQuery = opt.QueryEncode()
|
||||
return commits, c.getParsedResponse("GET", link.String(), nil, nil, &commits)
|
||||
resp, err := c.getParsedResponse("GET", link.String(), nil, nil, &commits)
|
||||
return commits, resp, err
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ func TestListRepoCommits(t *testing.T) {
|
|||
repo, err := createTestRepo(t, "ListRepoCommits", c)
|
||||
assert.NoError(t, err)
|
||||
|
||||
l, err := c.ListRepoCommits(repo.Owner.UserName, repo.Name, ListCommitOptions{})
|
||||
l, _, err := c.ListRepoCommits(repo.Owner.UserName, repo.Name, ListCommitOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, l, 1)
|
||||
assert.EqualValues(t, "Initial commit\n", l[0].RepoCommit.Message)
|
||||
|
|
|
@ -114,74 +114,76 @@ type FileDeleteResponse struct {
|
|||
|
||||
// GetFile downloads a file of repository, ref can be branch/tag/commit.
|
||||
// e.g.: ref -> master, tree -> macaron.go(no leading slash)
|
||||
func (c *Client) GetFile(user, repo, ref, tree string) ([]byte, error) {
|
||||
func (c *Client) GetFile(user, repo, ref, tree string) ([]byte, *Response, error) {
|
||||
return c.getResponse("GET", fmt.Sprintf("/repos/%s/%s/raw/%s/%s", user, repo, ref, tree), nil, nil)
|
||||
}
|
||||
|
||||
// GetContents get the metadata and contents (if a file) of an entry in a repository, or a list of entries if a dir
|
||||
// ref is optional
|
||||
func (c *Client) GetContents(owner, repo, ref, filepath string) (*ContentsResponse, error) {
|
||||
func (c *Client) GetContents(owner, repo, ref, filepath string) (*ContentsResponse, *Response, error) {
|
||||
cr := new(ContentsResponse)
|
||||
return cr, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/contents/%s?ref=%s", owner, repo, filepath, ref), jsonHeader, nil, cr)
|
||||
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/contents/%s?ref=%s", owner, repo, filepath, ref), jsonHeader, nil, cr)
|
||||
return cr, resp, err
|
||||
}
|
||||
|
||||
// CreateFile create a file in a repository
|
||||
func (c *Client) CreateFile(owner, repo, filepath string, opt CreateFileOptions) (*FileResponse, error) {
|
||||
func (c *Client) CreateFile(owner, repo, filepath string, opt CreateFileOptions) (*FileResponse, *Response, error) {
|
||||
var err error
|
||||
if opt.BranchName, err = c.setDefaultBranchForOldVersions(owner, repo, opt.BranchName); err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
fr := new(FileResponse)
|
||||
return fr, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/contents/%s", owner, repo, filepath), jsonHeader, bytes.NewReader(body), fr)
|
||||
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/contents/%s", owner, repo, filepath), jsonHeader, bytes.NewReader(body), fr)
|
||||
return fr, resp, err
|
||||
}
|
||||
|
||||
// UpdateFile update a file in a repository
|
||||
func (c *Client) UpdateFile(owner, repo, filepath string, opt UpdateFileOptions) (*FileResponse, error) {
|
||||
func (c *Client) UpdateFile(owner, repo, filepath string, opt UpdateFileOptions) (*FileResponse, *Response, error) {
|
||||
var err error
|
||||
if opt.BranchName, err = c.setDefaultBranchForOldVersions(owner, repo, opt.BranchName); err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
fr := new(FileResponse)
|
||||
return fr, c.getParsedResponse("PUT", fmt.Sprintf("/repos/%s/%s/contents/%s", owner, repo, filepath), jsonHeader, bytes.NewReader(body), fr)
|
||||
resp, err := c.getParsedResponse("PUT", fmt.Sprintf("/repos/%s/%s/contents/%s", owner, repo, filepath), jsonHeader, bytes.NewReader(body), fr)
|
||||
return fr, resp, err
|
||||
}
|
||||
|
||||
// DeleteFile delete a file from repository
|
||||
func (c *Client) DeleteFile(owner, repo, filepath string, opt DeleteFileOptions) error {
|
||||
func (c *Client) DeleteFile(owner, repo, filepath string, opt DeleteFileOptions) (*Response, error) {
|
||||
var err error
|
||||
if opt.BranchName, err = c.setDefaultBranchForOldVersions(owner, repo, opt.BranchName); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
status, err := c.getStatusCode("DELETE", fmt.Sprintf("/repos/%s/%s/contents/%s", owner, repo, filepath), jsonHeader, bytes.NewReader(body))
|
||||
status, resp, err := c.getStatusCode("DELETE", fmt.Sprintf("/repos/%s/%s/contents/%s", owner, repo, filepath), jsonHeader, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return err
|
||||
return resp, err
|
||||
}
|
||||
if status != 200 && status != 204 {
|
||||
return fmt.Errorf("unexpected Status: %d", status)
|
||||
return resp, fmt.Errorf("unexpected Status: %d", status)
|
||||
}
|
||||
return nil
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) setDefaultBranchForOldVersions(owner, repo, branch string) (string, error) {
|
||||
if len(branch) == 0 {
|
||||
// Gitea >= 1.12.0 Use DefaultBranch on "", mimic this for older versions
|
||||
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
||||
r, err := c.GetRepo(owner, repo)
|
||||
if c.checkServerVersionGreaterThanOrEqual(version1_12_0) != nil {
|
||||
r, _, err := c.GetRepo(owner, repo)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
|
@ -20,21 +20,21 @@ func TestFileCreateUpdateGet(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
assert.NotNil(t, repo)
|
||||
|
||||
raw, err := c.GetFile(repo.Owner.UserName, repo.Name, "master", "README.md")
|
||||
raw, _, err := c.GetFile(repo.Owner.UserName, repo.Name, "master", "README.md")
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, "IyBDaGFuZ2VGaWxlcwoKQSB0ZXN0IFJlcG86IENoYW5nZUZpbGVz", base64.StdEncoding.EncodeToString(raw))
|
||||
|
||||
newFile, err := c.CreateFile(repo.Owner.UserName, repo.Name, "A", CreateFileOptions{
|
||||
newFile, _, err := c.CreateFile(repo.Owner.UserName, repo.Name, "A", CreateFileOptions{
|
||||
FileOptions: FileOptions{
|
||||
Message: "create file A",
|
||||
},
|
||||
Content: "ZmlsZUEK",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
raw, _ = c.GetFile(repo.Owner.UserName, repo.Name, "master", "A")
|
||||
raw, _, _ = c.GetFile(repo.Owner.UserName, repo.Name, "master", "A")
|
||||
assert.EqualValues(t, "ZmlsZUEK", base64.StdEncoding.EncodeToString(raw))
|
||||
|
||||
updatedFile, err := c.UpdateFile(repo.Owner.UserName, repo.Name, "A", UpdateFileOptions{
|
||||
updatedFile, _, err := c.UpdateFile(repo.Owner.UserName, repo.Name, "A", UpdateFileOptions{
|
||||
FileOptions: FileOptions{
|
||||
Message: "add a new line",
|
||||
},
|
||||
|
@ -44,18 +44,18 @@ func TestFileCreateUpdateGet(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
assert.NotNil(t, updatedFile)
|
||||
|
||||
file, err := c.GetContents(repo.Owner.UserName, repo.Name, "master", "A")
|
||||
file, _, err := c.GetContents(repo.Owner.UserName, repo.Name, "master", "A")
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, updatedFile.Content.SHA, file.SHA)
|
||||
assert.EqualValues(t, &updatedFile.Content.Content, &file.Content)
|
||||
|
||||
err = c.DeleteFile(repo.Owner.UserName, repo.Name, "A", DeleteFileOptions{
|
||||
_, err = c.DeleteFile(repo.Owner.UserName, repo.Name, "A", DeleteFileOptions{
|
||||
FileOptions: FileOptions{
|
||||
Message: "Delete File A",
|
||||
},
|
||||
SHA: updatedFile.Content.SHA,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
_, err = c.GetFile(repo.Owner.UserName, repo.Name, "master", "A")
|
||||
_, _, err = c.GetFile(repo.Owner.UserName, repo.Name, "master", "A")
|
||||
assert.EqualValues(t, "404 Not Found", err.Error())
|
||||
}
|
||||
|
|
|
@ -45,32 +45,35 @@ func (opt *ListDeployKeysOptions) QueryEncode() string {
|
|||
}
|
||||
|
||||
// ListDeployKeys list all the deploy keys of one repository
|
||||
func (c *Client) ListDeployKeys(user, repo string, opt ListDeployKeysOptions) ([]*DeployKey, error) {
|
||||
func (c *Client) ListDeployKeys(user, repo string, opt ListDeployKeysOptions) ([]*DeployKey, *Response, error) {
|
||||
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/keys", user, repo))
|
||||
opt.setDefaults()
|
||||
link.RawQuery = opt.QueryEncode()
|
||||
keys := make([]*DeployKey, 0, opt.PageSize)
|
||||
return keys, c.getParsedResponse("GET", link.String(), nil, nil, &keys)
|
||||
resp, err := c.getParsedResponse("GET", link.String(), nil, nil, &keys)
|
||||
return keys, resp, err
|
||||
}
|
||||
|
||||
// GetDeployKey get one deploy key with key id
|
||||
func (c *Client) GetDeployKey(user, repo string, keyID int64) (*DeployKey, error) {
|
||||
func (c *Client) GetDeployKey(user, repo string, keyID int64) (*DeployKey, *Response, error) {
|
||||
key := new(DeployKey)
|
||||
return key, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/keys/%d", user, repo, keyID), nil, nil, &key)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/keys/%d", user, repo, keyID), nil, nil, &key)
|
||||
return key, resp, err
|
||||
}
|
||||
|
||||
// CreateDeployKey options when create one deploy key
|
||||
func (c *Client) CreateDeployKey(user, repo string, opt CreateKeyOption) (*DeployKey, error) {
|
||||
func (c *Client) CreateDeployKey(user, repo string, opt CreateKeyOption) (*DeployKey, *Response, error) {
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
key := new(DeployKey)
|
||||
return key, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/keys", user, repo), jsonHeader, bytes.NewReader(body), key)
|
||||
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/keys", user, repo), jsonHeader, bytes.NewReader(body), key)
|
||||
return key, resp, err
|
||||
}
|
||||
|
||||
// DeleteDeployKey delete deploy key with key id
|
||||
func (c *Client) DeleteDeployKey(owner, repo string, keyID int64) error {
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/keys/%d", owner, repo, keyID), nil, nil)
|
||||
return err
|
||||
func (c *Client) DeleteDeployKey(owner, repo string, keyID int64) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/keys/%d", owner, repo, keyID), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
|
124
gitea/repo_migrate.go
Normal file
124
gitea/repo_migrate.go
Normal file
|
@ -0,0 +1,124 @@
|
|||
// 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 (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// GitServiceType represents a git service
|
||||
type GitServiceType string
|
||||
|
||||
const (
|
||||
// GitServicePlain represents a plain git service
|
||||
GitServicePlain GitServiceType = "git"
|
||||
//GitServiceGithub represents github.com
|
||||
GitServiceGithub GitServiceType = "github"
|
||||
// GitServiceGitlab represents a gitlab service
|
||||
GitServiceGitlab GitServiceType = "gitlab"
|
||||
// GitServiceGitea represents a gitea service
|
||||
GitServiceGitea GitServiceType = "gitea"
|
||||
|
||||
// Not supported jet
|
||||
// // GitServiceGogs represents a gogs service
|
||||
// GitServiceGogs GitServiceType = "gogs"
|
||||
)
|
||||
|
||||
// MigrateRepoOption options for migrating a repository from an external service
|
||||
type MigrateRepoOption struct {
|
||||
RepoName string `json:"repo_name"`
|
||||
RepoOwner string `json:"repo_owner"`
|
||||
// deprecated use RepoOwner
|
||||
RepoOwnerID int64 `json:"uid"`
|
||||
CloneAddr string `json:"clone_addr"`
|
||||
Service GitServiceType `json:"service"`
|
||||
AuthUsername string `json:"auth_username"`
|
||||
AuthPassword string `json:"auth_password"`
|
||||
AuthToken string `json:"auth_token"`
|
||||
Mirror bool `json:"mirror"`
|
||||
Private bool `json:"private"`
|
||||
Description string `json:"description"`
|
||||
Wiki bool `json:"wiki"`
|
||||
Milestones bool `json:"milestones"`
|
||||
Labels bool `json:"labels"`
|
||||
Issues bool `json:"issues"`
|
||||
PullRequests bool `json:"pull_requests"`
|
||||
Releases bool `json:"releases"`
|
||||
}
|
||||
|
||||
// Validate the MigrateRepoOption struct
|
||||
func (opt *MigrateRepoOption) Validate(c *Client) error {
|
||||
// check user options
|
||||
if len(opt.CloneAddr) == 0 {
|
||||
return fmt.Errorf("CloneAddr required")
|
||||
}
|
||||
if len(opt.RepoName) == 0 {
|
||||
return fmt.Errorf("RepoName required")
|
||||
} else if len(opt.RepoName) > 100 {
|
||||
return fmt.Errorf("RepoName to long")
|
||||
}
|
||||
if len(opt.Description) > 255 {
|
||||
return fmt.Errorf("Description to long")
|
||||
}
|
||||
switch opt.Service {
|
||||
case GitServiceGithub:
|
||||
if len(opt.AuthToken) == 0 {
|
||||
return fmt.Errorf("github require token authentication")
|
||||
}
|
||||
case GitServiceGitlab, GitServiceGitea:
|
||||
if len(opt.AuthToken) == 0 {
|
||||
return fmt.Errorf("%s require token authentication", opt.Service)
|
||||
}
|
||||
// Gitlab is supported since 1.12.0 but api cant handle it until 1.13.0
|
||||
// https://github.com/go-gitea/gitea/pull/12672
|
||||
if c.checkServerVersionGreaterThanOrEqual(version1_13_0) != nil {
|
||||
return fmt.Errorf("migrate from service %s need gitea >= 1.13.0", opt.Service)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// MigrateRepo migrates a repository from other Git hosting sources for the authenticated user.
|
||||
//
|
||||
// To migrate a repository for a organization, the authenticated user must be a
|
||||
// owner of the specified organization.
|
||||
func (c *Client) MigrateRepo(opt MigrateRepoOption) (*Repository, *Response, error) {
|
||||
if err := opt.Validate(c); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_13_0); err != nil {
|
||||
if len(opt.AuthToken) != 0 {
|
||||
// gitea <= 1.12 dont understand AuthToken
|
||||
opt.AuthUsername = opt.AuthToken
|
||||
opt.AuthPassword, opt.AuthToken = "", ""
|
||||
}
|
||||
if len(opt.RepoOwner) != 0 {
|
||||
// gitea <= 1.12 dont understand RepoOwner
|
||||
u, _, err := c.GetUserInfo(opt.RepoOwner)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
opt.RepoOwnerID = u.ID
|
||||
} else if opt.RepoOwnerID == 0 {
|
||||
// gitea <= 1.12 require RepoOwnerID
|
||||
u, _, err := c.GetMyUserInfo()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
opt.RepoOwnerID = u.ID
|
||||
}
|
||||
}
|
||||
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
repo := new(Repository)
|
||||
resp, err := c.getParsedResponse("POST", "/repos/migrate", jsonHeader, bytes.NewReader(body), repo)
|
||||
return repo, resp, err
|
||||
}
|
|
@ -26,44 +26,44 @@ type GitObject struct {
|
|||
}
|
||||
|
||||
// GetRepoRef get one ref's information of one repository
|
||||
func (c *Client) GetRepoRef(user, repo, ref string) (*Reference, error) {
|
||||
func (c *Client) GetRepoRef(user, repo, ref string) (*Reference, *Response, error) {
|
||||
ref = strings.TrimPrefix(ref, "refs/")
|
||||
r := new(Reference)
|
||||
err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/git/refs/%s", user, repo, ref), nil, nil, &r)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/git/refs/%s", user, repo, ref), nil, nil, &r)
|
||||
if _, ok := err.(*json.UnmarshalTypeError); ok {
|
||||
// Multiple refs
|
||||
return nil, errors.New("no exact match found for this ref")
|
||||
return nil, resp, errors.New("no exact match found for this ref")
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
return r, nil
|
||||
return r, resp, nil
|
||||
}
|
||||
|
||||
// GetRepoRefs get list of ref's information of one repository
|
||||
func (c *Client) GetRepoRefs(user, repo, ref string) ([]*Reference, error) {
|
||||
func (c *Client) GetRepoRefs(user, repo, ref string) ([]*Reference, *Response, error) {
|
||||
ref = strings.TrimPrefix(ref, "refs/")
|
||||
resp, err := c.getResponse("GET", fmt.Sprintf("/repos/%s/%s/git/refs/%s", user, repo, ref), nil, nil)
|
||||
data, resp, err := c.getResponse("GET", fmt.Sprintf("/repos/%s/%s/git/refs/%s", user, repo, ref), nil, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
// Attempt to unmarshal single returned ref.
|
||||
r := new(Reference)
|
||||
refErr := json.Unmarshal(resp, r)
|
||||
refErr := json.Unmarshal(data, r)
|
||||
if refErr == nil {
|
||||
return []*Reference{r}, nil
|
||||
return []*Reference{r}, resp, nil
|
||||
}
|
||||
|
||||
// Attempt to unmarshal multiple refs.
|
||||
var rs []*Reference
|
||||
refsErr := json.Unmarshal(resp, &rs)
|
||||
refsErr := json.Unmarshal(data, &rs)
|
||||
if refsErr == nil {
|
||||
if len(rs) == 0 {
|
||||
return nil, errors.New("unexpected response: an array of refs with length 0")
|
||||
return nil, resp, errors.New("unexpected response: an array of refs with length 0")
|
||||
}
|
||||
return rs, nil
|
||||
return rs, resp, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("unmarshalling failed for both single and multiple refs: %s and %s", refErr, refsErr)
|
||||
return nil, resp, fmt.Errorf("unmarshalling failed for both single and multiple refs: %s and %s", refErr, refsErr)
|
||||
}
|
||||
|
|
|
@ -23,8 +23,9 @@ type ListRepoTagsOptions struct {
|
|||
}
|
||||
|
||||
// ListRepoTags list all the branches of one repository
|
||||
func (c *Client) ListRepoTags(user, repo string, opt ListRepoTagsOptions) ([]*Tag, error) {
|
||||
func (c *Client) ListRepoTags(user, repo string, opt ListRepoTagsOptions) ([]*Tag, *Response, error) {
|
||||
opt.setDefaults()
|
||||
tags := make([]*Tag, 0, opt.PageSize)
|
||||
return tags, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/tags?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &tags)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/tags?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &tags)
|
||||
return tags, resp, err
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ package gitea
|
|||
import (
|
||||
"log"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -14,37 +15,77 @@ import (
|
|||
func TestCreateRepo(t *testing.T) {
|
||||
log.Println("== TestCreateRepo ==")
|
||||
c := newTestClient()
|
||||
user, err := c.GetMyUserInfo()
|
||||
user, _, err := c.GetMyUserInfo()
|
||||
assert.NoError(t, err)
|
||||
|
||||
var repoName = "test1"
|
||||
_, err = c.GetRepo(user.UserName, repoName)
|
||||
_, _, err = c.GetRepo(user.UserName, repoName)
|
||||
if err != nil {
|
||||
repo, err := c.CreateRepo(CreateRepoOption{
|
||||
repo, _, err := c.CreateRepo(CreateRepoOption{
|
||||
Name: repoName,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, repo)
|
||||
}
|
||||
|
||||
err = c.DeleteRepo(user.UserName, repoName)
|
||||
_, err = c.DeleteRepo(user.UserName, repoName)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestRepoMigrateAndLanguages(t *testing.T) {
|
||||
log.Println("== TestMigrateRepo ==")
|
||||
c := newTestClient()
|
||||
user, _, uErr := c.GetMyUserInfo()
|
||||
assert.NoError(t, uErr)
|
||||
_, _, err := c.GetRepo(user.UserName, "sdk-mirror")
|
||||
if err == nil {
|
||||
_, _ = c.DeleteRepo(user.UserName, "sdk-mirror")
|
||||
}
|
||||
|
||||
repoM, _, err := c.MigrateRepo(MigrateRepoOption{
|
||||
CloneAddr: "https://gitea.com/gitea/go-sdk.git",
|
||||
RepoName: "sdk-mirror",
|
||||
RepoOwner: user.UserName,
|
||||
Mirror: true,
|
||||
Private: false,
|
||||
Description: "mirror sdk",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
|
||||
repoG, _, err := c.GetRepo(repoM.Owner.UserName, repoM.Name)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, repoM.ID, repoG.ID)
|
||||
assert.EqualValues(t, "master", repoG.DefaultBranch)
|
||||
assert.True(t, repoG.Mirror)
|
||||
assert.False(t, repoG.Empty)
|
||||
assert.EqualValues(t, 1, repoG.Watchers)
|
||||
|
||||
log.Println("== TestRepoLanguages ==")
|
||||
time.Sleep(time.Second)
|
||||
lang, _, err := c.GetRepoLanguages(repoM.Owner.UserName, repoM.Name)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, lang, 2)
|
||||
assert.True(t, 217441 < lang["Go"])
|
||||
assert.True(t, 3614 < lang["Makefile"] && 6000 > lang["Makefile"])
|
||||
}
|
||||
|
||||
func TestSearchRepo(t *testing.T) {
|
||||
log.Println("== TestSearchRepo ==")
|
||||
c := newTestClient()
|
||||
|
||||
repo, err := createTestRepo(t, "RepoSearch1", c)
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, c.AddRepoTopic(repo.Owner.UserName, repo.Name, "TestTopic1"))
|
||||
assert.NoError(t, c.AddRepoTopic(repo.Owner.UserName, repo.Name, "TestTopic2"))
|
||||
_, err = c.AddRepoTopic(repo.Owner.UserName, repo.Name, "TestTopic1")
|
||||
assert.NoError(t, err)
|
||||
_, err = c.AddRepoTopic(repo.Owner.UserName, repo.Name, "TestTopic2")
|
||||
assert.NoError(t, err)
|
||||
|
||||
repo, err = createTestRepo(t, "RepoSearch2", c)
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, c.AddRepoTopic(repo.Owner.UserName, repo.Name, "TestTopic1"))
|
||||
_, err = c.AddRepoTopic(repo.Owner.UserName, repo.Name, "TestTopic1")
|
||||
assert.NoError(t, err)
|
||||
|
||||
repos, err := c.SearchRepos(SearchRepoOptions{
|
||||
repos, _, err := c.SearchRepos(SearchRepoOptions{
|
||||
Keyword: "Search1",
|
||||
KeywordInDescription: true,
|
||||
})
|
||||
|
@ -52,7 +93,7 @@ func TestSearchRepo(t *testing.T) {
|
|||
assert.NotNil(t, repos)
|
||||
assert.Len(t, repos, 1)
|
||||
|
||||
repos, err = c.SearchRepos(SearchRepoOptions{
|
||||
repos, _, err = c.SearchRepos(SearchRepoOptions{
|
||||
Keyword: "Search",
|
||||
KeywordInDescription: true,
|
||||
})
|
||||
|
@ -60,7 +101,7 @@ func TestSearchRepo(t *testing.T) {
|
|||
assert.NotNil(t, repos)
|
||||
assert.Len(t, repos, 2)
|
||||
|
||||
repos, err = c.SearchRepos(SearchRepoOptions{
|
||||
repos, _, err = c.SearchRepos(SearchRepoOptions{
|
||||
Keyword: "TestTopic1",
|
||||
KeywordInDescription: true,
|
||||
})
|
||||
|
@ -68,7 +109,7 @@ func TestSearchRepo(t *testing.T) {
|
|||
assert.NotNil(t, repos)
|
||||
assert.Len(t, repos, 2)
|
||||
|
||||
repos, err = c.SearchRepos(SearchRepoOptions{
|
||||
repos, _, err = c.SearchRepos(SearchRepoOptions{
|
||||
Keyword: "TestTopic2",
|
||||
KeywordInDescription: true,
|
||||
})
|
||||
|
@ -76,7 +117,7 @@ func TestSearchRepo(t *testing.T) {
|
|||
assert.NotNil(t, repos)
|
||||
assert.Len(t, repos, 1)
|
||||
|
||||
err = c.DeleteRepo(repo.Owner.UserName, repo.Name)
|
||||
_, err = c.DeleteRepo(repo.Owner.UserName, repo.Name)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
|
@ -84,18 +125,29 @@ func TestDeleteRepo(t *testing.T) {
|
|||
log.Println("== TestDeleteRepo ==")
|
||||
c := newTestClient()
|
||||
repo, _ := createTestRepo(t, "TestDeleteRepo", c)
|
||||
assert.NoError(t, c.DeleteRepo(repo.Owner.UserName, repo.Name))
|
||||
_, err := c.DeleteRepo(repo.Owner.UserName, repo.Name)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestGetArchive(t *testing.T) {
|
||||
log.Println("== TestGetArchive ==")
|
||||
c := newTestClient()
|
||||
repo, _ := createTestRepo(t, "ToDownload", c)
|
||||
time.Sleep(time.Second / 2)
|
||||
archive, _, err := c.GetArchive(repo.Owner.UserName, repo.Name, "master", ZipArchive)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 1620, len(archive))
|
||||
}
|
||||
|
||||
// standard func to create a init repo for test routines
|
||||
func createTestRepo(t *testing.T, name string, c *Client) (*Repository, error) {
|
||||
user, uErr := c.GetMyUserInfo()
|
||||
user, _, uErr := c.GetMyUserInfo()
|
||||
assert.NoError(t, uErr)
|
||||
_, err := c.GetRepo(user.UserName, name)
|
||||
_, _, err := c.GetRepo(user.UserName, name)
|
||||
if err == nil {
|
||||
_ = c.DeleteRepo(user.UserName, name)
|
||||
_, _ = c.DeleteRepo(user.UserName, name)
|
||||
}
|
||||
repo, err := c.CreateRepo(CreateRepoOption{
|
||||
repo, _, err := c.CreateRepo(CreateRepoOption{
|
||||
Name: name,
|
||||
Description: "A test Repo: " + name,
|
||||
AutoInit: true,
|
||||
|
|
|
@ -21,38 +21,38 @@ type topicsList struct {
|
|||
}
|
||||
|
||||
// ListRepoTopics list all repository's topics
|
||||
func (c *Client) ListRepoTopics(user, repo string, opt ListRepoTopicsOptions) ([]string, error) {
|
||||
func (c *Client) ListRepoTopics(user, repo string, opt ListRepoTopicsOptions) ([]string, *Response, error) {
|
||||
opt.setDefaults()
|
||||
|
||||
list := new(topicsList)
|
||||
err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/topics?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, list)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/topics?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, list)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, resp, err
|
||||
}
|
||||
return list.Topics, nil
|
||||
return list.Topics, resp, nil
|
||||
}
|
||||
|
||||
// SetRepoTopics replaces the list of repo's topics
|
||||
func (c *Client) SetRepoTopics(user, repo string, list []string) error {
|
||||
func (c *Client) SetRepoTopics(user, repo string, list []string) (*Response, error) {
|
||||
|
||||
l := topicsList{Topics: list}
|
||||
|
||||
body, err := json.Marshal(&l)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
_, err = c.getResponse("PUT", fmt.Sprintf("/repos/%s/%s/topics", user, repo), jsonHeader, bytes.NewReader(body))
|
||||
return err
|
||||
_, resp, err := c.getResponse("PUT", fmt.Sprintf("/repos/%s/%s/topics", user, repo), jsonHeader, bytes.NewReader(body))
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// AddRepoTopic adds a topic to a repo's topics list
|
||||
func (c *Client) AddRepoTopic(user, repo, topic string) error {
|
||||
_, err := c.getResponse("PUT", fmt.Sprintf("/repos/%s/%s/topics/%s", user, repo, topic), nil, nil)
|
||||
return err
|
||||
func (c *Client) AddRepoTopic(user, repo, topic string) (*Response, error) {
|
||||
_, resp, err := c.getResponse("PUT", fmt.Sprintf("/repos/%s/%s/topics/%s", user, repo, topic), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// DeleteRepoTopic deletes a topic from repo's topics list
|
||||
func (c *Client) DeleteRepoTopic(user, repo, topic string) error {
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/topics/%s", user, repo, topic), nil, nil)
|
||||
return err
|
||||
func (c *Client) DeleteRepoTopic(user, repo, topic string) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/topics/%s", user, repo, topic), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
|
|
@ -19,34 +19,34 @@ func TestRepoTopics(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
|
||||
// Add
|
||||
err = c.AddRepoTopic(repo.Owner.UserName, repo.Name, "best")
|
||||
_, err = c.AddRepoTopic(repo.Owner.UserName, repo.Name, "best")
|
||||
assert.NoError(t, err)
|
||||
err = c.AddRepoTopic(repo.Owner.UserName, repo.Name, "git")
|
||||
_, err = c.AddRepoTopic(repo.Owner.UserName, repo.Name, "git")
|
||||
assert.NoError(t, err)
|
||||
err = c.AddRepoTopic(repo.Owner.UserName, repo.Name, "gitea")
|
||||
_, err = c.AddRepoTopic(repo.Owner.UserName, repo.Name, "gitea")
|
||||
assert.NoError(t, err)
|
||||
err = c.AddRepoTopic(repo.Owner.UserName, repo.Name, "drone")
|
||||
_, err = c.AddRepoTopic(repo.Owner.UserName, repo.Name, "drone")
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Get List
|
||||
tl, err := c.ListRepoTopics(repo.Owner.UserName, repo.Name, ListRepoTopicsOptions{})
|
||||
tl, _, err := c.ListRepoTopics(repo.Owner.UserName, repo.Name, ListRepoTopicsOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, tl, 4)
|
||||
|
||||
// Del
|
||||
err = c.DeleteRepoTopic(repo.Owner.UserName, repo.Name, "drone")
|
||||
_, err = c.DeleteRepoTopic(repo.Owner.UserName, repo.Name, "drone")
|
||||
assert.NoError(t, err)
|
||||
err = c.DeleteRepoTopic(repo.Owner.UserName, repo.Name, "best")
|
||||
_, err = c.DeleteRepoTopic(repo.Owner.UserName, repo.Name, "best")
|
||||
assert.NoError(t, err)
|
||||
tl, err = c.ListRepoTopics(repo.Owner.UserName, repo.Name, ListRepoTopicsOptions{})
|
||||
tl, _, err = c.ListRepoTopics(repo.Owner.UserName, repo.Name, ListRepoTopicsOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, tl, 2)
|
||||
|
||||
// Set List
|
||||
newTopics := []string{"analog", "digital", "cat"}
|
||||
err = c.SetRepoTopics(repo.Owner.UserName, repo.Name, newTopics)
|
||||
_, err = c.SetRepoTopics(repo.Owner.UserName, repo.Name, newTopics)
|
||||
assert.NoError(t, err)
|
||||
tl, _ = c.ListRepoTopics(repo.Owner.UserName, repo.Name, ListRepoTopicsOptions{})
|
||||
tl, _, _ = c.ListRepoTopics(repo.Owner.UserName, repo.Name, ListRepoTopicsOptions{})
|
||||
assert.Len(t, tl, 3)
|
||||
|
||||
sort.Strings(tl)
|
||||
|
|
|
@ -19,14 +19,15 @@ type TransferRepoOption struct {
|
|||
}
|
||||
|
||||
// TransferRepo transfers the ownership of a repository
|
||||
func (c *Client) TransferRepo(owner, reponame string, opt TransferRepoOption) (*Repository, error) {
|
||||
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
||||
return nil, err
|
||||
func (c *Client) TransferRepo(owner, reponame string, opt TransferRepoOption) (*Repository, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
repo := new(Repository)
|
||||
return repo, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/transfer", owner, reponame), jsonHeader, bytes.NewReader(body), repo)
|
||||
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/transfer", owner, reponame), jsonHeader, bytes.NewReader(body), repo)
|
||||
return repo, resp, err
|
||||
}
|
||||
|
|
|
@ -15,21 +15,24 @@ func TestRepoTransfer(t *testing.T) {
|
|||
log.Printf("== TestRepoTransfer ==")
|
||||
c := newTestClient()
|
||||
|
||||
org, err := c.AdminCreateOrg(c.username, CreateOrgOption{UserName: "TransferOrg"})
|
||||
org, _, err := c.AdminCreateOrg(c.username, CreateOrgOption{Name: "TransferOrg"})
|
||||
assert.NoError(t, err)
|
||||
repo, err := createTestRepo(t, "ToMove", c)
|
||||
assert.NoError(t, err)
|
||||
|
||||
newRepo, err := c.TransferRepo(repo.Owner.UserName, repo.Name, TransferRepoOption{NewOwner: org.UserName})
|
||||
newRepo, _, err := c.TransferRepo(repo.Owner.UserName, repo.Name, TransferRepoOption{NewOwner: org.UserName})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, newRepo)
|
||||
|
||||
repo, err = createTestRepo(t, "ToMove", c)
|
||||
assert.NoError(t, err)
|
||||
_, err = c.TransferRepo(repo.Owner.UserName, repo.Name, TransferRepoOption{NewOwner: org.UserName})
|
||||
_, _, err = c.TransferRepo(repo.Owner.UserName, repo.Name, TransferRepoOption{NewOwner: org.UserName})
|
||||
assert.Error(t, err)
|
||||
|
||||
assert.NoError(t, c.DeleteRepo(repo.Owner.UserName, repo.Name))
|
||||
assert.NoError(t, c.DeleteRepo(newRepo.Owner.UserName, newRepo.Name))
|
||||
assert.NoError(t, c.DeleteOrg(org.UserName))
|
||||
_, err = c.DeleteRepo(repo.Owner.UserName, repo.Name)
|
||||
assert.NoError(t, err)
|
||||
_, err = c.DeleteRepo(newRepo.Owner.UserName, newRepo.Name)
|
||||
assert.NoError(t, err)
|
||||
_, err = c.DeleteOrg(org.UserName)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
|
|
@ -30,11 +30,12 @@ type GitTreeResponse struct {
|
|||
|
||||
// GetTrees downloads a file of repository, ref can be branch/tag/commit.
|
||||
// e.g.: ref -> master, tree -> macaron.go(no leading slash)
|
||||
func (c *Client) GetTrees(user, repo, ref string, recursive bool) (*GitTreeResponse, error) {
|
||||
func (c *Client) GetTrees(user, repo, ref string, recursive bool) (*GitTreeResponse, *Response, error) {
|
||||
trees := new(GitTreeResponse)
|
||||
var path = fmt.Sprintf("/repos/%s/%s/git/trees/%s", user, repo, ref)
|
||||
if recursive {
|
||||
path += "?recursive=1"
|
||||
}
|
||||
return trees, c.getParsedResponse("GET", path, nil, nil, trees)
|
||||
resp, err := c.getParsedResponse("GET", path, nil, nil, trees)
|
||||
return trees, resp, err
|
||||
}
|
||||
|
|
|
@ -21,53 +21,55 @@ type WatchInfo struct {
|
|||
}
|
||||
|
||||
// GetWatchedRepos list all the watched repos of user
|
||||
func (c *Client) GetWatchedRepos(user string) ([]*Repository, error) {
|
||||
func (c *Client) GetWatchedRepos(user string) ([]*Repository, *Response, error) {
|
||||
repos := make([]*Repository, 0, 10)
|
||||
return repos, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/subscriptions", user), nil, nil, &repos)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/users/%s/subscriptions", user), nil, nil, &repos)
|
||||
return repos, resp, err
|
||||
}
|
||||
|
||||
// GetMyWatchedRepos list repositories watched by the authenticated user
|
||||
func (c *Client) GetMyWatchedRepos() ([]*Repository, error) {
|
||||
func (c *Client) GetMyWatchedRepos() ([]*Repository, *Response, error) {
|
||||
repos := make([]*Repository, 0, 10)
|
||||
return repos, c.getParsedResponse("GET", fmt.Sprintf("/user/subscriptions"), nil, nil, &repos)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/user/subscriptions"), nil, nil, &repos)
|
||||
return repos, resp, err
|
||||
}
|
||||
|
||||
// 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)
|
||||
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)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, resp, err
|
||||
}
|
||||
switch status {
|
||||
case http.StatusNotFound:
|
||||
return false, nil
|
||||
return false, resp, nil
|
||||
case http.StatusOK:
|
||||
return true, nil
|
||||
return true, resp, nil
|
||||
default:
|
||||
return false, fmt.Errorf("unexpected Status: %d", status)
|
||||
return false, resp, fmt.Errorf("unexpected Status: %d", status)
|
||||
}
|
||||
}
|
||||
|
||||
// WatchRepo start to watch a repository
|
||||
func (c *Client) WatchRepo(repoUser, repoName string) error {
|
||||
status, err := c.getStatusCode("PUT", fmt.Sprintf("/repos/%s/%s/subscription", repoUser, repoName), nil, nil)
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
return resp, err
|
||||
}
|
||||
if status == http.StatusOK {
|
||||
return nil
|
||||
return resp, nil
|
||||
}
|
||||
return fmt.Errorf("unexpected Status: %d", status)
|
||||
return resp, fmt.Errorf("unexpected Status: %d", status)
|
||||
}
|
||||
|
||||
// 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)
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
return resp, err
|
||||
}
|
||||
if status == http.StatusNoContent {
|
||||
return nil
|
||||
return resp, nil
|
||||
}
|
||||
return fmt.Errorf("unexpected Status: %d", status)
|
||||
return resp, fmt.Errorf("unexpected Status: %d", status)
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ import (
|
|||
func TestRepoWatch(t *testing.T) {
|
||||
log.Printf("== TestRepoWatch ==")
|
||||
c := newTestClient()
|
||||
rawVersion, err := c.ServerVersion()
|
||||
rawVersion, _, err := c.ServerVersion()
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, true, rawVersion != "")
|
||||
|
||||
|
@ -23,30 +23,30 @@ func TestRepoWatch(t *testing.T) {
|
|||
assert.NotEqual(t, repo1, repo2)
|
||||
|
||||
//GetWatchedRepos
|
||||
wl, err := c.GetWatchedRepos("test01")
|
||||
wl, _, err := c.GetWatchedRepos("test01")
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, wl)
|
||||
maxcount := len(wl)
|
||||
|
||||
//GetMyWatchedRepos
|
||||
wl, err = c.GetMyWatchedRepos()
|
||||
wl, _, err = c.GetMyWatchedRepos()
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, wl, maxcount)
|
||||
|
||||
//CheckRepoWatch
|
||||
isWatching, err := c.CheckRepoWatch(repo1.Owner.UserName, repo1.Name)
|
||||
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)
|
||||
_, err = c.UnWatchRepo(repo1.Owner.UserName, repo1.Name)
|
||||
assert.NoError(t, err)
|
||||
isWatching, _ = c.CheckRepoWatch(repo1.Owner.UserName, repo1.Name)
|
||||
isWatching, _, _ = c.CheckRepoWatch(repo1.Owner.UserName, repo1.Name)
|
||||
assert.False(t, isWatching)
|
||||
|
||||
//WatchRepo
|
||||
err = c.WatchRepo(repo1.Owner.UserName, repo1.Name)
|
||||
_, err = c.WatchRepo(repo1.Owner.UserName, repo1.Name)
|
||||
assert.NoError(t, err)
|
||||
isWatching, _ = c.CheckRepoWatch(repo1.Owner.UserName, repo1.Name)
|
||||
isWatching, _, _ = c.CheckRepoWatch(repo1.Owner.UserName, repo1.Name)
|
||||
assert.True(t, isWatching)
|
||||
}
|
||||
|
|
|
@ -15,20 +15,58 @@ type GlobalRepoSettings struct {
|
|||
HTTPGitDisabled bool `json:"http_git_disabled"`
|
||||
}
|
||||
|
||||
// GlobalAPISettings contains global api settings exposed by it
|
||||
type GlobalAPISettings struct {
|
||||
MaxResponseItems int `json:"max_response_items"`
|
||||
DefaultPagingNum int `json:"default_paging_num"`
|
||||
DefaultGitTreesPerPage int `json:"default_git_trees_per_page"`
|
||||
DefaultMaxBlobSize int64 `json:"default_max_blob_size"`
|
||||
}
|
||||
|
||||
// GlobalAttachmentSettings contains global Attachment settings exposed by API
|
||||
type GlobalAttachmentSettings struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
AllowedTypes string `json:"allowed_types"`
|
||||
MaxSize int64 `json:"max_size"`
|
||||
MaxFiles int `json:"max_files"`
|
||||
}
|
||||
|
||||
// GetGlobalUISettings get global ui settings witch are exposed by API
|
||||
func (c *Client) GetGlobalUISettings() (settings *GlobalUISettings, err error) {
|
||||
if err := c.CheckServerVersionConstraint(">=1.13.0"); err != nil {
|
||||
return nil, err
|
||||
func (c *Client) GetGlobalUISettings() (*GlobalUISettings, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_13_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
conf := new(GlobalUISettings)
|
||||
return conf, c.getParsedResponse("GET", "/settings/ui", jsonHeader, nil, &conf)
|
||||
resp, err := c.getParsedResponse("GET", "/settings/ui", jsonHeader, nil, &conf)
|
||||
return conf, resp, err
|
||||
}
|
||||
|
||||
// GetGlobalRepoSettings get global repository settings witch are exposed by API
|
||||
func (c *Client) GetGlobalRepoSettings() (settings *GlobalRepoSettings, err error) {
|
||||
if err := c.CheckServerVersionConstraint(">=1.13.0"); err != nil {
|
||||
return nil, err
|
||||
func (c *Client) GetGlobalRepoSettings() (*GlobalRepoSettings, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_13_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
conf := new(GlobalRepoSettings)
|
||||
return conf, c.getParsedResponse("GET", "/settings/repository", jsonHeader, nil, &conf)
|
||||
resp, err := c.getParsedResponse("GET", "/settings/repository", jsonHeader, nil, &conf)
|
||||
return conf, resp, err
|
||||
}
|
||||
|
||||
// GetGlobalAPISettings get global api settings witch are exposed by it
|
||||
func (c *Client) GetGlobalAPISettings() (*GlobalAPISettings, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_13_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
conf := new(GlobalAPISettings)
|
||||
resp, err := c.getParsedResponse("GET", "/settings/api", jsonHeader, nil, &conf)
|
||||
return conf, resp, err
|
||||
}
|
||||
|
||||
// GetGlobalAttachmentSettings get global repository settings witch are exposed by API
|
||||
func (c *Client) GetGlobalAttachmentSettings() (*GlobalAttachmentSettings, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_13_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
conf := new(GlobalAttachmentSettings)
|
||||
resp, err := c.getParsedResponse("GET", "/settings/attachment", jsonHeader, nil, &conf)
|
||||
return conf, resp, err
|
||||
}
|
||||
|
|
|
@ -15,13 +15,33 @@ func TestGetGlobalSettings(t *testing.T) {
|
|||
log.Println("== TestGetGlobalSettings ==")
|
||||
c := newTestClient()
|
||||
|
||||
uiSettings, err := c.GetGlobalUISettings()
|
||||
uiSettings, _, err := c.GetGlobalUISettings()
|
||||
assert.NoError(t, err)
|
||||
expectedAllowedReactions := []string{"+1", "-1", "laugh", "hooray", "confused", "heart", "rocket", "eyes"}
|
||||
assert.ElementsMatch(t, expectedAllowedReactions, uiSettings.AllowedReactions)
|
||||
|
||||
repoSettings, err := c.GetGlobalRepoSettings()
|
||||
repoSettings, _, err := c.GetGlobalRepoSettings()
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, repoSettings.HTTPGitDisabled)
|
||||
assert.False(t, repoSettings.MirrorsDisabled)
|
||||
assert.EqualValues(t, &GlobalRepoSettings{
|
||||
HTTPGitDisabled: false,
|
||||
MirrorsDisabled: false,
|
||||
}, repoSettings)
|
||||
|
||||
apiSettings, _, err := c.GetGlobalAPISettings()
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, &GlobalAPISettings{
|
||||
MaxResponseItems: 50,
|
||||
DefaultPagingNum: 30,
|
||||
DefaultGitTreesPerPage: 1000,
|
||||
DefaultMaxBlobSize: 10485760,
|
||||
}, apiSettings)
|
||||
|
||||
attachSettings, _, err := c.GetGlobalAttachmentSettings()
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, &GlobalAttachmentSettings{
|
||||
Enabled: true,
|
||||
AllowedTypes: ".docx,.gif,.gz,.jpeg,.jpg,.log,.pdf,.png,.pptx,.txt,.xlsx,.zip",
|
||||
MaxSize: 4,
|
||||
MaxFiles: 5,
|
||||
}, attachSettings)
|
||||
}
|
||||
|
|
|
@ -50,13 +50,14 @@ type CreateStatusOption struct {
|
|||
}
|
||||
|
||||
// CreateStatus creates a new Status for a given Commit
|
||||
func (c *Client) CreateStatus(owner, repo, sha string, opts CreateStatusOption) (*Status, error) {
|
||||
func (c *Client) CreateStatus(owner, repo, sha string, opts CreateStatusOption) (*Status, *Response, error) {
|
||||
body, err := json.Marshal(&opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
status := new(Status)
|
||||
return status, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/statuses/%s", owner, repo, sha), jsonHeader, bytes.NewReader(body), status)
|
||||
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/statuses/%s", owner, repo, sha), jsonHeader, bytes.NewReader(body), status)
|
||||
return status, resp, err
|
||||
}
|
||||
|
||||
// ListStatusesOption options for listing a repository's commit's statuses
|
||||
|
@ -65,10 +66,11 @@ type ListStatusesOption struct {
|
|||
}
|
||||
|
||||
// ListStatuses returns all statuses for a given Commit
|
||||
func (c *Client) ListStatuses(owner, repo, sha string, opt ListStatusesOption) ([]*Status, error) {
|
||||
func (c *Client) ListStatuses(owner, repo, sha string, opt ListStatusesOption) ([]*Status, *Response, error) {
|
||||
opt.setDefaults()
|
||||
statuses := make([]*Status, 0, opt.PageSize)
|
||||
return statuses, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s/statuses?%s", owner, repo, sha, opt.getURLQuery().Encode()), nil, nil, &statuses)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s/statuses?%s", owner, repo, sha, opt.getURLQuery().Encode()), nil, nil, &statuses)
|
||||
return statuses, resp, err
|
||||
}
|
||||
|
||||
// CombinedStatus holds the combined state of several statuses for a single commit
|
||||
|
@ -83,7 +85,8 @@ type CombinedStatus struct {
|
|||
}
|
||||
|
||||
// GetCombinedStatus returns the CombinedStatus for a given Commit
|
||||
func (c *Client) GetCombinedStatus(owner, repo, sha string) (*CombinedStatus, error) {
|
||||
func (c *Client) GetCombinedStatus(owner, repo, sha string) (*CombinedStatus, *Response, error) {
|
||||
status := new(CombinedStatus)
|
||||
return status, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s/status", owner, repo, sha), nil, nil, status)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s/status", owner, repo, sha), nil, nil, status)
|
||||
return status, resp, err
|
||||
}
|
||||
|
|
|
@ -29,15 +29,15 @@ type User struct {
|
|||
}
|
||||
|
||||
// GetUserInfo get user info by user's name
|
||||
func (c *Client) GetUserInfo(user string) (*User, error) {
|
||||
func (c *Client) GetUserInfo(user string) (*User, *Response, error) {
|
||||
u := new(User)
|
||||
err := c.getParsedResponse("GET", fmt.Sprintf("/users/%s", user), nil, nil, u)
|
||||
return u, err
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/users/%s", user), nil, nil, u)
|
||||
return u, resp, err
|
||||
}
|
||||
|
||||
// GetMyUserInfo get user info of current user
|
||||
func (c *Client) GetMyUserInfo() (*User, error) {
|
||||
func (c *Client) GetMyUserInfo() (*User, *Response, error) {
|
||||
u := new(User)
|
||||
err := c.getParsedResponse("GET", "/user", nil, nil, u)
|
||||
return u, err
|
||||
resp, err := c.getParsedResponse("GET", "/user", nil, nil, u)
|
||||
return u, resp, err
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// AccessToken represents an API access token.
|
||||
|
@ -25,13 +26,14 @@ type ListAccessTokensOptions struct {
|
|||
}
|
||||
|
||||
// ListAccessTokens lists all the access tokens of user
|
||||
func (c *Client) ListAccessTokens(opts ListAccessTokensOptions) ([]*AccessToken, error) {
|
||||
func (c *Client) ListAccessTokens(opts ListAccessTokensOptions) ([]*AccessToken, *Response, error) {
|
||||
if len(c.username) == 0 {
|
||||
return nil, fmt.Errorf("\"username\" not set: only BasicAuth allowed")
|
||||
return nil, nil, fmt.Errorf("\"username\" not set: only BasicAuth allowed")
|
||||
}
|
||||
opts.setDefaults()
|
||||
tokens := make([]*AccessToken, 0, opts.PageSize)
|
||||
return tokens, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/tokens?%s", c.username, opts.getURLQuery().Encode()), jsonHeader, nil, &tokens)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/users/%s/tokens?%s", c.username, opts.getURLQuery().Encode()), jsonHeader, nil, &tokens)
|
||||
return tokens, resp, err
|
||||
}
|
||||
|
||||
// CreateAccessTokenOption options when create access token
|
||||
|
@ -40,23 +42,39 @@ type CreateAccessTokenOption struct {
|
|||
}
|
||||
|
||||
// CreateAccessToken create one access token with options
|
||||
func (c *Client) CreateAccessToken(opt CreateAccessTokenOption) (*AccessToken, error) {
|
||||
func (c *Client) CreateAccessToken(opt CreateAccessTokenOption) (*AccessToken, *Response, error) {
|
||||
if len(c.username) == 0 {
|
||||
return nil, fmt.Errorf("\"username\" not set: only BasicAuth allowed")
|
||||
return nil, nil, fmt.Errorf("\"username\" not set: only BasicAuth allowed")
|
||||
}
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
t := new(AccessToken)
|
||||
return t, c.getParsedResponse("POST", fmt.Sprintf("/users/%s/tokens", c.username), jsonHeader, bytes.NewReader(body), t)
|
||||
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/users/%s/tokens", c.username), jsonHeader, bytes.NewReader(body), t)
|
||||
return t, resp, err
|
||||
}
|
||||
|
||||
// DeleteAccessToken delete token with key id
|
||||
func (c *Client) DeleteAccessToken(keyID int64) error {
|
||||
// DeleteAccessToken delete token, identified by ID and if not available by name
|
||||
func (c *Client) DeleteAccessToken(value interface{}) (*Response, error) {
|
||||
if len(c.username) == 0 {
|
||||
return fmt.Errorf("\"username\" not set: only BasicAuth allowed")
|
||||
return nil, fmt.Errorf("\"username\" not set: only BasicAuth allowed")
|
||||
}
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/users/%s/tokens/%d", c.username, keyID), jsonHeader, nil)
|
||||
return err
|
||||
|
||||
var token = ""
|
||||
|
||||
switch reflect.ValueOf(value).Kind() {
|
||||
case reflect.Int64:
|
||||
token = fmt.Sprintf("%d", value.(int64))
|
||||
case reflect.String:
|
||||
if err := c.CheckServerVersionConstraint(">= 1.13.0"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
token = value.(string)
|
||||
default:
|
||||
return nil, fmt.Errorf("only string and int64 supported")
|
||||
}
|
||||
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/users/%s/tokens/%s", c.username, token), jsonHeader, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
|
|
@ -23,10 +23,11 @@ type ListEmailsOptions struct {
|
|||
}
|
||||
|
||||
// ListEmails all the email addresses of user
|
||||
func (c *Client) ListEmails(opt ListEmailsOptions) ([]*Email, error) {
|
||||
func (c *Client) ListEmails(opt ListEmailsOptions) ([]*Email, *Response, error) {
|
||||
opt.setDefaults()
|
||||
emails := make([]*Email, 0, opt.PageSize)
|
||||
return emails, c.getParsedResponse("GET", fmt.Sprintf("/user/emails?%s", opt.getURLQuery().Encode()), nil, nil, &emails)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/user/emails?%s", opt.getURLQuery().Encode()), nil, nil, &emails)
|
||||
return emails, resp, err
|
||||
}
|
||||
|
||||
// CreateEmailOption options when creating email addresses
|
||||
|
@ -36,13 +37,14 @@ type CreateEmailOption struct {
|
|||
}
|
||||
|
||||
// AddEmail add one email to current user with options
|
||||
func (c *Client) AddEmail(opt CreateEmailOption) ([]*Email, error) {
|
||||
func (c *Client) AddEmail(opt CreateEmailOption) ([]*Email, *Response, error) {
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
emails := make([]*Email, 0, 3)
|
||||
return emails, c.getParsedResponse("POST", "/user/emails", jsonHeader, bytes.NewReader(body), &emails)
|
||||
resp, err := c.getParsedResponse("POST", "/user/emails", jsonHeader, bytes.NewReader(body), &emails)
|
||||
return emails, resp, err
|
||||
}
|
||||
|
||||
// DeleteEmailOption options when deleting email addresses
|
||||
|
@ -52,11 +54,11 @@ type DeleteEmailOption struct {
|
|||
}
|
||||
|
||||
// DeleteEmail delete one email of current users'
|
||||
func (c *Client) DeleteEmail(opt DeleteEmailOption) error {
|
||||
func (c *Client) DeleteEmail(opt DeleteEmailOption) (*Response, error) {
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
_, err = c.getResponse("DELETE", "/user/emails", jsonHeader, bytes.NewReader(body))
|
||||
return err
|
||||
_, resp, err := c.getResponse("DELETE", "/user/emails", jsonHeader, bytes.NewReader(body))
|
||||
return resp, err
|
||||
}
|
||||
|
|
|
@ -12,17 +12,19 @@ type ListFollowersOptions struct {
|
|||
}
|
||||
|
||||
// ListMyFollowers list all the followers of current user
|
||||
func (c *Client) ListMyFollowers(opt ListFollowersOptions) ([]*User, error) {
|
||||
func (c *Client) ListMyFollowers(opt ListFollowersOptions) ([]*User, *Response, error) {
|
||||
opt.setDefaults()
|
||||
users := make([]*User, 0, opt.PageSize)
|
||||
return users, c.getParsedResponse("GET", fmt.Sprintf("/user/followers?%s", opt.getURLQuery().Encode()), nil, nil, &users)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/user/followers?%s", opt.getURLQuery().Encode()), nil, nil, &users)
|
||||
return users, resp, err
|
||||
}
|
||||
|
||||
// ListFollowers list all the followers of one user
|
||||
func (c *Client) ListFollowers(user string, opt ListFollowersOptions) ([]*User, error) {
|
||||
func (c *Client) ListFollowers(user string, opt ListFollowersOptions) ([]*User, *Response, error) {
|
||||
opt.setDefaults()
|
||||
users := make([]*User, 0, opt.PageSize)
|
||||
return users, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/followers?%s", user, opt.getURLQuery().Encode()), nil, nil, &users)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/users/%s/followers?%s", user, opt.getURLQuery().Encode()), nil, nil, &users)
|
||||
return users, resp, err
|
||||
}
|
||||
|
||||
// ListFollowingOptions options for listing a user's users being followed
|
||||
|
@ -31,39 +33,41 @@ type ListFollowingOptions struct {
|
|||
}
|
||||
|
||||
// ListMyFollowing list all the users current user followed
|
||||
func (c *Client) ListMyFollowing(opt ListFollowingOptions) ([]*User, error) {
|
||||
func (c *Client) ListMyFollowing(opt ListFollowingOptions) ([]*User, *Response, error) {
|
||||
opt.setDefaults()
|
||||
users := make([]*User, 0, opt.PageSize)
|
||||
return users, c.getParsedResponse("GET", fmt.Sprintf("/user/following?%s", opt.getURLQuery().Encode()), nil, nil, &users)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/user/following?%s", opt.getURLQuery().Encode()), nil, nil, &users)
|
||||
return users, resp, err
|
||||
}
|
||||
|
||||
// ListFollowing list all the users the user followed
|
||||
func (c *Client) ListFollowing(user string, opt ListFollowingOptions) ([]*User, error) {
|
||||
func (c *Client) ListFollowing(user string, opt ListFollowingOptions) ([]*User, *Response, error) {
|
||||
opt.setDefaults()
|
||||
users := make([]*User, 0, opt.PageSize)
|
||||
return users, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/following?%s", user, opt.getURLQuery().Encode()), nil, nil, &users)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/users/%s/following?%s", user, opt.getURLQuery().Encode()), nil, nil, &users)
|
||||
return users, resp, err
|
||||
}
|
||||
|
||||
// IsFollowing if current user followed the target
|
||||
func (c *Client) IsFollowing(target string) bool {
|
||||
_, err := c.getResponse("GET", fmt.Sprintf("/user/following/%s", target), nil, nil)
|
||||
return err == nil
|
||||
func (c *Client) IsFollowing(target string) (bool, *Response) {
|
||||
_, resp, err := c.getResponse("GET", fmt.Sprintf("/user/following/%s", target), nil, nil)
|
||||
return err == nil, resp
|
||||
}
|
||||
|
||||
// IsUserFollowing if the user followed the target
|
||||
func (c *Client) IsUserFollowing(user, target string) bool {
|
||||
_, err := c.getResponse("GET", fmt.Sprintf("/users/%s/following/%s", user, target), nil, nil)
|
||||
return err == nil
|
||||
func (c *Client) IsUserFollowing(user, target string) (bool, *Response) {
|
||||
_, resp, err := c.getResponse("GET", fmt.Sprintf("/users/%s/following/%s", user, target), nil, nil)
|
||||
return err == nil, resp
|
||||
}
|
||||
|
||||
// Follow set current user follow the target
|
||||
func (c *Client) Follow(target string) error {
|
||||
_, err := c.getResponse("PUT", fmt.Sprintf("/user/following/%s", target), nil, nil)
|
||||
return err
|
||||
func (c *Client) Follow(target string) (*Response, error) {
|
||||
_, resp, err := c.getResponse("PUT", fmt.Sprintf("/user/following/%s", target), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// Unfollow set current user unfollow the target
|
||||
func (c *Client) Unfollow(target string) error {
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/user/following/%s", target), nil, nil)
|
||||
return err
|
||||
func (c *Client) Unfollow(target string) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/user/following/%s", target), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
|
|
@ -39,23 +39,26 @@ type ListGPGKeysOptions struct {
|
|||
}
|
||||
|
||||
// ListGPGKeys list all the GPG keys of the user
|
||||
func (c *Client) ListGPGKeys(user string, opt ListGPGKeysOptions) ([]*GPGKey, error) {
|
||||
func (c *Client) ListGPGKeys(user string, opt ListGPGKeysOptions) ([]*GPGKey, *Response, error) {
|
||||
opt.setDefaults()
|
||||
keys := make([]*GPGKey, 0, opt.PageSize)
|
||||
return keys, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/gpg_keys?%s", user, opt.getURLQuery().Encode()), nil, nil, &keys)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/users/%s/gpg_keys?%s", user, opt.getURLQuery().Encode()), nil, nil, &keys)
|
||||
return keys, resp, err
|
||||
}
|
||||
|
||||
// ListMyGPGKeys list all the GPG keys of current user
|
||||
func (c *Client) ListMyGPGKeys(opt *ListGPGKeysOptions) ([]*GPGKey, error) {
|
||||
func (c *Client) ListMyGPGKeys(opt *ListGPGKeysOptions) ([]*GPGKey, *Response, error) {
|
||||
opt.setDefaults()
|
||||
keys := make([]*GPGKey, 0, opt.PageSize)
|
||||
return keys, c.getParsedResponse("GET", fmt.Sprintf("/user/gpg_keys?%s", opt.getURLQuery().Encode()), nil, nil, &keys)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/user/gpg_keys?%s", opt.getURLQuery().Encode()), nil, nil, &keys)
|
||||
return keys, resp, err
|
||||
}
|
||||
|
||||
// GetGPGKey get current user's GPG key by key id
|
||||
func (c *Client) GetGPGKey(keyID int64) (*GPGKey, error) {
|
||||
func (c *Client) GetGPGKey(keyID int64) (*GPGKey, *Response, error) {
|
||||
key := new(GPGKey)
|
||||
return key, c.getParsedResponse("GET", fmt.Sprintf("/user/gpg_keys/%d", keyID), nil, nil, &key)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/user/gpg_keys/%d", keyID), nil, nil, &key)
|
||||
return key, resp, err
|
||||
}
|
||||
|
||||
// CreateGPGKeyOption options create user GPG key
|
||||
|
@ -66,17 +69,18 @@ type CreateGPGKeyOption struct {
|
|||
}
|
||||
|
||||
// CreateGPGKey create GPG key with options
|
||||
func (c *Client) CreateGPGKey(opt CreateGPGKeyOption) (*GPGKey, error) {
|
||||
func (c *Client) CreateGPGKey(opt CreateGPGKeyOption) (*GPGKey, *Response, error) {
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
key := new(GPGKey)
|
||||
return key, c.getParsedResponse("POST", "/user/gpg_keys", jsonHeader, bytes.NewReader(body), key)
|
||||
resp, err := c.getParsedResponse("POST", "/user/gpg_keys", jsonHeader, bytes.NewReader(body), key)
|
||||
return key, resp, err
|
||||
}
|
||||
|
||||
// DeleteGPGKey delete GPG key with key id
|
||||
func (c *Client) DeleteGPGKey(keyID int64) error {
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/user/gpg_keys/%d", keyID), nil, nil)
|
||||
return err
|
||||
func (c *Client) DeleteGPGKey(keyID int64) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/user/gpg_keys/%d", keyID), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
|
|
@ -30,23 +30,26 @@ type ListPublicKeysOptions struct {
|
|||
}
|
||||
|
||||
// ListPublicKeys list all the public keys of the user
|
||||
func (c *Client) ListPublicKeys(user string, opt ListPublicKeysOptions) ([]*PublicKey, error) {
|
||||
func (c *Client) ListPublicKeys(user string, opt ListPublicKeysOptions) ([]*PublicKey, *Response, error) {
|
||||
opt.setDefaults()
|
||||
keys := make([]*PublicKey, 0, opt.PageSize)
|
||||
return keys, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/keys?%s", user, opt.getURLQuery().Encode()), nil, nil, &keys)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/users/%s/keys?%s", user, opt.getURLQuery().Encode()), nil, nil, &keys)
|
||||
return keys, resp, err
|
||||
}
|
||||
|
||||
// ListMyPublicKeys list all the public keys of current user
|
||||
func (c *Client) ListMyPublicKeys(opt ListPublicKeysOptions) ([]*PublicKey, error) {
|
||||
func (c *Client) ListMyPublicKeys(opt ListPublicKeysOptions) ([]*PublicKey, *Response, error) {
|
||||
opt.setDefaults()
|
||||
keys := make([]*PublicKey, 0, opt.PageSize)
|
||||
return keys, c.getParsedResponse("GET", fmt.Sprintf("/user/keys?%s", opt.getURLQuery().Encode()), nil, nil, &keys)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/user/keys?%s", opt.getURLQuery().Encode()), nil, nil, &keys)
|
||||
return keys, resp, err
|
||||
}
|
||||
|
||||
// GetPublicKey get current user's public key by key id
|
||||
func (c *Client) GetPublicKey(keyID int64) (*PublicKey, error) {
|
||||
func (c *Client) GetPublicKey(keyID int64) (*PublicKey, *Response, error) {
|
||||
key := new(PublicKey)
|
||||
return key, c.getParsedResponse("GET", fmt.Sprintf("/user/keys/%d", keyID), nil, nil, &key)
|
||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/user/keys/%d", keyID), nil, nil, &key)
|
||||
return key, resp, err
|
||||
}
|
||||
|
||||
// CreateKeyOption options when creating a key
|
||||
|
@ -60,17 +63,18 @@ type CreateKeyOption struct {
|
|||
}
|
||||
|
||||
// CreatePublicKey create public key with options
|
||||
func (c *Client) CreatePublicKey(opt CreateKeyOption) (*PublicKey, error) {
|
||||
func (c *Client) CreatePublicKey(opt CreateKeyOption) (*PublicKey, *Response, error) {
|
||||
body, err := json.Marshal(&opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
key := new(PublicKey)
|
||||
return key, c.getParsedResponse("POST", "/user/keys", jsonHeader, bytes.NewReader(body), key)
|
||||
resp, err := c.getParsedResponse("POST", "/user/keys", jsonHeader, bytes.NewReader(body), key)
|
||||
return key, resp, err
|
||||
}
|
||||
|
||||
// DeletePublicKey delete public key with key id
|
||||
func (c *Client) DeletePublicKey(keyID int64) error {
|
||||
_, err := c.getResponse("DELETE", fmt.Sprintf("/user/keys/%d", keyID), nil, nil)
|
||||
return err
|
||||
func (c *Client) DeletePublicKey(keyID int64) (*Response, error) {
|
||||
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/user/keys/%d", keyID), nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
|
|
@ -35,10 +35,10 @@ func (opt *SearchUsersOption) QueryEncode() string {
|
|||
}
|
||||
|
||||
// SearchUsers finds users by query
|
||||
func (c *Client) SearchUsers(opt SearchUsersOption) ([]*User, error) {
|
||||
func (c *Client) SearchUsers(opt SearchUsersOption) ([]*User, *Response, error) {
|
||||
link, _ := url.Parse("/users/search")
|
||||
link.RawQuery = opt.QueryEncode()
|
||||
resp := new(searchUsersResponse)
|
||||
err := c.getParsedResponse("GET", link.String(), nil, nil, &resp)
|
||||
return resp.Users, err
|
||||
userResp := new(searchUsersResponse)
|
||||
resp, err := c.getParsedResponse("GET", link.String(), nil, nil, &userResp)
|
||||
return userResp.Users, resp, err
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ import (
|
|||
func TestMyUser(t *testing.T) {
|
||||
log.Println("== TestMyUser ==")
|
||||
c := newTestClient()
|
||||
user, err := c.GetMyUserInfo()
|
||||
user, _, err := c.GetMyUserInfo()
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.EqualValues(t, 1, user.ID)
|
||||
|
@ -29,20 +29,20 @@ func TestUserApp(t *testing.T) {
|
|||
log.Println("== TestUserApp ==")
|
||||
c := newTestClient()
|
||||
|
||||
result, err := c.ListAccessTokens(ListAccessTokensOptions{})
|
||||
result, _, err := c.ListAccessTokens(ListAccessTokensOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, result, 1)
|
||||
assert.EqualValues(t, "gitea-admin", result[0].Name)
|
||||
|
||||
t1, err := c.CreateAccessToken(CreateAccessTokenOption{Name: "TestCreateAccessToken"})
|
||||
t1, _, err := c.CreateAccessToken(CreateAccessTokenOption{Name: "TestCreateAccessToken"})
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, "TestCreateAccessToken", t1.Name)
|
||||
result, _ = c.ListAccessTokens(ListAccessTokensOptions{})
|
||||
result, _, _ = c.ListAccessTokens(ListAccessTokensOptions{})
|
||||
assert.Len(t, result, 2)
|
||||
|
||||
err = c.DeleteAccessToken(t1.ID)
|
||||
_, err = c.DeleteAccessToken(t1.ID)
|
||||
assert.NoError(t, err)
|
||||
result, _ = c.ListAccessTokens(ListAccessTokensOptions{})
|
||||
result, _, _ = c.ListAccessTokens(ListAccessTokensOptions{})
|
||||
assert.Len(t, result, 1)
|
||||
}
|
||||
|
||||
|
@ -57,15 +57,15 @@ func TestUserSearch(t *testing.T) {
|
|||
createTestUser(t, "1n2n3n", c)
|
||||
createTestUser(t, "otherIt", c)
|
||||
|
||||
ul, err := c.SearchUsers(SearchUsersOption{KeyWord: "other"})
|
||||
ul, _, err := c.SearchUsers(SearchUsersOption{KeyWord: "other"})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, ul, 1)
|
||||
|
||||
ul, err = c.SearchUsers(SearchUsersOption{KeyWord: "notInTESTcase"})
|
||||
ul, _, err = c.SearchUsers(SearchUsersOption{KeyWord: "notInTESTcase"})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, ul, 0)
|
||||
|
||||
ul, err = c.SearchUsers(SearchUsersOption{KeyWord: "It"})
|
||||
ul, _, err = c.SearchUsers(SearchUsersOption{KeyWord: "It"})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, ul, 2)
|
||||
}
|
||||
|
@ -73,7 +73,7 @@ func TestUserSearch(t *testing.T) {
|
|||
func TestUserFollow(t *testing.T) {
|
||||
log.Println("== TestUserFollow ==")
|
||||
c := newTestClient()
|
||||
me, _ := c.GetMyUserInfo()
|
||||
me, _, _ := c.GetMyUserInfo()
|
||||
|
||||
uA := "uFollow_A"
|
||||
uB := "uFollow_B"
|
||||
|
@ -86,49 +86,51 @@ func TestUserFollow(t *testing.T) {
|
|||
// B follow C & ME
|
||||
// C follow A & B & ME
|
||||
c.sudo = uA
|
||||
err := c.Follow(me.UserName)
|
||||
_, err := c.Follow(me.UserName)
|
||||
assert.NoError(t, err)
|
||||
c.sudo = uB
|
||||
err = c.Follow(me.UserName)
|
||||
_, err = c.Follow(me.UserName)
|
||||
assert.NoError(t, err)
|
||||
err = c.Follow(uC)
|
||||
_, err = c.Follow(uC)
|
||||
assert.NoError(t, err)
|
||||
c.sudo = uC
|
||||
err = c.Follow(me.UserName)
|
||||
_, err = c.Follow(me.UserName)
|
||||
assert.NoError(t, err)
|
||||
err = c.Follow(uA)
|
||||
_, err = c.Follow(uA)
|
||||
assert.NoError(t, err)
|
||||
err = c.Follow(uB)
|
||||
_, err = c.Follow(uB)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// C unfollow me
|
||||
err = c.Unfollow(me.UserName)
|
||||
_, err = c.Unfollow(me.UserName)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// ListMyFollowers of me
|
||||
c.sudo = ""
|
||||
f, err := c.ListMyFollowers(ListFollowersOptions{})
|
||||
f, _, err := c.ListMyFollowers(ListFollowersOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, f, 2)
|
||||
|
||||
// ListFollowers of A
|
||||
f, err = c.ListFollowers(uA, ListFollowersOptions{})
|
||||
f, _, err = c.ListFollowers(uA, ListFollowersOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, f, 1)
|
||||
|
||||
// ListMyFollowing of me
|
||||
f, err = c.ListMyFollowing(ListFollowingOptions{})
|
||||
f, _, err = c.ListMyFollowing(ListFollowingOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, f, 0)
|
||||
|
||||
// ListFollowing of A
|
||||
f, err = c.ListFollowing(uA, ListFollowingOptions{})
|
||||
f, _, err = c.ListFollowing(uA, ListFollowingOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, f, 1)
|
||||
assert.EqualValues(t, me.ID, f[0].ID)
|
||||
|
||||
assert.False(t, c.IsFollowing(uA))
|
||||
assert.True(t, c.IsUserFollowing(uB, uC))
|
||||
isFollow, _ := c.IsFollowing(uA)
|
||||
assert.False(t, isFollow)
|
||||
isFollow, _ = c.IsUserFollowing(uB, uC)
|
||||
assert.True(t, isFollow)
|
||||
}
|
||||
|
||||
func TestUserEmail(t *testing.T) {
|
||||
|
@ -139,7 +141,7 @@ func TestUserEmail(t *testing.T) {
|
|||
c.sudo = userN
|
||||
|
||||
// ListEmails
|
||||
el, err := c.ListEmails(ListEmailsOptions{})
|
||||
el, _, err := c.ListEmails(ListEmailsOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, el, 1)
|
||||
assert.EqualValues(t, "testuseremail@gitea.io", el[0].Email)
|
||||
|
@ -147,35 +149,34 @@ func TestUserEmail(t *testing.T) {
|
|||
|
||||
// AddEmail
|
||||
mails := []string{"wow@mail.send", "speed@mail.me"}
|
||||
el, err = c.AddEmail(CreateEmailOption{Emails: mails})
|
||||
el, _, err = c.AddEmail(CreateEmailOption{Emails: mails})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, el, 2)
|
||||
_, err = c.AddEmail(CreateEmailOption{Emails: []string{mails[1]}})
|
||||
_, _, err = c.AddEmail(CreateEmailOption{Emails: []string{mails[1]}})
|
||||
assert.Error(t, err)
|
||||
el, err = c.ListEmails(ListEmailsOptions{})
|
||||
el, _, err = c.ListEmails(ListEmailsOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, el, 3)
|
||||
|
||||
// DeleteEmail
|
||||
err = c.DeleteEmail(DeleteEmailOption{Emails: []string{mails[1]}})
|
||||
_, err = c.DeleteEmail(DeleteEmailOption{Emails: []string{mails[1]}})
|
||||
assert.NoError(t, err)
|
||||
err = c.DeleteEmail(DeleteEmailOption{Emails: []string{"imaginary@e.de"}})
|
||||
_, err = c.DeleteEmail(DeleteEmailOption{Emails: []string{"imaginary@e.de"}})
|
||||
assert.Error(t, err)
|
||||
|
||||
el, err = c.ListEmails(ListEmailsOptions{})
|
||||
el, _, err = c.ListEmails(ListEmailsOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, el, 2)
|
||||
err = c.DeleteEmail(DeleteEmailOption{Emails: []string{mails[0]}})
|
||||
_, err = c.DeleteEmail(DeleteEmailOption{Emails: []string{mails[0]}})
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func createTestUser(t *testing.T, username string, client *Client) *User {
|
||||
bFalse := false
|
||||
user, _ := client.GetUserInfo(username)
|
||||
user, _, _ := client.GetUserInfo(username)
|
||||
if user.ID != 0 {
|
||||
return user
|
||||
}
|
||||
user, err := client.AdminCreateUser(CreateUserOption{Username: username, Password: username + "!1234", Email: username + "@gitea.io", MustChangePassword: &bFalse, SendNotify: bFalse})
|
||||
user, _, err := client.AdminCreateUser(CreateUserOption{Username: username, Password: username + "!1234", Email: username + "@gitea.io", MustChangePassword: OptionalBool(false), SendNotify: false})
|
||||
assert.NoError(t, err)
|
||||
return user
|
||||
}
|
||||
|
|
|
@ -11,24 +11,19 @@ import (
|
|||
)
|
||||
|
||||
// ServerVersion returns the version of the server
|
||||
func (c *Client) ServerVersion() (string, error) {
|
||||
func (c *Client) ServerVersion() (string, *Response, error) {
|
||||
var v = struct {
|
||||
Version string `json:"version"`
|
||||
}{}
|
||||
return v.Version, c.getParsedResponse("GET", "/version", nil, nil, &v)
|
||||
resp, err := c.getParsedResponse("GET", "/version", nil, nil, &v)
|
||||
return v.Version, resp, err
|
||||
}
|
||||
|
||||
// CheckServerVersionConstraint validates that the login's server satisfies a
|
||||
// given version constraint such as ">= 1.11.0+dev"
|
||||
func (c *Client) CheckServerVersionConstraint(constraint string) error {
|
||||
c.versionLock.RLock()
|
||||
if c.serverVersion == nil {
|
||||
c.versionLock.RUnlock()
|
||||
if err := c.loadClientServerVersion(); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
c.versionLock.RUnlock()
|
||||
if err := c.loadServerVersion(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
check, err := version.NewConstraint(constraint)
|
||||
|
@ -41,17 +36,36 @@ func (c *Client) CheckServerVersionConstraint(constraint string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// loadClientServerVersion init the serverVersion variable
|
||||
func (c *Client) loadClientServerVersion() error {
|
||||
c.versionLock.Lock()
|
||||
defer c.versionLock.Unlock()
|
||||
// predefined versions only have to be parsed by library once
|
||||
var (
|
||||
version1_11_0, _ = version.NewVersion("1.11.0")
|
||||
version1_12_0, _ = version.NewVersion("1.12.0")
|
||||
version1_13_0, _ = version.NewVersion("1.13.0")
|
||||
)
|
||||
|
||||
raw, err := c.ServerVersion()
|
||||
if err != nil {
|
||||
// checkServerVersionGreaterThanOrEqual is internally used to speed up things and ignore issues with prerelease
|
||||
func (c *Client) checkServerVersionGreaterThanOrEqual(v *version.Version) error {
|
||||
if err := c.loadServerVersion(); err != nil {
|
||||
return err
|
||||
}
|
||||
if c.serverVersion, err = version.NewVersion(raw); err != nil {
|
||||
return err
|
||||
|
||||
if !c.serverVersion.GreaterThanOrEqual(v) {
|
||||
return fmt.Errorf("gitea server at %s is older than %s", c.url, v.Original())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// loadServerVersion init the serverVersion variable
|
||||
func (c *Client) loadServerVersion() (err error) {
|
||||
c.getVersionOnce.Do(func() {
|
||||
raw, _, err2 := c.ServerVersion()
|
||||
if err2 != nil {
|
||||
err = err2
|
||||
return
|
||||
}
|
||||
if c.serverVersion, err = version.NewVersion(raw); err != nil {
|
||||
return
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ import (
|
|||
func TestVersion(t *testing.T) {
|
||||
log.Printf("== TestVersion ==")
|
||||
c := newTestClient()
|
||||
rawVersion, err := c.ServerVersion()
|
||||
rawVersion, _, err := c.ServerVersion()
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, true, rawVersion != "")
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user