Enforce golangci-lint + gofumpt (#587)
- Enforce [gofumpt](https://github.com/mvdan/gofumpt) to enforce a more idiomatic go style. - Enforce golangci-lint a bunch of linters! Which were able to detect a few issues in the current codebase and have been fixed by this PR. - Updated the Makefile to use `go install ....` instead of the old deprecated way of `go get` Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/587 Reviewed-by: John Olheiser <john.olheiser@gmail.com> Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: Gusted <williamzijl7@hotmail.com> Co-committed-by: Gusted <williamzijl7@hotmail.com>
This commit is contained in:
parent
ce9d46682d
commit
8fab37e740
29 changed files with 83 additions and 101 deletions
|
@ -36,7 +36,7 @@ steps:
|
||||||
|
|
||||||
- name: testing
|
- name: testing
|
||||||
pull: always
|
pull: always
|
||||||
image: golang:1.16
|
image: golang:1.18
|
||||||
environment:
|
environment:
|
||||||
GOPROXY: "https://goproxy.io"
|
GOPROXY: "https://goproxy.io"
|
||||||
GO111MODULE: "on"
|
GO111MODULE: "on"
|
||||||
|
@ -48,7 +48,7 @@ steps:
|
||||||
commands:
|
commands:
|
||||||
- make clean
|
- make clean
|
||||||
- make vet
|
- make vet
|
||||||
- make revive
|
- make ci-lint
|
||||||
- make build
|
- make build
|
||||||
- curl --noproxy "*" http://gitea:3000/api/v1/version # verify connection to instance
|
- curl --noproxy "*" http://gitea:3000/api/v1/version # verify connection to instance
|
||||||
- make test
|
- make test
|
||||||
|
|
40
Makefile
40
Makefile
|
@ -54,7 +54,8 @@ clean:
|
||||||
|
|
||||||
.PHONY: fmt
|
.PHONY: fmt
|
||||||
fmt:
|
fmt:
|
||||||
find . -name "*.go" -type f ! -path "./vendor/*" ! -path "./benchmark/*" | xargs gofmt -s -w
|
find . -name "*.go" -type f | xargs gofmt -s -w; \
|
||||||
|
$(GO) run mvdan.cc/gofumpt@latest -extra -w ./gitea
|
||||||
|
|
||||||
.PHONY: vet
|
.PHONY: vet
|
||||||
vet:
|
vet:
|
||||||
|
@ -65,16 +66,25 @@ vet:
|
||||||
cd gitea && $(GO) build code.gitea.io/gitea-vet
|
cd gitea && $(GO) build code.gitea.io/gitea-vet
|
||||||
cd gitea && $(GO) vet -vettool=gitea-vet $(PACKAGE)
|
cd gitea && $(GO) vet -vettool=gitea-vet $(PACKAGE)
|
||||||
|
|
||||||
.PHONY: lint
|
.PHONY: ci-lint
|
||||||
lint:
|
ci-lint:
|
||||||
@echo 'make lint is depricated. Use "make revive" if you want to use the old lint tool, or "make golangci-lint" to run a complete code check.'
|
cd gitea/; \
|
||||||
|
$(GO) run github.com/mgechev/revive@latest -config ../.revive.toml .; \
|
||||||
.PHONY: revive
|
if [ $$? -eq 1 ]; then \
|
||||||
revive:
|
echo "Doesn't pass revive"; \
|
||||||
@hash revive > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
exit 1; \
|
||||||
$(GO) get -u github.com/mgechev/revive; \
|
fi; \
|
||||||
fi
|
diff=$$($(GO) run mvdan.cc/gofumpt@latest -extra -l .); \
|
||||||
revive -config .revive.toml -exclude=./vendor/... ./... || exit 1
|
if [ -n "$$diff" ]; then \
|
||||||
|
echo "Not gofumpt-ed"; \
|
||||||
|
exit 1; \
|
||||||
|
fi; \
|
||||||
|
$(GO) run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.44.2 run --timeout 5m; \
|
||||||
|
if [ $$? -eq 1 ]; then \
|
||||||
|
echo "Doesn't pass golangci-lint"; \
|
||||||
|
exit 1; \
|
||||||
|
fi; \
|
||||||
|
cd -; \
|
||||||
|
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
test:
|
test:
|
||||||
|
@ -111,11 +121,3 @@ bench:
|
||||||
.PHONY: build
|
.PHONY: build
|
||||||
build:
|
build:
|
||||||
cd gitea && $(GO) build
|
cd gitea && $(GO) build
|
||||||
|
|
||||||
.PHONY: golangci-lint
|
|
||||||
golangci-lint:
|
|
||||||
@hash golangci-lint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
|
||||||
export BINARY="golangci-lint"; \
|
|
||||||
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(GOPATH)/bin v1.22.2; \
|
|
||||||
fi
|
|
||||||
golangci-lint run --timeout 5m
|
|
||||||
|
|
|
@ -43,7 +43,7 @@ func (c *Client) ListReleaseAttachments(user, repo string, release int64, opt Li
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetReleaseAttachment returns the requested attachment
|
// GetReleaseAttachment returns the requested attachment
|
||||||
func (c *Client) GetReleaseAttachment(user, repo string, release int64, id int64) (*Attachment, *Response, error) {
|
func (c *Client) GetReleaseAttachment(user, repo string, release, id int64) (*Attachment, *Response, error) {
|
||||||
if err := escapeValidatePathSegments(&user, &repo); err != nil {
|
if err := escapeValidatePathSegments(&user, &repo); err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
@ -88,7 +88,7 @@ type EditAttachmentOptions struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// EditReleaseAttachment updates the given attachment with the given options
|
// EditReleaseAttachment updates the given attachment with the given options
|
||||||
func (c *Client) EditReleaseAttachment(user, repo string, release int64, attachment int64, form EditAttachmentOptions) (*Attachment, *Response, error) {
|
func (c *Client) EditReleaseAttachment(user, repo string, release, attachment int64, form EditAttachmentOptions) (*Attachment, *Response, error) {
|
||||||
if err := escapeValidatePathSegments(&user, &repo); err != nil {
|
if err := escapeValidatePathSegments(&user, &repo); err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
@ -102,7 +102,7 @@ func (c *Client) EditReleaseAttachment(user, repo string, release int64, attachm
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteReleaseAttachment deletes the given attachment including the uploaded file
|
// DeleteReleaseAttachment deletes the given attachment including the uploaded file
|
||||||
func (c *Client) DeleteReleaseAttachment(user, repo string, release int64, id int64) (*Response, error) {
|
func (c *Client) DeleteReleaseAttachment(user, repo string, release, id int64) (*Response, error) {
|
||||||
if err := escapeValidatePathSegments(&user, &repo); err != nil {
|
if err := escapeValidatePathSegments(&user, &repo); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -199,7 +199,7 @@ func (c *Client) getWebResponse(method, path string, body io.Reader) ([]byte, *R
|
||||||
if debug {
|
if debug {
|
||||||
fmt.Printf("Response: %v\n\n", resp)
|
fmt.Printf("Response: %v\n\n", resp)
|
||||||
}
|
}
|
||||||
return data, &Response{resp}, nil
|
return data, &Response{resp}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) doRequest(method, path string, header http.Header, body io.Reader) (*Response, error) {
|
func (c *Client) doRequest(method, path string, header http.Header, body io.Reader) (*Response, error) {
|
||||||
|
@ -285,7 +285,6 @@ func statusCodeToErr(resp *Response) (body []byte, err error) {
|
||||||
|
|
||||||
// If no error message, at least give status and data
|
// If no error message, at least give status and data
|
||||||
return data, fmt.Errorf("%s: %s", resp.Status, string(data))
|
return data, fmt.Errorf("%s: %s", resp.Status, string(data))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) getResponse(method, path string, header http.Header, body io.Reader) ([]byte, *Response, error) {
|
func (c *Client) getResponse(method, path string, header http.Header, body io.Reader) ([]byte, *Response, error) {
|
||||||
|
|
|
@ -16,7 +16,7 @@ type ListForksOptions struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListForks list a repository's forks
|
// ListForks list a repository's forks
|
||||||
func (c *Client) ListForks(user string, repo string, opt ListForksOptions) ([]*Repository, *Response, error) {
|
func (c *Client) ListForks(user, repo string, opt ListForksOptions) ([]*Repository, *Response, error) {
|
||||||
if err := escapeValidatePathSegments(&user, &repo); err != nil {
|
if err := escapeValidatePathSegments(&user, &repo); err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ module code.gitea.io/sdk/gitea
|
||||||
go 1.13
|
go 1.13
|
||||||
|
|
||||||
require (
|
require (
|
||||||
code.gitea.io/gitea-vet v0.2.1 // indirect
|
|
||||||
github.com/hashicorp/go-version v1.4.0
|
github.com/hashicorp/go-version v1.4.0
|
||||||
github.com/stretchr/testify v1.7.0
|
github.com/stretchr/testify v1.7.0
|
||||||
)
|
)
|
||||||
|
|
20
gitea/go.sum
20
gitea/go.sum
|
@ -1,5 +1,3 @@
|
||||||
code.gitea.io/gitea-vet v0.2.1 h1:b30by7+3SkmiftK0RjuXqFvZg2q4p68uoPGuxhzBN0s=
|
|
||||||
code.gitea.io/gitea-vet v0.2.1/go.mod h1:zcNbT/aJEmivCAhfmkHOlT645KNOf9W2KnkLgFjGGfE=
|
|
||||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
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/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/hashicorp/go-version v1.4.0 h1:aAQzgqIrRKRa7w75CKpbBxYsmUoPjzVm1W59ca1L0J4=
|
github.com/hashicorp/go-version v1.4.0 h1:aAQzgqIrRKRa7w75CKpbBxYsmUoPjzVm1W59ca1L0J4=
|
||||||
|
@ -9,24 +7,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
|
||||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
|
||||||
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
|
||||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
|
||||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
|
||||||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
|
||||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
|
||||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
|
||||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
|
||||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
|
||||||
golang.org/x/tools v0.0.0-20200325010219-a49f79bcc224 h1:azwY/v0y0K4mFHVsg5+UrTgchqALYWpqVo6vL5OmkmI=
|
|
||||||
golang.org/x/tools v0.0.0-20200325010219-a49f79bcc224/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8=
|
|
||||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||||
|
|
|
@ -72,7 +72,7 @@ func (c *Client) GetMilestone(owner, repo string, id int64) (*Milestone, *Respon
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMilestoneByName get one milestone by repo and milestone name
|
// GetMilestoneByName get one milestone by repo and milestone name
|
||||||
func (c *Client) GetMilestoneByName(owner, repo string, name string) (*Milestone, *Response, error) {
|
func (c *Client) GetMilestoneByName(owner, repo, name string) (*Milestone, *Response, error) {
|
||||||
if c.checkServerVersionGreaterThanOrEqual(version1_13_0) != nil {
|
if c.checkServerVersionGreaterThanOrEqual(version1_13_0) != nil {
|
||||||
// backwards compatibility mode
|
// backwards compatibility mode
|
||||||
m, resp, err := c.resolveMilestoneByName(owner, repo, name)
|
m, resp, err := c.resolveMilestoneByName(owner, repo, name)
|
||||||
|
@ -163,7 +163,7 @@ func (c *Client) EditMilestone(owner, repo string, id int64, opt EditMilestoneOp
|
||||||
}
|
}
|
||||||
|
|
||||||
// EditMilestoneByName modify milestone with options
|
// EditMilestoneByName modify milestone with options
|
||||||
func (c *Client) EditMilestoneByName(owner, repo string, name string, opt EditMilestoneOption) (*Milestone, *Response, error) {
|
func (c *Client) EditMilestoneByName(owner, repo, name string, opt EditMilestoneOption) (*Milestone, *Response, error) {
|
||||||
if c.checkServerVersionGreaterThanOrEqual(version1_13_0) != nil {
|
if c.checkServerVersionGreaterThanOrEqual(version1_13_0) != nil {
|
||||||
// backwards compatibility mode
|
// backwards compatibility mode
|
||||||
m, _, err := c.resolveMilestoneByName(owner, repo, name)
|
m, _, err := c.resolveMilestoneByName(owner, repo, name)
|
||||||
|
@ -197,7 +197,7 @@ func (c *Client) DeleteMilestone(owner, repo string, id int64) (*Response, error
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteMilestoneByName delete one milestone by name
|
// DeleteMilestoneByName delete one milestone by name
|
||||||
func (c *Client) DeleteMilestoneByName(owner, repo string, name string) (*Response, error) {
|
func (c *Client) DeleteMilestoneByName(owner, repo, name string) (*Response, error) {
|
||||||
if c.checkServerVersionGreaterThanOrEqual(version1_13_0) != nil {
|
if c.checkServerVersionGreaterThanOrEqual(version1_13_0) != nil {
|
||||||
// backwards compatibility mode
|
// backwards compatibility mode
|
||||||
m, _, err := c.resolveMilestoneByName(owner, repo, name)
|
m, _, err := c.resolveMilestoneByName(owner, repo, name)
|
||||||
|
@ -229,7 +229,7 @@ func (c *Client) resolveMilestoneByName(owner, repo, name string) (*Milestone, *
|
||||||
return nil, nil, fmt.Errorf("milestone '%s' do not exist", name)
|
return nil, nil, fmt.Errorf("milestone '%s' do not exist", name)
|
||||||
}
|
}
|
||||||
for _, m := range miles {
|
for _, m := range miles {
|
||||||
if strings.ToLower(strings.TrimSpace(m.Title)) == strings.ToLower(strings.TrimSpace(name)) {
|
if strings.EqualFold(strings.TrimSpace(m.Title), strings.TrimSpace(name)) {
|
||||||
return m, resp, nil
|
return m, resp, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,7 +59,7 @@ func TestMilestones(t *testing.T) {
|
||||||
m, _, err := c.resolveMilestoneByName(repo.Owner.UserName, repo.Name, "V3.0")
|
m, _, err := c.resolveMilestoneByName(repo.Owner.UserName, repo.Name, "V3.0")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.EqualValues(t, ml[0].ID, m.ID)
|
assert.EqualValues(t, ml[0].ID, m.ID)
|
||||||
m, _, err = c.resolveMilestoneByName(repo.Owner.UserName, repo.Name, "NoEvidenceOfExist")
|
_, _, err = c.resolveMilestoneByName(repo.Owner.UserName, repo.Name, "NoEvidenceOfExist")
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.EqualValues(t, "milestone 'NoEvidenceOfExist' do not exist", err.Error())
|
assert.EqualValues(t, "milestone 'NoEvidenceOfExist' do not exist", err.Error())
|
||||||
|
|
||||||
|
|
|
@ -82,7 +82,7 @@ func downGitea() (string, error) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = os.Chmod(f.Name(), 700); err != nil {
|
if err = os.Chmod(f.Name(), 0o700); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,7 +101,10 @@ func runGitea() (*os.Process, error) {
|
||||||
|
|
||||||
giteaDir := filepath.Dir(p)
|
giteaDir := filepath.Dir(p)
|
||||||
cfgDir := filepath.Join(giteaDir, "custom", "conf")
|
cfgDir := filepath.Join(giteaDir, "custom", "conf")
|
||||||
os.MkdirAll(cfgDir, os.ModePerm)
|
err = os.MkdirAll(cfgDir, os.ModePerm)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
cfg, err := os.Create(filepath.Join(cfgDir, "app.ini"))
|
cfg, err := os.Create(filepath.Join(cfgDir, "app.ini"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
@ -150,7 +153,9 @@ func TestMain(m *testing.M) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
p.Kill()
|
if err := p.Kill(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
log.Printf("testing with %v, %v, %v\n", getGiteaURL(), getGiteaUsername(), getGiteaPassword())
|
log.Printf("testing with %v, %v, %v\n", getGiteaURL(), getGiteaUsername(), getGiteaPassword())
|
||||||
|
|
|
@ -8,12 +8,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/go-version"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
version1_12_3, _ = version.NewVersion("1.12.3")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// NotificationThread expose Notification on API
|
// NotificationThread expose Notification on API
|
||||||
|
|
|
@ -125,7 +125,7 @@ type CreateTeamOption struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate the CreateTeamOption struct
|
// Validate the CreateTeamOption struct
|
||||||
func (opt CreateTeamOption) Validate() error {
|
func (opt *CreateTeamOption) Validate() error {
|
||||||
if opt.Permission == AccessModeOwner {
|
if opt.Permission == AccessModeOwner {
|
||||||
opt.Permission = AccessModeAdmin
|
opt.Permission = AccessModeAdmin
|
||||||
} else if opt.Permission != AccessModeRead && opt.Permission != AccessModeWrite && opt.Permission != AccessModeAdmin {
|
} else if opt.Permission != AccessModeRead && opt.Permission != AccessModeWrite && opt.Permission != AccessModeAdmin {
|
||||||
|
@ -148,7 +148,7 @@ func (c *Client) CreateTeam(org string, opt CreateTeamOption) (*Team, *Response,
|
||||||
if err := escapeValidatePathSegments(&org); err != nil {
|
if err := escapeValidatePathSegments(&org); err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
if err := opt.Validate(); err != nil {
|
if err := (&opt).Validate(); err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
body, err := json.Marshal(&opt)
|
body, err := json.Marshal(&opt)
|
||||||
|
@ -171,7 +171,7 @@ type EditTeamOption struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate the EditTeamOption struct
|
// Validate the EditTeamOption struct
|
||||||
func (opt EditTeamOption) Validate() error {
|
func (opt *EditTeamOption) Validate() error {
|
||||||
if opt.Permission == AccessModeOwner {
|
if opt.Permission == AccessModeOwner {
|
||||||
opt.Permission = AccessModeAdmin
|
opt.Permission = AccessModeAdmin
|
||||||
} else if opt.Permission != AccessModeRead && opt.Permission != AccessModeWrite && opt.Permission != AccessModeAdmin {
|
} else if opt.Permission != AccessModeRead && opt.Permission != AccessModeWrite && opt.Permission != AccessModeAdmin {
|
||||||
|
@ -191,7 +191,7 @@ func (opt EditTeamOption) Validate() error {
|
||||||
|
|
||||||
// EditTeam edits a team of an organization
|
// EditTeam edits a team of an organization
|
||||||
func (c *Client) EditTeam(id int64, opt EditTeamOption) (*Response, error) {
|
func (c *Client) EditTeam(id int64, opt EditTeamOption) (*Response, error) {
|
||||||
if err := opt.Validate(); err != nil {
|
if err := (&opt).Validate(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
body, err := json.Marshal(&opt)
|
body, err := json.Marshal(&opt)
|
||||||
|
|
|
@ -12,8 +12,6 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/go-version"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// PRBranchInfo information about a branch
|
// PRBranchInfo information about a branch
|
||||||
|
@ -219,8 +217,6 @@ type MergePullRequestOption struct {
|
||||||
ForceMerge bool `json:"force_merge"`
|
ForceMerge bool `json:"force_merge"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var version1_11_5, _ = version.NewVersion("1.11.5")
|
|
||||||
|
|
||||||
// Validate the MergePullRequestOption struct
|
// Validate the MergePullRequestOption struct
|
||||||
func (opt MergePullRequestOption) Validate(c *Client) error {
|
func (opt MergePullRequestOption) Validate(c *Client) error {
|
||||||
if opt.Style == MergeStyleSquash {
|
if opt.Style == MergeStyleSquash {
|
||||||
|
@ -256,7 +252,6 @@ func (c *Client) IsPullRequestMerged(owner, repo string, index int64) (bool, *Re
|
||||||
return false, nil, err
|
return false, nil, err
|
||||||
}
|
}
|
||||||
status, resp, err := c.getStatusCode("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d/merge", owner, repo, index), nil, nil)
|
status, resp, err := c.getStatusCode("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d/merge", owner, repo, index), nil, nil)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, resp, err
|
return false, resp, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,12 +33,12 @@ func TestPullReview(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
c.SetSudo(submitter.UserName)
|
c.SetSudo(submitter.UserName)
|
||||||
r2, _, err := c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
|
_, _, err = c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
|
||||||
State: ReviewStateApproved,
|
State: ReviewStateApproved,
|
||||||
Body: "lgtm it myself",
|
Body: "lgtm it myself",
|
||||||
})
|
})
|
||||||
assert.Error(t, err)
|
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,
|
State: ReviewStateComment,
|
||||||
Body: "no seriously please have a look at it",
|
Body: "no seriously please have a look at it",
|
||||||
})
|
})
|
||||||
|
|
|
@ -74,6 +74,7 @@ func TestPull(t *testing.T) {
|
||||||
// test Update pull
|
// 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.NoError(t, err)
|
||||||
|
assert.NotNil(t, pr)
|
||||||
assert.False(t, pullUpdateFile.HasMerged)
|
assert.False(t, pullUpdateFile.HasMerged)
|
||||||
assert.True(t, pullUpdateFile.Mergeable)
|
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{
|
||||||
|
|
|
@ -79,7 +79,7 @@ func (c *Client) GetRelease(owner, repo string, id int64) (*Release, *Response,
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetReleaseByTag get a release of a repository by tag
|
// GetReleaseByTag get a release of a repository by tag
|
||||||
func (c *Client) GetReleaseByTag(owner, repo string, tag string) (*Release, *Response, error) {
|
func (c *Client) GetReleaseByTag(owner, repo, tag string) (*Release, *Response, error) {
|
||||||
if c.checkServerVersionGreaterThanOrEqual(version1_13_0) != nil {
|
if c.checkServerVersionGreaterThanOrEqual(version1_13_0) != nil {
|
||||||
return c.fallbackGetReleaseByTag(owner, repo, tag)
|
return c.fallbackGetReleaseByTag(owner, repo, tag)
|
||||||
}
|
}
|
||||||
|
@ -168,7 +168,7 @@ func (c *Client) DeleteRelease(user, repo string, id int64) (*Response, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteReleaseByTag deletes a release frm a repository by tag
|
// DeleteReleaseByTag deletes a release frm a repository by tag
|
||||||
func (c *Client) DeleteReleaseByTag(user, repo string, tag string) (*Response, error) {
|
func (c *Client) DeleteReleaseByTag(user, repo, tag string) (*Response, error) {
|
||||||
if err := escapeValidatePathSegments(&user, &repo, &tag); err != nil {
|
if err := escapeValidatePathSegments(&user, &repo, &tag); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -182,7 +182,7 @@ func (c *Client) DeleteReleaseByTag(user, repo string, tag string) (*Response, e
|
||||||
}
|
}
|
||||||
|
|
||||||
// fallbackGetReleaseByTag is fallback for old gitea installations ( < 1.13.0 )
|
// fallbackGetReleaseByTag is fallback for old gitea installations ( < 1.13.0 )
|
||||||
func (c *Client) fallbackGetReleaseByTag(owner, repo string, tag string) (*Release, *Response, error) {
|
func (c *Client) fallbackGetReleaseByTag(owner, repo, tag string) (*Release, *Response, error) {
|
||||||
for i := 1; ; i++ {
|
for i := 1; ; i++ {
|
||||||
rl, resp, err := c.ListReleases(owner, repo, ListReleasesOptions{ListOptions: ListOptions{Page: i}})
|
rl, resp, err := c.ListReleases(owner, repo, ListReleasesOptions{ListOptions: ListOptions{Page: i}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -287,7 +287,9 @@ func (c *Client) SearchRepos(opt SearchRepoOptions) ([]*Repository, *Response, e
|
||||||
// private repos only not supported on gitea <= 1.11.x
|
// private repos only not supported on gitea <= 1.11.x
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
link.Query().Add("private", "false")
|
newQuery := link.Query()
|
||||||
|
newQuery.Add("private", "false")
|
||||||
|
link.RawQuery = newQuery.Encode()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -66,7 +66,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Validate the AddCollaboratorOption struct
|
// Validate the AddCollaboratorOption struct
|
||||||
func (opt AddCollaboratorOption) Validate() error {
|
func (opt *AddCollaboratorOption) Validate() error {
|
||||||
if opt.Permission != nil {
|
if opt.Permission != nil {
|
||||||
if *opt.Permission == AccessModeOwner {
|
if *opt.Permission == AccessModeOwner {
|
||||||
*opt.Permission = AccessModeAdmin
|
*opt.Permission = AccessModeAdmin
|
||||||
|
@ -88,7 +88,7 @@ func (c *Client) AddCollaborator(user, repo, collaborator string, opt AddCollabo
|
||||||
if err := escapeValidatePathSegments(&user, &repo, &collaborator); err != nil {
|
if err := escapeValidatePathSegments(&user, &repo, &collaborator); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err := opt.Validate(); err != nil {
|
if err := (&opt).Validate(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
body, err := json.Marshal(&opt)
|
body, err := json.Marshal(&opt)
|
||||||
|
|
|
@ -18,8 +18,12 @@ func TestRepoCollaborator(t *testing.T) {
|
||||||
repo, _ := createTestRepo(t, "RepoCollaborators", c)
|
repo, _ := createTestRepo(t, "RepoCollaborators", c)
|
||||||
createTestUser(t, "ping", c)
|
createTestUser(t, "ping", c)
|
||||||
createTestUser(t, "pong", c)
|
createTestUser(t, "pong", c)
|
||||||
defer c.AdminDeleteUser("ping")
|
defer func() {
|
||||||
defer c.AdminDeleteUser("pong")
|
_, err := c.AdminDeleteUser("ping")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
_, err = c.AdminDeleteUser("pong")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}()
|
||||||
|
|
||||||
collaborators, _, err := c.ListCollaborators(repo.Owner.UserName, repo.Name, ListCollaboratorsOptions{})
|
collaborators, _, err := c.ListCollaborators(repo.Owner.UserName, repo.Name, ListCollaboratorsOptions{})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
|
@ -65,6 +65,7 @@ func TestFileCreateUpdateGet(t *testing.T) {
|
||||||
licence, _, err := c.GetContents(repo.Owner.UserName, repo.Name, "", "LICENSE")
|
licence, _, err := c.GetContents(repo.Owner.UserName, repo.Name, "", "LICENSE")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
licenceRaw, _, err := c.GetFile(repo.Owner.UserName, repo.Name, "", "LICENSE")
|
licenceRaw, _, err := c.GetFile(repo.Owner.UserName, repo.Name, "", "LICENSE")
|
||||||
|
assert.NoError(t, err)
|
||||||
testContent := "Tk9USElORyBJUyBIRVJFIEFOWU1PUkUKSUYgWU9VIExJS0UgVE8gRklORCBTT01FVEhJTkcKV0FJVCBGT1IgVEhFIEZVVFVSRQo="
|
testContent := "Tk9USElORyBJUyBIRVJFIEFOWU1PUkUKSUYgWU9VIExJS0UgVE8gRklORCBTT01FVEhJTkcKV0FJVCBGT1IgVEhFIEZVVFVSRQo="
|
||||||
updatedFile, _, err = c.UpdateFile(repo.Owner.UserName, repo.Name, "LICENSE", UpdateFileOptions{
|
updatedFile, _, err = c.UpdateFile(repo.Owner.UserName, repo.Name, "LICENSE", UpdateFileOptions{
|
||||||
FileOptions: FileOptions{
|
FileOptions: FileOptions{
|
||||||
|
|
|
@ -35,7 +35,7 @@ func (c *Client) GetTrees(user, repo, ref string, recursive bool) (*GitTreeRespo
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
trees := new(GitTreeResponse)
|
trees := new(GitTreeResponse)
|
||||||
var path = fmt.Sprintf("/repos/%s/%s/git/trees/%s", user, repo, ref)
|
path := fmt.Sprintf("/repos/%s/%s/git/trees/%s", user, repo, ref)
|
||||||
if recursive {
|
if recursive {
|
||||||
path += "?recursive=1"
|
path += "?recursive=1"
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,7 @@ func (c *Client) GetWatchedRepos(user string) ([]*Repository, *Response, error)
|
||||||
// GetMyWatchedRepos list repositories watched by the authenticated user
|
// GetMyWatchedRepos list repositories watched by the authenticated user
|
||||||
func (c *Client) GetMyWatchedRepos() ([]*Repository, *Response, error) {
|
func (c *Client) GetMyWatchedRepos() ([]*Repository, *Response, error) {
|
||||||
repos := make([]*Repository, 0, 10)
|
repos := make([]*Repository, 0, 10)
|
||||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/user/subscriptions"), nil, nil, &repos)
|
resp, err := c.getParsedResponse("GET", "/user/subscriptions", nil, nil, &repos)
|
||||||
return repos, resp, err
|
return repos, resp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ func TestCommitStatus(t *testing.T) {
|
||||||
user, _, err := c.GetMyUserInfo()
|
user, _, err := c.GetMyUserInfo()
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
var repoName = "CommitStatuses"
|
repoName := "CommitStatuses"
|
||||||
origRepo, err := createTestRepo(t, repoName, c)
|
origRepo, err := createTestRepo(t, repoName, c)
|
||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
return
|
return
|
||||||
|
|
|
@ -77,7 +77,6 @@ func (c *Client) GetUserByID(id int64) (*User, *Response, error) {
|
||||||
query := make(url.Values)
|
query := make(url.Values)
|
||||||
query.Add("uid", strconv.FormatInt(id, 10))
|
query.Add("uid", strconv.FormatInt(id, 10))
|
||||||
users, resp, err := c.searchUsers(query.Encode())
|
users, resp, err := c.searchUsers(query.Encode())
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, resp, err
|
return nil, resp, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,7 +71,7 @@ func (c *Client) DeleteAccessToken(value interface{}) (*Response, error) {
|
||||||
return nil, fmt.Errorf("\"username\" not set: only BasicAuth allowed")
|
return nil, fmt.Errorf("\"username\" not set: only BasicAuth allowed")
|
||||||
}
|
}
|
||||||
|
|
||||||
var token = ""
|
token := ""
|
||||||
|
|
||||||
switch reflect.ValueOf(value).Kind() {
|
switch reflect.ValueOf(value).Kind() {
|
||||||
case reflect.Int64:
|
case reflect.Int64:
|
||||||
|
|
|
@ -52,7 +52,6 @@ func SetGiteaVersion(v string) ClientOption {
|
||||||
return func(c *Client) (err error) {
|
return func(c *Client) (err error) {
|
||||||
c.getVersionOnce.Do(func() {
|
c.getVersionOnce.Do(func() {
|
||||||
c.serverVersion, err = version.NewVersion(v)
|
c.serverVersion, err = version.NewVersion(v)
|
||||||
return
|
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -60,12 +59,14 @@ func SetGiteaVersion(v string) ClientOption {
|
||||||
|
|
||||||
// predefined versions only have to be parsed by library once
|
// predefined versions only have to be parsed by library once
|
||||||
var (
|
var (
|
||||||
version1_11_0, _ = version.NewVersion("1.11.0")
|
version1_11_0 = version.Must(version.NewVersion("1.11.0"))
|
||||||
version1_12_0, _ = version.NewVersion("1.12.0")
|
version1_11_5 = version.Must(version.NewVersion("1.11.5"))
|
||||||
version1_13_0, _ = version.NewVersion("1.13.0")
|
version1_12_0 = version.Must(version.NewVersion("1.12.0"))
|
||||||
version1_14_0, _ = version.NewVersion("1.14.0")
|
version1_12_3 = version.Must(version.NewVersion("1.12.3"))
|
||||||
version1_15_0, _ = version.NewVersion("1.15.0")
|
version1_13_0 = version.Must(version.NewVersion("1.13.0"))
|
||||||
version1_16_0, _ = version.NewVersion("1.16.0")
|
version1_14_0 = version.Must(version.NewVersion("1.14.0"))
|
||||||
|
version1_15_0 = version.Must(version.NewVersion("1.15.0"))
|
||||||
|
version1_16_0 = version.Must(version.NewVersion("1.16.0"))
|
||||||
)
|
)
|
||||||
|
|
||||||
// checkServerVersionGreaterThanOrEqual is the canonical way in the SDK to check for versions for API compatibility reasons
|
// checkServerVersionGreaterThanOrEqual is the canonical way in the SDK to check for versions for API compatibility reasons
|
||||||
|
|
Loading…
Reference in a new issue