Use golang v1.18 and drop vendor folder #478

Merged
6543 merged 5 commits from 6543/tea:migrate-golang into master 6 months ago
  1. 19
      .drone.yml
  2. 1
      .gitignore
  3. 51
      Makefile
  4. 30
      vendor/code.gitea.io/gitea-vet/.changelog.yml
  5. 45
      vendor/code.gitea.io/gitea-vet/.drone.yml
  6. 5
      vendor/code.gitea.io/gitea-vet/.gitignore
  7. 23
      vendor/code.gitea.io/gitea-vet/.golangci.yml
  8. 11
      vendor/code.gitea.io/gitea-vet/CHANGELOG.md
  9. 19
      vendor/code.gitea.io/gitea-vet/LICENSE
  10. 22
      vendor/code.gitea.io/gitea-vet/Makefile
  11. 11
      vendor/code.gitea.io/gitea-vet/README.md
  12. 46
      vendor/code.gitea.io/gitea-vet/checks/imports.go
  13. 73
      vendor/code.gitea.io/gitea-vet/checks/license.go
  14. 77
      vendor/code.gitea.io/gitea-vet/checks/migrations.go
  15. 5
      vendor/code.gitea.io/gitea-vet/go.mod
  16. 20
      vendor/code.gitea.io/gitea-vet/go.sum
  17. 19
      vendor/code.gitea.io/gitea-vet/main.go
  18. 20
      vendor/code.gitea.io/sdk/gitea/LICENSE
  19. 47
      vendor/code.gitea.io/sdk/gitea/admin_cron.go
  20. 39
      vendor/code.gitea.io/sdk/gitea/admin_org.go
  21. 25
      vendor/code.gitea.io/sdk/gitea/admin_repo.go
  22. 130
      vendor/code.gitea.io/sdk/gitea/admin_user.go
  23. 111
      vendor/code.gitea.io/sdk/gitea/attachment.go
  24. 333
      vendor/code.gitea.io/sdk/gitea/client.go
  25. 5
      vendor/code.gitea.io/sdk/gitea/doc.go
  26. 49
      vendor/code.gitea.io/sdk/gitea/fork.go
  27. 28
      vendor/code.gitea.io/sdk/gitea/git_blob.go
  28. 71
      vendor/code.gitea.io/sdk/gitea/git_hook.go
  29. 9
      vendor/code.gitea.io/sdk/gitea/go.mod
  30. 33
      vendor/code.gitea.io/sdk/gitea/go.sum
  31. 20
      vendor/code.gitea.io/sdk/gitea/helper.go
  32. 194
      vendor/code.gitea.io/sdk/gitea/hook.go
  33. 288
      vendor/code.gitea.io/sdk/gitea/issue.go
  34. 154
      vendor/code.gitea.io/sdk/gitea/issue_comment.go
  35. 211
      vendor/code.gitea.io/sdk/gitea/issue_label.go
  36. 237
      vendor/code.gitea.io/sdk/gitea/issue_milestone.go
  37. 104
      vendor/code.gitea.io/sdk/gitea/issue_reaction.go
  38. 57
      vendor/code.gitea.io/sdk/gitea/issue_stopwatch.go
  39. 87
      vendor/code.gitea.io/sdk/gitea/issue_subscription.go
  40. 142
      vendor/code.gitea.io/sdk/gitea/issue_tracked_time.go
  41. 42
      vendor/code.gitea.io/sdk/gitea/list_options.go
  42. 241
      vendor/code.gitea.io/sdk/gitea/notifications.go
  43. 91
      vendor/code.gitea.io/sdk/gitea/oauth2.go
  44. 155
      vendor/code.gitea.io/sdk/gitea/org.go
  45. 119
      vendor/code.gitea.io/sdk/gitea/org_member.go
  46. 242
      vendor/code.gitea.io/sdk/gitea/org_team.go
  47. 324
      vendor/code.gitea.io/sdk/gitea/pull.go
  48. 325
      vendor/code.gitea.io/sdk/gitea/pull_review.go
  49. 202
      vendor/code.gitea.io/sdk/gitea/release.go
  50. 534
      vendor/code.gitea.io/sdk/gitea/repo.go
  51. 143
      vendor/code.gitea.io/sdk/gitea/repo_branch.go
  52. 168
      vendor/code.gitea.io/sdk/gitea/repo_branch_protection.go
  53. 136
      vendor/code.gitea.io/sdk/gitea/repo_collaborator.go
  54. 101
      vendor/code.gitea.io/sdk/gitea/repo_commit.go
  55. 247
      vendor/code.gitea.io/sdk/gitea/repo_file.go
  56. 91
      vendor/code.gitea.io/sdk/gitea/repo_key.go
  57. 132
      vendor/code.gitea.io/sdk/gitea/repo_migrate.go
  58. 78
      vendor/code.gitea.io/sdk/gitea/repo_refs.go
  59. 96
      vendor/code.gitea.io/sdk/gitea/repo_stars.go
  60. 130
      vendor/code.gitea.io/sdk/gitea/repo_tag.go
  61. 65
      vendor/code.gitea.io/sdk/gitea/repo_team.go
  62. 65
      vendor/code.gitea.io/sdk/gitea/repo_template.go
  63. 68
      vendor/code.gitea.io/sdk/gitea/repo_topics.go
  64. 36
      vendor/code.gitea.io/sdk/gitea/repo_transfer.go
  65. 44
      vendor/code.gitea.io/sdk/gitea/repo_tree.go
  66. 87
      vendor/code.gitea.io/sdk/gitea/repo_watch.go
  67. 78
      vendor/code.gitea.io/sdk/gitea/settings.go
  68. 108
      vendor/code.gitea.io/sdk/gitea/status.go
  69. 90
      vendor/code.gitea.io/sdk/gitea/user.go
  70. 90
      vendor/code.gitea.io/sdk/gitea/user_app.go
  71. 64
      vendor/code.gitea.io/sdk/gitea/user_email.go
  72. 93
      vendor/code.gitea.io/sdk/gitea/user_follow.go
  73. 89
      vendor/code.gitea.io/sdk/gitea/user_gpgkey.go
  74. 83
      vendor/code.gitea.io/sdk/gitea/user_key.go
  75. 48
      vendor/code.gitea.io/sdk/gitea/user_search.go
  76. 62
      vendor/code.gitea.io/sdk/gitea/user_settings.go
  77. 79
      vendor/code.gitea.io/sdk/gitea/version.go
  78. 459
      vendor/gitea.com/noerw/unidiff-comments/changeset_reader.go
  79. 5
      vendor/gitea.com/noerw/unidiff-comments/go.mod
  80. 2
      vendor/gitea.com/noerw/unidiff-comments/go.sum
  81. 47
      vendor/gitea.com/noerw/unidiff-comments/types/changeset.go
  82. 121
      vendor/gitea.com/noerw/unidiff-comments/types/comment.go
  83. 61
      vendor/gitea.com/noerw/unidiff-comments/types/diff.go
  84. 10
      vendor/gitea.com/noerw/unidiff-comments/types/hunk.go
  85. 29
      vendor/gitea.com/noerw/unidiff-comments/types/line.go
  86. 39
      vendor/gitea.com/noerw/unidiff-comments/types/segment.go
  87. 22
      vendor/github.com/AlecAivazis/survey/v2/.travis.yml
  88. 77
      vendor/github.com/AlecAivazis/survey/v2/CONTRIBUTING.md
  89. 78
      vendor/github.com/AlecAivazis/survey/v2/Gopkg.lock
  90. 54
      vendor/github.com/AlecAivazis/survey/v2/Gopkg.toml
  91. 21
      vendor/github.com/AlecAivazis/survey/v2/LICENSE
  92. 480
      vendor/github.com/AlecAivazis/survey/v2/README.md
  93. 19
      vendor/github.com/AlecAivazis/survey/v2/_tasks.hcl
  94. 153
      vendor/github.com/AlecAivazis/survey/v2/confirm.go
  95. 91
      vendor/github.com/AlecAivazis/survey/v2/core/template.go
  96. 374
      vendor/github.com/AlecAivazis/survey/v2/core/write.go
  97. 222
      vendor/github.com/AlecAivazis/survey/v2/editor.go
  98. 1
      vendor/github.com/AlecAivazis/survey/v2/filter.go
  99. 19
      vendor/github.com/AlecAivazis/survey/v2/go.mod
  100. 35
      vendor/github.com/AlecAivazis/survey/v2/go.sum
  101. 225
      vendor/github.com/AlecAivazis/survey/v2/input.go
  102. 110
      vendor/github.com/AlecAivazis/survey/v2/multiline.go
  103. 346
      vendor/github.com/AlecAivazis/survey/v2/multiselect.go
  104. 101
      vendor/github.com/AlecAivazis/survey/v2/password.go
  105. 192
      vendor/github.com/AlecAivazis/survey/v2/renderer.go
  106. 347
      vendor/github.com/AlecAivazis/survey/v2/select.go
  107. 454
      vendor/github.com/AlecAivazis/survey/v2/survey.go
  108. 22
      vendor/github.com/AlecAivazis/survey/v2/terminal/LICENSE.txt
  109. 3
      vendor/github.com/AlecAivazis/survey/v2/terminal/README.md
  110. 22
      vendor/github.com/AlecAivazis/survey/v2/terminal/buffered_reader.go
  111. 190
      vendor/github.com/AlecAivazis/survey/v2/terminal/cursor.go
  112. 138
      vendor/github.com/AlecAivazis/survey/v2/terminal/cursor_windows.go
  113. 9
      vendor/github.com/AlecAivazis/survey/v2/terminal/display.go
  114. 11
      vendor/github.com/AlecAivazis/survey/v2/terminal/display_posix.go
  115. 27
      vendor/github.com/AlecAivazis/survey/v2/terminal/display_windows.go
  116. 9
      vendor/github.com/AlecAivazis/survey/v2/terminal/error.go
  117. 19
      vendor/github.com/AlecAivazis/survey/v2/terminal/output.go
  118. 227
      vendor/github.com/AlecAivazis/survey/v2/terminal/output_windows.go
  119. 373
      vendor/github.com/AlecAivazis/survey/v2/terminal/runereader.go
  120. 13
      vendor/github.com/AlecAivazis/survey/v2/terminal/runereader_bsd.go
  121. 13
      vendor/github.com/AlecAivazis/survey/v2/terminal/runereader_linux.go
  122. 111
      vendor/github.com/AlecAivazis/survey/v2/terminal/runereader_posix.go
  123. 7
      vendor/github.com/AlecAivazis/survey/v2/terminal/runereader_ppc64le.go
  124. 142
      vendor/github.com/AlecAivazis/survey/v2/terminal/runereader_windows.go
  125. 31
      vendor/github.com/AlecAivazis/survey/v2/terminal/sequences.go
  126. 24
      vendor/github.com/AlecAivazis/survey/v2/terminal/stdio.go
  127. 39
      vendor/github.com/AlecAivazis/survey/v2/terminal/syscall_windows.go
  128. 8
      vendor/github.com/AlecAivazis/survey/v2/terminal/terminal.go
  129. 79
      vendor/github.com/AlecAivazis/survey/v2/transform.go
  130. 127
      vendor/github.com/AlecAivazis/survey/v2/validate.go
  131. 1
      vendor/github.com/Microsoft/go-winio/.gitignore
  132. 1
      vendor/github.com/Microsoft/go-winio/CODEOWNERS
  133. 22
      vendor/github.com/Microsoft/go-winio/LICENSE
  134. 22
      vendor/github.com/Microsoft/go-winio/README.md
  135. 280
      vendor/github.com/Microsoft/go-winio/backup.go
  136. 137
      vendor/github.com/Microsoft/go-winio/ea.go
  137. 323
      vendor/github.com/Microsoft/go-winio/file.go
  138. 73
      vendor/github.com/Microsoft/go-winio/fileinfo.go
  139. 9
      vendor/github.com/Microsoft/go-winio/go.mod
  140. 14
      vendor/github.com/Microsoft/go-winio/go.sum
  141. 307
      vendor/github.com/Microsoft/go-winio/hvsock.go
  142. 517
      vendor/github.com/Microsoft/go-winio/pipe.go
  143. 237
      vendor/github.com/Microsoft/go-winio/pkg/guid/guid.go
  144. 203
      vendor/github.com/Microsoft/go-winio/privilege.go
  145. 128
      vendor/github.com/Microsoft/go-winio/reparse.go
  146. 98
      vendor/github.com/Microsoft/go-winio/sd.go
  147. 3
      vendor/github.com/Microsoft/go-winio/syscall.go
  148. 427
      vendor/github.com/Microsoft/go-winio/zsyscall_windows.go
  149. 3
      vendor/github.com/ProtonMail/go-crypto/AUTHORS
  150. 3
      vendor/github.com/ProtonMail/go-crypto/CONTRIBUTORS
  151. 27
      vendor/github.com/ProtonMail/go-crypto/LICENSE
  152. 22
      vendor/github.com/ProtonMail/go-crypto/PATENTS
  153. 381
      vendor/github.com/ProtonMail/go-crypto/bitcurves/bitcurve.go
  154. 134
      vendor/github.com/ProtonMail/go-crypto/brainpool/brainpool.go
  155. 83
      vendor/github.com/ProtonMail/go-crypto/brainpool/rcurve.go
  156. 162
      vendor/github.com/ProtonMail/go-crypto/eax/eax.go
  157. 58
      vendor/github.com/ProtonMail/go-crypto/eax/eax_test_vectors.go
  158. 131
      vendor/github.com/ProtonMail/go-crypto/eax/random_vectors.go
  159. 92
      vendor/github.com/ProtonMail/go-crypto/internal/byteutil/byteutil.go
  160. 317
      vendor/github.com/ProtonMail/go-crypto/ocb/ocb.go
  161. 136
      vendor/github.com/ProtonMail/go-crypto/ocb/random_vectors.go
  162. 78
      vendor/github.com/ProtonMail/go-crypto/ocb/rfc7253_test_vectors_suite_a.go
  163. 24
      vendor/github.com/ProtonMail/go-crypto/ocb/rfc7253_test_vectors_suite_b.go
  164. 153
      vendor/github.com/ProtonMail/go-crypto/openpgp/aes/keywrap/keywrap.go
  165. 224
      vendor/github.com/ProtonMail/go-crypto/openpgp/armor/armor.go
  166. 160
      vendor/github.com/ProtonMail/go-crypto/openpgp/armor/encode.go
  167. 65
      vendor/github.com/ProtonMail/go-crypto/openpgp/canonical_text.go
  168. 165
      vendor/github.com/ProtonMail/go-crypto/openpgp/ecdh/ecdh.go
  169. 157
      vendor/github.com/ProtonMail/go-crypto/openpgp/ecdh/x25519.go
  170. 124
      vendor/github.com/ProtonMail/go-crypto/openpgp/elgamal/elgamal.go
  171. 116
      vendor/github.com/ProtonMail/go-crypto/openpgp/errors/errors.go
  172. 65
      vendor/github.com/ProtonMail/go-crypto/openpgp/internal/algorithm/aead.go
  173. 107
      vendor/github.com/ProtonMail/go-crypto/openpgp/internal/algorithm/cipher.go
  174. 86
      vendor/github.com/ProtonMail/go-crypto/openpgp/internal/algorithm/hash.go
  175. 118
      vendor/github.com/ProtonMail/go-crypto/openpgp/internal/ecc/curveInfo.go
  176. 10
      vendor/github.com/ProtonMail/go-crypto/openpgp/internal/ecc/curveType.go
  177. 27
      vendor/github.com/ProtonMail/go-crypto/openpgp/internal/encoding/encoding.go
  178. 91
      vendor/github.com/ProtonMail/go-crypto/openpgp/internal/encoding/mpi.go
  179. 88
      vendor/github.com/ProtonMail/go-crypto/openpgp/internal/encoding/oid.go
  180. 375
      vendor/github.com/ProtonMail/go-crypto/openpgp/key_generation.go
  181. 707
      vendor/github.com/ProtonMail/go-crypto/openpgp/keys.go
  182. 336
      vendor/github.com/ProtonMail/go-crypto/openpgp/keys_test_data.go
  183. 56
      vendor/github.com/ProtonMail/go-crypto/openpgp/packet/aead_config.go
  184. 364
      vendor/github.com/ProtonMail/go-crypto/openpgp/packet/aead_encrypted.go
  185. 123
      vendor/github.com/ProtonMail/go-crypto/openpgp/packet/compressed.go
  186. 167
      vendor/github.com/ProtonMail/go-crypto/openpgp/packet/config.go
  187. 282
      vendor/github.com/ProtonMail/go-crypto/openpgp/packet/encrypted_key.go
  188. 91
      vendor/github.com/ProtonMail/go-crypto/openpgp/packet/literal.go
  189. 137
      vendor/github.com/ProtonMail/go-crypto/openpgp/packet/ocfb.go
  190. 73
      vendor/github.com/ProtonMail/go-crypto/openpgp/packet/one_pass_signature.go
  191. 162
      vendor/github.com/ProtonMail/go-crypto/openpgp/packet/opaque.go
  192. 522
      vendor/github.com/ProtonMail/go-crypto/openpgp/packet/packet.go
  193. 780
      vendor/github.com/ProtonMail/go-crypto/openpgp/packet/private_key.go
  194. 12
      vendor/github.com/ProtonMail/go-crypto/openpgp/packet/private_key_test_data.go
  195. 825
      vendor/github.com/ProtonMail/go-crypto/openpgp/packet/public_key.go
  196. 24
      vendor/github.com/ProtonMail/go-crypto/openpgp/packet/public_key_test_data.go
  197. 78
      vendor/github.com/ProtonMail/go-crypto/openpgp/packet/reader.go
  198. 983
      vendor/github.com/ProtonMail/go-crypto/openpgp/packet/signature.go
  199. 267
      vendor/github.com/ProtonMail/go-crypto/openpgp/packet/symmetric_key_encrypted.go
  200. 290
      vendor/github.com/ProtonMail/go-crypto/openpgp/packet/symmetrically_encrypted.go
  201. Some files were not shown because too many files have changed in this diff Show More

@ -7,9 +7,17 @@ platform:
arch: amd64
steps:
- name: vendor
pull: always
image: golang:1.18
environment:
GOPROXY: https://goproxy.io # proxy.golang.org is blocked in China, this proxy is not
commands:
- make vendor # use vendor folder as cache
- name: build
pull: always
image: golang:1.17
image: golang:1.18
environment:
GOPROXY: https://goproxy.io # proxy.golang.org is blocked in China, this proxy is not
commands:
@ -18,7 +26,6 @@ steps:
- make lint
- make fmt-check
- make misspell-check
- make test-vendor
- make build
when:
event:
@ -27,7 +34,7 @@ steps:
- pull_request
- name: unit-test
image: golang:1.17
image: golang:1.18
commands:
- make unit-test-coverage
settings:
@ -42,7 +49,7 @@ steps:
- pull_request
- name: release-test
image: golang:1.17
image: golang:1.18
commands:
- make test
settings:
@ -58,7 +65,7 @@ steps:
- name: tag-test
pull: always
image: golang:1.17
image: golang:1.18
commands:
- make test
settings:
@ -70,7 +77,7 @@ steps:
- tag
- name: static
image: golang:1.17
image: golang:1.18
environment:
GOPROXY: https://goproxy.io # proxy.golang.org is blocked in China, this proxy is not
commands:

1
.gitignore vendored

@ -6,3 +6,4 @@ tea
dist/
.vscode/
vendor/

@ -63,24 +63,15 @@ vet:
$(GO) vet -vettool=gitea-vet $(PACKAGES)
.PHONY: lint
lint:
@hash revive > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
cd /tmp && $(GO) get -u github.com/mgechev/revive; \
fi
lint: install-lint-tools
revive -config .revive.toml -exclude=./vendor/... ./... || exit 1
.PHONY: misspell-check
misspell-check:
@hash misspell > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
cd /tmp && $(GO) get -u github.com/client9/misspell/cmd/misspell; \
fi
misspell-check: install-lint-tools
misspell -error -i unknwon,destory $(GOFILES)
.PHONY: misspell
misspell:
@hash misspell > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
cd /tmp && $(GO) get -u github.com/client9/misspell/cmd/misspell; \
fi
misspell: install-lint-tools
misspell -w -i unknwon $(GOFILES)
.PHONY: fmt-check
@ -105,15 +96,6 @@ unit-test-coverage:
vendor:
$(GO) mod tidy && $(GO) mod vendor
.PHONY: test-vendor
test-vendor: vendor
@diff=$$(git diff vendor/); \
if [ -n "$$diff" ]; then \
echo "Please run 'make vendor' and commit the result:"; \
echo "$${diff}"; \
exit 1; \
fi;
.PHONY: check
check: test
@ -141,18 +123,29 @@ release-dirs:
.PHONY: release-os
release-os:
@hash gox > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
cd /tmp && $(GO) get -u github.com/mitchellh/gox; \
fi
CGO_ENABLED=0 gox -verbose -cgo=false $(GOFLAGS) -osarch='!darwin/386 !darwin/arm' -os="windows linux darwin" -arch="386 amd64 arm arm64" -output="$(DIST)/release/tea-$(VERSION)-{{.OS}}-{{.Arch}}"
.PHONY: release-compress
release-compress:
@hash gxz > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
GO111MODULE=off $(GO) get -u github.com/ulikunitz/xz/cmd/gxz; \
fi
release-compress: install-release-tools
cd $(DIST)/release/; for file in `find . -type f -name "*"`; do echo "compressing $${file}" && gxz -k -9 $${file}; done;
.PHONY: release-check
release-check:
release-check: install-release-tools
cd $(DIST)/release/; for file in `find . -type f -name "*"`; do echo "checksumming $${file}" && $(SHASUM) `echo $${file} | sed 's/^..//'` > $${file}.sha256; done;
### tools
install-release-tools:
@hash gox > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
$(GO) install github.com/mitchellh/gox@latest; \
fi
@hash gxz > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
$(GO) install github.com/ulikunitz/xz/cmd/gxz@latest; \
fi
install-lint-tools:
@hash revive > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
$(GO) install github.com/mgechev/revive@latest; \
fi
@hash misspell > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
$(GO) install github.com/client9/misspell/cmd/misspell@latest; \
fi

@ -1,30 +0,0 @@
# The full repository name
repo: gitea/gitea-vet
# Service type (gitea or github)
service: gitea
# Base URL for Gitea instance if using gitea service type (optional)
base-url: https://gitea.com
# Changelog groups and which labeled PRs to add to each group
groups:
-
name: BREAKING
labels:
- breaking
-
name: FEATURES
labels:
- feature
-
name: BUGFIXES
labels:
- bug
-
name: ENHANCEMENTS
labels:
- enhancement
# regex indicating which labels to skip for the changelog
skip-labels: skip-changelog|backport\/.+

@ -1,45 +0,0 @@
---
kind: pipeline
name: compliance
platform:
os: linux
arch: arm64
trigger:
event:
- pull_request
steps:
- name: check
pull: always
image: golang:1.14
environment:
GOPROXY: https://goproxy.cn
commands:
- make build
- make lint
- make vet
---
kind: pipeline
name: build-master
platform:
os: linux
arch: amd64
trigger:
branch:
- master
event:
- push
steps:
- name: build
pull: always
image: techknowlogick/xgo:latest
environment:
GOPROXY: https://goproxy.cn
commands:
- make build

@ -1,5 +0,0 @@
# GoLand
.idea/
# Binaries
/gitea-vet*

@ -1,23 +0,0 @@
linters:
enable:
- deadcode
- dogsled
- dupl
- errcheck
- gocognit
- goconst
- gocritic
- gocyclo
- gofmt
- golint
- gosimple
- govet
- maligned
- misspell
- prealloc
- staticcheck
- structcheck
- typecheck
- unparam
- unused
- varcheck

@ -1,11 +0,0 @@
## [v0.2.1](https://gitea.com/gitea/gitea-vet/releases/tag/v0.2.1) - 2020-08-15
* BUGFIXES
* Split migration check to Deps and Imports (#9)
## [0.2.0](https://gitea.com/gitea/gitea-vet/pulls?q=&type=all&state=closed&milestone=1272) - 2020-07-20
* FEATURES
* Add migrations check (#5)
* BUGFIXES
* Correct Import Paths (#6)

@ -1,19 +0,0 @@
Copyright (c) 2020 The Gitea Authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

@ -1,22 +0,0 @@
GO ?= go
.PHONY: build
build:
$(GO) build
.PHONY: fmt
fmt:
$(GO) fmt ./...
.PHONY: vet
vet: build
$(GO) vet ./...
$(GO) vet -vettool=gitea-vet ./...
.PHONY: lint
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 $(shell $(GO) env GOPATH)/bin v1.24.0; \
fi
golangci-lint run --timeout 5m

@ -1,11 +0,0 @@
# gitea-vet
[![Build Status](https://drone.gitea.com/api/badges/gitea/gitea-vet/status.svg)](https://drone.gitea.com/gitea/gitea-vet)
`go vet` tool for Gitea
| Analyzer | Description |
|------------|-----------------------------------------------------------------------------|
| Imports | Checks for import sorting. stdlib->code.gitea.io->other |
| License | Checks file headers for some form of `Copyright...YYYY...Gitea/Gogs` |
| Migrations | Checks for black-listed packages in `code.gitea.io/gitea/models/migrations` |

@ -1,46 +0,0 @@
// 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 checks
import (
"strings"
"golang.org/x/tools/go/analysis"
)
var Imports = &analysis.Analyzer{
Name: "imports",
Doc: "check for import order",
Run: runImports,
}
func runImports(pass *analysis.Pass) (interface{}, error) {
for _, file := range pass.Files {
level := 0
for _, im := range file.Imports {
var lvl int
val := im.Path.Value
switch {
case importHasPrefix(val, "code.gitea.io"):
lvl = 2
case strings.Contains(val, "."):
lvl = 3
default:
lvl = 1
}
if lvl < level {
pass.Reportf(file.Pos(), "Imports are sorted wrong")
break
}
level = lvl
}
}
return nil, nil
}
func importHasPrefix(s, p string) bool {
return strings.HasPrefix(s, "\""+p)
}

@ -1,73 +0,0 @@
// 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 checks
import (
"regexp"
"strings"
"golang.org/x/tools/go/analysis"
)
var (
header = regexp.MustCompile(`.*Copyright.*\d{4}.*(Gitea|Gogs)`)
goGenerate = "//go:generate"
buildTag = "// +build"
)
var License = &analysis.Analyzer{
Name: "license",
Doc: "check for a copyright header",
Run: runLicense,
}
func runLicense(pass *analysis.Pass) (interface{}, error) {
for _, file := range pass.Files {
if len(file.Comments) == 0 {
pass.Reportf(file.Pos(), "Copyright not found")
continue
}
if len(file.Comments[0].List) == 0 {
pass.Reportf(file.Pos(), "Copyright not found or wrong")
continue
}
commentGroup := 0
if strings.HasPrefix(file.Comments[0].List[0].Text, goGenerate) {
if len(file.Comments[0].List) > 1 {
pass.Reportf(file.Pos(), "Must be an empty line between the go:generate and the Copyright")
continue
}
commentGroup++
}
if strings.HasPrefix(file.Comments[0].List[0].Text, buildTag) {
commentGroup++
}
if len(file.Comments) < commentGroup+1 {
pass.Reportf(file.Pos(), "Copyright not found")
continue
}
if len(file.Comments[commentGroup].List) < 1 {
pass.Reportf(file.Pos(), "Copyright not found or wrong")
continue
}
var check bool
for _, comment := range file.Comments[commentGroup].List {
if header.MatchString(comment.Text) {
check = true
}
}
if !check {
pass.Reportf(file.Pos(), "Copyright did not match check")
}
}
return nil, nil
}

@ -1,77 +0,0 @@
// 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 checks
import (
"errors"
"os/exec"
"strings"
"golang.org/x/tools/go/analysis"
)
var Migrations = &analysis.Analyzer{
Name: "migrations",
Doc: "check migrations for black-listed packages.",
Run: checkMigrations,
}
var (
migrationDepBlockList = []string{
"code.gitea.io/gitea/models",
}
migrationImpBlockList = []string{
"code.gitea.io/gitea/modules/structs",
}
)
func checkMigrations(pass *analysis.Pass) (interface{}, error) {
if !strings.EqualFold(pass.Pkg.Path(), "code.gitea.io/gitea/models/migrations") {
return nil, nil
}
if _, err := exec.LookPath("go"); err != nil {
return nil, errors.New("go was not found in the PATH")
}
depsCmd := exec.Command("go", "list", "-f", `{{join .Deps "\n"}}`, "code.gitea.io/gitea/models/migrations")
depsOut, err := depsCmd.Output()
if err != nil {
return nil, err
}
deps := strings.Split(string(depsOut), "\n")
for _, dep := range deps {
if stringInSlice(dep, migrationDepBlockList) {
pass.Reportf(0, "code.gitea.io/gitea/models/migrations cannot depend on the following packages: %s", migrationDepBlockList)
return nil, nil
}
}
impsCmd := exec.Command("go", "list", "-f", `{{join .Imports "\n"}}`, "code.gitea.io/gitea/models/migrations")
impsOut, err := impsCmd.Output()
if err != nil {
return nil, err
}
imps := strings.Split(string(impsOut), "\n")
for _, imp := range imps {
if stringInSlice(imp, migrationImpBlockList) {
pass.Reportf(0, "code.gitea.io/gitea/models/migrations cannot import the following packages: %s", migrationImpBlockList)
return nil, nil
}
}
return nil, nil
}
func stringInSlice(needle string, haystack []string) bool {
for _, h := range haystack {
if strings.EqualFold(needle, h) {
return true
}
}
return false
}

@ -1,5 +0,0 @@
module code.gitea.io/gitea-vet
go 1.14
require golang.org/x/tools v0.0.0-20200325010219-a49f79bcc224

@ -1,20 +0,0 @@
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 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ=
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 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

@ -1,19 +0,0 @@
// 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 main
import (
"code.gitea.io/gitea-vet/checks"
"golang.org/x/tools/go/analysis/unitchecker"
)
func main() {
unitchecker.Main(
checks.Imports,
checks.License,
checks.Migrations,
)
}

@ -1,20 +0,0 @@
Copyright (c) 2016 The Gitea Authors
Copyright (c) 2014 The Gogs Authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

@ -1,47 +0,0 @@
// 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
}
if err := escapeValidatePathSegments(&task); err != nil {
return nil, err
}
_, resp, err := c.getResponse("POST", fmt.Sprintf("/admin/cron/%s", task), jsonHeader, nil)
return resp, err
}

@ -1,39 +0,0 @@
// Copyright 2015 The Gogs Authors. All rights reserved.
// Copyright 2019 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"
)
// AdminListOrgsOptions options for listing admin's organizations
type AdminListOrgsOptions struct {
ListOptions
}
// AdminListOrgs lists all orgs
func (c *Client) AdminListOrgs(opt AdminListOrgsOptions) ([]*Organization, *Response, error) {
opt.setDefaults()
orgs := make([]*Organization, 0, opt.PageSize)
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, *Response, error) {
if err := escapeValidatePathSegments(&user); err != nil {
return nil, nil, err
}
body, err := json.Marshal(&opt)
if err != nil {
return nil, nil, err
}
org := new(Organization)
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/orgs", user), jsonHeader, bytes.NewReader(body), org)
return org, resp, err
}

@ -1,25 +0,0 @@
// Copyright 2015 The Gogs 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"
)
// AdminCreateRepo create a repo
func (c *Client) AdminCreateRepo(user string, opt CreateRepoOption) (*Repository, *Response, error) {
if err := escapeValidatePathSegments(&user); err != nil {
return nil, nil, err
}
body, err := json.Marshal(&opt)
if err != nil {
return nil, nil, err
}
repo := new(Repository)
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/repos", user), jsonHeader, bytes.NewReader(body), repo)
return repo, resp, err
}

@ -1,130 +0,0 @@
// Copyright 2015 The Gogs Authors. All rights reserved.
// Copyright 2019 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"
)
// AdminListUsersOptions options for listing admin users
type AdminListUsersOptions struct {
ListOptions
}
// AdminListUsers lists all users
func (c *Client) AdminListUsers(opt AdminListUsersOptions) ([]*User, *Response, error) {
opt.setDefaults()
users := make([]*User, 0, opt.PageSize)
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/admin/users?%s", opt.getURLQuery().Encode()), nil, nil, &users)
return users, resp, err
}
// CreateUserOption create user options
type CreateUserOption struct {
SourceID int64 `json:"source_id"`
LoginName string `json:"login_name"`
Username string `json:"username"`
FullName string `json:"full_name"`
Email string `json:"email"`
Password string `json:"password"`
MustChangePassword *bool `json:"must_change_password"`
SendNotify bool `json:"send_notify"`
Visibility *VisibleType `json:"visibility"`
}
// Validate the CreateUserOption struct
func (opt CreateUserOption) Validate() error {
if len(opt.Email) == 0 {
return fmt.Errorf("email is empty")
}
if len(opt.Username) == 0 {
return fmt.Errorf("username is empty")
}
return nil
}
// AdminCreateUser create a user
func (c *Client) AdminCreateUser(opt CreateUserOption) (*User, *Response, error) {
if err := opt.Validate(); err != nil {
return nil, nil, err
}
body, err := json.Marshal(&opt)
if err != nil {
return nil, nil, err
}
user := new(User)
resp, err := c.getParsedResponse("POST", "/admin/users", jsonHeader, bytes.NewReader(body), user)
return user, resp, err
}
// EditUserOption edit user options
type EditUserOption struct {
SourceID int64 `json:"source_id"`
LoginName string `json:"login_name"`
Email *string `json:"email"`
FullName *string `json:"full_name"`
Password string `json:"password"`
Description *string `json:"description"`
MustChangePassword *bool `json:"must_change_password"`
Website *string `json:"website"`
Location *string `json:"location"`
Active *bool `json:"active"`
Admin *bool `json:"admin"`
AllowGitHook *bool `json:"allow_git_hook"`
AllowImportLocal *bool `json:"allow_import_local"`
MaxRepoCreation *int `json:"max_repo_creation"`
ProhibitLogin *bool `json:"prohibit_login"`
AllowCreateOrganization *bool `json:"allow_create_organization"`
Restricted *bool `json:"restricted"`
Visibility *VisibleType `json:"visibility"`
}
// AdminEditUser modify user informations
func (c *Client) AdminEditUser(user string, opt EditUserOption) (*Response, error) {
if err := escapeValidatePathSegments(&user); err != nil {
return nil, err
}
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) {
if err := escapeValidatePathSegments(&user); err != nil {
return nil, err
}
_, 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) {
if err := escapeValidatePathSegments(&user); err != nil {
return nil, nil, err
}
body, err := json.Marshal(&opt)
if err != nil {
return nil, nil, err
}
key := new(PublicKey)
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) (*Response, error) {
if err := escapeValidatePathSegments(&user); err != nil {
return nil, err
}
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/admin/users/%s/keys/%d", user, keyID), nil, nil)
return resp, err
}

@ -1,111 +0,0 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea // import "code.gitea.io/sdk/gitea"
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"time"
)
// Attachment a generic attachment
type Attachment struct {
ID int64 `json:"id"`
Name string `json:"name"`
Size int64 `json:"size"`
DownloadCount int64 `json:"download_count"`
Created time.Time `json:"created_at"`
UUID string `json:"uuid"`
DownloadURL string `json:"browser_download_url"`
}
// ListReleaseAttachmentsOptions options for listing release's attachments
type ListReleaseAttachmentsOptions struct {
ListOptions
}
// ListReleaseAttachments list release's attachments
func (c *Client) ListReleaseAttachments(user, repo string, release int64, opt ListReleaseAttachmentsOptions) ([]*Attachment, *Response, error) {
if err := escapeValidatePathSegments(&user, &repo); err != nil {
return nil, nil, err
}
opt.setDefaults()
attachments := make([]*Attachment, 0, opt.PageSize)
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, resp, err
}
// GetReleaseAttachment returns the requested attachment
func (c *Client) GetReleaseAttachment(user, repo string, release int64, id int64) (*Attachment, *Response, error) {
if err := escapeValidatePathSegments(&user, &repo); err != nil {
return nil, nil, err
}
a := new(Attachment)
resp, err := c.getParsedResponse("GET",
fmt.Sprintf("/repos/%s/%s/releases/%d/assets/%d", user, repo, release, id),
nil, nil, &a)
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, *Response, error) {
if err := escapeValidatePathSegments(&user, &repo); err != nil {
return nil, nil, err
}
// Write file to body
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("attachment", filename)
if err != nil {
return nil, nil, err
}
if _, err = io.Copy(part, file); err != nil {
return nil, nil, err
}
if err = writer.Close(); err != nil {
return nil, nil, err
}
// Send request
attachment := new(Attachment)
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, resp, err
}
// EditAttachmentOptions options for editing attachments
type EditAttachmentOptions struct {
Name string `json:"name"`
}
// 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) {
if err := escapeValidatePathSegments(&user, &repo); err != nil {
return nil, nil, err
}
body, err := json.Marshal(&form)
if err != nil {
return nil, nil, err
}
attach := new(Attachment)
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) (*Response, error) {
if err := escapeValidatePathSegments(&user, &repo); err != nil {
return nil, err
}
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/releases/%d/assets/%d", user, repo, release, id), nil, nil)
return resp, err
}

@ -1,333 +0,0 @@
// 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.
package gitea
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"sync"
"github.com/hashicorp/go-version"
)
var jsonHeader = http.Header{"content-type": []string{"application/json"}}
// Version return the library version
func Version() string {
return "0.14.0"
}
// Client represents a thread-safe Gitea API client.
type Client struct {
url string
accessToken string
username string
password string
otp string
sudo string
debug bool
client *http.Client
ctx context.Context
mutex sync.RWMutex
serverVersion *version.Version
getVersionOnce sync.Once
}
// Response represents the gitea response
type Response struct {
*http.Response
}
// NewClient initializes and returns a API client.
// Usage of all gitea.Client methods is concurrency-safe.
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, SetHTTPClient(httpClient))
return client
}
// SetHTTPClient is an option for NewClient to set custom http client
func SetHTTPClient(httpClient *http.Client) func(client *Client) {
return func(client *Client) {
client.SetHTTPClient(httpClient)
}
}
// SetHTTPClient replaces default http.Client with user given one.
func (c *Client) SetHTTPClient(client *http.Client) {
c.mutex.Lock()
c.client = client
c.mutex.Unlock()
}
// SetToken is an option for NewClient to set token
func SetToken(token string) func(client *Client) {
return func(client *Client) {
client.mutex.Lock()
client.accessToken = token
client.mutex.Unlock()
}
}
// 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.mutex.Lock()
c.username, c.password = username, password
c.mutex.Unlock()
}
// 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.mutex.Lock()
c.otp = otp
c.mutex.Unlock()
}
// 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.mutex.Lock()
c.ctx = ctx
c.mutex.Unlock()
}
// 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.mutex.Lock()
c.sudo = sudo
c.mutex.Unlock()
}
// SetDebugMode is an option for NewClient to enable debug mode
func SetDebugMode() func(client *Client) {
return func(client *Client) {
client.mutex.Lock()
client.debug = true
client.mutex.Unlock()
}
}
func (c *Client) getWebResponse(method, path string, body io.Reader) ([]byte, *Response, error) {
c.mutex.RLock()
debug := c.debug
if debug {
fmt.Printf("%s: %s\nBody: %v\n", method, c.url+path, body)
}
req, err := http.NewRequestWithContext(c.ctx, method, c.url+path, body)
client := c.client // client ref can change from this point on so safe it
c.mutex.RUnlock()
if err != nil {
return nil, nil, err
}
resp, err := client.Do(req)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if 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) {
c.mutex.RLock()
debug := c.debug
if 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 {
c.mutex.RUnlock()
return nil, err
}
if len(c.accessToken) != 0 {
req.Header.Set("Authorization", "token "+c.accessToken)
}
if len(c.otp) != 0 {
req.Header.Set("X-GITEA-OTP", c.otp)
}
if len(c.username) != 0 {
req.SetBasicAuth(c.username, c.password)
}
if len(c.sudo) != 0 {
req.Header.Set("Sudo", c.sudo)
}
client := c.client // client ref can change from this point on so safe it
c.mutex.RUnlock()
for k, v := range header {
req.Header[k] = v
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
if debug {
fmt.Printf("Response: %v\n\n", resp)
}
return &Response{resp}, nil
}
// Converts a response for a HTTP status code indicating an error condition
// (non-2XX) to a well-known error value and response body. For non-problematic
// (2XX) status codes nil will be returned. Note that on a non-2XX response, the
// response body stream will have been read and, hence, is closed on return.
func statusCodeToErr(resp *Response) (body []byte, err error) {
// no error
if resp.StatusCode/100 == 2 {
return nil, nil
}
//
// error: body will be read for details
//
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("body read on HTTP error %d: %v", resp.StatusCode, err)
}
switch resp.StatusCode {
case 403:
return data, errors.New("403 Forbidden")
case 404:
return data, errors.New("404 Not Found")
case 409:
return data, errors.New("409 Conflict")
case 422:
return data, fmt.Errorf("422 Unprocessable Entity: %s", string(data))
}
path := resp.Request.URL.Path
method := resp.Request.Method
header := resp.Request.Header
errMap := make(map[string]interface{})
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 data, 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, errors.New(errMap["message"].(string))
}
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