mirror of
https://gitea.com/gitea/tea.git
synced 2026-07-16 11:07:39 +02:00
Compare commits
85 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 88f5cdcafa | |||
| 5cfee362c8 | |||
| fff82ac723 | |||
| b11d991d1e | |||
| 4b209d68de | |||
| 2b844786f0 | |||
| 67bb0e7789 | |||
| 6de58b2223 | |||
| 27bcbf80ae | |||
| dfbbda83f3 | |||
| df320c74ca | |||
| 09fc09c2f7 | |||
| 5fa24b9a65 | |||
| 18274f1ebc | |||
| d1b9b7735e | |||
| 6dd33b5f4f | |||
| 23a3967e15 | |||
| 06e4d16bf3 | |||
| 28ba9b915b | |||
| 579099f9d9 | |||
| a664449282 | |||
| 8e0666ab85 | |||
| bbe97a5e23 | |||
| 82323c7270 | |||
| 861201541d | |||
| 3d667c1594 | |||
| ef0dc62dd6 | |||
| 70c7ee11f2 | |||
| 6134351048 | |||
| 8be4dae66e | |||
| b8dcb8a442 | |||
| 01632d927e | |||
| 1b79be7cea | |||
| 2cc45f1cce | |||
| 2b64762a32 | |||
| 19dd8b1b4b | |||
| f617f26da0 | |||
| a5ecf06c2a | |||
| e686e8d0bd | |||
| 22ff601988 | |||
| 9d6ae4bf02 | |||
| 2985824ab0 | |||
| 83b718ac34 | |||
| 1f6fd97fc1 | |||
| 27e6083e23 | |||
| 5d2d1a6e0c | |||
| 88421bb888 | |||
| dd81b33cec | |||
| b100d4c939 | |||
| 892905d482 | |||
| 5103496232 | |||
| a58c35c3e2 | |||
| 783ac7684a | |||
| d0b7ea09e8 | |||
| 20914a1375 | |||
| 3c1c9b2904 | |||
| 63bc90ea52 | |||
| 9e0a6203ae | |||
| 84ecd16f9c | |||
| 53e53e1067 | |||
| 0489d8c275 | |||
| f538c05282 | |||
| 662e339bf9 | |||
| 5bb73667d1 | |||
| f329f6fab2 | |||
| 366069315f | |||
| 1e13681663 | |||
| bfbec3fc00 | |||
| e31a167e54 | |||
| 6a7c3e4efa | |||
| b05e03416b | |||
| 21881525a8 | |||
| 9a462247bd | |||
| 5f74fb37df | |||
| ec658cfc33 | |||
| cb9824b451 | |||
| a531faa626 | |||
| 302c946cb8 | |||
| 0346e1cbb5 | |||
| cd4051ed38 | |||
| c797624fcf | |||
| 3372c9ec59 | |||
| 1ac8492ac7 | |||
| d019f0dd72 | |||
| c031db2413 |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "Tea DevContainer",
|
"name": "Tea DevContainer",
|
||||||
"image": "mcr.microsoft.com/devcontainers/go:2.0-trixie",
|
"image": "mcr.microsoft.com/devcontainers/go:2.1-trixie",
|
||||||
"features": {
|
"features": {
|
||||||
"ghcr.io/devcontainers/features/git-lfs:1.2.5": {}
|
"ghcr.io/devcontainers/features/git-lfs:1.2.5": {}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ jobs:
|
|||||||
goreleaser:
|
goreleaser:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v7
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
- run: git fetch --force --tags
|
- run: git fetch --force --tags
|
||||||
@@ -17,15 +17,15 @@ jobs:
|
|||||||
go-version-file: "go.mod"
|
go-version-file: "go.mod"
|
||||||
- name: import gpg
|
- name: import gpg
|
||||||
id: import_gpg
|
id: import_gpg
|
||||||
uses: crazy-max/ghaction-import-gpg@v6
|
uses: crazy-max/ghaction-import-gpg@v7
|
||||||
with:
|
with:
|
||||||
gpg_private_key: ${{ secrets.GPGSIGN_KEY }}
|
gpg_private_key: ${{ secrets.GPGSIGN_KEY }}
|
||||||
passphrase: ${{ secrets.GPGSIGN_PASSPHRASE }}
|
passphrase: ${{ secrets.GPGSIGN_PASSPHRASE }}
|
||||||
- name: get SDK version
|
- name: get SDK version
|
||||||
id: sdk_version
|
id: sdk_version
|
||||||
run: echo "version=$(go list -f '{{.Version}}' -m code.gitea.io/sdk/gitea)" >> "$GITHUB_OUTPUT"
|
run: echo "version=$(go list -f '{{.Version}}' -m gitea.dev/sdk)" >> "$GITHUB_OUTPUT"
|
||||||
- name: goreleaser
|
- name: goreleaser
|
||||||
uses: goreleaser/goreleaser-action@v6
|
uses: goreleaser/goreleaser-action@v7
|
||||||
with:
|
with:
|
||||||
distribution: goreleaser-pro
|
distribution: goreleaser-pro
|
||||||
version: "~> v1"
|
version: "~> v1"
|
||||||
@@ -49,24 +49,24 @@ jobs:
|
|||||||
DOCKER_LATEST: nightly
|
DOCKER_LATEST: nightly
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v7
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0 # all history for all branches and tags
|
fetch-depth: 0 # all history for all branches and tags
|
||||||
|
|
||||||
- name: Set up QEMU
|
- name: Set up QEMU
|
||||||
uses: docker/setup-qemu-action@v3
|
uses: docker/setup-qemu-action@v4
|
||||||
|
|
||||||
- name: Set up Docker BuildX
|
- name: Set up Docker BuildX
|
||||||
uses: docker/setup-buildx-action@v3
|
uses: docker/setup-buildx-action@v4
|
||||||
|
|
||||||
- name: Login to DockerHub
|
- name: Login to DockerHub
|
||||||
uses: docker/login-action@v3
|
uses: docker/login-action@v4
|
||||||
with:
|
with:
|
||||||
username: ${{ secrets.DOCKER_USERNAME }}
|
username: ${{ secrets.DOCKER_USERNAME }}
|
||||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||||
|
|
||||||
- name: Build and push
|
- name: Build and push
|
||||||
uses: docker/build-push-action@v6
|
uses: docker/build-push-action@v7
|
||||||
env:
|
env:
|
||||||
ACTIONS_RUNTIME_TOKEN: '' # See https://gitea.com/gitea/act_runner/issues/119
|
ACTIONS_RUNTIME_TOKEN: '' # See https://gitea.com/gitea/act_runner/issues/119
|
||||||
with:
|
with:
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ jobs:
|
|||||||
goreleaser:
|
goreleaser:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v7
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
- run: git fetch --force --tags
|
- run: git fetch --force --tags
|
||||||
@@ -18,15 +18,15 @@ jobs:
|
|||||||
go-version-file: 'go.mod'
|
go-version-file: 'go.mod'
|
||||||
- name: import gpg
|
- name: import gpg
|
||||||
id: import_gpg
|
id: import_gpg
|
||||||
uses: crazy-max/ghaction-import-gpg@v6
|
uses: crazy-max/ghaction-import-gpg@v7
|
||||||
with:
|
with:
|
||||||
gpg_private_key: ${{ secrets.GPGSIGN_KEY }}
|
gpg_private_key: ${{ secrets.GPGSIGN_KEY }}
|
||||||
passphrase: ${{ secrets.GPGSIGN_PASSPHRASE }}
|
passphrase: ${{ secrets.GPGSIGN_PASSPHRASE }}
|
||||||
- name: get SDK version
|
- name: get SDK version
|
||||||
id: sdk_version
|
id: sdk_version
|
||||||
run: echo "version=$(go list -f '{{.Version}}' -m code.gitea.io/sdk/gitea)" >> "$GITHUB_OUTPUT"
|
run: echo "version=$(go list -f '{{.Version}}' -m gitea.dev/sdk)" >> "$GITHUB_OUTPUT"
|
||||||
- name: goreleaser
|
- name: goreleaser
|
||||||
uses: goreleaser/goreleaser-action@v6
|
uses: goreleaser/goreleaser-action@v7
|
||||||
with:
|
with:
|
||||||
distribution: goreleaser-pro
|
distribution: goreleaser-pro
|
||||||
version: "~> v1"
|
version: "~> v1"
|
||||||
@@ -50,18 +50,18 @@ jobs:
|
|||||||
DOCKER_LATEST: nightly
|
DOCKER_LATEST: nightly
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v7
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0 # all history for all branches and tags
|
fetch-depth: 0 # all history for all branches and tags
|
||||||
|
|
||||||
- name: Set up QEMU
|
- name: Set up QEMU
|
||||||
uses: docker/setup-qemu-action@v3
|
uses: docker/setup-qemu-action@v4
|
||||||
|
|
||||||
- name: Set up Docker BuildX
|
- name: Set up Docker BuildX
|
||||||
uses: docker/setup-buildx-action@v3
|
uses: docker/setup-buildx-action@v4
|
||||||
|
|
||||||
- name: Login to DockerHub
|
- name: Login to DockerHub
|
||||||
uses: docker/login-action@v3
|
uses: docker/login-action@v4
|
||||||
with:
|
with:
|
||||||
username: ${{ secrets.DOCKER_USERNAME }}
|
username: ${{ secrets.DOCKER_USERNAME }}
|
||||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||||
@@ -71,7 +71,7 @@ jobs:
|
|||||||
run: echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_ENV
|
run: echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_ENV
|
||||||
|
|
||||||
- name: Build and push
|
- name: Build and push
|
||||||
uses: docker/build-push-action@v6
|
uses: docker/build-push-action@v7
|
||||||
env:
|
env:
|
||||||
ACTIONS_RUNTIME_TOKEN: '' # See https://gitea.com/gitea/act_runner/issues/119
|
ACTIONS_RUNTIME_TOKEN: '' # See https://gitea.com/gitea/act_runner/issues/119
|
||||||
with:
|
with:
|
||||||
|
|||||||
@@ -12,15 +12,11 @@ jobs:
|
|||||||
# uses: golang/govulncheck-action@v1
|
# uses: golang/govulncheck-action@v1
|
||||||
# with:
|
# with:
|
||||||
# go-version-file: 'go.mod'
|
# go-version-file: 'go.mod'
|
||||||
check-and-test:
|
check-and-unit:
|
||||||
|
name: Lint Build And Unit Coverage
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
env:
|
|
||||||
HTTP_PROXY: ""
|
|
||||||
GITEA_TEA_TEST_URL: "http://gitea:3000"
|
|
||||||
GITEA_TEA_TEST_USERNAME: "test01"
|
|
||||||
GITEA_TEA_TEST_PASSWORD: "test01"
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v7
|
||||||
- uses: actions/setup-go@v6
|
- uses: actions/setup-go@v6
|
||||||
with:
|
with:
|
||||||
go-version-file: 'go.mod'
|
go-version-file: 'go.mod'
|
||||||
@@ -32,14 +28,30 @@ jobs:
|
|||||||
make fmt-check
|
make fmt-check
|
||||||
make docs-check
|
make docs-check
|
||||||
make build
|
make build
|
||||||
- run: curl --noproxy "*" http://gitea:3000/api/v1/version # verify connection to instance
|
- name: unit test and coverage
|
||||||
- name: test and coverage
|
|
||||||
run: |
|
run: |
|
||||||
make test
|
|
||||||
make unit-test-coverage
|
make unit-test-coverage
|
||||||
|
|
||||||
|
integration-test:
|
||||||
|
name: Integration Test
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
env:
|
||||||
|
HTTP_PROXY: ""
|
||||||
|
GITEA_TEA_TEST_URL: "http://gitea:3000"
|
||||||
|
GITEA_TEA_TEST_USERNAME: "test01"
|
||||||
|
GITEA_TEA_TEST_PASSWORD: "test01"
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v7
|
||||||
|
- uses: actions/setup-go@v6
|
||||||
|
with:
|
||||||
|
go-version-file: 'go.mod'
|
||||||
|
- run: curl --noproxy "*" http://gitea:3000/api/v1/version # verify connection to instance
|
||||||
|
- name: integration test
|
||||||
|
run: |
|
||||||
|
make integration-test
|
||||||
services:
|
services:
|
||||||
gitea:
|
gitea:
|
||||||
image: docker.gitea.com/gitea:1.25.4
|
image: docker.gitea.com/gitea:1.26.2
|
||||||
cmd:
|
cmd:
|
||||||
- bash
|
- bash
|
||||||
- -c
|
- -c
|
||||||
|
|||||||
@@ -17,3 +17,5 @@ dist/
|
|||||||
.direnv/
|
.direnv/
|
||||||
result
|
result
|
||||||
result-*
|
result-*
|
||||||
|
|
||||||
|
.DS_Store
|
||||||
+1
-1
@@ -56,7 +56,7 @@ builds:
|
|||||||
flags:
|
flags:
|
||||||
- -trimpath
|
- -trimpath
|
||||||
ldflags:
|
ldflags:
|
||||||
- -s -w -X "code.gitea.io/tea/modules/version.Version={{ trimprefix .Summary "v" }}" -X "code.gitea.io/tea/modules/version.Tags=" -X "code.gitea.io/tea/modules/version.SDK={{ .Env.SDK_VERSION }}"
|
- -s -w -X "gitea.dev/tea/modules/version.Version={{ trimprefix .Summary "v" }}" -X "gitea.dev/tea/modules/version.Tags=" -X "gitea.dev/tea/modules/version.SDK={{ .Env.SDK_VERSION }}"
|
||||||
binary: >-
|
binary: >-
|
||||||
{{ .ProjectName }}-
|
{{ .ProjectName }}-
|
||||||
{{- .Version }}-
|
{{- .Version }}-
|
||||||
|
|||||||
@@ -1,5 +1,20 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## [v0.13.0](https://gitea.com/gitea/tea/releases/tag/v0.13.0) - 2026-04-05
|
||||||
|
|
||||||
|
* FEATURES
|
||||||
|
* Add `tea pr edit` subcommand for pull requests (#944)
|
||||||
|
* Add `tea repo edit` subcommand (#928)
|
||||||
|
* Support owner-based repository listing in `tea repo ls` (#931)
|
||||||
|
* Store OAuth tokens in OS keyring via credstore (#926)
|
||||||
|
* Support parsing multiple values in `tea api` subcommand (#911)
|
||||||
|
* ENHANCEMENTS
|
||||||
|
* Replace log.Fatal/os.Exit with proper error returns (#941)
|
||||||
|
* Update to charm libraries v2 (#923)
|
||||||
|
* MISC
|
||||||
|
* Bump Go version to 1.26
|
||||||
|
* Update dependencies: go-git/v5 v5.17.2, gitea SDK v0.24.1, urfave/cli/v3 v3.8.0, oauth2 v0.36.0, tablewriter v1.1.4, go-authgate/sdk-go v0.6.1
|
||||||
|
|
||||||
## [v0.9.1](https://gitea.com/gitea/tea/releases/tag/v0.9.1) - 2023-02-15
|
## [v0.9.1](https://gitea.com/gitea/tea/releases/tag/v0.9.1) - 2023-02-15
|
||||||
|
|
||||||
* BUGFIXES
|
* BUGFIXES
|
||||||
|
|||||||
+1
-1
@@ -124,7 +124,7 @@ import (
|
|||||||
|
|
||||||
// local packages
|
// local packages
|
||||||
"code.gitea.io/gitea/models"
|
"code.gitea.io/gitea/models"
|
||||||
"code.gitea.io/sdk/gitea"
|
"gitea.dev/sdk"
|
||||||
|
|
||||||
// external packages
|
// external packages
|
||||||
"github.com/foo/bar"
|
"github.com/foo/bar"
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ GOFILES := $(shell find . -name "*.go" -type f ! -path "*/bindata.go")
|
|||||||
|
|
||||||
# Tool packages with pinned versions
|
# Tool packages with pinned versions
|
||||||
GOFUMPT_PACKAGE ?= mvdan.cc/gofumpt@v0.9.2
|
GOFUMPT_PACKAGE ?= mvdan.cc/gofumpt@v0.9.2
|
||||||
GOLANGCI_LINT_PACKAGE ?= github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.8.0
|
GOLANGCI_LINT_PACKAGE ?= github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.11.4
|
||||||
|
|
||||||
ifneq ($(DRONE_TAG),)
|
ifneq ($(DRONE_TAG),)
|
||||||
VERSION ?= $(subst v,,$(DRONE_TAG))
|
VERSION ?= $(subst v,,$(DRONE_TAG))
|
||||||
@@ -24,13 +24,17 @@ endif
|
|||||||
TEA_VERSION_TAG ?= $(shell sed 's/+/_/' <<< $(TEA_VERSION))
|
TEA_VERSION_TAG ?= $(shell sed 's/+/_/' <<< $(TEA_VERSION))
|
||||||
|
|
||||||
TAGS ?=
|
TAGS ?=
|
||||||
SDK ?= $(shell $(GO) list -f '{{.Version}}' -m code.gitea.io/sdk/gitea)
|
SDK ?= $(shell $(GO) list -f '{{.Version}}' -m gitea.dev/sdk)
|
||||||
LDFLAGS := -X "code.gitea.io/tea/modules/version.Version=$(TEA_VERSION)" -X "code.gitea.io/tea/modules/version.Tags=$(TAGS)" -X "code.gitea.io/tea/modules/version.SDK=$(SDK)" -s -w
|
LDFLAGS := -X "gitea.dev/tea/modules/version.Version=$(TEA_VERSION)" -X "gitea.dev/tea/modules/version.Tags=$(TAGS)" -X "gitea.dev/tea/modules/version.SDK=$(SDK)" -s -w
|
||||||
|
|
||||||
# override to allow passing additional goflags via make CLI
|
# override to allow passing additional goflags via make CLI
|
||||||
override GOFLAGS := $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)'
|
override GOFLAGS := $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)'
|
||||||
|
|
||||||
PACKAGES ?= $(shell $(GO) list ./...)
|
PACKAGES ?= $(shell $(GO) list ./... | grep -v '^gitea.dev/tea/tests')
|
||||||
|
UNIT_PACKAGES ?= $(PACKAGES)
|
||||||
|
INTEGRATION_PACKAGES ?= $(shell $(GO) list ./tests/... 2>/dev/null)
|
||||||
|
INTEGRATION_TEST_TAGS ?= testtools
|
||||||
|
INTEGRATION_TEST_GOFLAGS ?= -v
|
||||||
SOURCES ?= $(shell find . -name "*.go" -type f)
|
SOURCES ?= $(shell find . -name "*.go" -type f)
|
||||||
|
|
||||||
# OS specific vars.
|
# OS specific vars.
|
||||||
@@ -64,11 +68,11 @@ vet:
|
|||||||
|
|
||||||
.PHONY: lint
|
.PHONY: lint
|
||||||
lint:
|
lint:
|
||||||
$(GO) run $(GOLANGCI_LINT_PACKAGE) run
|
$(GO) run $(GOLANGCI_LINT_PACKAGE) run --build-tags testtools
|
||||||
|
|
||||||
.PHONY: lint-fix
|
.PHONY: lint-fix
|
||||||
lint-fix:
|
lint-fix:
|
||||||
$(GO) run $(GOLANGCI_LINT_PACKAGE) run --fix
|
$(GO) run $(GOLANGCI_LINT_PACKAGE) run --build-tags testtools --fix
|
||||||
|
|
||||||
.PHONY: fmt-check
|
.PHONY: fmt-check
|
||||||
fmt-check:
|
fmt-check:
|
||||||
@@ -93,13 +97,24 @@ docs-check:
|
|||||||
exit 1; \
|
exit 1; \
|
||||||
fi;
|
fi;
|
||||||
|
|
||||||
|
.PHONY: unit-test
|
||||||
|
unit-test:
|
||||||
|
$(GO) test $(UNIT_PACKAGES)
|
||||||
|
|
||||||
|
.PHONY: integration-test
|
||||||
|
integration-test:
|
||||||
|
@if [ -n "$(INTEGRATION_PACKAGES)" ]; then \
|
||||||
|
$(GO) test $(INTEGRATION_TEST_GOFLAGS) -tags='$(INTEGRATION_TEST_TAGS)' $(INTEGRATION_PACKAGES); \
|
||||||
|
else \
|
||||||
|
echo "No integration test packages found"; \
|
||||||
|
fi
|
||||||
|
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
test:
|
test: unit-test integration-test
|
||||||
$(GO) test -tags='sqlite sqlite_unlock_notify' $(PACKAGES)
|
|
||||||
|
|
||||||
.PHONY: unit-test-coverage
|
.PHONY: unit-test-coverage
|
||||||
unit-test-coverage:
|
unit-test-coverage:
|
||||||
$(GO) test -tags='sqlite sqlite_unlock_notify' -cover -coverprofile coverage.out $(PACKAGES) && echo "\n==>\033[32m Ok\033[m\n" || exit 1
|
$(GO) test -cover -coverprofile coverage.out $(UNIT_PACKAGES) && echo "\n==>\033[32m Ok\033[m\n" || exit 1
|
||||||
|
|
||||||
.PHONY: tidy
|
.PHONY: tidy
|
||||||
tidy:
|
tidy:
|
||||||
@@ -122,4 +137,3 @@ $(EXECUTABLE): $(SOURCES)
|
|||||||
.PHONY: build-image
|
.PHONY: build-image
|
||||||
build-image:
|
build-image:
|
||||||
docker build --build-arg VERSION=$(TEA_VERSION) -t gitea/tea:$(TEA_VERSION_TAG) .
|
docker build --build-arg VERSION=$(TEA_VERSION) -t gitea/tea:$(TEA_VERSION_TAG) .
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
[](https://opensource.org/licenses/MIT)
|
[](https://opensource.org/licenses/MIT)
|
||||||
[](https://gitea.com/gitea/tea/releases)
|
[](https://gitea.com/gitea/tea/releases)
|
||||||
[](https://discord.gg/Gitea)
|
[](https://discord.gg/Gitea)
|
||||||
[](https://goreportcard.com/report/code.gitea.io/tea) [](https://godoc.org/code.gitea.io/tea)
|
[](https://goreportcard.com/report/gitea.dev/tea) [](https://godoc.org/gitea.dev/tea)
|
||||||

|

|
||||||
|
|
||||||
## The official CLI for Gitea
|
## The official CLI for Gitea
|
||||||
@@ -97,7 +97,7 @@ ABOUT
|
|||||||
More info about Gitea itself on https://about.gitea.com.
|
More info about Gitea itself on https://about.gitea.com.
|
||||||
```
|
```
|
||||||
|
|
||||||
- tea uses [code.gitea.io/sdk](https://code.gitea.io/sdk) and interacts with the Gitea API.
|
- tea uses [gitea.dev/sdk](https://gitea.dev/sdk) and interacts with the Gitea API.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
@@ -169,7 +169,7 @@ tea man --out ./tea.man
|
|||||||
|
|
||||||
## Compilation
|
## Compilation
|
||||||
|
|
||||||
Make sure you have a current go version installed (1.13 or newer).
|
Make sure you have a current Go version installed (1.26 or newer).
|
||||||
|
|
||||||
- To compile the source yourself with the recommended flags & tags:
|
- To compile the source yourself with the recommended flags & tags:
|
||||||
```sh
|
```sh
|
||||||
@@ -186,7 +186,7 @@ Make sure you have a current go version installed (1.13 or newer).
|
|||||||
|
|
||||||
- For a quick installation without `git` & `make`, set $version and exec:
|
- For a quick installation without `git` & `make`, set $version and exec:
|
||||||
```sh
|
```sh
|
||||||
go install code.gitea.io/tea@${version}
|
go install gitea.dev/tea@${version}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|||||||
+1
-1
@@ -6,7 +6,7 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
stdctx "context"
|
stdctx "context"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/actions"
|
"gitea.dev/tea/cmd/actions"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|||||||
+1
-1
@@ -6,7 +6,7 @@ package actions
|
|||||||
import (
|
import (
|
||||||
stdctx "context"
|
stdctx "context"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/actions/runs"
|
"gitea.dev/tea/cmd/actions/runs"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
"gitea.dev/tea/cmd/flags"
|
||||||
"code.gitea.io/tea/modules/context"
|
"gitea.dev/tea/modules/context"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
@@ -36,7 +36,13 @@ func runRunsDelete(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
return fmt.Errorf("run ID is required")
|
return fmt.Errorf("run ID is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
c := context.InitCommand(cmd)
|
c, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := c.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
client := c.Login.Client()
|
client := c.Login.Client()
|
||||||
|
|
||||||
runIDStr := cmd.Args().First()
|
runIDStr := cmd.Args().First()
|
||||||
@@ -55,7 +61,7 @@ func runRunsDelete(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = client.DeleteRepoActionRun(c.Owner, c.Repo, runID)
|
_, err = client.Actions.DeleteRepoRun(ctx, c.Owner, c.Repo, runID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to delete run: %w", err)
|
return fmt.Errorf("failed to delete run: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-11
@@ -8,11 +8,11 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
gitea "gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
"code.gitea.io/tea/modules/print"
|
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/print"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -83,7 +83,13 @@ func parseTimeFlag(value string) (time.Time, error) {
|
|||||||
|
|
||||||
// RunRunsList lists workflow runs
|
// RunRunsList lists workflow runs
|
||||||
func RunRunsList(ctx stdctx.Context, cmd *cli.Command) error {
|
func RunRunsList(ctx stdctx.Context, cmd *cli.Command) error {
|
||||||
c := context.InitCommand(cmd)
|
c, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := c.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
client := c.Login.Client()
|
client := c.Login.Client()
|
||||||
|
|
||||||
// Parse time filters
|
// Parse time filters
|
||||||
@@ -98,9 +104,9 @@ func RunRunsList(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Build list options
|
// Build list options
|
||||||
listOpts := flags.GetListOptions()
|
listOpts := flags.GetListOptions(cmd)
|
||||||
|
|
||||||
runs, _, err := client.ListRepoActionRuns(c.Owner, c.Repo, gitea.ListRepoActionRunsOptions{
|
runs, _, err := client.Actions.ListRepoRuns(ctx, c.Owner, c.Repo, gitea.ListRepoActionRunsOptions{
|
||||||
ListOptions: listOpts,
|
ListOptions: listOpts,
|
||||||
Status: cmd.String("status"),
|
Status: cmd.String("status"),
|
||||||
Branch: cmd.String("branch"),
|
Branch: cmd.String("branch"),
|
||||||
@@ -112,15 +118,13 @@ func RunRunsList(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if runs == nil {
|
if runs == nil {
|
||||||
print.ActionRunsList(nil, c.Output)
|
return print.ActionRunsList(nil, c.Output)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter by time if specified
|
// Filter by time if specified
|
||||||
filteredRuns := filterRunsByTime(runs.WorkflowRuns, since, until)
|
filteredRuns := filterRunsByTime(runs.WorkflowRuns, since, until)
|
||||||
|
|
||||||
print.ActionRunsList(filteredRuns, c.Output)
|
return print.ActionRunsList(filteredRuns, c.Output)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// filterRunsByTime filters runs based on time range
|
// filterRunsByTime filters runs based on time range
|
||||||
|
|||||||
@@ -4,10 +4,15 @@
|
|||||||
package runs
|
package runs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
gitea "gitea.dev/sdk"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/urfave/cli/v3"
|
||||||
|
|
||||||
|
"gitea.dev/tea/modules/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFilterRunsByTime(t *testing.T) {
|
func TestFilterRunsByTime(t *testing.T) {
|
||||||
@@ -75,3 +80,32 @@ func TestFilterRunsByTime(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRunRunsListRequiresRepoContext(t *testing.T) {
|
||||||
|
oldWd, err := os.Getwd()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
require.NoError(t, os.Chdir(t.TempDir()))
|
||||||
|
t.Cleanup(func() {
|
||||||
|
require.NoError(t, os.Chdir(oldWd))
|
||||||
|
})
|
||||||
|
|
||||||
|
config.SetConfigForTesting(config.LocalConfig{
|
||||||
|
Logins: []config.Login{{
|
||||||
|
Name: "test",
|
||||||
|
URL: "https://gitea.example.com",
|
||||||
|
Token: "token",
|
||||||
|
User: "tester",
|
||||||
|
Default: true,
|
||||||
|
}},
|
||||||
|
})
|
||||||
|
|
||||||
|
cmd := &cli.Command{
|
||||||
|
Name: CmdRunsList.Name,
|
||||||
|
Flags: CmdRunsList.Flags,
|
||||||
|
}
|
||||||
|
require.NoError(t, cmd.Set("login", "test"))
|
||||||
|
|
||||||
|
err = RunRunsList(t.Context(), cmd)
|
||||||
|
require.ErrorContains(t, err, "remote repository required")
|
||||||
|
}
|
||||||
|
|||||||
+19
-13
@@ -9,10 +9,10 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
gitea "gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -42,7 +42,13 @@ func runRunsLogs(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
return fmt.Errorf("run ID is required")
|
return fmt.Errorf("run ID is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
c := context.InitCommand(cmd)
|
c, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := c.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
client := c.Login.Client()
|
client := c.Login.Client()
|
||||||
|
|
||||||
runIDStr := cmd.Args().First()
|
runIDStr := cmd.Args().First()
|
||||||
@@ -63,10 +69,10 @@ func runRunsLogs(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if follow {
|
if follow {
|
||||||
return followJobLogs(client, c, jobID, "")
|
return followJobLogs(ctx, client, c, jobID, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
logs, _, err := client.GetRepoActionJobLogs(c.Owner, c.Repo, jobID)
|
logs, _, err := client.Actions.GetRepoRunJobLogs(ctx, c.Owner, c.Repo, jobID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get logs for job %d: %w", jobID, err)
|
return fmt.Errorf("failed to get logs for job %d: %w", jobID, err)
|
||||||
}
|
}
|
||||||
@@ -77,8 +83,8 @@ func runRunsLogs(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, fetch all jobs and their logs
|
// Otherwise, fetch all jobs and their logs
|
||||||
jobs, _, err := client.ListRepoActionRunJobs(c.Owner, c.Repo, runID, gitea.ListRepoActionJobsOptions{
|
jobs, _, err := client.Actions.ListRepoJobsByRun(ctx, c.Owner, c.Repo, runID, gitea.ListRepoActionJobsOptions{
|
||||||
ListOptions: flags.GetListOptions(),
|
ListOptions: flags.GetListOptions(cmd),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get jobs: %w", err)
|
return fmt.Errorf("failed to get jobs: %w", err)
|
||||||
@@ -96,7 +102,7 @@ func runRunsLogs(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
|
|
||||||
// If following with single job, follow it
|
// If following with single job, follow it
|
||||||
if follow && len(jobs.Jobs) == 1 {
|
if follow && len(jobs.Jobs) == 1 {
|
||||||
return followJobLogs(client, c, jobs.Jobs[0].ID, jobs.Jobs[0].Name)
|
return followJobLogs(ctx, client, c, jobs.Jobs[0].ID, jobs.Jobs[0].Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch logs for each job
|
// Fetch logs for each job
|
||||||
@@ -109,7 +115,7 @@ func runRunsLogs(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
fmt.Printf("Status: %s\n", job.Status)
|
fmt.Printf("Status: %s\n", job.Status)
|
||||||
fmt.Println("---")
|
fmt.Println("---")
|
||||||
|
|
||||||
logs, _, err := client.GetRepoActionJobLogs(c.Owner, c.Repo, job.ID)
|
logs, _, err := client.Actions.GetRepoRunJobLogs(ctx, c.Owner, c.Repo, job.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error fetching logs: %v\n", err)
|
fmt.Printf("Error fetching logs: %v\n", err)
|
||||||
continue
|
continue
|
||||||
@@ -122,7 +128,7 @@ func runRunsLogs(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// followJobLogs continuously fetches and displays logs for a running job
|
// followJobLogs continuously fetches and displays logs for a running job
|
||||||
func followJobLogs(client *gitea.Client, c *context.TeaContext, jobID int64, jobName string) error {
|
func followJobLogs(requestCtx stdctx.Context, client *gitea.Client, c *context.TeaContext, jobID int64, jobName string) error {
|
||||||
var lastLogLength int
|
var lastLogLength int
|
||||||
|
|
||||||
if jobName != "" {
|
if jobName != "" {
|
||||||
@@ -134,7 +140,7 @@ func followJobLogs(client *gitea.Client, c *context.TeaContext, jobID int64, job
|
|||||||
|
|
||||||
for {
|
for {
|
||||||
// Fetch job status
|
// Fetch job status
|
||||||
job, _, err := client.GetRepoActionJob(c.Owner, c.Repo, jobID)
|
job, _, err := client.Actions.GetRepoRunJob(requestCtx, c.Owner, c.Repo, jobID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get job: %w", err)
|
return fmt.Errorf("failed to get job: %w", err)
|
||||||
}
|
}
|
||||||
@@ -143,7 +149,7 @@ func followJobLogs(client *gitea.Client, c *context.TeaContext, jobID int64, job
|
|||||||
isRunning := job.Status == "in_progress" || job.Status == "queued" || job.Status == "pending"
|
isRunning := job.Status == "in_progress" || job.Status == "queued" || job.Status == "pending"
|
||||||
|
|
||||||
// Fetch logs
|
// Fetch logs
|
||||||
logs, _, err := client.GetRepoActionJobLogs(c.Owner, c.Repo, jobID)
|
logs, _, err := client.Actions.GetRepoRunJobLogs(requestCtx, c.Owner, c.Repo, jobID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get logs: %w", err)
|
return fmt.Errorf("failed to get logs: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-10
@@ -8,11 +8,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
gitea "gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/modules/context"
|
"gitea.dev/tea/cmd/flags"
|
||||||
"code.gitea.io/tea/modules/print"
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/print"
|
||||||
"code.gitea.io/sdk/gitea"
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -38,7 +37,13 @@ func runRunsView(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
return fmt.Errorf("run ID is required")
|
return fmt.Errorf("run ID is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
c := context.InitCommand(cmd)
|
c, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := c.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
client := c.Login.Client()
|
client := c.Login.Client()
|
||||||
|
|
||||||
runIDStr := cmd.Args().First()
|
runIDStr := cmd.Args().First()
|
||||||
@@ -48,7 +53,7 @@ func runRunsView(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fetch run details
|
// Fetch run details
|
||||||
run, _, err := client.GetRepoActionRun(c.Owner, c.Repo, runID)
|
run, _, err := client.Actions.GetRepoRun(ctx, c.Owner, c.Repo, runID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get run: %w", err)
|
return fmt.Errorf("failed to get run: %w", err)
|
||||||
}
|
}
|
||||||
@@ -58,8 +63,8 @@ func runRunsView(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
|
|
||||||
// Fetch and print jobs if requested
|
// Fetch and print jobs if requested
|
||||||
if cmd.Bool("jobs") {
|
if cmd.Bool("jobs") {
|
||||||
jobs, _, err := client.ListRepoActionRunJobs(c.Owner, c.Repo, runID, gitea.ListRepoActionJobsOptions{
|
jobs, _, err := client.Actions.ListRepoJobsByRun(ctx, c.Owner, c.Repo, runID, gitea.ListRepoActionJobsOptions{
|
||||||
ListOptions: flags.GetListOptions(),
|
ListOptions: flags.GetListOptions(cmd),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get jobs: %w", err)
|
return fmt.Errorf("failed to get jobs: %w", err)
|
||||||
@@ -67,7 +72,9 @@ func runRunsView(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
|
|
||||||
if jobs != nil && len(jobs.Jobs) > 0 {
|
if jobs != nil && len(jobs.Jobs) > 0 {
|
||||||
fmt.Printf("\nJobs:\n\n")
|
fmt.Printf("\nJobs:\n\n")
|
||||||
print.ActionWorkflowJobsList(jobs.Jobs, c.Output)
|
if err := print.ActionWorkflowJobsList(jobs.Jobs, c.Output); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ package actions
|
|||||||
import (
|
import (
|
||||||
stdctx "context"
|
stdctx "context"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/actions/secrets"
|
"gitea.dev/tea/cmd/actions/secrets"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -7,11 +7,11 @@ import (
|
|||||||
stdctx "context"
|
stdctx "context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
gitea "gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
"code.gitea.io/tea/modules/utils"
|
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/utils"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -40,7 +40,13 @@ func runSecretsCreate(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
return fmt.Errorf("secret name is required")
|
return fmt.Errorf("secret name is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
c := context.InitCommand(cmd)
|
c, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := c.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
client := c.Login.Client()
|
client := c.Login.Client()
|
||||||
|
|
||||||
secretName := cmd.Args().First()
|
secretName := cmd.Args().First()
|
||||||
@@ -56,8 +62,7 @@ func runSecretsCreate(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = client.CreateRepoActionSecret(c.Owner, c.Repo, gitea.CreateSecretOption{
|
_, err = client.Actions.CreateRepoSecret(ctx, c.Owner, c.Repo, secretName, gitea.CreateOrUpdateSecretOption{
|
||||||
Name: secretName,
|
|
||||||
Data: secretValue,
|
Data: secretValue,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ import (
|
|||||||
stdctx "context"
|
stdctx "context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
"gitea.dev/tea/cmd/flags"
|
||||||
"code.gitea.io/tea/modules/context"
|
"gitea.dev/tea/modules/context"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
@@ -35,7 +35,13 @@ func runSecretsDelete(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
return fmt.Errorf("secret name is required")
|
return fmt.Errorf("secret name is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
c := context.InitCommand(cmd)
|
c, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := c.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
client := c.Login.Client()
|
client := c.Login.Client()
|
||||||
|
|
||||||
secretName := cmd.Args().First()
|
secretName := cmd.Args().First()
|
||||||
@@ -50,7 +56,7 @@ func runSecretsDelete(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := client.DeleteRepoActionSecret(c.Owner, c.Repo, secretName)
|
_, err = client.Actions.DeleteRepoSecret(ctx, c.Owner, c.Repo, secretName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,11 +6,11 @@ package secrets
|
|||||||
import (
|
import (
|
||||||
stdctx "context"
|
stdctx "context"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
gitea "gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
"code.gitea.io/tea/modules/print"
|
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/print"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -29,16 +29,21 @@ var CmdSecretsList = cli.Command{
|
|||||||
|
|
||||||
// RunSecretsList list action secrets
|
// RunSecretsList list action secrets
|
||||||
func RunSecretsList(ctx stdctx.Context, cmd *cli.Command) error {
|
func RunSecretsList(ctx stdctx.Context, cmd *cli.Command) error {
|
||||||
c := context.InitCommand(cmd)
|
c, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := c.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
client := c.Login.Client()
|
client := c.Login.Client()
|
||||||
|
|
||||||
secrets, _, err := client.ListRepoActionSecret(c.Owner, c.Repo, gitea.ListRepoActionSecretOption{
|
secrets, _, err := client.Actions.ListRepoSecrets(ctx, c.Owner, c.Repo, gitea.ListRepoActionSecretOption{
|
||||||
ListOptions: flags.GetListOptions(),
|
ListOptions: flags.GetListOptions(cmd),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
print.ActionSecretsList(secrets, c.Output)
|
return print.ActionSecretsList(secrets, c.Output)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,13 @@
|
|||||||
package secrets
|
package secrets
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/urfave/cli/v3"
|
||||||
|
|
||||||
|
"gitea.dev/tea/modules/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSecretsListFlags(t *testing.T) {
|
func TestSecretsListFlags(t *testing.T) {
|
||||||
@@ -61,3 +67,32 @@ func TestSecretsListValidation(t *testing.T) {
|
|||||||
// This is fine - list commands typically ignore extra args
|
// This is fine - list commands typically ignore extra args
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRunSecretsListRequiresRepoContext(t *testing.T) {
|
||||||
|
oldWd, err := os.Getwd()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
require.NoError(t, os.Chdir(t.TempDir()))
|
||||||
|
t.Cleanup(func() {
|
||||||
|
require.NoError(t, os.Chdir(oldWd))
|
||||||
|
})
|
||||||
|
|
||||||
|
config.SetConfigForTesting(config.LocalConfig{
|
||||||
|
Logins: []config.Login{{
|
||||||
|
Name: "test",
|
||||||
|
URL: "https://gitea.example.com",
|
||||||
|
Token: "token",
|
||||||
|
User: "tester",
|
||||||
|
Default: true,
|
||||||
|
}},
|
||||||
|
})
|
||||||
|
|
||||||
|
cmd := &cli.Command{
|
||||||
|
Name: CmdSecretsList.Name,
|
||||||
|
Flags: CmdSecretsList.Flags,
|
||||||
|
}
|
||||||
|
require.NoError(t, cmd.Set("login", "test"))
|
||||||
|
|
||||||
|
err = RunSecretsList(t.Context(), cmd)
|
||||||
|
require.ErrorContains(t, err, "remote repository required")
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ package actions
|
|||||||
import (
|
import (
|
||||||
stdctx "context"
|
stdctx "context"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/actions/variables"
|
"gitea.dev/tea/cmd/actions/variables"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ import (
|
|||||||
stdctx "context"
|
stdctx "context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
"gitea.dev/tea/cmd/flags"
|
||||||
"code.gitea.io/tea/modules/context"
|
"gitea.dev/tea/modules/context"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
@@ -35,7 +35,13 @@ func runVariablesDelete(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
return fmt.Errorf("variable name is required")
|
return fmt.Errorf("variable name is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
c := context.InitCommand(cmd)
|
c, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := c.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
client := c.Login.Client()
|
client := c.Login.Client()
|
||||||
|
|
||||||
variableName := cmd.Args().First()
|
variableName := cmd.Args().First()
|
||||||
@@ -50,7 +56,7 @@ func runVariablesDelete(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := client.DeleteRepoActionVariable(c.Owner, c.Repo, variableName)
|
_, err = client.Actions.DeleteRepoVariable(ctx, c.Owner, c.Repo, variableName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,9 +7,9 @@ import (
|
|||||||
stdctx "context"
|
stdctx "context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
"gitea.dev/tea/cmd/flags"
|
||||||
"code.gitea.io/tea/modules/context"
|
"gitea.dev/tea/modules/context"
|
||||||
"code.gitea.io/tea/modules/print"
|
"gitea.dev/tea/modules/print"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
@@ -31,12 +31,18 @@ var CmdVariablesList = cli.Command{
|
|||||||
|
|
||||||
// RunVariablesList list action variables
|
// RunVariablesList list action variables
|
||||||
func RunVariablesList(ctx stdctx.Context, cmd *cli.Command) error {
|
func RunVariablesList(ctx stdctx.Context, cmd *cli.Command) error {
|
||||||
c := context.InitCommand(cmd)
|
c, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := c.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
client := c.Login.Client()
|
client := c.Login.Client()
|
||||||
|
|
||||||
if name := cmd.String("name"); name != "" {
|
if name := cmd.String("name"); name != "" {
|
||||||
// Get specific variable
|
// Get specific variable
|
||||||
variable, _, err := client.GetRepoActionVariable(c.Owner, c.Repo, name)
|
variable, _, err := client.Actions.GetRepoVariable(ctx, c.Owner, c.Repo, name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,13 @@
|
|||||||
package variables
|
package variables
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/urfave/cli/v3"
|
||||||
|
|
||||||
|
"gitea.dev/tea/modules/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestVariablesListFlags(t *testing.T) {
|
func TestVariablesListFlags(t *testing.T) {
|
||||||
@@ -61,3 +67,32 @@ func TestVariablesListValidation(t *testing.T) {
|
|||||||
// This is fine - list commands typically ignore extra args
|
// This is fine - list commands typically ignore extra args
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRunVariablesListRequiresRepoContext(t *testing.T) {
|
||||||
|
oldWd, err := os.Getwd()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
require.NoError(t, os.Chdir(t.TempDir()))
|
||||||
|
t.Cleanup(func() {
|
||||||
|
require.NoError(t, os.Chdir(oldWd))
|
||||||
|
})
|
||||||
|
|
||||||
|
config.SetConfigForTesting(config.LocalConfig{
|
||||||
|
Logins: []config.Login{{
|
||||||
|
Name: "test",
|
||||||
|
URL: "https://gitea.example.com",
|
||||||
|
Token: "token",
|
||||||
|
User: "tester",
|
||||||
|
Default: true,
|
||||||
|
}},
|
||||||
|
})
|
||||||
|
|
||||||
|
cmd := &cli.Command{
|
||||||
|
Name: CmdVariablesList.Name,
|
||||||
|
Flags: CmdVariablesList.Flags,
|
||||||
|
}
|
||||||
|
require.NoError(t, cmd.Set("login", "test"))
|
||||||
|
|
||||||
|
err = RunVariablesList(t.Context(), cmd)
|
||||||
|
require.ErrorContains(t, err, "remote repository required")
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,9 +8,9 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
"gitea.dev/tea/cmd/flags"
|
||||||
"code.gitea.io/tea/modules/context"
|
"gitea.dev/tea/modules/context"
|
||||||
"code.gitea.io/tea/modules/utils"
|
"gitea.dev/tea/modules/utils"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
@@ -40,7 +40,13 @@ func runVariablesSet(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
return fmt.Errorf("variable name is required")
|
return fmt.Errorf("variable name is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
c := context.InitCommand(cmd)
|
c, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := c.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
client := c.Login.Client()
|
client := c.Login.Client()
|
||||||
|
|
||||||
variableName := cmd.Args().First()
|
variableName := cmd.Args().First()
|
||||||
@@ -63,7 +69,7 @@ func runVariablesSet(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = client.CreateRepoActionVariable(c.Owner, c.Repo, variableName, variableValue)
|
_, err = client.Actions.CreateRepoVariable(ctx, c.Owner, c.Repo, variableName, variableValue)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ package actions
|
|||||||
import (
|
import (
|
||||||
stdctx "context"
|
stdctx "context"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/actions/workflows"
|
"gitea.dev/tea/cmd/actions/workflows"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
@@ -20,6 +20,10 @@ var CmdActionsWorkflows = cli.Command{
|
|||||||
Action: runWorkflowsDefault,
|
Action: runWorkflowsDefault,
|
||||||
Commands: []*cli.Command{
|
Commands: []*cli.Command{
|
||||||
&workflows.CmdWorkflowsList,
|
&workflows.CmdWorkflowsList,
|
||||||
|
&workflows.CmdWorkflowsView,
|
||||||
|
&workflows.CmdWorkflowsDispatch,
|
||||||
|
&workflows.CmdWorkflowsEnable,
|
||||||
|
&workflows.CmdWorkflowsDisable,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,65 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package workflows
|
||||||
|
|
||||||
|
import (
|
||||||
|
stdctx "context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
|
||||||
|
"github.com/urfave/cli/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CmdWorkflowsDisable represents a sub command to disable a workflow
|
||||||
|
var CmdWorkflowsDisable = cli.Command{
|
||||||
|
Name: "disable",
|
||||||
|
Usage: "Disable a workflow",
|
||||||
|
Description: "Disable a workflow in the repository",
|
||||||
|
ArgsUsage: "<workflow-id>",
|
||||||
|
Action: runWorkflowsDisable,
|
||||||
|
Flags: append([]cli.Flag{
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "confirm",
|
||||||
|
Aliases: []string{"y"},
|
||||||
|
Usage: "confirm disable without prompting",
|
||||||
|
},
|
||||||
|
}, flags.AllDefaultFlags...),
|
||||||
|
}
|
||||||
|
|
||||||
|
func runWorkflowsDisable(ctx stdctx.Context, cmd *cli.Command) error {
|
||||||
|
if cmd.Args().Len() == 0 {
|
||||||
|
return fmt.Errorf("workflow ID is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
c, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := c.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
client := c.Login.Client()
|
||||||
|
|
||||||
|
workflowID := cmd.Args().First()
|
||||||
|
|
||||||
|
if !cmd.Bool("confirm") {
|
||||||
|
fmt.Printf("Are you sure you want to disable workflow %s? [y/N] ", workflowID)
|
||||||
|
var response string
|
||||||
|
fmt.Scanln(&response)
|
||||||
|
if response != "y" && response != "Y" && response != "yes" {
|
||||||
|
fmt.Println("Disable canceled.")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = client.Actions.DisableRepoWorkflow(ctx, c.Owner, c.Repo, workflowID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to disable workflow: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Workflow %s disabled successfully\n", workflowID)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,174 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package workflows
|
||||||
|
|
||||||
|
import (
|
||||||
|
stdctx "context"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
gitea "gitea.dev/sdk"
|
||||||
|
|
||||||
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/print"
|
||||||
|
"github.com/urfave/cli/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CmdWorkflowsDispatch represents a sub command to dispatch a workflow
|
||||||
|
var CmdWorkflowsDispatch = cli.Command{
|
||||||
|
Name: "dispatch",
|
||||||
|
Aliases: []string{"trigger", "run"},
|
||||||
|
Usage: "Dispatch a workflow run",
|
||||||
|
Description: "Trigger a workflow_dispatch event for a workflow",
|
||||||
|
ArgsUsage: "<workflow-id>",
|
||||||
|
Action: runWorkflowsDispatch,
|
||||||
|
Flags: append([]cli.Flag{
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "ref",
|
||||||
|
Aliases: []string{"r"},
|
||||||
|
Usage: "branch or tag to dispatch on (default: current branch)",
|
||||||
|
},
|
||||||
|
&cli.StringSliceFlag{
|
||||||
|
Name: "input",
|
||||||
|
Aliases: []string{"i"},
|
||||||
|
Usage: "workflow input in key=value format (can be specified multiple times)",
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "follow",
|
||||||
|
Aliases: []string{"f"},
|
||||||
|
Usage: "follow log output after dispatching",
|
||||||
|
},
|
||||||
|
}, flags.AllDefaultFlags...),
|
||||||
|
}
|
||||||
|
|
||||||
|
func runWorkflowsDispatch(ctx stdctx.Context, cmd *cli.Command) error {
|
||||||
|
if cmd.Args().Len() == 0 {
|
||||||
|
return fmt.Errorf("workflow ID is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
c, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := c.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
client := c.Login.Client()
|
||||||
|
|
||||||
|
workflowID := cmd.Args().First()
|
||||||
|
|
||||||
|
ref := cmd.String("ref")
|
||||||
|
if ref == "" {
|
||||||
|
if c.LocalRepo != nil {
|
||||||
|
branchName, _, localErr := c.LocalRepo.TeaGetCurrentBranchNameAndSHA()
|
||||||
|
if localErr == nil && branchName != "" {
|
||||||
|
ref = branchName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ref == "" {
|
||||||
|
return fmt.Errorf("--ref is required (no local branch detected)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inputs := make(map[string]string)
|
||||||
|
for _, input := range cmd.StringSlice("input") {
|
||||||
|
key, value, ok := strings.Cut(input, "=")
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("invalid input format %q, expected key=value", input)
|
||||||
|
}
|
||||||
|
inputs[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := gitea.CreateActionWorkflowDispatchOption{
|
||||||
|
Ref: ref,
|
||||||
|
Inputs: inputs,
|
||||||
|
}
|
||||||
|
|
||||||
|
details, _, err := client.Actions.DispatchRepoWorkflow(ctx, c.Owner, c.Repo, workflowID, opt, true)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to dispatch workflow: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
print.ActionWorkflowDispatchResult(details)
|
||||||
|
|
||||||
|
if cmd.Bool("follow") && details != nil && details.WorkflowRunID > 0 {
|
||||||
|
return followDispatchedRun(ctx, client, c, details.WorkflowRunID)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
followPollInterval = 2 * time.Second
|
||||||
|
followMaxDuration = 30 * time.Minute
|
||||||
|
)
|
||||||
|
|
||||||
|
// followDispatchedRun waits for the dispatched run to start, then follows its logs
|
||||||
|
func followDispatchedRun(ctx stdctx.Context, client *gitea.Client, c *context.TeaContext, runID int64) error {
|
||||||
|
fmt.Printf("\nWaiting for run %d to start...\n", runID)
|
||||||
|
|
||||||
|
var jobs *gitea.ActionWorkflowJobsResponse
|
||||||
|
for range 30 {
|
||||||
|
time.Sleep(followPollInterval)
|
||||||
|
|
||||||
|
var err error
|
||||||
|
jobs, _, err = client.Actions.ListRepoJobsByRun(ctx, c.Owner, c.Repo, runID, gitea.ListRepoActionJobsOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get jobs: %w", err)
|
||||||
|
}
|
||||||
|
if len(jobs.Jobs) > 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if jobs == nil || len(jobs.Jobs) == 0 {
|
||||||
|
return fmt.Errorf("timed out waiting for jobs to appear")
|
||||||
|
}
|
||||||
|
|
||||||
|
jobID := jobs.Jobs[0].ID
|
||||||
|
jobName := jobs.Jobs[0].Name
|
||||||
|
fmt.Printf("Following logs for job '%s' (ID: %d) - press Ctrl+C to stop...\n", jobName, jobID)
|
||||||
|
fmt.Println("---")
|
||||||
|
|
||||||
|
deadline := time.Now().Add(followMaxDuration)
|
||||||
|
var lastLogLength int
|
||||||
|
for time.Now().Before(deadline) {
|
||||||
|
job, _, err := client.Actions.GetRepoRunJob(ctx, c.Owner, c.Repo, jobID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get job: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
isRunning := job.Status == "in_progress" || job.Status == "queued" || job.Status == "pending"
|
||||||
|
|
||||||
|
logs, _, logErr := client.Actions.GetRepoRunJobLogs(ctx, c.Owner, c.Repo, jobID)
|
||||||
|
if logErr != nil && isRunning {
|
||||||
|
time.Sleep(followPollInterval)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if logErr == nil && len(logs) > lastLogLength {
|
||||||
|
fmt.Print(string(logs[lastLogLength:]))
|
||||||
|
lastLogLength = len(logs)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isRunning {
|
||||||
|
if logErr != nil {
|
||||||
|
fmt.Printf("\n---\nJob completed with status: %s (failed to fetch final logs: %v)\n", job.Status, logErr)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("\n---\nJob completed with status: %s\n", job.Status)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(followPollInterval)
|
||||||
|
}
|
||||||
|
|
||||||
|
if time.Now().After(deadline) {
|
||||||
|
return fmt.Errorf("timed out after %s following logs", followMaxDuration)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package workflows
|
||||||
|
|
||||||
|
import (
|
||||||
|
stdctx "context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
|
||||||
|
"github.com/urfave/cli/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CmdWorkflowsEnable represents a sub command to enable a workflow
|
||||||
|
var CmdWorkflowsEnable = cli.Command{
|
||||||
|
Name: "enable",
|
||||||
|
Usage: "Enable a workflow",
|
||||||
|
Description: "Enable a disabled workflow in the repository",
|
||||||
|
ArgsUsage: "<workflow-id>",
|
||||||
|
Action: runWorkflowsEnable,
|
||||||
|
Flags: flags.AllDefaultFlags,
|
||||||
|
}
|
||||||
|
|
||||||
|
func runWorkflowsEnable(ctx stdctx.Context, cmd *cli.Command) error {
|
||||||
|
if cmd.Args().Len() == 0 {
|
||||||
|
return fmt.Errorf("workflow ID is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
c, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := c.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
client := c.Login.Client()
|
||||||
|
|
||||||
|
workflowID := cmd.Args().First()
|
||||||
|
_, err = client.Actions.EnableRepoWorkflow(ctx, c.Owner, c.Repo, workflowID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to enable workflow: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Workflow %s enabled successfully\n", workflowID)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -6,14 +6,12 @@ package workflows
|
|||||||
import (
|
import (
|
||||||
stdctx "context"
|
stdctx "context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
gitea "gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
"code.gitea.io/tea/modules/print"
|
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/print"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -22,65 +20,31 @@ var CmdWorkflowsList = cli.Command{
|
|||||||
Name: "list",
|
Name: "list",
|
||||||
Aliases: []string{"ls"},
|
Aliases: []string{"ls"},
|
||||||
Usage: "List repository workflows",
|
Usage: "List repository workflows",
|
||||||
Description: "List workflow files in the repository with active/inactive status",
|
Description: "List workflows in the repository with their status",
|
||||||
Action: RunWorkflowsList,
|
Action: RunWorkflowsList,
|
||||||
Flags: append([]cli.Flag{
|
Flags: flags.AllDefaultFlags,
|
||||||
&flags.PaginationPageFlag,
|
|
||||||
&flags.PaginationLimitFlag,
|
|
||||||
}, flags.AllDefaultFlags...),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunWorkflowsList lists workflow files in the repository
|
// RunWorkflowsList lists workflows in the repository using the workflow API
|
||||||
func RunWorkflowsList(ctx stdctx.Context, cmd *cli.Command) error {
|
func RunWorkflowsList(ctx stdctx.Context, cmd *cli.Command) error {
|
||||||
c := context.InitCommand(cmd)
|
c, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := c.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
client := c.Login.Client()
|
client := c.Login.Client()
|
||||||
|
|
||||||
// Try to list workflow files from .gitea/workflows directory
|
resp, _, err := client.Actions.ListRepoWorkflows(ctx, c.Owner, c.Repo)
|
||||||
var workflows []*gitea.ContentsResponse
|
|
||||||
|
|
||||||
// Try .gitea/workflows first, then .github/workflows
|
|
||||||
workflowDir := ".gitea/workflows"
|
|
||||||
contents, _, err := client.ListContents(c.Owner, c.Repo, "", workflowDir)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
workflowDir = ".github/workflows"
|
return fmt.Errorf("failed to list workflows: %w", err)
|
||||||
contents, _, err = client.ListContents(c.Owner, c.Repo, "", workflowDir)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("No workflow files found\n")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter for workflow files (.yml and .yaml)
|
var workflows []*gitea.ActionWorkflow
|
||||||
for _, content := range contents {
|
if resp != nil {
|
||||||
if content.Type == "file" {
|
workflows = resp.Workflows
|
||||||
ext := strings.ToLower(filepath.Ext(content.Name))
|
|
||||||
if ext == ".yml" || ext == ".yaml" {
|
|
||||||
content.Path = workflowDir + "/" + content.Name
|
|
||||||
workflows = append(workflows, content)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(workflows) == 0 {
|
return print.ActionWorkflowsList(workflows, c.Output)
|
||||||
fmt.Printf("No workflow files found\n")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check which workflows have runs to determine active status
|
|
||||||
workflowStatus := make(map[string]bool)
|
|
||||||
|
|
||||||
// Get recent runs to check activity
|
|
||||||
runs, _, err := client.ListRepoActionRuns(c.Owner, c.Repo, gitea.ListRepoActionRunsOptions{
|
|
||||||
ListOptions: flags.GetListOptions(),
|
|
||||||
})
|
|
||||||
if err == nil && runs != nil {
|
|
||||||
for _, run := range runs.WorkflowRuns {
|
|
||||||
// Extract workflow file name from path
|
|
||||||
workflowFile := filepath.Base(run.Path)
|
|
||||||
workflowStatus[workflowFile] = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
print.WorkflowsList(workflows, workflowStatus, c.Output)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package workflows
|
||||||
|
|
||||||
|
import (
|
||||||
|
stdctx "context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/print"
|
||||||
|
|
||||||
|
"github.com/urfave/cli/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CmdWorkflowsView represents a sub command to view workflow details
|
||||||
|
var CmdWorkflowsView = cli.Command{
|
||||||
|
Name: "view",
|
||||||
|
Aliases: []string{"show", "get"},
|
||||||
|
Usage: "View workflow details",
|
||||||
|
Description: "View details of a specific workflow",
|
||||||
|
ArgsUsage: "<workflow-id>",
|
||||||
|
Action: runWorkflowsView,
|
||||||
|
Flags: flags.AllDefaultFlags,
|
||||||
|
}
|
||||||
|
|
||||||
|
func runWorkflowsView(ctx stdctx.Context, cmd *cli.Command) error {
|
||||||
|
if cmd.Args().Len() == 0 {
|
||||||
|
return fmt.Errorf("workflow ID is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
c, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := c.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
client := c.Login.Client()
|
||||||
|
|
||||||
|
workflowID := cmd.Args().First()
|
||||||
|
wf, _, err := client.Actions.GetRepoWorkflow(ctx, c.Owner, c.Repo, workflowID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get workflow: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
print.ActionWorkflowDetails(wf)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
+14
-7
@@ -6,10 +6,11 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
stdctx "context"
|
stdctx "context"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/admin/users"
|
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
"code.gitea.io/tea/modules/print"
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
|
|
||||||
|
"gitea.dev/tea/cmd/admin/users"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/print"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CmdAdmin represents the namespace of admin commands.
|
// CmdAdmin represents the namespace of admin commands.
|
||||||
@@ -19,7 +20,7 @@ var CmdAdmin = cli.Command{
|
|||||||
Usage: "Operations requiring admin access on the Gitea instance",
|
Usage: "Operations requiring admin access on the Gitea instance",
|
||||||
Aliases: []string{"a"},
|
Aliases: []string{"a"},
|
||||||
Category: catMisc,
|
Category: catMisc,
|
||||||
Action: func(_ stdctx.Context, cmd *cli.Command) error {
|
Action: func(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
return cli.ShowSubcommandHelp(cmd)
|
return cli.ShowSubcommandHelp(cmd)
|
||||||
},
|
},
|
||||||
Commands: []*cli.Command{
|
Commands: []*cli.Command{
|
||||||
@@ -39,14 +40,20 @@ var cmdAdminUsers = cli.Command{
|
|||||||
},
|
},
|
||||||
Commands: []*cli.Command{
|
Commands: []*cli.Command{
|
||||||
&users.CmdUserList,
|
&users.CmdUserList,
|
||||||
|
&users.CmdUserCreate,
|
||||||
|
&users.CmdUserEdit,
|
||||||
|
&users.CmdUserDelete,
|
||||||
},
|
},
|
||||||
Flags: users.CmdUserList.Flags,
|
Flags: users.CmdUserList.Flags,
|
||||||
}
|
}
|
||||||
|
|
||||||
func runAdminUserDetail(_ stdctx.Context, cmd *cli.Command, u string) error {
|
func runAdminUserDetail(requestCtx stdctx.Context, cmd *cli.Command, u string) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
client := ctx.Login.Client()
|
client := ctx.Login.Client()
|
||||||
user, _, err := client.GetUserInfo(u)
|
user, _, err := client.Users.GetUserInfo(requestCtx, u)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,220 @@
|
|||||||
|
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package users
|
||||||
|
|
||||||
|
import (
|
||||||
|
stdctx "context"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
gitea "gitea.dev/sdk"
|
||||||
|
|
||||||
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/print"
|
||||||
|
"github.com/urfave/cli/v3"
|
||||||
|
"golang.org/x/term"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CmdUserCreate represents a sub command of users to create a user
|
||||||
|
var CmdUserCreate = cli.Command{
|
||||||
|
Name: "create",
|
||||||
|
Aliases: []string{"add", "new"},
|
||||||
|
Usage: "Create a new user",
|
||||||
|
Description: "Create a new user account",
|
||||||
|
ArgsUsage: " ", // command does not accept arguments
|
||||||
|
Action: RunUserCreate,
|
||||||
|
Flags: append([]cli.Flag{
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "username",
|
||||||
|
Aliases: []string{"u"},
|
||||||
|
Usage: "Username for the new user (required)",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "password",
|
||||||
|
Aliases: []string{"p"},
|
||||||
|
Usage: "Password for the new user (will prompt if not provided)",
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "password-file",
|
||||||
|
Usage: "Read password from file",
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "password-stdin",
|
||||||
|
Usage: "Read password from stdin",
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "email",
|
||||||
|
Aliases: []string{"e"},
|
||||||
|
Usage: "Email address for the new user (required)",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "full-name",
|
||||||
|
Usage: "Full name for the new user",
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "admin",
|
||||||
|
Usage: "Make the user an administrator",
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "restricted",
|
||||||
|
Usage: "Make the user restricted",
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "prohibit-login",
|
||||||
|
Usage: "Prohibit the user from logging in",
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "no-must-change-password",
|
||||||
|
Usage: "Don't require the user to change password on first login (default: password change required)",
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "visibility",
|
||||||
|
Usage: "Visibility of the user profile (public, limited, private)",
|
||||||
|
Value: "public",
|
||||||
|
},
|
||||||
|
}, flags.AllDefaultFlags...),
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunUserCreate creates a new user
|
||||||
|
func RunUserCreate(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
|
ctx, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
username := ctx.String("username")
|
||||||
|
password := ctx.String("password")
|
||||||
|
email := ctx.String("email")
|
||||||
|
fullName := ctx.String("full-name")
|
||||||
|
isAdmin := ctx.Bool("admin")
|
||||||
|
restricted := ctx.Bool("restricted")
|
||||||
|
prohibitLogin := ctx.Bool("prohibit-login")
|
||||||
|
noMustChangePassword := ctx.Bool("no-must-change-password")
|
||||||
|
visibility := ctx.String("visibility")
|
||||||
|
|
||||||
|
// Get password from various sources in priority order
|
||||||
|
if password == "" {
|
||||||
|
if ctx.String("password-file") != "" {
|
||||||
|
// Read from file
|
||||||
|
content, err := os.ReadFile(ctx.String("password-file"))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to read password file: %w", err)
|
||||||
|
}
|
||||||
|
password = strings.TrimSpace(string(content))
|
||||||
|
} else if ctx.Bool("password-stdin") {
|
||||||
|
// Read from stdin
|
||||||
|
content, err := io.ReadAll(os.Stdin)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to read password from stdin: %w", err)
|
||||||
|
}
|
||||||
|
password = strings.TrimSpace(string(content))
|
||||||
|
} else {
|
||||||
|
// Interactive prompt (hidden input)
|
||||||
|
fmt.Printf("Enter password for '%s': ", username)
|
||||||
|
bytePassword, err := term.ReadPassword(int(syscall.Stdin))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to read password: %w", err)
|
||||||
|
}
|
||||||
|
fmt.Println() // Add newline after hidden input
|
||||||
|
password = string(bytePassword)
|
||||||
|
|
||||||
|
if password == "" {
|
||||||
|
return fmt.Errorf("password cannot be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Confirm password (only for interactive mode)
|
||||||
|
fmt.Printf("Confirm password for '%s': ", username)
|
||||||
|
bytePasswordConfirm, err := term.ReadPassword(int(syscall.Stdin))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to read password confirmation: %w", err)
|
||||||
|
}
|
||||||
|
fmt.Println() // Add newline after hidden input
|
||||||
|
passwordConfirm := string(bytePasswordConfirm)
|
||||||
|
|
||||||
|
if password != passwordConfirm {
|
||||||
|
return fmt.Errorf("passwords do not match")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if password == "" {
|
||||||
|
return fmt.Errorf("password cannot be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
if email == "" {
|
||||||
|
return fmt.Errorf("email is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
client := ctx.Login.Client()
|
||||||
|
|
||||||
|
// Build create options
|
||||||
|
createOpts := gitea.CreateUserOption{
|
||||||
|
LoginName: username,
|
||||||
|
Username: username,
|
||||||
|
Password: password,
|
||||||
|
Email: email,
|
||||||
|
FullName: fullName,
|
||||||
|
SendNotify: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set must change password flag (pointer to bool required)
|
||||||
|
// By default, require user to change password on first login
|
||||||
|
// Only set to false if --no-must-change-password flag is explicitly set
|
||||||
|
mustChangePassword := !noMustChangePassword
|
||||||
|
createOpts.MustChangePassword = &mustChangePassword
|
||||||
|
|
||||||
|
vis, err := parseUserVisibility(visibility)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
createOpts.Visibility = vis
|
||||||
|
|
||||||
|
// Create the user
|
||||||
|
user, _, err := client.Admin.CreateUser(requestCtx, createOpts)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Admin, Restricted, and ProhibitLogin cannot be set during user creation
|
||||||
|
// We need to update them via AdminEditUser after creation if any of these flags are set
|
||||||
|
if isAdmin || restricted || prohibitLogin {
|
||||||
|
editOpts := gitea.EditUserOption{
|
||||||
|
LoginName: username, // Required field
|
||||||
|
}
|
||||||
|
|
||||||
|
if isAdmin {
|
||||||
|
editOpts.Admin = &isAdmin
|
||||||
|
}
|
||||||
|
|
||||||
|
if restricted {
|
||||||
|
editOpts.Restricted = &restricted
|
||||||
|
}
|
||||||
|
|
||||||
|
if prohibitLogin {
|
||||||
|
editOpts.ProhibitLogin = &prohibitLogin
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update user with admin/restricted/prohibit-login settings
|
||||||
|
_, err = client.Admin.EditUser(requestCtx, username, editOpts)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("user created but failed to update admin/restricted/prohibit-login status: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Refresh user info to reflect the changes
|
||||||
|
user, _, err = client.Users.GetUserInfo(requestCtx, username)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("user updated but failed to retrieve updated user info: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
print.UserDetails(user)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package users
|
||||||
|
|
||||||
|
import (
|
||||||
|
stdctx "context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
|
||||||
|
"github.com/urfave/cli/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CmdUserDelete represents a sub command of users to delete a user
|
||||||
|
var CmdUserDelete = cli.Command{
|
||||||
|
Name: "delete",
|
||||||
|
Aliases: []string{"rm", "remove"},
|
||||||
|
Usage: "Delete a user",
|
||||||
|
Description: "Delete a user account",
|
||||||
|
ArgsUsage: "<username>",
|
||||||
|
Action: RunUserDelete,
|
||||||
|
Flags: append([]cli.Flag{
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "confirm",
|
||||||
|
Aliases: []string{"y"},
|
||||||
|
Usage: "confirm deletion without prompting",
|
||||||
|
},
|
||||||
|
}, flags.AllDefaultFlags...),
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunUserDelete deletes a user
|
||||||
|
func RunUserDelete(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
|
ctx, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if ctx.Args().Len() == 0 {
|
||||||
|
return fmt.Errorf("username is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
client := ctx.Login.Client()
|
||||||
|
username := ctx.Args().First()
|
||||||
|
|
||||||
|
// Get user details first to show what we're deleting
|
||||||
|
user, _, err := client.Users.GetUserInfo(requestCtx, username)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get user info: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !ctx.Bool("confirm") {
|
||||||
|
userInfo := fmt.Sprintf("%s (ID: %d)", user.UserName, user.ID)
|
||||||
|
if user.Email != "" {
|
||||||
|
userInfo += fmt.Sprintf(" - %s", user.Email)
|
||||||
|
}
|
||||||
|
if user.IsAdmin {
|
||||||
|
userInfo += " [admin]"
|
||||||
|
}
|
||||||
|
fmt.Printf("Are you sure you want to delete user %s? [y/N] ", userInfo)
|
||||||
|
var response string
|
||||||
|
fmt.Scanln(&response)
|
||||||
|
if !isConfirmationAccepted(response) {
|
||||||
|
fmt.Println("Deletion canceled.")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = client.Admin.DeleteUser(requestCtx, username)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to delete user: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("User '%s' deleted successfully\n", username)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,373 @@
|
|||||||
|
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package users
|
||||||
|
|
||||||
|
import (
|
||||||
|
stdctx "context"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
gitea "gitea.dev/sdk"
|
||||||
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/print"
|
||||||
|
"github.com/urfave/cli/v3"
|
||||||
|
"golang.org/x/term"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CmdUserEdit represents a sub command of users to edit a user
|
||||||
|
var CmdUserEdit = cli.Command{
|
||||||
|
Name: "edit",
|
||||||
|
Aliases: []string{"update", "e", "u"},
|
||||||
|
Usage: "Edit a user",
|
||||||
|
Description: "Edit user account properties",
|
||||||
|
ArgsUsage: "<username>",
|
||||||
|
Action: RunUserEdit,
|
||||||
|
Flags: append([]cli.Flag{
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "password",
|
||||||
|
Usage: "New password (use empty value --password=\"\" to trigger interactive prompt)",
|
||||||
|
Value: "",
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "password-file",
|
||||||
|
Usage: "Read password from file",
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "password-stdin",
|
||||||
|
Usage: "Read password from stdin",
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "email",
|
||||||
|
Aliases: []string{"e"},
|
||||||
|
Usage: "Email address",
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "full-name",
|
||||||
|
Usage: "Full name",
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "description",
|
||||||
|
Usage: "User description",
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "website",
|
||||||
|
Usage: "Website URL",
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "location",
|
||||||
|
Usage: "Location",
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "admin",
|
||||||
|
Usage: "Make the user an administrator",
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "no-admin",
|
||||||
|
Usage: "Remove administrator status",
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "restricted",
|
||||||
|
Usage: "Make the user restricted",
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "no-restricted",
|
||||||
|
Usage: "Remove restricted status",
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "prohibit-login",
|
||||||
|
Usage: "Prohibit the user from logging in",
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "allow-login",
|
||||||
|
Usage: "Allow the user to log in",
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "active",
|
||||||
|
Usage: "Activate the user",
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "inactive",
|
||||||
|
Usage: "Deactivate the user",
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "no-must-change-password",
|
||||||
|
Usage: "Don't require the user to change password on next login (default: password change required)",
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "visibility",
|
||||||
|
Usage: "Visibility of the user profile (public, limited, private)",
|
||||||
|
},
|
||||||
|
&cli.IntFlag{
|
||||||
|
Name: "max-repo-creation",
|
||||||
|
Usage: "Maximum number of repositories the user can create (-1 for unlimited)",
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "allow-git-hook",
|
||||||
|
Usage: "Allow the user to use git hooks",
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "no-allow-git-hook",
|
||||||
|
Usage: "Disallow the user from using git hooks",
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "allow-import-local",
|
||||||
|
Usage: "Allow the user to import local repositories",
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "no-allow-import-local",
|
||||||
|
Usage: "Disallow the user from importing local repositories",
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "allow-create-organization",
|
||||||
|
Usage: "Allow the user to create organizations",
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "no-allow-create-organization",
|
||||||
|
Usage: "Disallow the user from creating organizations",
|
||||||
|
},
|
||||||
|
}, flags.AllDefaultFlags...),
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunUserEdit edits an existing user
|
||||||
|
func RunUserEdit(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
|
ctx, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if ctx.Args().Len() == 0 {
|
||||||
|
return fmt.Errorf("username is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
client := ctx.Login.Client()
|
||||||
|
username := ctx.Args().First()
|
||||||
|
|
||||||
|
// Verify the user exists before attempting an update.
|
||||||
|
_, _, err = client.Users.GetUserInfo(requestCtx, username)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get user info: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build edit options, starting with required LoginName
|
||||||
|
editOpts := gitea.EditUserOption{
|
||||||
|
LoginName: username,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update email if set
|
||||||
|
if ctx.IsSet("email") {
|
||||||
|
email := ctx.String("email")
|
||||||
|
editOpts.Email = &email
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update full name if set
|
||||||
|
if ctx.IsSet("full-name") {
|
||||||
|
fullName := ctx.String("full-name")
|
||||||
|
editOpts.FullName = &fullName
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update description if set
|
||||||
|
if ctx.IsSet("description") {
|
||||||
|
description := ctx.String("description")
|
||||||
|
editOpts.Description = &description
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update website if set
|
||||||
|
if ctx.IsSet("website") {
|
||||||
|
website := ctx.String("website")
|
||||||
|
editOpts.Website = &website
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update location if set
|
||||||
|
if ctx.IsSet("location") {
|
||||||
|
location := ctx.String("location")
|
||||||
|
editOpts.Location = &location
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle admin status
|
||||||
|
if ctx.IsSet("admin") {
|
||||||
|
admin := ctx.Bool("admin")
|
||||||
|
editOpts.Admin = &admin
|
||||||
|
} else if ctx.IsSet("no-admin") {
|
||||||
|
admin := false
|
||||||
|
editOpts.Admin = &admin
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle restricted status
|
||||||
|
if ctx.IsSet("restricted") {
|
||||||
|
restricted := ctx.Bool("restricted")
|
||||||
|
editOpts.Restricted = &restricted
|
||||||
|
} else if ctx.IsSet("no-restricted") {
|
||||||
|
restricted := false
|
||||||
|
editOpts.Restricted = &restricted
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle prohibit login status
|
||||||
|
if ctx.IsSet("prohibit-login") {
|
||||||
|
prohibitLogin := ctx.Bool("prohibit-login")
|
||||||
|
editOpts.ProhibitLogin = &prohibitLogin
|
||||||
|
} else if ctx.IsSet("allow-login") {
|
||||||
|
prohibitLogin := false
|
||||||
|
editOpts.ProhibitLogin = &prohibitLogin
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle active status
|
||||||
|
if ctx.IsSet("active") {
|
||||||
|
active := ctx.Bool("active")
|
||||||
|
editOpts.Active = &active
|
||||||
|
} else if ctx.IsSet("inactive") {
|
||||||
|
active := false
|
||||||
|
editOpts.Active = &active
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle must change password - will be set when password is changed unless flag is set
|
||||||
|
|
||||||
|
// Handle visibility
|
||||||
|
if ctx.IsSet("visibility") {
|
||||||
|
vis, err := parseUserVisibility(ctx.String("visibility"))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
editOpts.Visibility = vis
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle max repo creation
|
||||||
|
if ctx.IsSet("max-repo-creation") {
|
||||||
|
maxRepoCreation := ctx.Int("max-repo-creation")
|
||||||
|
editOpts.MaxRepoCreation = &maxRepoCreation
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle allow git hook
|
||||||
|
if ctx.IsSet("allow-git-hook") {
|
||||||
|
allowGitHook := ctx.Bool("allow-git-hook")
|
||||||
|
editOpts.AllowGitHook = &allowGitHook
|
||||||
|
} else if ctx.IsSet("no-allow-git-hook") {
|
||||||
|
allowGitHook := false
|
||||||
|
editOpts.AllowGitHook = &allowGitHook
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle allow import local
|
||||||
|
if ctx.IsSet("allow-import-local") {
|
||||||
|
allowImportLocal := ctx.Bool("allow-import-local")
|
||||||
|
editOpts.AllowImportLocal = &allowImportLocal
|
||||||
|
} else if ctx.IsSet("no-allow-import-local") {
|
||||||
|
allowImportLocal := false
|
||||||
|
editOpts.AllowImportLocal = &allowImportLocal
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle allow create organization
|
||||||
|
if ctx.IsSet("allow-create-organization") {
|
||||||
|
allowCreateOrg := ctx.Bool("allow-create-organization")
|
||||||
|
editOpts.AllowCreateOrganization = &allowCreateOrg
|
||||||
|
} else if ctx.IsSet("no-allow-create-organization") {
|
||||||
|
allowCreateOrg := false
|
||||||
|
editOpts.AllowCreateOrganization = &allowCreateOrg
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle password if any password flag is set or if password flag was provided (even without value)
|
||||||
|
shouldChangePassword := ctx.IsSet("password") || ctx.IsSet("password-file") || ctx.Bool("password-stdin")
|
||||||
|
if shouldChangePassword {
|
||||||
|
password := ctx.String("password")
|
||||||
|
|
||||||
|
// Get password from various sources in priority order
|
||||||
|
if password == "" {
|
||||||
|
if ctx.IsSet("password-file") && ctx.String("password-file") != "" {
|
||||||
|
// Read from file
|
||||||
|
content, err := os.ReadFile(ctx.String("password-file"))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to read password file: %w", err)
|
||||||
|
}
|
||||||
|
password = strings.TrimSpace(string(content))
|
||||||
|
} else if ctx.Bool("password-stdin") {
|
||||||
|
// Read from stdin
|
||||||
|
content, err := io.ReadAll(os.Stdin)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to read password from stdin: %w", err)
|
||||||
|
}
|
||||||
|
password = strings.TrimSpace(string(content))
|
||||||
|
} else {
|
||||||
|
// Interactive prompt (hidden input) - triggered when --password is used without value
|
||||||
|
fmt.Printf("Enter new password for '%s': ", username)
|
||||||
|
bytePassword, err := term.ReadPassword(int(syscall.Stdin))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to read password: %w", err)
|
||||||
|
}
|
||||||
|
fmt.Println() // Add newline after hidden input
|
||||||
|
password = string(bytePassword)
|
||||||
|
|
||||||
|
if password == "" {
|
||||||
|
return fmt.Errorf("password cannot be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Confirm password (only for interactive mode)
|
||||||
|
fmt.Printf("Confirm new password for '%s': ", username)
|
||||||
|
bytePasswordConfirm, err := term.ReadPassword(int(syscall.Stdin))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to read password confirmation: %w", err)
|
||||||
|
}
|
||||||
|
fmt.Println() // Add newline after hidden input
|
||||||
|
passwordConfirm := string(bytePasswordConfirm)
|
||||||
|
|
||||||
|
if password != passwordConfirm {
|
||||||
|
return fmt.Errorf("passwords do not match")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if password == "" {
|
||||||
|
return fmt.Errorf("password cannot be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
editOpts.Password = password
|
||||||
|
|
||||||
|
// When password is changed, require user to change password on next login by default
|
||||||
|
// Only set to false if --no-must-change-password flag is explicitly set
|
||||||
|
if !ctx.IsSet("no-must-change-password") {
|
||||||
|
mustChangePassword := true
|
||||||
|
editOpts.MustChangePassword = &mustChangePassword
|
||||||
|
} else {
|
||||||
|
mustChangePassword := false
|
||||||
|
editOpts.MustChangePassword = &mustChangePassword
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only proceed with update if at least one field is being modified
|
||||||
|
hasChanges := editOpts.Email != nil ||
|
||||||
|
editOpts.FullName != nil ||
|
||||||
|
editOpts.Description != nil ||
|
||||||
|
editOpts.Website != nil ||
|
||||||
|
editOpts.Location != nil ||
|
||||||
|
editOpts.Admin != nil ||
|
||||||
|
editOpts.Restricted != nil ||
|
||||||
|
editOpts.ProhibitLogin != nil ||
|
||||||
|
editOpts.Active != nil ||
|
||||||
|
editOpts.Visibility != nil ||
|
||||||
|
editOpts.MaxRepoCreation != nil ||
|
||||||
|
editOpts.AllowGitHook != nil ||
|
||||||
|
editOpts.AllowImportLocal != nil ||
|
||||||
|
editOpts.AllowCreateOrganization != nil ||
|
||||||
|
editOpts.Password != ""
|
||||||
|
|
||||||
|
if !hasChanges {
|
||||||
|
return fmt.Errorf("no changes specified")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the user
|
||||||
|
_, err = client.Admin.EditUser(requestCtx, username, editOpts)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to update user: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Refresh user info to reflect the changes
|
||||||
|
updatedUser, _, err := client.Users.GetUserInfo(requestCtx, username)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("user updated but failed to retrieve updated user info: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
print.UserDetails(updatedUser)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
+12
-11
@@ -6,11 +6,11 @@ package users
|
|||||||
import (
|
import (
|
||||||
stdctx "context"
|
stdctx "context"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
gitea "gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
"code.gitea.io/tea/modules/print"
|
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/print"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -33,8 +33,11 @@ var CmdUserList = cli.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RunUserList list users
|
// RunUserList list users
|
||||||
func RunUserList(_ stdctx.Context, cmd *cli.Command) error {
|
func RunUserList(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
fields, err := userFieldsFlag.GetValues(cmd)
|
fields, err := userFieldsFlag.GetValues(cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -42,14 +45,12 @@ func RunUserList(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
client := ctx.Login.Client()
|
client := ctx.Login.Client()
|
||||||
users, _, err := client.AdminListUsers(gitea.AdminListUsersOptions{
|
users, _, err := client.Admin.ListUsers(requestCtx, gitea.AdminListUsersOptions{
|
||||||
ListOptions: flags.GetListOptions(),
|
ListOptions: flags.GetListOptions(cmd),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
print.UserList(users, ctx.Output, fields)
|
return print.UserList(users, ctx.Output, fields)
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package users
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"gitea.dev/sdk"
|
||||||
|
)
|
||||||
|
|
||||||
|
func parseUserVisibility(visibility string) (*gitea.VisibleType, error) {
|
||||||
|
switch visibility {
|
||||||
|
case "public":
|
||||||
|
vis := gitea.VisibleTypePublic
|
||||||
|
return &vis, nil
|
||||||
|
case "limited":
|
||||||
|
vis := gitea.VisibleTypeLimited
|
||||||
|
return &vis, nil
|
||||||
|
case "private":
|
||||||
|
vis := gitea.VisibleTypePrivate
|
||||||
|
return &vis, nil
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("invalid visibility: %s (must be public, limited, or private)", visibility)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func isConfirmationAccepted(response string) bool {
|
||||||
|
trimmed := strings.TrimSpace(response)
|
||||||
|
return strings.EqualFold(trimmed, "y") || strings.EqualFold(trimmed, "yes")
|
||||||
|
}
|
||||||
+216
-82
@@ -4,6 +4,7 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
stdctx "context"
|
stdctx "context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -12,31 +13,19 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
"gitea.dev/tea/cmd/flags"
|
||||||
"code.gitea.io/tea/modules/api"
|
"gitea.dev/tea/modules/api"
|
||||||
"code.gitea.io/tea/modules/context"
|
"gitea.dev/tea/modules/context"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CmdApi represents the api command
|
// apiFlags returns a fresh set of flag instances for the api command.
|
||||||
var CmdApi = cli.Command{
|
// This is a factory function so that each invocation gets independent flag
|
||||||
Name: "api",
|
// objects, avoiding shared hasBeenSet state across tests.
|
||||||
Usage: "Make an authenticated API request",
|
func apiFlags() []cli.Flag {
|
||||||
Description: `Makes an authenticated HTTP request to the Gitea API and prints the response.
|
return []cli.Flag{
|
||||||
|
|
||||||
The endpoint argument is the path to the API endpoint, which will be prefixed
|
|
||||||
with /api/v1/ if it doesn't start with /api/ or http(s)://.
|
|
||||||
|
|
||||||
Placeholders like {owner} and {repo} in the endpoint will be replaced with
|
|
||||||
values from the current repository context.
|
|
||||||
|
|
||||||
Use -f for string fields and -F for typed fields (numbers, booleans, null).
|
|
||||||
With -F, prefix value with @ to read from file (@- for stdin).`,
|
|
||||||
ArgsUsage: "<endpoint>",
|
|
||||||
Action: runApi,
|
|
||||||
Flags: append([]cli.Flag{
|
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "method",
|
Name: "method",
|
||||||
Aliases: []string{"X"},
|
Aliases: []string{"X"},
|
||||||
@@ -58,6 +47,11 @@ With -F, prefix value with @ to read from file (@- for stdin).`,
|
|||||||
Aliases: []string{"H"},
|
Aliases: []string{"H"},
|
||||||
Usage: "Add a custom header (key:value)",
|
Usage: "Add a custom header (key:value)",
|
||||||
},
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "data",
|
||||||
|
Aliases: []string{"d"},
|
||||||
|
Usage: "Raw JSON request body (use @file to read from file, @- for stdin)",
|
||||||
|
},
|
||||||
&cli.BoolFlag{
|
&cli.BoolFlag{
|
||||||
Name: "include",
|
Name: "include",
|
||||||
Aliases: []string{"i"},
|
Aliases: []string{"i"},
|
||||||
@@ -68,80 +62,74 @@ With -F, prefix value with @ to read from file (@- for stdin).`,
|
|||||||
Aliases: []string{"o"},
|
Aliases: []string{"o"},
|
||||||
Usage: "Write response body to file instead of stdout (use '-' for stdout)",
|
Usage: "Write response body to file instead of stdout (use '-' for stdout)",
|
||||||
},
|
},
|
||||||
}, flags.LoginRepoFlags...),
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func runApi(_ stdctx.Context, cmd *cli.Command) error {
|
// CmdApi represents the api command
|
||||||
ctx := context.InitCommand(cmd)
|
var CmdApi = cli.Command{
|
||||||
|
Name: "api",
|
||||||
|
Category: catHelpers,
|
||||||
|
DisableSliceFlagSeparator: true,
|
||||||
|
Usage: "Make an authenticated API request",
|
||||||
|
Description: `Makes an authenticated HTTP request to the Gitea API and prints the response.
|
||||||
|
|
||||||
// Get the endpoint argument
|
The endpoint argument is the path to the API endpoint, which will be prefixed
|
||||||
if cmd.NArg() < 1 {
|
with /api/v1/ if it doesn't start with /api/ or http(s)://.
|
||||||
return fmt.Errorf("endpoint argument required")
|
|
||||||
|
Placeholders like {owner} and {repo} in the endpoint will be replaced with
|
||||||
|
values from the current repository context.
|
||||||
|
|
||||||
|
Use -f for string fields and -F for typed fields (numbers, booleans, null).
|
||||||
|
With -F, prefix value with @ to read from file (@- for stdin). Values starting
|
||||||
|
with [ or { are parsed as JSON arrays/objects. Wrap values in quotes to force
|
||||||
|
string type (e.g., -F key="null" for literal string "null").
|
||||||
|
|
||||||
|
Use -d/--data to send a raw JSON body. Use @file to read from a file, or @-
|
||||||
|
to read from stdin. The -d flag cannot be combined with -f or -F.
|
||||||
|
|
||||||
|
When a request body is provided via -f, -F, or -d, the method defaults to POST
|
||||||
|
unless explicitly set with -X/--method.
|
||||||
|
|
||||||
|
Note: if your endpoint contains ? or &, quote it to prevent shell expansion
|
||||||
|
(e.g., '/repos/{owner}/{repo}/issues?state=open').`,
|
||||||
|
ArgsUsage: "<endpoint>",
|
||||||
|
Action: runApi,
|
||||||
|
Flags: append(apiFlags(), flags.LoginRepoFlags...),
|
||||||
|
}
|
||||||
|
|
||||||
|
type preparedAPIRequest struct {
|
||||||
|
Method string
|
||||||
|
Endpoint string
|
||||||
|
Headers map[string]string
|
||||||
|
Body []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func runApi(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
|
ctx, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
endpoint := cmd.Args().First()
|
request, err := prepareAPIRequest(cmd, ctx)
|
||||||
|
if err != nil {
|
||||||
// Expand placeholders in endpoint
|
return err
|
||||||
endpoint = expandPlaceholders(endpoint, ctx)
|
|
||||||
|
|
||||||
// Parse headers
|
|
||||||
headers := make(map[string]string)
|
|
||||||
for _, h := range cmd.StringSlice("header") {
|
|
||||||
parts := strings.SplitN(h, ":", 2)
|
|
||||||
if len(parts) != 2 {
|
|
||||||
return fmt.Errorf("invalid header format: %q (expected key:value)", h)
|
|
||||||
}
|
|
||||||
headers[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build request body from fields
|
|
||||||
var body io.Reader
|
var body io.Reader
|
||||||
stringFields := cmd.StringSlice("field")
|
if request.Body != nil {
|
||||||
typedFields := cmd.StringSlice("Field")
|
body = bytes.NewReader(request.Body)
|
||||||
|
|
||||||
if len(stringFields) > 0 || len(typedFields) > 0 {
|
|
||||||
bodyMap := make(map[string]any)
|
|
||||||
|
|
||||||
// Process string fields (-f)
|
|
||||||
for _, f := range stringFields {
|
|
||||||
parts := strings.SplitN(f, "=", 2)
|
|
||||||
if len(parts) != 2 {
|
|
||||||
return fmt.Errorf("invalid field format: %q (expected key=value)", f)
|
|
||||||
}
|
|
||||||
bodyMap[parts[0]] = parts[1]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Process typed fields (-F)
|
|
||||||
for _, f := range typedFields {
|
|
||||||
parts := strings.SplitN(f, "=", 2)
|
|
||||||
if len(parts) != 2 {
|
|
||||||
return fmt.Errorf("invalid field format: %q (expected key=value)", f)
|
|
||||||
}
|
|
||||||
key := parts[0]
|
|
||||||
value := parts[1]
|
|
||||||
|
|
||||||
parsedValue, err := parseTypedValue(value)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to parse field %q: %w", key, err)
|
|
||||||
}
|
|
||||||
bodyMap[key] = parsedValue
|
|
||||||
}
|
|
||||||
|
|
||||||
bodyBytes, err := json.Marshal(bodyMap)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to encode request body: %w", err)
|
|
||||||
}
|
|
||||||
body = strings.NewReader(string(bodyBytes))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create API client and make request
|
// Create API client and make request
|
||||||
client := api.NewClient(ctx.Login)
|
client := api.NewClient(ctx.Login)
|
||||||
method := strings.ToUpper(cmd.String("method"))
|
resp, err := client.Do(request.Method, request.Endpoint, body, request.Headers)
|
||||||
|
|
||||||
resp, err := client.Do(method, endpoint, body, headers)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("request failed: %w", err)
|
return fmt.Errorf("request failed: %w", err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer func() {
|
||||||
|
if closeErr := resp.Body.Close(); closeErr != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "warning: failed to close response body: %v\n", closeErr)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// Print headers to stderr if requested (so redirects/pipes work correctly)
|
// Print headers to stderr if requested (so redirects/pipes work correctly)
|
||||||
if cmd.Bool("include") {
|
if cmd.Bool("include") {
|
||||||
@@ -172,7 +160,11 @@ func runApi(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create output file: %w", err)
|
return fmt.Errorf("failed to create output file: %w", err)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer func() {
|
||||||
|
if closeErr := file.Close(); closeErr != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "warning: failed to close output file: %v\n", closeErr)
|
||||||
|
}
|
||||||
|
}()
|
||||||
output = file
|
output = file
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -190,15 +182,139 @@ func runApi(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func prepareAPIRequest(cmd *cli.Command, ctx *context.TeaContext) (*preparedAPIRequest, error) {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
// Get the endpoint argument
|
||||||
|
if cmd.NArg() < 1 {
|
||||||
|
return nil, fmt.Errorf("endpoint argument required")
|
||||||
|
}
|
||||||
|
endpoint := cmd.Args().First()
|
||||||
|
|
||||||
|
// Expand placeholders in endpoint
|
||||||
|
endpoint = expandPlaceholders(endpoint, ctx)
|
||||||
|
|
||||||
|
// Parse headers
|
||||||
|
headers := make(map[string]string)
|
||||||
|
for _, h := range cmd.StringSlice("header") {
|
||||||
|
parts := strings.SplitN(h, ":", 2)
|
||||||
|
if len(parts) != 2 {
|
||||||
|
return nil, fmt.Errorf("invalid header format: %q (expected key:value)", h)
|
||||||
|
}
|
||||||
|
headers[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build request body from fields
|
||||||
|
var bodyBytes []byte
|
||||||
|
stringFields := cmd.StringSlice("field")
|
||||||
|
typedFields := cmd.StringSlice("Field")
|
||||||
|
dataRaw := cmd.String("data")
|
||||||
|
|
||||||
|
if dataRaw != "" && (len(stringFields) > 0 || len(typedFields) > 0) {
|
||||||
|
return nil, fmt.Errorf("--data/-d cannot be combined with --field/-f or --Field/-F")
|
||||||
|
}
|
||||||
|
|
||||||
|
if dataRaw != "" {
|
||||||
|
var dataBytes []byte
|
||||||
|
var dataSource string
|
||||||
|
if strings.HasPrefix(dataRaw, "@") {
|
||||||
|
filename := dataRaw[1:]
|
||||||
|
if filename == "-" {
|
||||||
|
dataBytes, err = io.ReadAll(os.Stdin)
|
||||||
|
dataSource = "stdin"
|
||||||
|
} else {
|
||||||
|
dataBytes, err = os.ReadFile(filename)
|
||||||
|
dataSource = filename
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to read %q: %w", dataRaw, err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
dataBytes = []byte(dataRaw)
|
||||||
|
}
|
||||||
|
if !json.Valid(dataBytes) {
|
||||||
|
if dataSource != "" {
|
||||||
|
return nil, fmt.Errorf("--data/-d value from %s is not valid JSON", dataSource)
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("--data/-d value is not valid JSON")
|
||||||
|
}
|
||||||
|
bodyBytes = dataBytes
|
||||||
|
} else if len(stringFields) > 0 || len(typedFields) > 0 {
|
||||||
|
bodyMap := make(map[string]any)
|
||||||
|
|
||||||
|
// Process string fields (-f)
|
||||||
|
for _, f := range stringFields {
|
||||||
|
parts := strings.SplitN(f, "=", 2)
|
||||||
|
if len(parts) != 2 {
|
||||||
|
return nil, fmt.Errorf("invalid field format: %q (expected key=value)", f)
|
||||||
|
}
|
||||||
|
key := parts[0]
|
||||||
|
if key == "" {
|
||||||
|
return nil, fmt.Errorf("field key cannot be empty in %q", f)
|
||||||
|
}
|
||||||
|
if _, exists := bodyMap[key]; exists {
|
||||||
|
return nil, fmt.Errorf("duplicate field key %q", key)
|
||||||
|
}
|
||||||
|
bodyMap[key] = parts[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process typed fields (-F)
|
||||||
|
for _, f := range typedFields {
|
||||||
|
parts := strings.SplitN(f, "=", 2)
|
||||||
|
if len(parts) != 2 {
|
||||||
|
return nil, fmt.Errorf("invalid field format: %q (expected key=value)", f)
|
||||||
|
}
|
||||||
|
key := parts[0]
|
||||||
|
if key == "" {
|
||||||
|
return nil, fmt.Errorf("field key cannot be empty in %q", f)
|
||||||
|
}
|
||||||
|
if _, exists := bodyMap[key]; exists {
|
||||||
|
return nil, fmt.Errorf("duplicate field key %q", key)
|
||||||
|
}
|
||||||
|
value := parts[1]
|
||||||
|
|
||||||
|
parsedValue, err := parseTypedValue(value)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to parse field %q: %w", key, err)
|
||||||
|
}
|
||||||
|
bodyMap[key] = parsedValue
|
||||||
|
}
|
||||||
|
|
||||||
|
bodyBytes, err = json.Marshal(bodyMap)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to encode request body: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
method := strings.ToUpper(cmd.String("method"))
|
||||||
|
if !cmd.IsSet("method") {
|
||||||
|
if bodyBytes != nil {
|
||||||
|
method = "POST"
|
||||||
|
} else {
|
||||||
|
method = "GET"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &preparedAPIRequest{
|
||||||
|
Method: method,
|
||||||
|
Endpoint: endpoint,
|
||||||
|
Headers: headers,
|
||||||
|
Body: bodyBytes,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
// parseTypedValue parses a value for -F flag, handling:
|
// parseTypedValue parses a value for -F flag, handling:
|
||||||
// - @filename: read content from file
|
// - @filename: read content from file
|
||||||
// - @-: read content from stdin
|
// - @-: read content from stdin
|
||||||
|
// - "quoted": literal string (prevents type parsing)
|
||||||
// - true/false: boolean
|
// - true/false: boolean
|
||||||
// - null: nil
|
// - null: nil
|
||||||
// - numbers: int or float
|
// - numbers: int or float
|
||||||
|
// - []/{}: JSON arrays/objects
|
||||||
// - otherwise: string
|
// - otherwise: string
|
||||||
func parseTypedValue(value string) (any, error) {
|
func parseTypedValue(value string) (any, error) {
|
||||||
// Handle file references
|
// Handle file references.
|
||||||
|
// Note: if multiple fields use @- (stdin), only the first will get data;
|
||||||
|
// subsequent reads will return empty since stdin is consumed once.
|
||||||
if strings.HasPrefix(value, "@") {
|
if strings.HasPrefix(value, "@") {
|
||||||
filename := value[1:]
|
filename := value[1:]
|
||||||
var content []byte
|
var content []byte
|
||||||
@@ -215,6 +331,16 @@ func parseTypedValue(value string) (any, error) {
|
|||||||
return strings.TrimSuffix(string(content), "\n"), nil
|
return strings.TrimSuffix(string(content), "\n"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle quoted strings (literal strings, no type parsing).
|
||||||
|
// Uses strconv.Unquote so escape sequences like \" are handled correctly.
|
||||||
|
if len(value) >= 2 && value[0] == '"' && value[len(value)-1] == '"' {
|
||||||
|
unquoted, err := strconv.Unquote(value)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("invalid quoted string %s: %w", value, err)
|
||||||
|
}
|
||||||
|
return unquoted, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Handle null
|
// Handle null
|
||||||
if value == "null" {
|
if value == "null" {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
@@ -238,6 +364,14 @@ func parseTypedValue(value string) (any, error) {
|
|||||||
return f, nil
|
return f, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle JSON arrays and objects
|
||||||
|
if len(value) > 0 && (value[0] == '[' || value[0] == '{') {
|
||||||
|
var jsonVal any
|
||||||
|
if err := json.Unmarshal([]byte(value), &jsonVal); err == nil {
|
||||||
|
return jsonVal, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Default to string
|
// Default to string
|
||||||
return value, nil
|
return value, nil
|
||||||
}
|
}
|
||||||
|
|||||||
+632
@@ -0,0 +1,632 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
stdctx "context"
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gitea.dev/tea/modules/config"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
tea_git "gitea.dev/tea/modules/git"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/urfave/cli/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseTypedValue(t *testing.T) {
|
||||||
|
t.Run("null", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue("null")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Nil(t, v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("bool true", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue("true")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, true, v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("bool false", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue("false")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, false, v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("integer", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue("42")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, int64(42), v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("float", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue("3.14")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, 3.14, v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("string", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue("hello")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "hello", v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("JSON array", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue("[1,2,3]")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, []any{float64(1), float64(2), float64(3)}, v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("JSON object", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue(`{"key":"val"}`)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, map[string]any{"key": "val"}, v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("invalid JSON array falls back to string", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue("[not json")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "[not json", v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("invalid JSON object falls back to string", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue("{not json")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "{not json", v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("file reference", func(t *testing.T) {
|
||||||
|
tmpFile := filepath.Join(t.TempDir(), "test.txt")
|
||||||
|
require.NoError(t, os.WriteFile(tmpFile, []byte("file content\n"), 0o644))
|
||||||
|
v, err := parseTypedValue("@" + tmpFile)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "file content", v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("file reference without trailing newline", func(t *testing.T) {
|
||||||
|
tmpFile := filepath.Join(t.TempDir(), "test.txt")
|
||||||
|
require.NoError(t, os.WriteFile(tmpFile, []byte("no newline"), 0o644))
|
||||||
|
v, err := parseTypedValue("@" + tmpFile)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "no newline", v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("empty file reference", func(t *testing.T) {
|
||||||
|
tmpFile := filepath.Join(t.TempDir(), "empty.txt")
|
||||||
|
require.NoError(t, os.WriteFile(tmpFile, []byte(""), 0o644))
|
||||||
|
v, err := parseTypedValue("@" + tmpFile)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "", v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("nonexistent file reference", func(t *testing.T) {
|
||||||
|
_, err := parseTypedValue("@/nonexistent/file.txt")
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "failed to read")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("negative integer", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue("-42")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, int64(-42), v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("negative float", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue("-3.14")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, -3.14, v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("scientific notation", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue("1.5e10")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, 1.5e10, v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("empty string", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue("")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "", v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("string starting with number", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue("123abc")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "123abc", v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("nested JSON object", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue(`{"user":{"name":"alice","id":1}}`)
|
||||||
|
require.NoError(t, err)
|
||||||
|
expected := map[string]any{
|
||||||
|
"user": map[string]any{
|
||||||
|
"name": "alice",
|
||||||
|
"id": float64(1),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("complex JSON array", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue(`[{"id":1},{"id":2}]`)
|
||||||
|
require.NoError(t, err)
|
||||||
|
expected := []any{
|
||||||
|
map[string]any{"id": float64(1)},
|
||||||
|
map[string]any{"id": float64(2)},
|
||||||
|
}
|
||||||
|
assert.Equal(t, expected, v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("quoted string prevents type parsing", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue(`"null"`)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "null", v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("quoted true becomes string", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue(`"true"`)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "true", v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("quoted false becomes string", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue(`"false"`)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "false", v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("quoted number becomes string", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue(`"123"`)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "123", v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("quoted empty string", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue(`""`)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "", v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("quoted string with spaces", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue(`"hello world"`)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "hello world", v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("single quote not treated as quote", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue(`'hello'`)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "'hello'", v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("unmatched quote at start only", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue(`"hello`)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, `"hello`, v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("unmatched quote at end only", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue(`hello"`)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, `hello"`, v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("quoted string with escaped quote", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue(`"hello \"world\""`)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, `hello "world"`, v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("quoted string with backslash-n", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue(`"line1\nline2"`)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "line1\nline2", v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("quoted string with tab escape", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue(`"col1\tcol2"`)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "col1\tcol2", v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("quoted string with backslash", func(t *testing.T) {
|
||||||
|
v, err := parseTypedValue(`"path\\to\\file"`)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, `path\to\file`, v)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("invalid escape sequence in quoted string", func(t *testing.T) {
|
||||||
|
_, err := parseTypedValue(`"bad \z escape"`)
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "invalid quoted string")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// runApiWithArgs configures a test login, parses the command line, and captures
|
||||||
|
// the prepared request without opening sockets or making HTTP requests.
|
||||||
|
func runApiWithArgs(t *testing.T, args []string) (method string, body []byte, err error) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
var capturedMethod string
|
||||||
|
var capturedBody []byte
|
||||||
|
|
||||||
|
config.SetConfigForTesting(config.LocalConfig{
|
||||||
|
Logins: []config.Login{{
|
||||||
|
Name: "testLogin",
|
||||||
|
URL: "https://gitea.example.com",
|
||||||
|
Token: "test-token",
|
||||||
|
User: "testUser",
|
||||||
|
Default: true,
|
||||||
|
}},
|
||||||
|
})
|
||||||
|
|
||||||
|
// Use the apiFlags factory to get fresh flag instances, avoiding shared
|
||||||
|
// hasBeenSet state between tests. Append minimal login/repo flags needed
|
||||||
|
// for the test harness.
|
||||||
|
cmd := cli.Command{
|
||||||
|
Name: "api",
|
||||||
|
DisableSliceFlagSeparator: true,
|
||||||
|
Action: func(_ stdctx.Context, cmd *cli.Command) error {
|
||||||
|
ctx, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
request, err := prepareAPIRequest(cmd, ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
capturedMethod = request.Method
|
||||||
|
capturedBody = append([]byte(nil), request.Body...)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
Flags: append(apiFlags(), []cli.Flag{
|
||||||
|
&cli.StringFlag{Name: "login", Aliases: []string{"l"}},
|
||||||
|
&cli.StringFlag{Name: "repo", Aliases: []string{"r"}},
|
||||||
|
&cli.StringFlag{Name: "remote", Aliases: []string{"R"}},
|
||||||
|
}...),
|
||||||
|
Writer: io.Discard,
|
||||||
|
ErrWriter: io.Discard,
|
||||||
|
}
|
||||||
|
|
||||||
|
fullArgs := append([]string{"api", "--login", "testLogin"}, args...)
|
||||||
|
runErr := cmd.Run(t.Context(), fullArgs)
|
||||||
|
|
||||||
|
return capturedMethod, capturedBody, runErr
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestApiCommaInFieldValue(t *testing.T) {
|
||||||
|
_, body, err := runApiWithArgs(t, []string{"-f", "body=hello, world", "-X", "POST", "/test"})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
var parsed map[string]any
|
||||||
|
require.NoError(t, json.Unmarshal(body, &parsed))
|
||||||
|
assert.Equal(t, "hello, world", parsed["body"])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestApiRawDataFlag(t *testing.T) {
|
||||||
|
_, body, err := runApiWithArgs(t, []string{"-d", `{"title":"test","body":"hello"}`, "/test"})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
var parsed map[string]any
|
||||||
|
require.NoError(t, json.Unmarshal(body, &parsed))
|
||||||
|
assert.Equal(t, "test", parsed["title"])
|
||||||
|
assert.Equal(t, "hello", parsed["body"])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestApiDataFieldMutualExclusion(t *testing.T) {
|
||||||
|
_, _, err := runApiWithArgs(t, []string{"-d", `{"title":"test"}`, "-f", "key=val", "/test"})
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "--data/-d cannot be combined with --field/-f or --Field/-F")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestApiMethodAutoDefault(t *testing.T) {
|
||||||
|
t.Run("POST when body provided without explicit method", func(t *testing.T) {
|
||||||
|
method, _, err := runApiWithArgs(t, []string{"-d", `{"title":"test"}`, "/test"})
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "POST", method)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("explicit method overrides auto-POST", func(t *testing.T) {
|
||||||
|
method, _, err := runApiWithArgs(t, []string{"-d", `{"title":"test"}`, "-X", "PATCH", "/test"})
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "PATCH", method)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("GET when no body", func(t *testing.T) {
|
||||||
|
method, _, err := runApiWithArgs(t, []string{"/test"})
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "GET", method)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestApiMultipleFields(t *testing.T) {
|
||||||
|
t.Run("multiple -f flags", func(t *testing.T) {
|
||||||
|
_, body, err := runApiWithArgs(t, []string{
|
||||||
|
"-f", "title=Test Issue",
|
||||||
|
"-f", "body=Description here",
|
||||||
|
"-X", "POST",
|
||||||
|
"/test",
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
var parsed map[string]any
|
||||||
|
require.NoError(t, json.Unmarshal(body, &parsed))
|
||||||
|
assert.Equal(t, "Test Issue", parsed["title"])
|
||||||
|
assert.Equal(t, "Description here", parsed["body"])
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("multiple -F flags with different types", func(t *testing.T) {
|
||||||
|
_, body, err := runApiWithArgs(t, []string{
|
||||||
|
"-F", "milestone=5",
|
||||||
|
"-F", "closed=true",
|
||||||
|
"-F", "title=Test",
|
||||||
|
"-X", "POST",
|
||||||
|
"/test",
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
var parsed map[string]any
|
||||||
|
require.NoError(t, json.Unmarshal(body, &parsed))
|
||||||
|
assert.Equal(t, float64(5), parsed["milestone"])
|
||||||
|
assert.Equal(t, true, parsed["closed"])
|
||||||
|
assert.Equal(t, "Test", parsed["title"])
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("combining -f and -F flags", func(t *testing.T) {
|
||||||
|
_, body, err := runApiWithArgs(t, []string{
|
||||||
|
"-f", "title=Test",
|
||||||
|
"-F", "milestone=3",
|
||||||
|
"-F", "closed=false",
|
||||||
|
"-X", "POST",
|
||||||
|
"/test",
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
var parsed map[string]any
|
||||||
|
require.NoError(t, json.Unmarshal(body, &parsed))
|
||||||
|
assert.Equal(t, "Test", parsed["title"])
|
||||||
|
assert.Equal(t, float64(3), parsed["milestone"])
|
||||||
|
assert.Equal(t, false, parsed["closed"])
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("-F with JSON array", func(t *testing.T) {
|
||||||
|
_, body, err := runApiWithArgs(t, []string{
|
||||||
|
"-F", `labels=["bug","enhancement"]`,
|
||||||
|
"-X", "POST",
|
||||||
|
"/test",
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
var parsed map[string]any
|
||||||
|
require.NoError(t, json.Unmarshal(body, &parsed))
|
||||||
|
assert.Equal(t, []any{"bug", "enhancement"}, parsed["labels"])
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("-F with JSON object", func(t *testing.T) {
|
||||||
|
_, body, err := runApiWithArgs(t, []string{
|
||||||
|
"-F", `assignee={"login":"alice","id":123}`,
|
||||||
|
"-X", "POST",
|
||||||
|
"/test",
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
var parsed map[string]any
|
||||||
|
require.NoError(t, json.Unmarshal(body, &parsed))
|
||||||
|
assignee, ok := parsed["assignee"].(map[string]any)
|
||||||
|
require.True(t, ok)
|
||||||
|
assert.Equal(t, "alice", assignee["login"])
|
||||||
|
assert.Equal(t, float64(123), assignee["id"])
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("-F with quoted string to prevent type parsing", func(t *testing.T) {
|
||||||
|
_, body, err := runApiWithArgs(t, []string{
|
||||||
|
"-F", `status="null"`,
|
||||||
|
"-F", `enabled="true"`,
|
||||||
|
"-F", `count="42"`,
|
||||||
|
"-X", "POST",
|
||||||
|
"/test",
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
var parsed map[string]any
|
||||||
|
require.NoError(t, json.Unmarshal(body, &parsed))
|
||||||
|
assert.Equal(t, "null", parsed["status"])
|
||||||
|
assert.Equal(t, "true", parsed["enabled"])
|
||||||
|
assert.Equal(t, "42", parsed["count"])
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestApiDataFromFile(t *testing.T) {
|
||||||
|
t.Run("read JSON from file", func(t *testing.T) {
|
||||||
|
tmpFile := filepath.Join(t.TempDir(), "data.json")
|
||||||
|
jsonData := `{"title":"From File","body":"File content"}`
|
||||||
|
require.NoError(t, os.WriteFile(tmpFile, []byte(jsonData), 0o644))
|
||||||
|
|
||||||
|
_, body, err := runApiWithArgs(t, []string{"-d", "@" + tmpFile, "/test"})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
var parsed map[string]any
|
||||||
|
require.NoError(t, json.Unmarshal(body, &parsed))
|
||||||
|
assert.Equal(t, "From File", parsed["title"])
|
||||||
|
assert.Equal(t, "File content", parsed["body"])
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("invalid JSON in --data flag", func(t *testing.T) {
|
||||||
|
_, _, err := runApiWithArgs(t, []string{"-d", `{invalid json}`, "/test"})
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "not valid JSON")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("invalid JSON from file includes filename", func(t *testing.T) {
|
||||||
|
tmpFile := filepath.Join(t.TempDir(), "bad.json")
|
||||||
|
require.NoError(t, os.WriteFile(tmpFile, []byte("not json"), 0o644))
|
||||||
|
|
||||||
|
_, _, err := runApiWithArgs(t, []string{"-d", "@" + tmpFile, "/test"})
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "not valid JSON")
|
||||||
|
assert.Contains(t, err.Error(), "bad.json")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestApiErrorHandling(t *testing.T) {
|
||||||
|
t.Run("missing endpoint argument", func(t *testing.T) {
|
||||||
|
_, _, err := runApiWithArgs(t, []string{})
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "endpoint argument required")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("invalid field format", func(t *testing.T) {
|
||||||
|
_, _, err := runApiWithArgs(t, []string{"-f", "invalidformat", "-X", "POST", "/test"})
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "invalid field format")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("invalid Field format", func(t *testing.T) {
|
||||||
|
_, _, err := runApiWithArgs(t, []string{"-F", "noequalsign", "-X", "POST", "/test"})
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "invalid field format")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("empty field key with -f", func(t *testing.T) {
|
||||||
|
_, _, err := runApiWithArgs(t, []string{"-f", "=value", "-X", "POST", "/test"})
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "field key cannot be empty")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("empty field key with -F", func(t *testing.T) {
|
||||||
|
_, _, err := runApiWithArgs(t, []string{"-F", "=123", "-X", "POST", "/test"})
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "field key cannot be empty")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("duplicate field key in -f flags", func(t *testing.T) {
|
||||||
|
_, _, err := runApiWithArgs(t, []string{"-f", "key=first", "-f", "key=second", "-X", "POST", "/test"})
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "duplicate field key")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("duplicate field key in -F flags", func(t *testing.T) {
|
||||||
|
_, _, err := runApiWithArgs(t, []string{"-F", "key=1", "-F", "key=2", "-X", "POST", "/test"})
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "duplicate field key")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("duplicate field key across -f and -F flags", func(t *testing.T) {
|
||||||
|
_, _, err := runApiWithArgs(t, []string{"-f", "key=string", "-F", "key=123", "-X", "POST", "/test"})
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "duplicate field key")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExpandPlaceholders(t *testing.T) {
|
||||||
|
t.Run("replaces owner and repo", func(t *testing.T) {
|
||||||
|
ctx := &context.TeaContext{
|
||||||
|
Owner: "myorg",
|
||||||
|
Repo: "myrepo",
|
||||||
|
}
|
||||||
|
result := expandPlaceholders("/repos/{owner}/{repo}/issues", ctx)
|
||||||
|
assert.Equal(t, "/repos/myorg/myrepo/issues", result)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("replaces multiple occurrences", func(t *testing.T) {
|
||||||
|
ctx := &context.TeaContext{
|
||||||
|
Owner: "alice",
|
||||||
|
Repo: "proj",
|
||||||
|
}
|
||||||
|
result := expandPlaceholders("/repos/{owner}/{repo}/branches?owner={owner}", ctx)
|
||||||
|
assert.Equal(t, "/repos/alice/proj/branches?owner=alice", result)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("no placeholders returns unchanged", func(t *testing.T) {
|
||||||
|
ctx := &context.TeaContext{
|
||||||
|
Owner: "alice",
|
||||||
|
Repo: "proj",
|
||||||
|
}
|
||||||
|
result := expandPlaceholders("/api/v1/version", ctx)
|
||||||
|
assert.Equal(t, "/api/v1/version", result)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("empty owner and repo produce empty replacements", func(t *testing.T) {
|
||||||
|
ctx := &context.TeaContext{}
|
||||||
|
result := expandPlaceholders("/repos/{owner}/{repo}", ctx)
|
||||||
|
assert.Equal(t, "/repos//", result)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("branch left unreplaced when no local repo", func(t *testing.T) {
|
||||||
|
ctx := &context.TeaContext{
|
||||||
|
Owner: "alice",
|
||||||
|
Repo: "proj",
|
||||||
|
}
|
||||||
|
result := expandPlaceholders("/repos/{owner}/{repo}/branches/{branch}", ctx)
|
||||||
|
assert.Equal(t, "/repos/alice/proj/branches/{branch}", result)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("replaces branch from local repo HEAD", func(t *testing.T) {
|
||||||
|
tmpDir := t.TempDir()
|
||||||
|
runGit := func(args ...string) {
|
||||||
|
cmd := exec.Command("git", args...)
|
||||||
|
cmd.Dir = tmpDir
|
||||||
|
require.NoError(t, cmd.Run())
|
||||||
|
}
|
||||||
|
|
||||||
|
runGit("init")
|
||||||
|
runGit("config", "user.email", "test@test.com")
|
||||||
|
runGit("config", "user.name", "test")
|
||||||
|
|
||||||
|
// Create an initial commit so HEAD points to a branch.
|
||||||
|
tmpFile := filepath.Join(tmpDir, "init.txt")
|
||||||
|
require.NoError(t, os.WriteFile(tmpFile, []byte("init"), 0o644))
|
||||||
|
runGit("add", "init.txt")
|
||||||
|
runGit("commit", "-m", "initial commit")
|
||||||
|
|
||||||
|
// Create and checkout a feature branch.
|
||||||
|
runGit("checkout", "-b", "feature/my-branch")
|
||||||
|
|
||||||
|
repo, err := tea_git.RepoFromPath(tmpDir)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
ctx := &context.TeaContext{
|
||||||
|
Owner: "alice",
|
||||||
|
Repo: "proj",
|
||||||
|
LocalRepo: repo,
|
||||||
|
}
|
||||||
|
result := expandPlaceholders("/repos/{owner}/{repo}/branches/{branch}", ctx)
|
||||||
|
assert.Equal(t, "/repos/alice/proj/branches/feature/my-branch", result)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsTextContentType(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
contentType string
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{"empty string defaults to text", "", true},
|
||||||
|
{"plain text", "text/plain", true},
|
||||||
|
{"html", "text/html", true},
|
||||||
|
{"json", "application/json", true},
|
||||||
|
{"json with charset", "application/json; charset=utf-8", true},
|
||||||
|
{"xml", "application/xml", true},
|
||||||
|
{"javascript", "application/javascript", true},
|
||||||
|
{"yaml", "application/yaml", true},
|
||||||
|
{"toml", "application/toml", true},
|
||||||
|
{"binary", "application/octet-stream", false},
|
||||||
|
{"image", "image/png", false},
|
||||||
|
{"pdf", "application/pdf", false},
|
||||||
|
{"zip", "application/zip", false},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got := isTextContentType(tt.contentType)
|
||||||
|
assert.Equal(t, tt.want, got)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
-2
@@ -4,8 +4,8 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"code.gitea.io/tea/cmd/attachments"
|
"gitea.dev/tea/cmd/attachments"
|
||||||
"code.gitea.io/tea/cmd/flags"
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -9,8 +9,9 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
"gitea.dev/tea/cmd/flags"
|
||||||
"code.gitea.io/tea/modules/context"
|
"gitea.dev/tea/cmd/releases"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
@@ -26,21 +27,26 @@ var CmdReleaseAttachmentCreate = cli.Command{
|
|||||||
Flags: flags.AllDefaultFlags,
|
Flags: flags.AllDefaultFlags,
|
||||||
}
|
}
|
||||||
|
|
||||||
func runReleaseAttachmentCreate(_ stdctx.Context, cmd *cli.Command) error {
|
func runReleaseAttachmentCreate(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
client := ctx.Login.Client()
|
client := ctx.Login.Client()
|
||||||
|
|
||||||
if ctx.Args().Len() < 2 {
|
if ctx.Args().Len() < 2 {
|
||||||
return fmt.Errorf("No release tag or assets specified.\nUsage:\t%s", ctx.Command.UsageText)
|
return fmt.Errorf("no release tag or assets specified.\nUsage:\t%s", ctx.Command.UsageText)
|
||||||
}
|
}
|
||||||
|
|
||||||
tag := ctx.Args().First()
|
tag := ctx.Args().First()
|
||||||
if len(tag) == 0 {
|
if len(tag) == 0 {
|
||||||
return fmt.Errorf("Release tag needed to create attachment")
|
return fmt.Errorf("release tag needed to create attachment")
|
||||||
}
|
}
|
||||||
|
|
||||||
release, err := getReleaseByTag(ctx.Owner, ctx.Repo, tag, client)
|
release, err := releases.GetReleaseByTag(requestCtx, ctx.Owner, ctx.Repo, tag, client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -53,7 +59,7 @@ func runReleaseAttachmentCreate(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
|
|
||||||
filePath := filepath.Base(asset)
|
filePath := filepath.Base(asset)
|
||||||
|
|
||||||
if _, _, err = ctx.Login.Client().CreateReleaseAttachment(ctx.Owner, ctx.Repo, release.ID, file, filePath); err != nil {
|
if _, _, err = ctx.Login.Client().Releases.CreateReleaseAttachment(requestCtx, ctx.Owner, ctx.Repo, release.ID, file, filePath); err != nil {
|
||||||
file.Close()
|
file.Close()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
+27
-13
@@ -7,10 +7,11 @@ import (
|
|||||||
stdctx "context"
|
stdctx "context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
gitea "gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/cmd/releases"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -31,18 +32,23 @@ var CmdReleaseAttachmentDelete = cli.Command{
|
|||||||
}, flags.AllDefaultFlags...),
|
}, flags.AllDefaultFlags...),
|
||||||
}
|
}
|
||||||
|
|
||||||
func runReleaseAttachmentDelete(_ stdctx.Context, cmd *cli.Command) error {
|
func runReleaseAttachmentDelete(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
client := ctx.Login.Client()
|
client := ctx.Login.Client()
|
||||||
|
|
||||||
if ctx.Args().Len() < 2 {
|
if ctx.Args().Len() < 2 {
|
||||||
return fmt.Errorf("No release tag or attachment names specified.\nUsage:\t%s", ctx.Command.UsageText)
|
return fmt.Errorf("no release tag or attachment names specified.\nUsage:\t%s", ctx.Command.UsageText)
|
||||||
}
|
}
|
||||||
|
|
||||||
tag := ctx.Args().First()
|
tag := ctx.Args().First()
|
||||||
if len(tag) == 0 {
|
if len(tag) == 0 {
|
||||||
return fmt.Errorf("Release tag needed to delete attachment")
|
return fmt.Errorf("release tag needed to delete attachment")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !ctx.Bool("confirm") {
|
if !ctx.Bool("confirm") {
|
||||||
@@ -50,17 +56,25 @@ func runReleaseAttachmentDelete(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
release, err := getReleaseByTag(ctx.Owner, ctx.Repo, tag, client)
|
release, err := releases.GetReleaseByTag(requestCtx, ctx.Owner, ctx.Repo, tag, client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
existing, _, err := client.ListReleaseAttachments(ctx.Owner, ctx.Repo, release.ID, gitea.ListReleaseAttachmentsOptions{
|
var existing []*gitea.Attachment
|
||||||
ListOptions: gitea.ListOptions{Page: -1},
|
for page := 1; ; {
|
||||||
|
page_attachments, resp, err := client.Releases.ListReleaseAttachments(requestCtx, ctx.Owner, ctx.Repo, release.ID, gitea.ListReleaseAttachmentsOptions{
|
||||||
|
ListOptions: gitea.ListOptions{Page: page, PageSize: 50},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
existing = append(existing, page_attachments...)
|
||||||
|
if resp == nil || resp.NextPage == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
page = resp.NextPage
|
||||||
|
}
|
||||||
|
|
||||||
for _, name := range ctx.Args().Slice()[1:] {
|
for _, name := range ctx.Args().Slice()[1:] {
|
||||||
var attachment *gitea.Attachment
|
var attachment *gitea.Attachment
|
||||||
@@ -70,10 +84,10 @@ func runReleaseAttachmentDelete(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if attachment == nil {
|
if attachment == nil {
|
||||||
return fmt.Errorf("Release does not have attachment named '%s'", name)
|
return fmt.Errorf("release does not have attachment named '%s'", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = client.DeleteReleaseAttachment(ctx.Owner, ctx.Repo, release.ID, attachment.ID)
|
_, err = client.Releases.DeleteReleaseAttachment(requestCtx, ctx.Owner, ctx.Repo, release.ID, attachment.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
+18
-32
@@ -7,11 +7,11 @@ import (
|
|||||||
stdctx "context"
|
stdctx "context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
gitea "gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/modules/context"
|
"gitea.dev/tea/cmd/flags"
|
||||||
"code.gitea.io/tea/modules/print"
|
"gitea.dev/tea/cmd/releases"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
"code.gitea.io/sdk/gitea"
|
"gitea.dev/tea/modules/print"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -30,46 +30,32 @@ var CmdReleaseAttachmentList = cli.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RunReleaseAttachmentList list release attachments
|
// RunReleaseAttachmentList list release attachments
|
||||||
func RunReleaseAttachmentList(_ stdctx.Context, cmd *cli.Command) error {
|
func RunReleaseAttachmentList(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
client := ctx.Login.Client()
|
client := ctx.Login.Client()
|
||||||
|
|
||||||
tag := ctx.Args().First()
|
tag := ctx.Args().First()
|
||||||
if len(tag) == 0 {
|
if len(tag) == 0 {
|
||||||
return fmt.Errorf("Release tag needed to list attachments")
|
return fmt.Errorf("release tag needed to list attachments")
|
||||||
}
|
}
|
||||||
|
|
||||||
release, err := getReleaseByTag(ctx.Owner, ctx.Repo, tag, client)
|
release, err := releases.GetReleaseByTag(requestCtx, ctx.Owner, ctx.Repo, tag, client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
attachments, _, err := ctx.Login.Client().ListReleaseAttachments(ctx.Owner, ctx.Repo, release.ID, gitea.ListReleaseAttachmentsOptions{
|
attachments, _, err := ctx.Login.Client().Releases.ListReleaseAttachments(requestCtx, ctx.Owner, ctx.Repo, release.ID, gitea.ListReleaseAttachmentsOptions{
|
||||||
ListOptions: flags.GetListOptions(),
|
ListOptions: flags.GetListOptions(cmd),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
print.ReleaseAttachmentsList(attachments, ctx.Output)
|
return print.ReleaseAttachmentsList(attachments, ctx.Output)
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getReleaseByTag(owner, repo, tag string, client *gitea.Client) (*gitea.Release, error) {
|
|
||||||
rl, _, err := client.ListReleases(owner, repo, gitea.ListReleasesOptions{
|
|
||||||
ListOptions: gitea.ListOptions{Page: -1},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if len(rl) == 0 {
|
|
||||||
return nil, fmt.Errorf("Repo does not have any release")
|
|
||||||
}
|
|
||||||
for _, r := range rl {
|
|
||||||
if r.TagName == tag {
|
|
||||||
return r, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("Release tag does not exist")
|
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -6,7 +6,7 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/branches"
|
"gitea.dev/tea/cmd/branches"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
@@ -24,6 +24,7 @@ var CmdBranches = cli.Command{
|
|||||||
&branches.CmdBranchesList,
|
&branches.CmdBranchesList,
|
||||||
&branches.CmdBranchesProtect,
|
&branches.CmdBranchesProtect,
|
||||||
&branches.CmdBranchesUnprotect,
|
&branches.CmdBranchesUnprotect,
|
||||||
|
&branches.CmdBranchesRename,
|
||||||
},
|
},
|
||||||
Flags: append([]cli.Flag{
|
Flags: append([]cli.Flag{
|
||||||
&cli.BoolFlag{
|
&cli.BoolFlag{
|
||||||
|
|||||||
+17
-14
@@ -6,11 +6,11 @@ package branches
|
|||||||
import (
|
import (
|
||||||
stdctx "context"
|
stdctx "context"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
gitea "gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
"code.gitea.io/tea/modules/print"
|
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/print"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -37,9 +37,14 @@ var CmdBranchesList = cli.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RunBranchesList list branches
|
// RunBranchesList list branches
|
||||||
func RunBranchesList(_ stdctx.Context, cmd *cli.Command) error {
|
func RunBranchesList(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
owner := ctx.Owner
|
owner := ctx.Owner
|
||||||
if ctx.IsSet("owner") {
|
if ctx.IsSet("owner") {
|
||||||
@@ -48,16 +53,15 @@ func RunBranchesList(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
|
|
||||||
var branches []*gitea.Branch
|
var branches []*gitea.Branch
|
||||||
var protections []*gitea.BranchProtection
|
var protections []*gitea.BranchProtection
|
||||||
var err error
|
branches, _, err = ctx.Login.Client().Repositories.ListRepoBranches(requestCtx, owner, ctx.Repo, gitea.ListRepoBranchesOptions{
|
||||||
branches, _, err = ctx.Login.Client().ListRepoBranches(owner, ctx.Repo, gitea.ListRepoBranchesOptions{
|
ListOptions: flags.GetListOptions(cmd),
|
||||||
ListOptions: flags.GetListOptions(),
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
protections, _, err = ctx.Login.Client().ListBranchProtections(owner, ctx.Repo, gitea.ListBranchProtectionsOptions{
|
protections, _, err = ctx.Login.Client().Repositories.ListBranchProtections(requestCtx, owner, ctx.Repo, gitea.ListBranchProtectionsOptions{
|
||||||
ListOptions: flags.GetListOptions(),
|
ListOptions: flags.GetListOptions(cmd),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -68,6 +72,5 @@ func RunBranchesList(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
print.BranchesList(branches, protections, ctx.Output, fields)
|
return print.BranchesList(branches, protections, ctx.Output, fields)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-8
@@ -7,10 +7,10 @@ import (
|
|||||||
stdctx "context"
|
stdctx "context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
gitea "gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -44,9 +44,14 @@ var CmdBranchesUnprotect = cli.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RunBranchesProtect function to protect/unprotect a list of branches
|
// RunBranchesProtect function to protect/unprotect a list of branches
|
||||||
func RunBranchesProtect(_ stdctx.Context, cmd *cli.Command) error {
|
func RunBranchesProtect(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if !cmd.Args().Present() {
|
if !cmd.Args().Present() {
|
||||||
return fmt.Errorf("must specify at least one branch")
|
return fmt.Errorf("must specify at least one branch")
|
||||||
@@ -62,7 +67,7 @@ func RunBranchesProtect(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
var err error
|
var err error
|
||||||
command := ctx.Command.Name
|
command := ctx.Command.Name
|
||||||
if command == "protect" {
|
if command == "protect" {
|
||||||
_, _, err = ctx.Login.Client().CreateBranchProtection(owner, ctx.Repo, gitea.CreateBranchProtectionOption{
|
_, _, err = ctx.Login.Client().Repositories.CreateBranchProtection(requestCtx, owner, ctx.Repo, gitea.CreateBranchProtectionOption{
|
||||||
BranchName: branch,
|
BranchName: branch,
|
||||||
RuleName: "",
|
RuleName: "",
|
||||||
EnablePush: false,
|
EnablePush: false,
|
||||||
@@ -88,7 +93,7 @@ func RunBranchesProtect(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
UnprotectedFilePatterns: "",
|
UnprotectedFilePatterns: "",
|
||||||
})
|
})
|
||||||
} else if command == "unprotect" {
|
} else if command == "unprotect" {
|
||||||
_, err = ctx.Login.Client().DeleteBranchProtection(owner, ctx.Repo, branch)
|
_, err = ctx.Login.Client().Repositories.DeleteBranchProtection(requestCtx, owner, ctx.Repo, branch)
|
||||||
} else {
|
} else {
|
||||||
return fmt.Errorf("command %s is not supported", command)
|
return fmt.Errorf("command %s is not supported", command)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,78 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package branches
|
||||||
|
|
||||||
|
import (
|
||||||
|
stdctx "context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
gitea "gitea.dev/sdk"
|
||||||
|
|
||||||
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"github.com/urfave/cli/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CmdBranchesRenameFlags Flags for command rename
|
||||||
|
var CmdBranchesRenameFlags = append([]cli.Flag{
|
||||||
|
branchFieldsFlag,
|
||||||
|
&flags.PaginationPageFlag,
|
||||||
|
&flags.PaginationLimitFlag,
|
||||||
|
}, flags.AllDefaultFlags...)
|
||||||
|
|
||||||
|
// CmdBranchesRename represents a sub command of branches to rename a branch
|
||||||
|
var CmdBranchesRename = cli.Command{
|
||||||
|
Name: "rename",
|
||||||
|
Aliases: []string{"rn"},
|
||||||
|
Usage: "Rename a branch",
|
||||||
|
Description: `Rename a branch in a repository`,
|
||||||
|
ArgsUsage: "<old_branch_name> <new_branch_name>",
|
||||||
|
Action: RunBranchesRename,
|
||||||
|
Flags: CmdBranchesRenameFlags,
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunBranchesRename function to rename a branch
|
||||||
|
func RunBranchesRename(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
|
ctx, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := ValidateRenameArgs(ctx.Args().Slice()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
oldBranchName := ctx.Args().Get(0)
|
||||||
|
newBranchName := ctx.Args().Get(1)
|
||||||
|
|
||||||
|
owner := ctx.Owner
|
||||||
|
if ctx.IsSet("owner") {
|
||||||
|
owner = ctx.String("owner")
|
||||||
|
}
|
||||||
|
|
||||||
|
successful, _, err := ctx.Login.Client().Repositories.RenameRepoBranch(requestCtx, owner, ctx.Repo, oldBranchName, gitea.RenameRepoBranchOption{
|
||||||
|
Name: newBranchName,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to rename branch: %w", err)
|
||||||
|
}
|
||||||
|
if !successful {
|
||||||
|
return fmt.Errorf("failed to rename branch")
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Successfully renamed branch '%s' to '%s'\n", oldBranchName, newBranchName)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateRenameArgs validates arguments for the rename command
|
||||||
|
func ValidateRenameArgs(args []string) error {
|
||||||
|
if len(args) != 2 {
|
||||||
|
return fmt.Errorf("must specify exactly two arguments: <old_branch_name> <new_branch_name>")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package branches
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBranchesRenameArgs(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args []string
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "valid args",
|
||||||
|
args: []string{"main", "develop"},
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "missing both args",
|
||||||
|
args: []string{},
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "missing new branch name",
|
||||||
|
args: []string{"main"},
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "too many args",
|
||||||
|
args: []string{"main", "develop", "extra"},
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
err := ValidateRenameArgs(tt.args)
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("ValidateRenameArgs() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
+19
-11
@@ -7,14 +7,14 @@ import (
|
|||||||
stdctx "context"
|
stdctx "context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
"gitea.dev/tea/cmd/flags"
|
||||||
"code.gitea.io/tea/modules/config"
|
"gitea.dev/tea/modules/config"
|
||||||
"code.gitea.io/tea/modules/context"
|
"gitea.dev/tea/modules/context"
|
||||||
"code.gitea.io/tea/modules/debug"
|
"gitea.dev/tea/modules/debug"
|
||||||
"code.gitea.io/tea/modules/git"
|
"gitea.dev/tea/modules/git"
|
||||||
"code.gitea.io/tea/modules/interact"
|
"gitea.dev/tea/modules/interact"
|
||||||
"code.gitea.io/tea/modules/task"
|
"gitea.dev/tea/modules/task"
|
||||||
"code.gitea.io/tea/modules/utils"
|
"gitea.dev/tea/modules/utils"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
@@ -48,7 +48,10 @@ When a host is specified in the repo-slug, it will override the login specified
|
|||||||
}
|
}
|
||||||
|
|
||||||
func runRepoClone(ctx stdctx.Context, cmd *cli.Command) error {
|
func runRepoClone(ctx stdctx.Context, cmd *cli.Command) error {
|
||||||
teaCmd := context.InitCommand(cmd)
|
teaCmd, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
args := teaCmd.Args()
|
args := teaCmd.Args()
|
||||||
if args.Len() < 1 {
|
if args.Len() < 1 {
|
||||||
@@ -73,14 +76,19 @@ func runRepoClone(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
|
|
||||||
owner, repo = utils.GetOwnerAndRepo(url.Path, login.User)
|
owner, repo = utils.GetOwnerAndRepo(url.Path, login.User)
|
||||||
if url.Host != "" {
|
if url.Host != "" {
|
||||||
login = config.GetLoginByHost(url.Host)
|
var lookupErr error
|
||||||
|
login, lookupErr = config.GetLoginByHost(url.Host)
|
||||||
|
if lookupErr != nil {
|
||||||
|
return lookupErr
|
||||||
|
}
|
||||||
if login == nil {
|
if login == nil {
|
||||||
return fmt.Errorf("No login configured matching host '%s', run `tea login add` first", url.Host)
|
return fmt.Errorf("no login configured matching host '%s', run 'tea login add' first", url.Host)
|
||||||
}
|
}
|
||||||
debug.Printf("Matched login '%s' for host '%s'", login.Name, url.Host)
|
debug.Printf("Matched login '%s' for host '%s'", login.Name, url.Host)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = task.RepoClone(
|
_, err = task.RepoClone(
|
||||||
|
ctx,
|
||||||
dir,
|
dir,
|
||||||
login,
|
login,
|
||||||
owner,
|
owner,
|
||||||
|
|||||||
+7
-3
@@ -2,13 +2,14 @@
|
|||||||
// SPDX-License-Identifier: MIT
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
// Tea is command line tool for Gitea.
|
// Tea is command line tool for Gitea.
|
||||||
package cmd // import "code.gitea.io/tea"
|
package cmd // import "gitea.dev/tea"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"code.gitea.io/tea/modules/version"
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
|
|
||||||
|
"gitea.dev/tea/modules/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
// App creates and returns a tea Command with all subcommands set
|
// App creates and returns a tea Command with all subcommands set
|
||||||
@@ -38,13 +39,16 @@ func App() *cli.Command {
|
|||||||
&CmdRepos,
|
&CmdRepos,
|
||||||
&CmdBranches,
|
&CmdBranches,
|
||||||
&CmdActions,
|
&CmdActions,
|
||||||
|
&CmdWiki,
|
||||||
&CmdWebhooks,
|
&CmdWebhooks,
|
||||||
&CmdAddComment,
|
&CmdComments,
|
||||||
|
|
||||||
&CmdOpen,
|
&CmdOpen,
|
||||||
&CmdNotifications,
|
&CmdNotifications,
|
||||||
&CmdRepoClone,
|
&CmdRepoClone,
|
||||||
|
|
||||||
|
&CmdSSHKeys,
|
||||||
|
|
||||||
&CmdAdmin,
|
&CmdAdmin,
|
||||||
|
|
||||||
&CmdApi,
|
&CmdApi,
|
||||||
|
|||||||
+18
-77
@@ -4,87 +4,28 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
stdctx "context"
|
"gitea.dev/tea/cmd/comments"
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
|
||||||
"code.gitea.io/tea/modules/config"
|
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
"code.gitea.io/tea/modules/interact"
|
|
||||||
"code.gitea.io/tea/modules/print"
|
|
||||||
"code.gitea.io/tea/modules/theme"
|
|
||||||
"code.gitea.io/tea/modules/utils"
|
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
|
||||||
"github.com/charmbracelet/huh"
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CmdAddComment is the main command to operate with notifications
|
// CmdComments is the top-level command for managing comments on issues and pull requests.
|
||||||
var CmdAddComment = cli.Command{
|
var CmdComments = cli.Command{
|
||||||
Name: "comment",
|
Name: "comments",
|
||||||
Aliases: []string{"c"},
|
Aliases: []string{"comment", "c"},
|
||||||
Category: catEntities,
|
Category: catEntities,
|
||||||
Usage: "Add a comment to an issue / pr",
|
Usage: "Manage comments on issues and pull requests",
|
||||||
Description: "Add a comment to an issue / pr",
|
Description: `Manage comments on issues and pull requests.
|
||||||
|
|
||||||
|
When invoked with an issue/PR index and an optional body, behaves like 'tea comments add'
|
||||||
|
(this preserves the historical 'tea comment <idx> "<body>"' shorthand).`,
|
||||||
ArgsUsage: "<issue / pr index> [<comment body>]",
|
ArgsUsage: "<issue / pr index> [<comment body>]",
|
||||||
Action: runAddComment,
|
Action: comments.RunCommentsAdd,
|
||||||
Flags: flags.AllDefaultFlags,
|
Flags: comments.CmdCommentsAdd.Flags,
|
||||||
}
|
Commands: []*cli.Command{
|
||||||
|
&comments.CmdCommentsAdd,
|
||||||
func runAddComment(_ stdctx.Context, cmd *cli.Command) error {
|
&comments.CmdCommentsList,
|
||||||
ctx := context.InitCommand(cmd)
|
&comments.CmdCommentsEdit,
|
||||||
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
&comments.CmdCommentsDelete,
|
||||||
|
},
|
||||||
args := ctx.Args()
|
|
||||||
if args.Len() == 0 {
|
|
||||||
return fmt.Errorf("Please specify issue / pr index")
|
|
||||||
}
|
|
||||||
|
|
||||||
idx, err := utils.ArgToIndex(ctx.Args().First())
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
body := strings.Join(ctx.Args().Tail(), " ")
|
|
||||||
if interact.IsStdinPiped() {
|
|
||||||
// custom solution until https://github.com/AlecAivazis/survey/issues/328 is fixed
|
|
||||||
if bodyStdin, err := io.ReadAll(ctx.Reader); err != nil {
|
|
||||||
return err
|
|
||||||
} else if len(bodyStdin) != 0 {
|
|
||||||
body = strings.Join([]string{body, string(bodyStdin)}, "\n\n")
|
|
||||||
}
|
|
||||||
} else if len(body) == 0 {
|
|
||||||
if err := huh.NewForm(
|
|
||||||
huh.NewGroup(
|
|
||||||
huh.NewText().
|
|
||||||
Title("Comment(markdown):").
|
|
||||||
ExternalEditor(config.GetPreferences().Editor).
|
|
||||||
EditorExtension("md").
|
|
||||||
Value(&body),
|
|
||||||
),
|
|
||||||
).WithTheme(theme.GetTheme()).
|
|
||||||
Run(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(body) == 0 {
|
|
||||||
return errors.New("no comment content provided")
|
|
||||||
}
|
|
||||||
|
|
||||||
client := ctx.Login.Client()
|
|
||||||
comment, _, err := client.CreateIssueComment(ctx.Owner, ctx.Repo, idx, gitea.CreateIssueCommentOption{
|
|
||||||
Body: body,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
print.Comment(comment)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,97 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package comments
|
||||||
|
|
||||||
|
import (
|
||||||
|
stdctx "context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
gitea "gitea.dev/sdk"
|
||||||
|
|
||||||
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/config"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/interact"
|
||||||
|
"gitea.dev/tea/modules/print"
|
||||||
|
"gitea.dev/tea/modules/theme"
|
||||||
|
"gitea.dev/tea/modules/utils"
|
||||||
|
|
||||||
|
"charm.land/huh/v2"
|
||||||
|
"github.com/urfave/cli/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CmdCommentsAdd adds a comment to an issue or pull request.
|
||||||
|
var CmdCommentsAdd = cli.Command{
|
||||||
|
Name: "add",
|
||||||
|
Aliases: []string{"a"},
|
||||||
|
Usage: "Add a comment to an issue or pull request",
|
||||||
|
Description: "Add a comment to an issue or pull request.",
|
||||||
|
ArgsUsage: "<issue / pr index> [<comment body>]",
|
||||||
|
Action: RunCommentsAdd,
|
||||||
|
Flags: flags.AllDefaultFlags,
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunCommentsAdd creates a new comment.
|
||||||
|
func RunCommentsAdd(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
|
ctx, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if ctx.Args().Len() == 0 {
|
||||||
|
return fmt.Errorf("please specify issue / pr index")
|
||||||
|
}
|
||||||
|
|
||||||
|
idx, err := utils.ArgToIndex(ctx.Args().First())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
body := strings.Join(ctx.Args().Tail(), " ")
|
||||||
|
// Only consume stdin if no positional body was given. interact.IsStdinPiped()
|
||||||
|
// is true for any non-TTY stdin (CI, subshells, agent harnesses) — not just
|
||||||
|
// piped data — so reading unconditionally would block forever in those
|
||||||
|
// contexts when the body is supplied via args.
|
||||||
|
if len(body) == 0 && interact.IsStdinPiped() {
|
||||||
|
if bodyStdin, err := io.ReadAll(ctx.Reader); err != nil {
|
||||||
|
return err
|
||||||
|
} else if len(bodyStdin) != 0 {
|
||||||
|
body = string(bodyStdin)
|
||||||
|
}
|
||||||
|
} else if len(body) == 0 {
|
||||||
|
if err := huh.NewForm(
|
||||||
|
huh.NewGroup(
|
||||||
|
huh.NewText().
|
||||||
|
Title("Comment(markdown):").
|
||||||
|
ExternalEditor(config.GetPreferences().Editor).
|
||||||
|
EditorExtension("md").
|
||||||
|
Value(&body),
|
||||||
|
),
|
||||||
|
).WithTheme(theme.GetTheme()).
|
||||||
|
Run(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(body) == 0 {
|
||||||
|
return errors.New("no comment content provided")
|
||||||
|
}
|
||||||
|
|
||||||
|
client := ctx.Login.Client()
|
||||||
|
comment, _, err := client.Issues.CreateIssueComment(requestCtx, ctx.Owner, ctx.Repo, idx, gitea.CreateIssueCommentOption{
|
||||||
|
Body: body,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
print.Comment(comment)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package comments
|
||||||
|
|
||||||
|
import (
|
||||||
|
stdctx "context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/utils"
|
||||||
|
|
||||||
|
"github.com/urfave/cli/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CmdCommentsDelete deletes one or more comments by ID.
|
||||||
|
var CmdCommentsDelete = cli.Command{
|
||||||
|
Name: "delete",
|
||||||
|
Aliases: []string{"rm"},
|
||||||
|
Usage: "Delete one or more comments by ID",
|
||||||
|
Description: "Delete one or more comments by their comment ID. Use 'tea comments list <issue>' to find IDs.",
|
||||||
|
ArgsUsage: "<comment id> [<comment id>...]",
|
||||||
|
Action: RunCommentsDelete,
|
||||||
|
Flags: flags.AllDefaultFlags,
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunCommentsDelete removes one or more comments.
|
||||||
|
func RunCommentsDelete(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
|
ctx, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if ctx.Args().Len() == 0 {
|
||||||
|
return fmt.Errorf("please specify at least one comment id")
|
||||||
|
}
|
||||||
|
|
||||||
|
ids, err := utils.ArgsToIndices(ctx.Args().Slice())
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("invalid comment id: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
client := ctx.Login.Client()
|
||||||
|
for _, id := range ids {
|
||||||
|
if _, err := client.Issues.DeleteIssueComment(requestCtx, ctx.Owner, ctx.Repo, id); err != nil {
|
||||||
|
return fmt.Errorf("could not delete comment %d: %s", id, err)
|
||||||
|
}
|
||||||
|
fmt.Printf("Deleted comment %d\n", id)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,102 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package comments
|
||||||
|
|
||||||
|
import (
|
||||||
|
stdctx "context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
gitea "gitea.dev/sdk"
|
||||||
|
|
||||||
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/config"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/interact"
|
||||||
|
"gitea.dev/tea/modules/print"
|
||||||
|
"gitea.dev/tea/modules/theme"
|
||||||
|
"gitea.dev/tea/modules/utils"
|
||||||
|
|
||||||
|
"charm.land/huh/v2"
|
||||||
|
"github.com/urfave/cli/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CmdCommentsEdit edits an existing comment.
|
||||||
|
var CmdCommentsEdit = cli.Command{
|
||||||
|
Name: "edit",
|
||||||
|
Aliases: []string{"e"},
|
||||||
|
Usage: "Edit the body of an existing comment",
|
||||||
|
Description: `Edit the body of an existing comment by its comment ID. Use 'tea comments list <issue>' to find IDs.
|
||||||
|
|
||||||
|
The new body can be supplied as a positional argument, piped on stdin, or (if neither is given and stdin is a terminal) entered in your $EDITOR.`,
|
||||||
|
ArgsUsage: "<comment id> [<new body>]",
|
||||||
|
Action: RunCommentsEdit,
|
||||||
|
Flags: flags.AllDefaultFlags,
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunCommentsEdit updates the body of an existing comment.
|
||||||
|
func RunCommentsEdit(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
|
ctx, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if ctx.Args().Len() == 0 {
|
||||||
|
return fmt.Errorf("please specify comment id")
|
||||||
|
}
|
||||||
|
|
||||||
|
id, err := utils.ArgToIndex(ctx.Args().First())
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("invalid comment id %q: %s", ctx.Args().First(), err)
|
||||||
|
}
|
||||||
|
|
||||||
|
body := strings.Join(ctx.Args().Tail(), " ")
|
||||||
|
if len(body) == 0 && interact.IsStdinPiped() {
|
||||||
|
if bodyStdin, err := io.ReadAll(ctx.Reader); err != nil {
|
||||||
|
return err
|
||||||
|
} else if len(bodyStdin) != 0 {
|
||||||
|
body = string(bodyStdin)
|
||||||
|
}
|
||||||
|
} else if len(body) == 0 {
|
||||||
|
// Fetch current body to pre-populate the editor.
|
||||||
|
client := ctx.Login.Client()
|
||||||
|
current, _, fetchErr := client.Issues.GetIssueComment(requestCtx, ctx.Owner, ctx.Repo, id)
|
||||||
|
if fetchErr != nil {
|
||||||
|
return fmt.Errorf("could not fetch comment %d: %s", id, fetchErr)
|
||||||
|
}
|
||||||
|
body = current.Body
|
||||||
|
if err := huh.NewForm(
|
||||||
|
huh.NewGroup(
|
||||||
|
huh.NewText().
|
||||||
|
Title("Comment(markdown):").
|
||||||
|
ExternalEditor(config.GetPreferences().Editor).
|
||||||
|
EditorExtension("md").
|
||||||
|
Value(&body),
|
||||||
|
),
|
||||||
|
).WithTheme(theme.GetTheme()).
|
||||||
|
Run(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(body) == 0 {
|
||||||
|
return errors.New("no comment content provided")
|
||||||
|
}
|
||||||
|
|
||||||
|
client := ctx.Login.Client()
|
||||||
|
comment, _, err := client.Issues.EditIssueComment(requestCtx, ctx.Owner, ctx.Repo, id, gitea.EditIssueCommentOption{
|
||||||
|
Body: body,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
print.Comment(comment)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package comments
|
||||||
|
|
||||||
|
import (
|
||||||
|
stdctx "context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
gitea "gitea.dev/sdk"
|
||||||
|
|
||||||
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/print"
|
||||||
|
"gitea.dev/tea/modules/utils"
|
||||||
|
|
||||||
|
"github.com/urfave/cli/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CmdCommentsList lists comments on an issue or pull request.
|
||||||
|
var CmdCommentsList = cli.Command{
|
||||||
|
Name: "list",
|
||||||
|
Aliases: []string{"ls"},
|
||||||
|
Usage: "List comments on an issue or pull request",
|
||||||
|
Description: "List comments on an issue or pull request. Comment IDs returned here are the IDs accepted by 'tea comments edit' and 'tea comments delete'.",
|
||||||
|
ArgsUsage: "<issue / pr index>",
|
||||||
|
Action: RunCommentsList,
|
||||||
|
Flags: append([]cli.Flag{
|
||||||
|
&flags.PaginationPageFlag,
|
||||||
|
&flags.PaginationLimitFlag,
|
||||||
|
}, flags.AllDefaultFlags...),
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunCommentsList lists comments on the given issue/PR.
|
||||||
|
func RunCommentsList(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
|
ctx, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if ctx.Args().Len() == 0 {
|
||||||
|
return fmt.Errorf("please specify issue / pr index")
|
||||||
|
}
|
||||||
|
|
||||||
|
idx, err := utils.ArgToIndex(ctx.Args().First())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
client := ctx.Login.Client()
|
||||||
|
comments, _, err := client.Issues.ListIssueComments(requestCtx, ctx.Owner, ctx.Repo, idx, gitea.ListIssueCommentOptions{
|
||||||
|
ListOptions: flags.GetListOptions(cmd),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return print.CommentsList(comments, ctx.Output)
|
||||||
|
}
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitea.dev/sdk"
|
||||||
|
)
|
||||||
|
|
||||||
|
type detailLabelData struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Color string `json:"color"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type detailCommentData struct {
|
||||||
|
ID int64 `json:"id"`
|
||||||
|
Author string `json:"author"`
|
||||||
|
Created time.Time `json:"created"`
|
||||||
|
Body string `json:"body"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type detailReviewData struct {
|
||||||
|
ID int64 `json:"id"`
|
||||||
|
Reviewer string `json:"reviewer"`
|
||||||
|
State gitea.ReviewStateType `json:"state"`
|
||||||
|
Body string `json:"body"`
|
||||||
|
Created time.Time `json:"created"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildDetailLabels(labels []*gitea.Label) []detailLabelData {
|
||||||
|
labelSlice := make([]detailLabelData, 0, len(labels))
|
||||||
|
for _, label := range labels {
|
||||||
|
labelSlice = append(labelSlice, detailLabelData{
|
||||||
|
Name: label.Name,
|
||||||
|
Color: label.Color,
|
||||||
|
Description: label.Description,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return labelSlice
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildDetailAssignees(assignees []*gitea.User) []string {
|
||||||
|
assigneeSlice := make([]string, 0, len(assignees))
|
||||||
|
for _, assignee := range assignees {
|
||||||
|
assigneeSlice = append(assigneeSlice, username(assignee))
|
||||||
|
}
|
||||||
|
return assigneeSlice
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildDetailComments(comments []*gitea.Comment) []detailCommentData {
|
||||||
|
commentSlice := make([]detailCommentData, 0, len(comments))
|
||||||
|
for _, comment := range comments {
|
||||||
|
commentSlice = append(commentSlice, detailCommentData{
|
||||||
|
ID: comment.ID,
|
||||||
|
Author: username(comment.Poster),
|
||||||
|
Body: comment.Body,
|
||||||
|
Created: comment.Created,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return commentSlice
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildDetailReviews(reviews []*gitea.PullReview) []detailReviewData {
|
||||||
|
reviewSlice := make([]detailReviewData, 0, len(reviews))
|
||||||
|
for _, review := range reviews {
|
||||||
|
reviewSlice = append(reviewSlice, detailReviewData{
|
||||||
|
ID: review.ID,
|
||||||
|
Reviewer: username(review.Reviewer),
|
||||||
|
State: review.State,
|
||||||
|
Body: review.Body,
|
||||||
|
Created: review.Submitted,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return reviewSlice
|
||||||
|
}
|
||||||
|
|
||||||
|
func username(user *gitea.User) string {
|
||||||
|
if user == nil {
|
||||||
|
return "ghost"
|
||||||
|
}
|
||||||
|
return user.UserName
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeIndentedJSON(w io.Writer, data any) error {
|
||||||
|
encoder := json.NewEncoder(w)
|
||||||
|
encoder.SetIndent("", "\t")
|
||||||
|
return encoder.Encode(data)
|
||||||
|
}
|
||||||
@@ -7,8 +7,9 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"code.gitea.io/tea/modules/utils"
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
|
|
||||||
|
"gitea.dev/tea/modules/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CsvFlag is a wrapper around cli.StringFlag, with an added GetValues() method
|
// CsvFlag is a wrapper around cli.StringFlag, with an added GetValues() method
|
||||||
@@ -44,7 +45,7 @@ func (f CsvFlag) GetValues(cmd *cli.Command) ([]string, error) {
|
|||||||
if f.AvailableFields != nil && val != "" {
|
if f.AvailableFields != nil && val != "" {
|
||||||
for _, field := range selection {
|
for _, field := range selection {
|
||||||
if !utils.Contains(f.AvailableFields, field) {
|
if !utils.Contains(f.AvailableFields, field) {
|
||||||
return nil, fmt.Errorf("Invalid field '%s'", field)
|
return nil, fmt.Errorf("invalid field '%s'", field)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+27
-11
@@ -5,8 +5,9 @@ package flags
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
gitea "gitea.dev/sdk"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -39,16 +40,33 @@ var OutputFlag = cli.StringFlag{
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
paging gitea.ListOptions
|
|
||||||
// ErrPage indicates that the provided page value is invalid (less than -1 or equal to 0).
|
// ErrPage indicates that the provided page value is invalid (less than -1 or equal to 0).
|
||||||
ErrPage = errors.New("page cannot be smaller than 1")
|
ErrPage = errors.New("page cannot be smaller than 1")
|
||||||
// ErrLimit indicates that the provided limit value is invalid (negative).
|
// ErrLimit indicates that the provided limit value is invalid (negative).
|
||||||
ErrLimit = errors.New("limit cannot be negative")
|
ErrLimit = errors.New("limit cannot be negative")
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetListOptions returns configured paging struct
|
const (
|
||||||
func GetListOptions() gitea.ListOptions {
|
defaultPageValue = 1
|
||||||
return paging
|
defaultLimitValue = 30
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetListOptions returns list options derived from the active command.
|
||||||
|
func GetListOptions(cmd *cli.Command) gitea.ListOptions {
|
||||||
|
page := cmd.Int("page")
|
||||||
|
if page == 0 {
|
||||||
|
page = defaultPageValue
|
||||||
|
}
|
||||||
|
|
||||||
|
pageSize := cmd.Int("limit")
|
||||||
|
if pageSize == 0 {
|
||||||
|
pageSize = defaultLimitValue
|
||||||
|
}
|
||||||
|
|
||||||
|
return gitea.ListOptions{
|
||||||
|
Page: page,
|
||||||
|
PageSize: pageSize,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// PaginationFlags provides all pagination related flags
|
// PaginationFlags provides all pagination related flags
|
||||||
@@ -62,14 +80,13 @@ var PaginationPageFlag = cli.IntFlag{
|
|||||||
Name: "page",
|
Name: "page",
|
||||||
Aliases: []string{"p"},
|
Aliases: []string{"p"},
|
||||||
Usage: "specify page",
|
Usage: "specify page",
|
||||||
Value: 1,
|
Value: defaultPageValue,
|
||||||
Validator: func(i int) error {
|
Validator: func(i int) error {
|
||||||
if i < 1 && i != -1 {
|
if i < 1 && i != -1 {
|
||||||
return ErrPage
|
return ErrPage
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Destination: &paging.Page,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// PaginationLimitFlag provides flag for pagination options
|
// PaginationLimitFlag provides flag for pagination options
|
||||||
@@ -77,14 +94,13 @@ var PaginationLimitFlag = cli.IntFlag{
|
|||||||
Name: "limit",
|
Name: "limit",
|
||||||
Aliases: []string{"lm"},
|
Aliases: []string{"lm"},
|
||||||
Usage: "specify limit of items per page",
|
Usage: "specify limit of items per page",
|
||||||
Value: 30,
|
Value: defaultLimitValue,
|
||||||
Validator: func(i int) error {
|
Validator: func(i int) error {
|
||||||
if i < 0 {
|
if i < 0 {
|
||||||
return ErrLimit
|
return ErrLimit
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Destination: &paging.PageSize,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoginOutputFlags defines login and output flags that should
|
// LoginOutputFlags defines login and output flags that should
|
||||||
@@ -152,7 +168,7 @@ func ParseState(stateStr string) (gitea.StateType, error) {
|
|||||||
case "closed":
|
case "closed":
|
||||||
return gitea.StateClosed, nil
|
return gitea.StateClosed, nil
|
||||||
default:
|
default:
|
||||||
return "", errors.New("unknown state '" + stateStr + "'")
|
return "", fmt.Errorf("unknown state '%s'", stateStr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,6 +185,6 @@ func ParseIssueKind(kindStr string, defaultKind gitea.IssueType) (gitea.IssueTyp
|
|||||||
case "pull", "pulls", "pr":
|
case "pull", "pulls", "pr":
|
||||||
return gitea.IssueTypePull, nil
|
return gitea.IssueTypePull, nil
|
||||||
default:
|
default:
|
||||||
return "", errors.New("unknown kind '" + kindStr + "'")
|
return "", fmt.Errorf("unknown kind '%s'", kindStr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
gitea "gitea.dev/sdk"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
@@ -74,7 +75,7 @@ func TestPaginationFlags(t *testing.T) {
|
|||||||
},
|
},
|
||||||
Flags: PaginationFlags,
|
Flags: PaginationFlags,
|
||||||
}
|
}
|
||||||
err := cmd.Run(context.Background(), tc.args)
|
err := cmd.Run(t.Context(), tc.args)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -117,9 +118,35 @@ func TestPaginationFailures(t *testing.T) {
|
|||||||
ErrWriter: io.Discard,
|
ErrWriter: io.Discard,
|
||||||
}
|
}
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
err := cmd.Run(context.Background(), tc.args)
|
err := cmd.Run(t.Context(), tc.args)
|
||||||
require.ErrorContains(t, err, tc.expectedError.Error())
|
require.ErrorContains(t, err, tc.expectedError.Error())
|
||||||
// require.ErrorIs(t, err, tc.expectedError)
|
// require.ErrorIs(t, err, tc.expectedError)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetListOptionsDoesNotLeakBetweenCommands(t *testing.T) {
|
||||||
|
var results []gitea.ListOptions
|
||||||
|
|
||||||
|
run := func(args []string) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
cmd := cli.Command{
|
||||||
|
Name: "test-paging",
|
||||||
|
Action: func(_ context.Context, cmd *cli.Command) error {
|
||||||
|
results = append(results, GetListOptions(cmd))
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
Flags: PaginationFlags,
|
||||||
|
}
|
||||||
|
|
||||||
|
require.NoError(t, cmd.Run(t.Context(), args))
|
||||||
|
}
|
||||||
|
|
||||||
|
run([]string{"test", "--page", "5", "--limit", "10"})
|
||||||
|
run([]string{"test"})
|
||||||
|
|
||||||
|
require.Len(t, results, 2)
|
||||||
|
assert.Equal(t, gitea.ListOptions{Page: 5, PageSize: 10}, results[0])
|
||||||
|
assert.Equal(t, gitea.ListOptions{Page: defaultPageValue, PageSize: defaultLimitValue}, results[1])
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,13 +4,15 @@
|
|||||||
package flags
|
package flags
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
stdctx "context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
gitea "gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
"code.gitea.io/tea/modules/task"
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/task"
|
||||||
|
|
||||||
"github.com/araddon/dateparse"
|
"github.com/araddon/dateparse"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
@@ -130,7 +132,7 @@ var IssuePRCreateFlags = append([]cli.Flag{
|
|||||||
}, issuePRFlags...)
|
}, issuePRFlags...)
|
||||||
|
|
||||||
// GetIssuePRCreateFlags parses all IssuePREditFlags
|
// GetIssuePRCreateFlags parses all IssuePREditFlags
|
||||||
func GetIssuePRCreateFlags(ctx *context.TeaContext) (*gitea.CreateIssueOption, error) {
|
func GetIssuePRCreateFlags(requestCtx stdctx.Context, ctx *context.TeaContext) (*gitea.CreateIssueOption, error) {
|
||||||
opts := gitea.CreateIssueOption{
|
opts := gitea.CreateIssueOption{
|
||||||
Title: ctx.String("title"),
|
Title: ctx.String("title"),
|
||||||
Body: ctx.String("description"),
|
Body: ctx.String("description"),
|
||||||
@@ -154,7 +156,7 @@ func GetIssuePRCreateFlags(ctx *context.TeaContext) (*gitea.CreateIssueOption, e
|
|||||||
if client == nil {
|
if client == nil {
|
||||||
client = ctx.Login.Client()
|
client = ctx.Login.Client()
|
||||||
}
|
}
|
||||||
if opts.Labels, err = task.ResolveLabelNames(client, ctx.Owner, ctx.Repo, labelNames); err != nil {
|
if opts.Labels, err = task.ResolveLabelNames(requestCtx, client, ctx.Owner, ctx.Repo, labelNames); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -163,9 +165,9 @@ func GetIssuePRCreateFlags(ctx *context.TeaContext) (*gitea.CreateIssueOption, e
|
|||||||
if client == nil {
|
if client == nil {
|
||||||
client = ctx.Login.Client()
|
client = ctx.Login.Client()
|
||||||
}
|
}
|
||||||
ms, _, err := client.GetMilestoneByName(ctx.Owner, ctx.Repo, milestoneName)
|
ms, _, err := client.Repositories.GetMilestoneByName(requestCtx, ctx.Owner, ctx.Repo, milestoneName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Milestone '%s' not found", milestoneName)
|
return nil, fmt.Errorf("milestone '%s' not found", milestoneName)
|
||||||
}
|
}
|
||||||
opts.Milestone = ms.ID
|
opts.Milestone = ms.ID
|
||||||
}
|
}
|
||||||
|
|||||||
+66
-70
@@ -5,26 +5,22 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
stdctx "context"
|
stdctx "context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
gitea "gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/cmd/flags"
|
|
||||||
"code.gitea.io/tea/cmd/issues"
|
"gitea.dev/tea/cmd/flags"
|
||||||
"code.gitea.io/tea/modules/context"
|
"gitea.dev/tea/cmd/issues"
|
||||||
"code.gitea.io/tea/modules/interact"
|
"gitea.dev/tea/modules/context"
|
||||||
"code.gitea.io/tea/modules/print"
|
"gitea.dev/tea/modules/interact"
|
||||||
"code.gitea.io/tea/modules/utils"
|
"gitea.dev/tea/modules/print"
|
||||||
|
"gitea.dev/tea/modules/utils"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
type labelData struct {
|
type labelData = detailLabelData
|
||||||
Name string `json:"name"`
|
|
||||||
Color string `json:"color"`
|
|
||||||
Description string `json:"description"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type issueData struct {
|
type issueData struct {
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
@@ -41,13 +37,17 @@ type issueData struct {
|
|||||||
Comments []commentData `json:"comments"`
|
Comments []commentData `json:"comments"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type commentData struct {
|
type issueDetailClient interface {
|
||||||
ID int64 `json:"id"`
|
GetIssue(ctx stdctx.Context, owner, repo string, index int64) (*gitea.Issue, *gitea.Response, error)
|
||||||
Author string `json:"author"`
|
ListIssueReactions(ctx stdctx.Context, owner, repo string, index int64, opt gitea.ListIssueReactionsOptions) ([]*gitea.Reaction, *gitea.Response, error)
|
||||||
Created time.Time `json:"created"`
|
|
||||||
Body string `json:"body"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type issueCommentClient interface {
|
||||||
|
ListIssueComments(ctx stdctx.Context, owner, repo string, index int64, opt gitea.ListIssueCommentOptions) ([]*gitea.Comment, *gitea.Response, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type commentData = detailCommentData
|
||||||
|
|
||||||
// CmdIssues represents to login a gitea server.
|
// CmdIssues represents to login a gitea server.
|
||||||
var CmdIssues = cli.Command{
|
var CmdIssues = cli.Command{
|
||||||
Name: "issues",
|
Name: "issues",
|
||||||
@@ -79,23 +79,41 @@ func runIssues(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
return issues.RunIssuesList(ctx, cmd)
|
return issues.RunIssuesList(ctx, cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func runIssueDetail(_ stdctx.Context, cmd *cli.Command, index string) error {
|
func runIssueDetail(requestCtx stdctx.Context, cmd *cli.Command, index string) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, idx, err := resolveIssueDetailContext(cmd, index)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return runIssueDetailWithClient(requestCtx, ctx, idx, ctx.Login.Client().Issues)
|
||||||
|
}
|
||||||
|
|
||||||
|
func resolveIssueDetailContext(cmd *cli.Command, index string) (*context.TeaContext, int64, error) {
|
||||||
|
ctx, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
if ctx.IsSet("owner") {
|
if ctx.IsSet("owner") {
|
||||||
ctx.Owner = ctx.String("owner")
|
ctx.Owner = ctx.String("owner")
|
||||||
}
|
}
|
||||||
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
idx, err := utils.ArgToIndex(index)
|
idx, err := utils.ArgToIndex(index)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
client := ctx.Login.Client()
|
|
||||||
issue, _, err := client.GetIssue(ctx.Owner, ctx.Repo, idx)
|
return ctx, idx, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func runIssueDetailWithClient(requestCtx stdctx.Context, ctx *context.TeaContext, idx int64, client issueDetailClient) error {
|
||||||
|
issue, _, err := client.GetIssue(requestCtx, ctx.Owner, ctx.Repo, idx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
reactions, _, err := client.GetIssueReactions(ctx.Owner, ctx.Repo, idx)
|
reactions, _, err := client.ListIssueReactions(requestCtx, ctx.Owner, ctx.Repo, idx, gitea.ListIssueReactionsOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -103,14 +121,14 @@ func runIssueDetail(_ stdctx.Context, cmd *cli.Command, index string) error {
|
|||||||
if ctx.IsSet("output") {
|
if ctx.IsSet("output") {
|
||||||
switch ctx.String("output") {
|
switch ctx.String("output") {
|
||||||
case "json":
|
case "json":
|
||||||
return runIssueDetailAsJSON(ctx, issue)
|
return runIssueDetailAsJSON(requestCtx, ctx, issue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
print.IssueDetails(issue, reactions)
|
print.IssueDetails(issue, reactions)
|
||||||
|
|
||||||
if issue.Comments > 0 {
|
if issue.Comments > 0 {
|
||||||
err = interact.ShowCommentsMaybeInteractive(ctx, idx, issue.Comments)
|
err = interact.ShowCommentsMaybeInteractive(requestCtx, ctx, idx, issue.Comments)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error loading comments: %v", err)
|
return fmt.Errorf("error loading comments: %v", err)
|
||||||
}
|
}
|
||||||
@@ -119,60 +137,38 @@ func runIssueDetail(_ stdctx.Context, cmd *cli.Command, index string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func runIssueDetailAsJSON(ctx *context.TeaContext, issue *gitea.Issue) error {
|
func runIssueDetailAsJSON(requestCtx stdctx.Context, ctx *context.TeaContext, issue *gitea.Issue) error {
|
||||||
c := ctx.Login.Client()
|
return runIssueDetailAsJSONWithClient(requestCtx, ctx, issue, ctx.Login.Client().Issues)
|
||||||
opts := gitea.ListIssueCommentOptions{ListOptions: flags.GetListOptions()}
|
}
|
||||||
|
|
||||||
labelSlice := make([]labelData, 0, len(issue.Labels))
|
func runIssueDetailAsJSONWithClient(requestCtx stdctx.Context, ctx *context.TeaContext, issue *gitea.Issue, c issueCommentClient) error {
|
||||||
for _, label := range issue.Labels {
|
opts := gitea.ListIssueCommentOptions{ListOptions: flags.GetListOptions(ctx.Command)}
|
||||||
labelSlice = append(labelSlice, labelData{label.Name, label.Color, label.Description})
|
comments := []*gitea.Comment{}
|
||||||
|
|
||||||
|
if ctx.Bool("comments") {
|
||||||
|
var err error
|
||||||
|
comments, _, err = c.ListIssueComments(requestCtx, ctx.Owner, ctx.Repo, issue.Index, opts)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assigneesSlice := make([]string, 0, len(issue.Assignees))
|
return writeIndentedJSON(ctx.Writer, buildIssueData(issue, comments))
|
||||||
for _, assignee := range issue.Assignees {
|
}
|
||||||
assigneesSlice = append(assigneesSlice, assignee.UserName)
|
|
||||||
}
|
|
||||||
|
|
||||||
issueSlice := issueData{
|
func buildIssueData(issue *gitea.Issue, comments []*gitea.Comment) issueData {
|
||||||
|
return issueData{
|
||||||
ID: issue.ID,
|
ID: issue.ID,
|
||||||
Index: issue.Index,
|
Index: issue.Index,
|
||||||
Title: issue.Title,
|
Title: issue.Title,
|
||||||
State: issue.State,
|
State: issue.State,
|
||||||
Created: issue.Created,
|
Created: issue.Created,
|
||||||
User: issue.Poster.UserName,
|
User: username(issue.Poster),
|
||||||
Body: issue.Body,
|
Body: issue.Body,
|
||||||
Labels: labelSlice,
|
Labels: buildDetailLabels(issue.Labels),
|
||||||
Assignees: assigneesSlice,
|
Assignees: buildDetailAssignees(issue.Assignees),
|
||||||
URL: issue.HTMLURL,
|
URL: issue.HTMLURL,
|
||||||
ClosedAt: issue.Closed,
|
ClosedAt: issue.Closed,
|
||||||
Comments: make([]commentData, 0),
|
Comments: buildDetailComments(comments),
|
||||||
}
|
}
|
||||||
|
|
||||||
if ctx.Bool("comments") {
|
|
||||||
comments, _, err := c.ListIssueComments(ctx.Owner, ctx.Repo, issue.Index, opts)
|
|
||||||
issueSlice.Comments = make([]commentData, 0, len(comments))
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, comment := range comments {
|
|
||||||
issueSlice.Comments = append(issueSlice.Comments, commentData{
|
|
||||||
ID: comment.ID,
|
|
||||||
Author: comment.Poster.UserName,
|
|
||||||
Body: comment.Body, // Selected Field
|
|
||||||
Created: comment.Created,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
jsonData, err := json.MarshalIndent(issueSlice, "", "\t")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = fmt.Fprintf(ctx.Writer, "%s\n", jsonData)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-9
@@ -7,12 +7,12 @@ import (
|
|||||||
stdctx "context"
|
stdctx "context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
gitea "gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
"code.gitea.io/tea/modules/print"
|
|
||||||
"code.gitea.io/tea/modules/utils"
|
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/print"
|
||||||
|
"gitea.dev/tea/modules/utils"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -30,9 +30,14 @@ var CmdIssuesClose = cli.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// editIssueState abstracts the arg parsing to edit the given issue
|
// editIssueState abstracts the arg parsing to edit the given issue
|
||||||
func editIssueState(_ stdctx.Context, cmd *cli.Command, opts gitea.EditIssueOption) error {
|
func editIssueState(requestCtx stdctx.Context, cmd *cli.Command, opts gitea.EditIssueOption) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if ctx.Args().Len() == 0 {
|
if ctx.Args().Len() == 0 {
|
||||||
return fmt.Errorf("missing required argument: %s", ctx.Command.ArgsUsage)
|
return fmt.Errorf("missing required argument: %s", ctx.Command.ArgsUsage)
|
||||||
}
|
}
|
||||||
@@ -44,7 +49,7 @@ func editIssueState(_ stdctx.Context, cmd *cli.Command, opts gitea.EditIssueOpti
|
|||||||
|
|
||||||
client := ctx.Login.Client()
|
client := ctx.Login.Client()
|
||||||
for _, index := range indices {
|
for _, index := range indices {
|
||||||
issue, _, err := client.EditIssue(ctx.Owner, ctx.Repo, index, opts)
|
issue, _, err := client.Issues.EditIssue(requestCtx, ctx.Owner, ctx.Repo, index, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-11
@@ -6,10 +6,10 @@ package issues
|
|||||||
import (
|
import (
|
||||||
stdctx "context"
|
stdctx "context"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
"gitea.dev/tea/cmd/flags"
|
||||||
"code.gitea.io/tea/modules/context"
|
"gitea.dev/tea/modules/context"
|
||||||
"code.gitea.io/tea/modules/interact"
|
"gitea.dev/tea/modules/interact"
|
||||||
"code.gitea.io/tea/modules/task"
|
"gitea.dev/tea/modules/task"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
@@ -25,25 +25,29 @@ var CmdIssuesCreate = cli.Command{
|
|||||||
Flags: flags.IssuePRCreateFlags,
|
Flags: flags.IssuePRCreateFlags,
|
||||||
}
|
}
|
||||||
|
|
||||||
func runIssuesCreate(_ stdctx.Context, cmd *cli.Command) error {
|
func runIssuesCreate(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if ctx.IsInteractiveMode() {
|
if ctx.IsInteractiveMode() {
|
||||||
err := interact.CreateIssue(ctx.Login, ctx.Owner, ctx.Repo)
|
err := interact.CreateIssue(requestCtx, ctx.Login, ctx.Owner, ctx.Repo)
|
||||||
if err != nil && !interact.IsQuitting(err) {
|
if err != nil && !interact.IsQuitting(err) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
opts, err := flags.GetIssuePRCreateFlags(ctx)
|
opts, err := flags.GetIssuePRCreateFlags(requestCtx, ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return task.CreateIssue(
|
return task.CreateIssue(requestCtx, ctx.Login,
|
||||||
ctx.Login,
|
|
||||||
ctx.Owner,
|
ctx.Owner,
|
||||||
ctx.Repo,
|
ctx.Repo,
|
||||||
*opts,
|
*opts,
|
||||||
|
|||||||
+17
-11
@@ -8,13 +8,14 @@ import (
|
|||||||
|
|
||||||
stdctx "context"
|
stdctx "context"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
"code.gitea.io/tea/modules/interact"
|
|
||||||
"code.gitea.io/tea/modules/print"
|
|
||||||
"code.gitea.io/tea/modules/task"
|
|
||||||
"code.gitea.io/tea/modules/utils"
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
|
|
||||||
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/interact"
|
||||||
|
"gitea.dev/tea/modules/print"
|
||||||
|
"gitea.dev/tea/modules/task"
|
||||||
|
"gitea.dev/tea/modules/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CmdIssuesEdit is the subcommand of issues to edit issues
|
// CmdIssuesEdit is the subcommand of issues to edit issues
|
||||||
@@ -29,9 +30,14 @@ use an empty string (eg. --milestone "").`,
|
|||||||
Flags: flags.IssuePREditFlags,
|
Flags: flags.IssuePREditFlags,
|
||||||
}
|
}
|
||||||
|
|
||||||
func runIssuesEdit(_ stdctx.Context, cmd *cli.Command) error {
|
func runIssuesEdit(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if !cmd.Args().Present() {
|
if !cmd.Args().Present() {
|
||||||
return fmt.Errorf("must specify at least one issue index")
|
return fmt.Errorf("must specify at least one issue index")
|
||||||
@@ -51,7 +57,7 @@ func runIssuesEdit(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
for _, opts.Index = range indices {
|
for _, opts.Index = range indices {
|
||||||
if ctx.IsInteractiveMode() {
|
if ctx.IsInteractiveMode() {
|
||||||
var err error
|
var err error
|
||||||
opts, err = interact.EditIssue(*ctx, opts.Index)
|
opts, err = interact.EditIssue(requestCtx, *ctx, opts.Index)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if interact.IsQuitting(err) {
|
if interact.IsQuitting(err) {
|
||||||
return nil // user quit
|
return nil // user quit
|
||||||
@@ -60,7 +66,7 @@ func runIssuesEdit(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
issue, err := task.EditIssue(ctx, client, *opts)
|
issue, err := task.EditIssue(requestCtx, ctx, client, *opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
+19
-14
@@ -5,13 +5,14 @@ package issues
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
stdctx "context"
|
stdctx "context"
|
||||||
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
gitea "gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
"code.gitea.io/tea/modules/print"
|
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/print"
|
||||||
"github.com/araddon/dateparse"
|
"github.com/araddon/dateparse"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
@@ -32,8 +33,11 @@ var CmdIssuesList = cli.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RunIssuesList list issues
|
// RunIssuesList list issues
|
||||||
func RunIssuesList(_ stdctx.Context, cmd *cli.Command) error {
|
func RunIssuesList(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
state, err := flags.ParseState(ctx.String("state"))
|
state, err := flags.ParseState(ctx.String("state"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -68,13 +72,13 @@ func RunIssuesList(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
milestones, _ := flags.MilestoneFilterFlag.GetValues(cmd)
|
milestones, _ := flags.MilestoneFilterFlag.GetValues(cmd)
|
||||||
var issues []*gitea.Issue
|
var issues []*gitea.Issue
|
||||||
if ctx.Repo != "" {
|
if ctx.Repo != "" {
|
||||||
issues, _, err = ctx.Login.Client().ListRepoIssues(owner, ctx.Repo, gitea.ListIssueOption{
|
issues, _, err = ctx.Login.Client().Issues.ListRepoIssues(requestCtx, owner, ctx.Repo, gitea.ListIssueOption{
|
||||||
ListOptions: flags.GetListOptions(),
|
ListOptions: flags.GetListOptions(cmd),
|
||||||
State: state,
|
State: state,
|
||||||
Type: kind,
|
Type: kind,
|
||||||
KeyWord: ctx.String("keyword"),
|
KeyWord: ctx.String("keyword"),
|
||||||
CreatedBy: ctx.String("author"),
|
CreatedBy: ctx.String("author"),
|
||||||
AssignedBy: ctx.String("assigned-to"),
|
AssignedBy: ctx.String("assignee"),
|
||||||
MentionedBy: ctx.String("mentions"),
|
MentionedBy: ctx.String("mentions"),
|
||||||
Labels: labels,
|
Labels: labels,
|
||||||
Milestones: milestones,
|
Milestones: milestones,
|
||||||
@@ -85,13 +89,15 @@ func RunIssuesList(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
issues, _, err = ctx.Login.Client().ListIssues(gitea.ListIssueOption{
|
if ctx.IsSet("assignee") {
|
||||||
ListOptions: flags.GetListOptions(),
|
return errors.New("--assignee requires --repo (global issue search does not support assignee filter)")
|
||||||
|
}
|
||||||
|
issues, _, err = ctx.Login.Client().Issues.ListIssues(requestCtx, gitea.ListIssueOption{
|
||||||
|
ListOptions: flags.GetListOptions(cmd),
|
||||||
State: state,
|
State: state,
|
||||||
Type: kind,
|
Type: kind,
|
||||||
KeyWord: ctx.String("keyword"),
|
KeyWord: ctx.String("keyword"),
|
||||||
CreatedBy: ctx.String("author"),
|
CreatedBy: ctx.String("author"),
|
||||||
AssignedBy: ctx.String("assigned-to"),
|
|
||||||
MentionedBy: ctx.String("mentions"),
|
MentionedBy: ctx.String("mentions"),
|
||||||
Labels: labels,
|
Labels: labels,
|
||||||
Milestones: milestones,
|
Milestones: milestones,
|
||||||
@@ -109,6 +115,5 @@ func RunIssuesList(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
print.IssuesPullsList(issues, ctx.Output, fields)
|
return print.IssuesPullsList(issues, ctx.Output, fields)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,9 +6,9 @@ package issues
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
"gitea.dev/sdk"
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"gitea.dev/tea/cmd/flags"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
+75
-56
@@ -8,18 +8,17 @@ import (
|
|||||||
stdctx "context"
|
stdctx "context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
gitea "gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/cmd/flags"
|
|
||||||
"code.gitea.io/tea/modules/config"
|
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
|
|
||||||
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/config"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -27,6 +26,51 @@ const (
|
|||||||
testRepo = "testRepo"
|
testRepo = "testRepo"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type fakeIssueCommentClient struct {
|
||||||
|
owner string
|
||||||
|
repo string
|
||||||
|
index int64
|
||||||
|
comments []*gitea.Comment
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeIssueCommentClient) ListIssueComments(_ stdctx.Context, owner, repo string, index int64, _ gitea.ListIssueCommentOptions) ([]*gitea.Comment, *gitea.Response, error) {
|
||||||
|
f.owner = owner
|
||||||
|
f.repo = repo
|
||||||
|
f.index = index
|
||||||
|
return f.comments, nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type fakeIssueDetailClient struct {
|
||||||
|
owner string
|
||||||
|
repo string
|
||||||
|
index int64
|
||||||
|
issue *gitea.Issue
|
||||||
|
reactions []*gitea.Reaction
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeIssueDetailClient) GetIssue(_ stdctx.Context, owner, repo string, index int64) (*gitea.Issue, *gitea.Response, error) {
|
||||||
|
f.owner = owner
|
||||||
|
f.repo = repo
|
||||||
|
f.index = index
|
||||||
|
return f.issue, nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeIssueDetailClient) ListIssueReactions(_ stdctx.Context, owner, repo string, index int64, _ gitea.ListIssueReactionsOptions) ([]*gitea.Reaction, *gitea.Response, error) {
|
||||||
|
f.owner = owner
|
||||||
|
f.repo = repo
|
||||||
|
f.index = index
|
||||||
|
return f.reactions, nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func toCommentPointers(comments []gitea.Comment) []*gitea.Comment {
|
||||||
|
result := make([]*gitea.Comment, 0, len(comments))
|
||||||
|
for i := range comments {
|
||||||
|
comment := comments[i]
|
||||||
|
result = append(result, &comment)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
func createTestIssue(comments int, isClosed bool) gitea.Issue {
|
func createTestIssue(comments int, isClosed bool) gitea.Issue {
|
||||||
issue := gitea.Issue{
|
issue := gitea.Issue{
|
||||||
ID: 42,
|
ID: 42,
|
||||||
@@ -160,25 +204,11 @@ func TestRunIssueDetailAsJSON(t *testing.T) {
|
|||||||
|
|
||||||
for _, testCase := range testCases {
|
for _, testCase := range testCases {
|
||||||
t.Run(testCase.name, func(t *testing.T) {
|
t.Run(testCase.name, func(t *testing.T) {
|
||||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
client := &fakeIssueCommentClient{
|
||||||
path := r.URL.Path
|
comments: toCommentPointers(testCase.comments),
|
||||||
if path == fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/comments", testOwner, testRepo, testCase.issue.Index) {
|
|
||||||
jsonComments, err := json.Marshal(testCase.comments)
|
|
||||||
if err != nil {
|
|
||||||
require.NoError(t, err, "Testing setup failed: failed to marshal comments")
|
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
_, err = w.Write(jsonComments)
|
|
||||||
require.NoError(t, err, "Testing setup failed: failed to write out comments")
|
|
||||||
} else {
|
|
||||||
http.NotFound(w, r)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
server := httptest.NewServer(handler)
|
testContext.Login.URL = "https://gitea.example.com"
|
||||||
|
|
||||||
testContext.Login.URL = server.URL
|
|
||||||
testCase.issue.HTMLURL = fmt.Sprintf("%s/%s/%s/issues/%d/", testContext.Login.URL, testOwner, testRepo, testCase.issue.Index)
|
testCase.issue.HTMLURL = fmt.Sprintf("%s/%s/%s/issues/%d/", testContext.Login.URL, testOwner, testRepo, testCase.issue.Index)
|
||||||
|
|
||||||
var outBuffer bytes.Buffer
|
var outBuffer bytes.Buffer
|
||||||
@@ -187,16 +217,19 @@ func TestRunIssueDetailAsJSON(t *testing.T) {
|
|||||||
testContext.ErrWriter = &errBuffer
|
testContext.ErrWriter = &errBuffer
|
||||||
|
|
||||||
if testCase.flagComments {
|
if testCase.flagComments {
|
||||||
_ = testContext.Command.Set("comments", "true")
|
require.NoError(t, testContext.Set("comments", "true"))
|
||||||
} else {
|
} else {
|
||||||
_ = testContext.Command.Set("comments", "false")
|
require.NoError(t, testContext.Set("comments", "false"))
|
||||||
}
|
}
|
||||||
|
|
||||||
err := runIssueDetailAsJSON(&testContext, &testCase.issue)
|
err := runIssueDetailAsJSONWithClient(t.Context(), &testContext, &testCase.issue, client)
|
||||||
|
|
||||||
server.Close()
|
|
||||||
|
|
||||||
require.NoError(t, err, "Failed to run issue detail as JSON")
|
require.NoError(t, err, "Failed to run issue detail as JSON")
|
||||||
|
if testCase.flagComments {
|
||||||
|
assert.Equal(t, testOwner, client.owner)
|
||||||
|
assert.Equal(t, testRepo, client.repo)
|
||||||
|
assert.Equal(t, testCase.issue.Index, client.index)
|
||||||
|
}
|
||||||
|
|
||||||
out := outBuffer.String()
|
out := outBuffer.String()
|
||||||
|
|
||||||
@@ -269,7 +302,7 @@ func TestRunIssueDetailUsesOwnerFlag(t *testing.T) {
|
|||||||
issueIndex := int64(12)
|
issueIndex := int64(12)
|
||||||
expectedOwner := "overrideOwner"
|
expectedOwner := "overrideOwner"
|
||||||
expectedRepo := "overrideRepo"
|
expectedRepo := "overrideRepo"
|
||||||
issue := gitea.Issue{
|
issue := &gitea.Issue{
|
||||||
ID: 99,
|
ID: 99,
|
||||||
Index: issueIndex,
|
Index: issueIndex,
|
||||||
Title: "Owner override test",
|
Title: "Owner override test",
|
||||||
@@ -281,34 +314,10 @@ func TestRunIssueDetailUsesOwnerFlag(t *testing.T) {
|
|||||||
HTMLURL: "https://example.test/issues/12",
|
HTMLURL: "https://example.test/issues/12",
|
||||||
}
|
}
|
||||||
|
|
||||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
switch r.URL.Path {
|
|
||||||
case fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d", expectedOwner, expectedRepo, issueIndex):
|
|
||||||
jsonIssue, err := json.Marshal(issue)
|
|
||||||
require.NoError(t, err, "Testing setup failed: failed to marshal issue")
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
_, err = w.Write(jsonIssue)
|
|
||||||
require.NoError(t, err, "Testing setup failed: failed to write issue")
|
|
||||||
case fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/reactions", expectedOwner, expectedRepo, issueIndex):
|
|
||||||
jsonReactions, err := json.Marshal([]gitea.Reaction{})
|
|
||||||
require.NoError(t, err, "Testing setup failed: failed to marshal reactions")
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
_, err = w.Write(jsonReactions)
|
|
||||||
require.NoError(t, err, "Testing setup failed: failed to write reactions")
|
|
||||||
default:
|
|
||||||
http.NotFound(w, r)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
server := httptest.NewServer(handler)
|
|
||||||
defer server.Close()
|
|
||||||
|
|
||||||
config.SetConfigForTesting(config.LocalConfig{
|
config.SetConfigForTesting(config.LocalConfig{
|
||||||
Logins: []config.Login{{
|
Logins: []config.Login{{
|
||||||
Name: "testLogin",
|
Name: "testLogin",
|
||||||
URL: server.URL,
|
URL: "https://gitea.example.com",
|
||||||
Token: "token",
|
Token: "token",
|
||||||
User: "loginUser",
|
User: "loginUser",
|
||||||
Default: true,
|
Default: true,
|
||||||
@@ -333,9 +342,19 @@ func TestRunIssueDetailUsesOwnerFlag(t *testing.T) {
|
|||||||
require.NoError(t, cmd.Set("login", "testLogin"))
|
require.NoError(t, cmd.Set("login", "testLogin"))
|
||||||
require.NoError(t, cmd.Set("repo", expectedRepo))
|
require.NoError(t, cmd.Set("repo", expectedRepo))
|
||||||
require.NoError(t, cmd.Set("owner", expectedOwner))
|
require.NoError(t, cmd.Set("owner", expectedOwner))
|
||||||
require.NoError(t, cmd.Set("output", "json"))
|
|
||||||
require.NoError(t, cmd.Set("comments", "false"))
|
require.NoError(t, cmd.Set("comments", "false"))
|
||||||
|
|
||||||
err := runIssueDetail(stdctx.Background(), &cmd, fmt.Sprintf("%d", issueIndex))
|
teaCtx, idx, err := resolveIssueDetailContext(&cmd, fmt.Sprintf("%d", issueIndex))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
client := &fakeIssueDetailClient{
|
||||||
|
issue: issue,
|
||||||
|
reactions: []*gitea.Reaction{},
|
||||||
|
}
|
||||||
|
|
||||||
|
err = runIssueDetailWithClient(t.Context(), teaCtx, idx, client)
|
||||||
require.NoError(t, err, "Expected runIssueDetail to succeed")
|
require.NoError(t, err, "Expected runIssueDetail to succeed")
|
||||||
|
assert.Equal(t, expectedOwner, client.owner)
|
||||||
|
assert.Equal(t, expectedRepo, client.repo)
|
||||||
|
assert.Equal(t, issueIndex, client.index)
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-2
@@ -7,8 +7,9 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/labels"
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
|
|
||||||
|
"gitea.dev/tea/cmd/labels"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CmdLabels represents to operate repositories' labels.
|
// CmdLabels represents to operate repositories' labels.
|
||||||
@@ -37,5 +38,5 @@ func runLabels(ctx context.Context, cmd *cli.Command) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func runLabelsDetails(cmd *cli.Command) error {
|
func runLabelsDetails(cmd *cli.Command) error {
|
||||||
return fmt.Errorf("Not yet implemented")
|
return fmt.Errorf("not yet implemented")
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-8
@@ -10,10 +10,10 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
"gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -45,13 +45,18 @@ var CmdLabelCreate = cli.Command{
|
|||||||
}, flags.AllDefaultFlags...),
|
}, flags.AllDefaultFlags...),
|
||||||
}
|
}
|
||||||
|
|
||||||
func runLabelCreate(_ stdctx.Context, cmd *cli.Command) error {
|
func runLabelCreate(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
labelFile := ctx.String("file")
|
labelFile := ctx.String("file")
|
||||||
if len(labelFile) == 0 {
|
if len(labelFile) == 0 {
|
||||||
_, _, err := ctx.Login.Client().CreateLabel(ctx.Owner, ctx.Repo, gitea.CreateLabelOption{
|
_, _, err := ctx.Login.Client().Repositories.CreateLabel(requestCtx, ctx.Owner, ctx.Repo, gitea.CreateLabelOption{
|
||||||
Name: ctx.String("name"),
|
Name: ctx.String("name"),
|
||||||
Color: ctx.String("color"),
|
Color: ctx.String("color"),
|
||||||
Description: ctx.String("description"),
|
Description: ctx.String("description"),
|
||||||
@@ -73,7 +78,7 @@ func runLabelCreate(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
if color == "" || name == "" {
|
if color == "" || name == "" {
|
||||||
log.Printf("Line %d ignored because lack of enough fields: %s\n", i, line)
|
log.Printf("Line %d ignored because lack of enough fields: %s\n", i, line)
|
||||||
} else {
|
} else {
|
||||||
_, _, err = ctx.Login.Client().CreateLabel(ctx.Owner, ctx.Repo, gitea.CreateLabelOption{
|
_, _, err = ctx.Login.Client().Repositories.CreateLabel(requestCtx, ctx.Owner, ctx.Repo, gitea.CreateLabelOption{
|
||||||
Name: name,
|
Name: name,
|
||||||
Color: color,
|
Color: color,
|
||||||
Description: description,
|
Description: description,
|
||||||
|
|||||||
+12
-7
@@ -7,8 +7,8 @@ import (
|
|||||||
stdctx "context"
|
stdctx "context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
"gitea.dev/tea/cmd/flags"
|
||||||
"code.gitea.io/tea/modules/context"
|
"gitea.dev/tea/modules/context"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
@@ -30,20 +30,25 @@ var CmdLabelDelete = cli.Command{
|
|||||||
}, flags.AllDefaultFlags...),
|
}, flags.AllDefaultFlags...),
|
||||||
}
|
}
|
||||||
|
|
||||||
func runLabelDelete(_ stdctx.Context, cmd *cli.Command) error {
|
func runLabelDelete(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
labelID := ctx.Int64("id")
|
labelID := ctx.Int64("id")
|
||||||
client := ctx.Login.Client()
|
client := ctx.Login.Client()
|
||||||
|
|
||||||
// Verify the label exists first
|
// Verify the label exists first
|
||||||
label, _, err := client.GetRepoLabel(ctx.Owner, ctx.Repo, labelID)
|
label, _, err := client.Repositories.GetRepoLabel(requestCtx, ctx.Owner, ctx.Repo, labelID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get label %d: %w", labelID, err)
|
return fmt.Errorf("failed to get label %d: %w", labelID, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = client.DeleteLabel(ctx.Owner, ctx.Repo, labelID)
|
_, err = client.Repositories.DeleteLabel(requestCtx, ctx.Owner, ctx.Repo, labelID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to delete label '%s' (id: %d): %w", label.Name, labelID, err)
|
return fmt.Errorf("failed to delete label '%s' (id: %d): %w", label.Name, labelID, err)
|
||||||
}
|
}
|
||||||
|
|||||||
+64
-13
@@ -5,13 +5,14 @@ package labels
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
stdctx "context"
|
stdctx "context"
|
||||||
|
"log"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
gitea "gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
"code.gitea.io/tea/modules/print"
|
|
||||||
"code.gitea.io/tea/modules/task"
|
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/print"
|
||||||
|
"gitea.dev/tea/modules/task"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -29,20 +30,37 @@ var CmdLabelsList = cli.Command{
|
|||||||
Aliases: []string{"s"},
|
Aliases: []string{"s"},
|
||||||
Usage: "Save all the labels as a file",
|
Usage: "Save all the labels as a file",
|
||||||
},
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "org",
|
||||||
|
Usage: "List organization labels",
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "exclude-org",
|
||||||
|
Usage: "Exclude organization labels from the list",
|
||||||
|
},
|
||||||
&flags.PaginationPageFlag,
|
&flags.PaginationPageFlag,
|
||||||
&flags.PaginationLimitFlag,
|
&flags.PaginationLimitFlag,
|
||||||
}, flags.AllDefaultFlags...),
|
}, flags.AllDefaultFlags...),
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunLabelsList list labels.
|
// RunLabelsList list labels.
|
||||||
func RunLabelsList(_ stdctx.Context, cmd *cli.Command) error {
|
func RunLabelsList(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if cmd.String("org") == "" && cmd.String("repo") == "" {
|
||||||
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cmd.String("org") != "" && cmd.String("repo") != "" {
|
||||||
|
log.Printf("Warning: Both --org and --repo flags are set. Ignoring --org and using --repo value as owner.\n")
|
||||||
|
}
|
||||||
|
|
||||||
client := ctx.Login.Client()
|
client := ctx.Login.Client()
|
||||||
labels, _, err := client.ListRepoLabels(ctx.Owner, ctx.Repo, gitea.ListLabelsOptions{
|
labels, err := collectLabels(requestCtx, client, ctx, cmd)
|
||||||
ListOptions: flags.GetListOptions(),
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -51,6 +69,39 @@ func RunLabelsList(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
return task.LabelsExport(labels, ctx.String("save"))
|
return task.LabelsExport(labels, ctx.String("save"))
|
||||||
}
|
}
|
||||||
|
|
||||||
print.LabelsList(labels, ctx.Output)
|
return print.LabelsList(labels, ctx.Output)
|
||||||
return nil
|
}
|
||||||
|
|
||||||
|
// CollectLabels collects labels based on the provided command flags and context.
|
||||||
|
func collectLabels(requestCtx stdctx.Context, client *gitea.Client, ctx *context.TeaContext, cmd *cli.Command) ([]*gitea.Label, error) {
|
||||||
|
var labels []*gitea.Label
|
||||||
|
var err error
|
||||||
|
owner := ctx.Owner
|
||||||
|
|
||||||
|
// If --org is set but --repo is not, list organization labels only and skip repository labels.
|
||||||
|
if cmd.String("org") != "" && cmd.String("repo") == "" {
|
||||||
|
owner = ctx.Org
|
||||||
|
} else {
|
||||||
|
labels, _, err = client.Repositories.ListRepoLabels(requestCtx, owner, ctx.Repo, gitea.ListLabelsOptions{
|
||||||
|
ListOptions: flags.GetListOptions(cmd),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to list repository labels: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If --exclude is not set and the owner is an organization, also list organization labels and append them to the list.
|
||||||
|
if !ctx.IsSet("exclude-org") {
|
||||||
|
if _, _, err = client.Organizations.GetOrg(requestCtx, owner); err == nil {
|
||||||
|
orgLabels, _, err := client.Organizations.ListOrgLabels(requestCtx, owner, gitea.ListOrgLabelsOptions{
|
||||||
|
ListOptions: flags.GetListOptions(cmd),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
labels = append(labels, orgLabels...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return labels, nil
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-8
@@ -6,10 +6,10 @@ package labels
|
|||||||
import (
|
import (
|
||||||
stdctx "context"
|
stdctx "context"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
"gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -40,9 +40,14 @@ var CmdLabelUpdate = cli.Command{
|
|||||||
}, flags.AllDefaultFlags...),
|
}, flags.AllDefaultFlags...),
|
||||||
}
|
}
|
||||||
|
|
||||||
func runLabelUpdate(_ stdctx.Context, cmd *cli.Command) error {
|
func runLabelUpdate(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
id := ctx.Int64("id")
|
id := ctx.Int64("id")
|
||||||
var pName, pColor, pDescription *string
|
var pName, pColor, pDescription *string
|
||||||
@@ -61,8 +66,7 @@ func runLabelUpdate(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
pDescription = &description
|
pDescription = &description
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
_, _, err = ctx.Login.Client().Repositories.EditLabel(requestCtx, ctx.Owner, ctx.Repo, id, gitea.EditLabelOption{
|
||||||
_, _, err = ctx.Login.Client().EditLabel(ctx.Owner, ctx.Repo, id, gitea.EditLabelOption{
|
|
||||||
Name: pName,
|
Name: pName,
|
||||||
Color: pColor,
|
Color: pColor,
|
||||||
Description: pDescription,
|
Description: pDescription,
|
||||||
|
|||||||
+7
-4
@@ -7,9 +7,9 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/login"
|
"gitea.dev/tea/cmd/login"
|
||||||
"code.gitea.io/tea/modules/config"
|
"gitea.dev/tea/modules/config"
|
||||||
"code.gitea.io/tea/modules/print"
|
"gitea.dev/tea/modules/print"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
@@ -42,7 +42,10 @@ func runLogins(ctx context.Context, cmd *cli.Command) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func runLoginDetail(name string) error {
|
func runLoginDetail(name string) error {
|
||||||
l := config.GetLoginByName(name)
|
l, err := config.GetLoginByName(name)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if l == nil {
|
if l == nil {
|
||||||
fmt.Printf("Login '%s' do not exist\n\n", name)
|
fmt.Printf("Login '%s' do not exist\n\n", name)
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
+17
-11
@@ -7,9 +7,9 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"code.gitea.io/tea/modules/auth"
|
"gitea.dev/tea/modules/auth"
|
||||||
"code.gitea.io/tea/modules/interact"
|
"gitea.dev/tea/modules/interact"
|
||||||
"code.gitea.io/tea/modules/task"
|
"gitea.dev/tea/modules/task"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
@@ -18,7 +18,12 @@ import (
|
|||||||
var CmdLoginAdd = cli.Command{
|
var CmdLoginAdd = cli.Command{
|
||||||
Name: "add",
|
Name: "add",
|
||||||
Usage: "Add a Gitea login",
|
Usage: "Add a Gitea login",
|
||||||
Description: `Add a Gitea login, without args it will create one interactively`,
|
Description: `Add a Gitea login, without args it will create one interactively.
|
||||||
|
|
||||||
|
By default tea only stores the token for its own API use. Pass --git-credentials
|
||||||
|
to also register tea as a git credential helper for the login's URL, so that
|
||||||
|
'git push' and 'git clone' over HTTPS authenticate silently using the stored
|
||||||
|
token. Equivalent to running 'tea login helper setup' afterwards.`,
|
||||||
ArgsUsage: " ", // command does not accept arguments
|
ArgsUsage: " ", // command does not accept arguments
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
@@ -89,9 +94,9 @@ var CmdLoginAdd = cli.Command{
|
|||||||
Usage: "Use SSH public key or SSH fingerprint to login (needs a running ssh-agent with ssh key loaded)",
|
Usage: "Use SSH public key or SSH fingerprint to login (needs a running ssh-agent with ssh key loaded)",
|
||||||
},
|
},
|
||||||
&cli.BoolFlag{
|
&cli.BoolFlag{
|
||||||
Name: "helper",
|
Name: "git-credentials",
|
||||||
Aliases: []string{"j"},
|
Aliases: []string{"helper", "j"},
|
||||||
Usage: "Add helper",
|
Usage: "Register tea as a git credential helper for this login's URL, so 'git push' and 'git clone' over HTTPS authenticate silently using the stored token",
|
||||||
},
|
},
|
||||||
&cli.BoolFlag{
|
&cli.BoolFlag{
|
||||||
Name: "oauth",
|
Name: "oauth",
|
||||||
@@ -110,10 +115,10 @@ var CmdLoginAdd = cli.Command{
|
|||||||
Action: runLoginAdd,
|
Action: runLoginAdd,
|
||||||
}
|
}
|
||||||
|
|
||||||
func runLoginAdd(_ context.Context, cmd *cli.Command) error {
|
func runLoginAdd(requestCtx context.Context, cmd *cli.Command) error {
|
||||||
// if no args create login interactive
|
// if no args create login interactive
|
||||||
if cmd.NumFlags() == 0 {
|
if cmd.NumFlags() == 0 {
|
||||||
if err := interact.CreateLogin(); err != nil && !interact.IsQuitting(err) {
|
if err := interact.CreateLogin(requestCtx); err != nil && !interact.IsQuitting(err) {
|
||||||
return fmt.Errorf("error adding login: %w", err)
|
return fmt.Errorf("error adding login: %w", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -137,7 +142,7 @@ func runLoginAdd(_ context.Context, cmd *cli.Command) error {
|
|||||||
opts.RedirectURL = cmd.String("redirect-url")
|
opts.RedirectURL = cmd.String("redirect-url")
|
||||||
}
|
}
|
||||||
|
|
||||||
return auth.OAuthLoginWithFullOptions(opts)
|
return auth.OAuthLoginWithFullOptions(requestCtx, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
sshAgent := false
|
sshAgent := false
|
||||||
@@ -147,6 +152,7 @@ func runLoginAdd(_ context.Context, cmd *cli.Command) error {
|
|||||||
|
|
||||||
// else use args to add login
|
// else use args to add login
|
||||||
return task.CreateLogin(
|
return task.CreateLogin(
|
||||||
|
requestCtx,
|
||||||
cmd.String("name"),
|
cmd.String("name"),
|
||||||
cmd.String("token"),
|
cmd.String("token"),
|
||||||
cmd.String("user"),
|
cmd.String("user"),
|
||||||
@@ -160,6 +166,6 @@ func runLoginAdd(_ context.Context, cmd *cli.Command) error {
|
|||||||
cmd.Bool("insecure"),
|
cmd.Bool("insecure"),
|
||||||
sshAgent,
|
sshAgent,
|
||||||
!cmd.Bool("no-version-check"),
|
!cmd.Bool("no-version-check"),
|
||||||
cmd.Bool("helper"),
|
cmd.Bool("git-credentials"),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
"gitea.dev/tea/cmd/flags"
|
||||||
"code.gitea.io/tea/modules/config"
|
"gitea.dev/tea/modules/config"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
@@ -23,7 +23,7 @@ var CmdLoginSetDefault = cli.Command{
|
|||||||
Flags: []cli.Flag{&flags.OutputFlag},
|
Flags: []cli.Flag{&flags.OutputFlag},
|
||||||
}
|
}
|
||||||
|
|
||||||
func runLoginSetDefault(_ context.Context, cmd *cli.Command) error {
|
func runLoginSetDefault(requestCtx context.Context, cmd *cli.Command) error {
|
||||||
if cmd.Args().Len() == 0 {
|
if cmd.Args().Len() == 0 {
|
||||||
l, err := config.GetDefaultLogin()
|
l, err := config.GetDefaultLogin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
+5
-6
@@ -5,10 +5,9 @@ package login
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"fmt"
|
||||||
"log"
|
|
||||||
|
|
||||||
"code.gitea.io/tea/modules/config"
|
"gitea.dev/tea/modules/config"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
@@ -24,10 +23,10 @@ var CmdLoginDelete = cli.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RunLoginDelete runs the action of a login delete command
|
// RunLoginDelete runs the action of a login delete command
|
||||||
func RunLoginDelete(_ context.Context, cmd *cli.Command) error {
|
func RunLoginDelete(requestCtx context.Context, cmd *cli.Command) error {
|
||||||
logins, err := config.GetLogins()
|
logins, err := config.GetLogins()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var name string
|
var name string
|
||||||
@@ -37,7 +36,7 @@ func RunLoginDelete(_ context.Context, cmd *cli.Command) error {
|
|||||||
} else if len(logins) == 1 {
|
} else if len(logins) == 1 {
|
||||||
name = logins[0].Name
|
name = logins[0].Name
|
||||||
} else {
|
} else {
|
||||||
return errors.New("Please specify a login name")
|
return fmt.Errorf("please specify a login name")
|
||||||
}
|
}
|
||||||
|
|
||||||
return config.DeleteLogin(name)
|
return config.DeleteLogin(name)
|
||||||
|
|||||||
+12
-8
@@ -5,12 +5,13 @@ package login
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
"gitea.dev/tea/cmd/flags"
|
||||||
"code.gitea.io/tea/modules/config"
|
"gitea.dev/tea/modules/config"
|
||||||
|
"gitea.dev/tea/modules/utils"
|
||||||
|
|
||||||
"github.com/skratchdot/open-golang/open"
|
"github.com/skratchdot/open-golang/open"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
@@ -27,15 +28,18 @@ var CmdLoginEdit = cli.Command{
|
|||||||
Flags: []cli.Flag{&flags.OutputFlag},
|
Flags: []cli.Flag{&flags.OutputFlag},
|
||||||
}
|
}
|
||||||
|
|
||||||
func runLoginEdit(_ context.Context, _ *cli.Command) error {
|
func runLoginEdit(requestCtx context.Context, _ *cli.Command) error {
|
||||||
|
ymlPath := config.GetConfigPath()
|
||||||
if e, ok := os.LookupEnv("EDITOR"); ok && e != "" {
|
if e, ok := os.LookupEnv("EDITOR"); ok && e != "" {
|
||||||
cmd := exec.Command(e, config.GetConfigPath())
|
cmd := exec.Command(e, ymlPath)
|
||||||
cmd.Stdin = os.Stdin
|
cmd.Stdin = os.Stdin
|
||||||
cmd.Stdout = os.Stdout
|
cmd.Stdout = os.Stdout
|
||||||
cmd.Stderr = os.Stderr
|
cmd.Stderr = os.Stderr
|
||||||
if err := cmd.Run(); err != nil {
|
return cmd.Run()
|
||||||
log.Fatal(err.Error())
|
|
||||||
}
|
}
|
||||||
|
if exist, _ := utils.FileExist(ymlPath); !exist {
|
||||||
|
fmt.Printf("Config file does not exist, please run login add first\n")
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return open.Start(config.GetConfigPath())
|
return open.Start(ymlPath)
|
||||||
}
|
}
|
||||||
|
|||||||
+37
-20
@@ -7,36 +7,44 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"code.gitea.io/tea/modules/config"
|
|
||||||
"code.gitea.io/tea/modules/task"
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
|
|
||||||
|
"gitea.dev/tea/modules/config"
|
||||||
|
"gitea.dev/tea/modules/task"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CmdLoginHelper represents to login a gitea helper.
|
// CmdLoginHelper represents to login a gitea helper.
|
||||||
var CmdLoginHelper = cli.Command{
|
var CmdLoginHelper = cli.Command{
|
||||||
Name: "helper",
|
Name: "helper",
|
||||||
Aliases: []string{"git-credential"},
|
Aliases: []string{"git-credential"},
|
||||||
Usage: "Git helper",
|
Usage: "Act as a git credential helper for stored Gitea logins",
|
||||||
Description: `Git helper`,
|
Description: `Speaks git's credential helper protocol so that HTTPS push and clone
|
||||||
Hidden: true,
|
operations against your configured Gitea instances authenticate silently
|
||||||
|
using the tokens tea already stores.
|
||||||
|
|
||||||
|
Typical use is automatic: 'tea login add --git-credentials' (or 'tea login
|
||||||
|
helper setup' for existing logins) registers '!tea login helper' as a
|
||||||
|
credential helper in ~/.gitconfig. Git then invokes the 'get' subcommand
|
||||||
|
when it needs credentials for a configured host.`,
|
||||||
Commands: []*cli.Command{
|
Commands: []*cli.Command{
|
||||||
{
|
{
|
||||||
Name: "store",
|
Name: "store",
|
||||||
Description: "Command drops",
|
|
||||||
Aliases: []string{"erase"},
|
Aliases: []string{"erase"},
|
||||||
Action: func(_ context.Context, _ *cli.Command) error {
|
Usage: "No-op (git credential protocol store/erase)",
|
||||||
|
Description: "No-op invoked by git on credential store/erase; tea persists credentials only in its own config",
|
||||||
|
Action: func(requestCtx context.Context, _ *cli.Command) error {
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "setup",
|
Name: "setup",
|
||||||
Description: "Setup helper to tea authenticate",
|
Usage: "Register tea as a git credential helper for every configured login",
|
||||||
Action: func(_ context.Context, _ *cli.Command) error {
|
Description: "Register tea as a git credential helper in ~/.gitconfig for every configured login",
|
||||||
|
Action: func(requestCtx context.Context, _ *cli.Command) error {
|
||||||
logins, err := config.GetLogins()
|
logins, err := config.GetLogins()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -56,7 +64,8 @@ var CmdLoginHelper = cli.Command{
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "get",
|
Name: "get",
|
||||||
Description: "Get token to auth",
|
Usage: "Return the stored token for a URL (git credential protocol)",
|
||||||
|
Description: "Return the stored token for a given URL in git's credential helper protocol (called by git, reads request from stdin)",
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "login",
|
Name: "login",
|
||||||
@@ -64,7 +73,7 @@ var CmdLoginHelper = cli.Command{
|
|||||||
Usage: "Use a specific login",
|
Usage: "Use a specific login",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Action: func(_ context.Context, cmd *cli.Command) error {
|
Action: func(requestCtx context.Context, cmd *cli.Command) error {
|
||||||
wants := map[string]string{}
|
wants := map[string]string{}
|
||||||
s := bufio.NewScanner(os.Stdin)
|
s := bufio.NewScanner(os.Stdin)
|
||||||
for s.Scan() {
|
for s.Scan() {
|
||||||
@@ -93,7 +102,7 @@ var CmdLoginHelper = cli.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(wants["host"]) == 0 {
|
if len(wants["host"]) == 0 {
|
||||||
log.Fatal("Hostname is required")
|
return fmt.Errorf("hostname is required")
|
||||||
} else if len(wants["protocol"]) == 0 {
|
} else if len(wants["protocol"]) == 0 {
|
||||||
wants["protocol"] = "http"
|
wants["protocol"] = "http"
|
||||||
}
|
}
|
||||||
@@ -101,19 +110,27 @@ var CmdLoginHelper = cli.Command{
|
|||||||
// Use --login flag if provided, otherwise fall back to host lookup
|
// Use --login flag if provided, otherwise fall back to host lookup
|
||||||
var userConfig *config.Login
|
var userConfig *config.Login
|
||||||
if loginName := cmd.String("login"); loginName != "" {
|
if loginName := cmd.String("login"); loginName != "" {
|
||||||
userConfig = config.GetLoginByName(loginName)
|
var lookupErr error
|
||||||
|
userConfig, lookupErr = config.GetLoginByName(loginName)
|
||||||
|
if lookupErr != nil {
|
||||||
|
return lookupErr
|
||||||
|
}
|
||||||
if userConfig == nil {
|
if userConfig == nil {
|
||||||
log.Fatalf("Login '%s' not found", loginName)
|
return fmt.Errorf("login '%s' not found", loginName)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
userConfig = config.GetLoginByHost(wants["host"])
|
var lookupErr error
|
||||||
|
userConfig, lookupErr = config.GetLoginByHost(wants["host"])
|
||||||
|
if lookupErr != nil {
|
||||||
|
return lookupErr
|
||||||
|
}
|
||||||
if userConfig == nil {
|
if userConfig == nil {
|
||||||
log.Fatalf("No login found for host '%s'", wants["host"])
|
return fmt.Errorf("no login found for host '%s'", wants["host"])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(userConfig.Token) == 0 {
|
if len(userConfig.GetAccessToken()) == 0 {
|
||||||
log.Fatal("User not set")
|
return fmt.Errorf("user not set")
|
||||||
}
|
}
|
||||||
|
|
||||||
host, err := url.Parse(userConfig.URL)
|
host, err := url.Parse(userConfig.URL)
|
||||||
@@ -126,7 +143,7 @@ var CmdLoginHelper = cli.Command{
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = fmt.Fprintf(os.Stdout, "protocol=%s\nhost=%s\nusername=%s\npassword=%s\n", host.Scheme, host.Host, userConfig.User, userConfig.Token)
|
_, err = fmt.Fprintf(os.Stdout, "protocol=%s\nhost=%s\nusername=%s\npassword=%s\n", host.Scheme, host.Host, userConfig.User, userConfig.GetAccessToken())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-6
@@ -6,9 +6,9 @@ package login
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
"gitea.dev/tea/cmd/flags"
|
||||||
"code.gitea.io/tea/modules/config"
|
"gitea.dev/tea/modules/config"
|
||||||
"code.gitea.io/tea/modules/print"
|
"gitea.dev/tea/modules/print"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
@@ -25,11 +25,10 @@ var CmdLoginList = cli.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RunLoginList list all logins
|
// RunLoginList list all logins
|
||||||
func RunLoginList(_ context.Context, cmd *cli.Command) error {
|
func RunLoginList(requestCtx context.Context, cmd *cli.Command) error {
|
||||||
logins, err := config.GetLogins()
|
logins, err := config.GetLogins()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
print.LoginsList(logins, cmd.String("output"))
|
return print.LoginsList(logins, cmd.String("output"))
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"code.gitea.io/tea/modules/auth"
|
"gitea.dev/tea/modules/auth"
|
||||||
"code.gitea.io/tea/modules/config"
|
"gitea.dev/tea/modules/config"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
@@ -22,7 +22,7 @@ var CmdLoginOAuthRefresh = cli.Command{
|
|||||||
Action: runLoginOAuthRefresh,
|
Action: runLoginOAuthRefresh,
|
||||||
}
|
}
|
||||||
|
|
||||||
func runLoginOAuthRefresh(_ context.Context, cmd *cli.Command) error {
|
func runLoginOAuthRefresh(requestCtx context.Context, cmd *cli.Command) error {
|
||||||
var loginName string
|
var loginName string
|
||||||
|
|
||||||
// Get login name from args or use default
|
// Get login name from args or use default
|
||||||
@@ -38,18 +38,21 @@ func runLoginOAuthRefresh(_ context.Context, cmd *cli.Command) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get the login from config
|
// Get the login from config
|
||||||
login := config.GetLoginByName(loginName)
|
login, err := config.GetLoginByName(loginName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if login == nil {
|
if login == nil {
|
||||||
return fmt.Errorf("login '%s' not found", loginName)
|
return fmt.Errorf("login '%s' not found", loginName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the login has a refresh token
|
// Check if the login has a refresh token
|
||||||
if login.RefreshToken == "" {
|
if login.GetRefreshToken() == "" {
|
||||||
return fmt.Errorf("login '%s' does not have a refresh token. It may have been created using a different authentication method", loginName)
|
return fmt.Errorf("login '%s' does not have a refresh token. It may have been created using a different authentication method", loginName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to refresh the token
|
// Try to refresh the token
|
||||||
err := auth.RefreshAccessToken(login)
|
err = auth.RefreshAccessToken(login)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
fmt.Printf("Successfully refreshed OAuth token for %s\n", loginName)
|
fmt.Printf("Successfully refreshed OAuth token for %s\n", loginName)
|
||||||
return nil
|
return nil
|
||||||
@@ -59,7 +62,7 @@ func runLoginOAuthRefresh(_ context.Context, cmd *cli.Command) error {
|
|||||||
fmt.Printf("Token refresh failed: %s\n", err)
|
fmt.Printf("Token refresh failed: %s\n", err)
|
||||||
fmt.Println("Opening browser for re-authentication...")
|
fmt.Println("Opening browser for re-authentication...")
|
||||||
|
|
||||||
if err := auth.ReauthenticateLogin(login); err != nil {
|
if err := auth.ReauthenticateLogin(requestCtx, login); err != nil {
|
||||||
return fmt.Errorf("re-authentication failed: %s", err)
|
return fmt.Errorf("re-authentication failed: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"code.gitea.io/tea/cmd/login"
|
"gitea.dev/tea/cmd/login"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|||||||
+3
-1
@@ -29,7 +29,9 @@ var CmdGenerateManPage = cli.Command{
|
|||||||
Hidden: true,
|
Hidden: true,
|
||||||
Flags: DocRenderFlags,
|
Flags: DocRenderFlags,
|
||||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||||
return RenderDocs(cmd, cmd.Root(), docs.ToMan)
|
return RenderDocs(cmd, cmd.Root(), func(cmd *cli.Command) (string, error) {
|
||||||
|
return docs.ToManWithSection(cmd, 1)
|
||||||
|
})
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+13
-7
@@ -6,10 +6,11 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
stdctx "context"
|
stdctx "context"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/milestones"
|
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
"code.gitea.io/tea/modules/print"
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
|
|
||||||
|
"gitea.dev/tea/cmd/milestones"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/print"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CmdMilestones represents to operate repositories milestones.
|
// CmdMilestones represents to operate repositories milestones.
|
||||||
@@ -39,12 +40,17 @@ func runMilestones(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
return milestones.RunMilestonesList(ctx, cmd)
|
return milestones.RunMilestonesList(ctx, cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func runMilestoneDetail(_ stdctx.Context, cmd *cli.Command, name string) error {
|
func runMilestoneDetail(requestCtx stdctx.Context, cmd *cli.Command, name string) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
client := ctx.Login.Client()
|
client := ctx.Login.Client()
|
||||||
|
|
||||||
milestone, _, err := client.GetMilestoneByName(ctx.Owner, ctx.Repo, name)
|
milestone, _, err := client.Repositories.GetMilestoneByName(requestCtx, ctx.Owner, ctx.Repo, name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,9 @@ package milestones
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
|
|
||||||
|
"gitea.dev/tea/cmd/flags"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CmdMilestonesClose represents a sub command of milestones to close an milestone
|
// CmdMilestonesClose represents a sub command of milestones to close an milestone
|
||||||
|
|||||||
+13
-10
@@ -8,13 +8,14 @@ import (
|
|||||||
|
|
||||||
stdctx "context"
|
stdctx "context"
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
gitea "gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/cmd/flags"
|
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
"code.gitea.io/tea/modules/interact"
|
|
||||||
"code.gitea.io/tea/modules/task"
|
|
||||||
"github.com/araddon/dateparse"
|
"github.com/araddon/dateparse"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
|
|
||||||
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/interact"
|
||||||
|
"gitea.dev/tea/modules/task"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CmdMilestonesCreate represents a sub command of milestones to create milestone
|
// CmdMilestonesCreate represents a sub command of milestones to create milestone
|
||||||
@@ -49,8 +50,11 @@ var CmdMilestonesCreate = cli.Command{
|
|||||||
}, flags.AllDefaultFlags...),
|
}, flags.AllDefaultFlags...),
|
||||||
}
|
}
|
||||||
|
|
||||||
func runMilestonesCreate(_ stdctx.Context, cmd *cli.Command) error {
|
func runMilestonesCreate(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
date := ctx.String("deadline")
|
date := ctx.String("deadline")
|
||||||
deadline := &time.Time{}
|
deadline := &time.Time{}
|
||||||
@@ -68,14 +72,13 @@ func runMilestonesCreate(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ctx.IsInteractiveMode() {
|
if ctx.IsInteractiveMode() {
|
||||||
if err := interact.CreateMilestone(ctx.Login, ctx.Owner, ctx.Repo); err != nil && !interact.IsQuitting(err) {
|
if err := interact.CreateMilestone(requestCtx, ctx.Login, ctx.Owner, ctx.Repo); err != nil && !interact.IsQuitting(err) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return task.CreateMilestone(
|
return task.CreateMilestone(requestCtx, ctx.Login,
|
||||||
ctx.Login,
|
|
||||||
ctx.Owner,
|
ctx.Owner,
|
||||||
ctx.Repo,
|
ctx.Repo,
|
||||||
ctx.String("title"),
|
ctx.String("title"),
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ package milestones
|
|||||||
import (
|
import (
|
||||||
stdctx "context"
|
stdctx "context"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
"gitea.dev/tea/cmd/flags"
|
||||||
"code.gitea.io/tea/modules/context"
|
"gitea.dev/tea/modules/context"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
@@ -23,11 +23,16 @@ var CmdMilestonesDelete = cli.Command{
|
|||||||
Flags: flags.AllDefaultFlags,
|
Flags: flags.AllDefaultFlags,
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteMilestone(_ stdctx.Context, cmd *cli.Command) error {
|
func deleteMilestone(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
client := ctx.Login.Client()
|
client := ctx.Login.Client()
|
||||||
|
|
||||||
_, err := client.DeleteMilestoneByName(ctx.Owner, ctx.Repo, ctx.Args().First())
|
_, err = client.Repositories.DeleteMilestoneByName(requestCtx, ctx.Owner, ctx.Repo, ctx.Args().First())
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
+38
-23
@@ -8,12 +8,13 @@ import (
|
|||||||
|
|
||||||
stdctx "context"
|
stdctx "context"
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
gitea "gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/cmd/flags"
|
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
"code.gitea.io/tea/modules/print"
|
|
||||||
"code.gitea.io/tea/modules/utils"
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
|
|
||||||
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/print"
|
||||||
|
"gitea.dev/tea/modules/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
var msIssuesFieldsFlag = flags.FieldsFlag(print.IssueFields, []string{
|
var msIssuesFieldsFlag = flags.FieldsFlag(print.IssueFields, []string{
|
||||||
@@ -70,9 +71,14 @@ var CmdMilestoneRemoveIssue = cli.Command{
|
|||||||
Flags: flags.AllDefaultFlags,
|
Flags: flags.AllDefaultFlags,
|
||||||
}
|
}
|
||||||
|
|
||||||
func runMilestoneIssueList(_ stdctx.Context, cmd *cli.Command) error {
|
func runMilestoneIssueList(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
client := ctx.Login.Client()
|
client := ctx.Login.Client()
|
||||||
|
|
||||||
state, err := flags.ParseState(ctx.String("state"))
|
state, err := flags.ParseState(ctx.String("state"))
|
||||||
@@ -91,13 +97,13 @@ func runMilestoneIssueList(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
|
|
||||||
milestone := ctx.Args().First()
|
milestone := ctx.Args().First()
|
||||||
// make sure milestone exist
|
// make sure milestone exist
|
||||||
_, _, err = client.GetMilestoneByName(ctx.Owner, ctx.Repo, milestone)
|
_, _, err = client.Repositories.GetMilestoneByName(requestCtx, ctx.Owner, ctx.Repo, milestone)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
issues, _, err := client.ListRepoIssues(ctx.Owner, ctx.Repo, gitea.ListIssueOption{
|
issues, _, err := client.Issues.ListRepoIssues(requestCtx, ctx.Owner, ctx.Repo, gitea.ListIssueOption{
|
||||||
ListOptions: flags.GetListOptions(),
|
ListOptions: flags.GetListOptions(cmd),
|
||||||
Milestones: []string{milestone},
|
Milestones: []string{milestone},
|
||||||
Type: kind,
|
Type: kind,
|
||||||
State: state,
|
State: state,
|
||||||
@@ -110,13 +116,17 @@ func runMilestoneIssueList(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
print.IssuesPullsList(issues, ctx.Output, fields)
|
return print.IssuesPullsList(issues, ctx.Output, fields)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func runMilestoneIssueAdd(_ stdctx.Context, cmd *cli.Command) error {
|
func runMilestoneIssueAdd(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
client := ctx.Login.Client()
|
client := ctx.Login.Client()
|
||||||
if ctx.Args().Len() != 2 {
|
if ctx.Args().Len() != 2 {
|
||||||
return fmt.Errorf("need two arguments")
|
return fmt.Errorf("need two arguments")
|
||||||
@@ -130,12 +140,12 @@ func runMilestoneIssueAdd(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// make sure milestone exist
|
// make sure milestone exist
|
||||||
mile, _, err := client.GetMilestoneByName(ctx.Owner, ctx.Repo, mileName)
|
mile, _, err := client.Repositories.GetMilestoneByName(requestCtx, ctx.Owner, ctx.Repo, mileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get milestone '%s': %w", mileName, err)
|
return fmt.Errorf("failed to get milestone '%s': %w", mileName, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, err = client.EditIssue(ctx.Owner, ctx.Repo, idx, gitea.EditIssueOption{
|
_, _, err = client.Issues.EditIssue(requestCtx, ctx.Owner, ctx.Repo, idx, gitea.EditIssueOption{
|
||||||
Milestone: &mile.ID,
|
Milestone: &mile.ID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -144,9 +154,14 @@ func runMilestoneIssueAdd(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func runMilestoneIssueRemove(_ stdctx.Context, cmd *cli.Command) error {
|
func runMilestoneIssueRemove(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
client := ctx.Login.Client()
|
client := ctx.Login.Client()
|
||||||
if ctx.Args().Len() != 2 {
|
if ctx.Args().Len() != 2 {
|
||||||
return fmt.Errorf("need two arguments")
|
return fmt.Errorf("need two arguments")
|
||||||
@@ -159,7 +174,7 @@ func runMilestoneIssueRemove(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
return fmt.Errorf("invalid issue index '%s': %w", issueIndex, err)
|
return fmt.Errorf("invalid issue index '%s': %w", issueIndex, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
issue, _, err := client.GetIssue(ctx.Owner, ctx.Repo, idx)
|
issue, _, err := client.Issues.GetIssue(requestCtx, ctx.Owner, ctx.Repo, idx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get issue #%d: %w", idx, err)
|
return fmt.Errorf("failed to get issue #%d: %w", idx, err)
|
||||||
}
|
}
|
||||||
@@ -173,7 +188,7 @@ func runMilestoneIssueRemove(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
zero := int64(0)
|
zero := int64(0)
|
||||||
_, _, err = client.EditIssue(ctx.Owner, ctx.Repo, idx, gitea.EditIssueOption{
|
_, _, err = client.Issues.EditIssue(requestCtx, ctx.Owner, ctx.Repo, idx, gitea.EditIssueOption{
|
||||||
Milestone: &zero,
|
Milestone: &zero,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
+15
-11
@@ -6,11 +6,11 @@ package milestones
|
|||||||
import (
|
import (
|
||||||
stdctx "context"
|
stdctx "context"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
gitea "gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
"code.gitea.io/tea/modules/print"
|
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/print"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -39,9 +39,14 @@ var CmdMilestonesList = cli.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RunMilestonesList list milestones
|
// RunMilestonesList list milestones
|
||||||
func RunMilestonesList(_ stdctx.Context, cmd *cli.Command) error {
|
func RunMilestonesList(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
fields, err := fieldsFlag.GetValues(cmd)
|
fields, err := fieldsFlag.GetValues(cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -57,14 +62,13 @@ func RunMilestonesList(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
client := ctx.Login.Client()
|
client := ctx.Login.Client()
|
||||||
milestones, _, err := client.ListRepoMilestones(ctx.Owner, ctx.Repo, gitea.ListMilestoneOption{
|
milestones, _, err := client.Repositories.ListMilestones(requestCtx, ctx.Owner, ctx.Repo, gitea.ListMilestoneOption{
|
||||||
ListOptions: flags.GetListOptions(),
|
ListOptions: flags.GetListOptions(cmd),
|
||||||
State: state,
|
State: state,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
print.MilestonesList(milestones, ctx.Output, fields)
|
return print.MilestonesList(milestones, ctx.Output, fields)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,11 +7,11 @@ import (
|
|||||||
stdctx "context"
|
stdctx "context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
gitea "gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
"code.gitea.io/tea/modules/print"
|
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/print"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -28,9 +28,14 @@ var CmdMilestonesReopen = cli.Command{
|
|||||||
Flags: flags.AllDefaultFlags,
|
Flags: flags.AllDefaultFlags,
|
||||||
}
|
}
|
||||||
|
|
||||||
func editMilestoneStatus(_ stdctx.Context, cmd *cli.Command, close bool) error {
|
func editMilestoneStatus(requestCtx stdctx.Context, cmd *cli.Command, close bool) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if ctx.Args().Len() == 0 {
|
if ctx.Args().Len() == 0 {
|
||||||
return fmt.Errorf("missing required argument: %s", ctx.Command.ArgsUsage)
|
return fmt.Errorf("missing required argument: %s", ctx.Command.ArgsUsage)
|
||||||
}
|
}
|
||||||
@@ -41,18 +46,25 @@ func editMilestoneStatus(_ stdctx.Context, cmd *cli.Command, close bool) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
client := ctx.Login.Client()
|
client := ctx.Login.Client()
|
||||||
|
repoURL := ""
|
||||||
|
if ctx.Args().Len() > 1 {
|
||||||
|
repoURL, err = ctx.GetRemoteRepoHTMLURL()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
for _, ms := range ctx.Args().Slice() {
|
for _, ms := range ctx.Args().Slice() {
|
||||||
opts := gitea.EditMilestoneOption{
|
opts := gitea.EditMilestoneOption{
|
||||||
State: &state,
|
State: &state,
|
||||||
Title: ms,
|
Title: ms,
|
||||||
}
|
}
|
||||||
milestone, _, err := client.EditMilestoneByName(ctx.Owner, ctx.Repo, ms, opts)
|
milestone, _, err := client.Repositories.EditMilestoneByName(requestCtx, ctx.Owner, ctx.Repo, ms, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if ctx.Args().Len() > 1 {
|
if ctx.Args().Len() > 1 {
|
||||||
fmt.Printf("%s/milestone/%d\n", ctx.GetRemoteRepoHTMLURL(), milestone.ID)
|
fmt.Printf("%s/milestone/%d\n", repoURL, milestone.ID)
|
||||||
} else {
|
} else {
|
||||||
print.MilestoneDetails(milestone)
|
print.MilestoneDetails(milestone)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"code.gitea.io/tea/cmd/notifications"
|
"gitea.dev/tea/cmd/notifications"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|||||||
+17
-14
@@ -5,13 +5,12 @@ package notifications
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
stdctx "context"
|
stdctx "context"
|
||||||
"log"
|
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
gitea "gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
"code.gitea.io/tea/modules/print"
|
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/print"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -60,16 +59,19 @@ func RunNotificationsList(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// listNotifications will get the notifications based on status and subject type
|
// listNotifications will get the notifications based on status and subject type
|
||||||
func listNotifications(_ stdctx.Context, cmd *cli.Command, status []gitea.NotifyStatus, subjects []gitea.NotifySubjectType) error {
|
func listNotifications(requestCtx stdctx.Context, cmd *cli.Command, status []gitea.NotifyStatus, subjects []gitea.NotifySubjectType) error {
|
||||||
var news []*gitea.NotificationThread
|
var news []*gitea.NotificationThread
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
client := ctx.Login.Client()
|
client := ctx.Login.Client()
|
||||||
all := ctx.Bool("mine")
|
all := ctx.Bool("mine")
|
||||||
|
|
||||||
// This enforces pagination (see https://github.com/go-gitea/gitea/issues/16733)
|
// This enforces pagination (see https://github.com/go-gitea/gitea/issues/16733)
|
||||||
listOpts := flags.GetListOptions()
|
listOpts := flags.GetListOptions(cmd)
|
||||||
if listOpts.Page == 0 {
|
if listOpts.Page == 0 {
|
||||||
listOpts.Page = 1
|
listOpts.Page = 1
|
||||||
}
|
}
|
||||||
@@ -85,23 +87,24 @@ func listNotifications(_ stdctx.Context, cmd *cli.Command, status []gitea.Notify
|
|||||||
fields = append(fields, "repository")
|
fields = append(fields, "repository")
|
||||||
}
|
}
|
||||||
|
|
||||||
news, _, err = client.ListNotifications(gitea.ListNotificationOptions{
|
news, _, err = client.Notifications.List(requestCtx, gitea.ListNotificationOptions{
|
||||||
ListOptions: listOpts,
|
ListOptions: listOpts,
|
||||||
Status: status,
|
Status: status,
|
||||||
SubjectTypes: subjects,
|
SubjectTypes: subjects,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
news, _, err = client.ListRepoNotifications(ctx.Owner, ctx.Repo, gitea.ListNotificationOptions{
|
return err
|
||||||
|
}
|
||||||
|
news, _, err = client.Notifications.ListByRepo(requestCtx, ctx.Owner, ctx.Repo, gitea.ListNotificationOptions{
|
||||||
ListOptions: listOpts,
|
ListOptions: listOpts,
|
||||||
Status: status,
|
Status: status,
|
||||||
SubjectTypes: subjects,
|
SubjectTypes: subjects,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
print.NotificationsList(news, ctx.Output, fields)
|
return print.NotificationsList(news, ctx.Output, fields)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,11 +7,12 @@ import (
|
|||||||
stdctx "context"
|
stdctx "context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
gitea "gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/cmd/flags"
|
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
"code.gitea.io/tea/modules/utils"
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
|
|
||||||
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CmdNotificationsMarkRead represents a sub command of notifications to list read notifications
|
// CmdNotificationsMarkRead represents a sub command of notifications to list read notifications
|
||||||
@@ -22,8 +23,11 @@ var CmdNotificationsMarkRead = cli.Command{
|
|||||||
Description: "Mark all filtered or a specific notification as read",
|
Description: "Mark all filtered or a specific notification as read",
|
||||||
ArgsUsage: "[all | <notification id>]",
|
ArgsUsage: "[all | <notification id>]",
|
||||||
Flags: flags.NotificationFlags,
|
Flags: flags.NotificationFlags,
|
||||||
Action: func(_ stdctx.Context, cmd *cli.Command) error {
|
Action: func(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
filter, err := flags.NotificationStateFlag.GetValues(cmd)
|
filter, err := flags.NotificationStateFlag.GetValues(cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -31,7 +35,7 @@ var CmdNotificationsMarkRead = cli.Command{
|
|||||||
if !ctx.IsSet(flags.NotificationStateFlag.Name) {
|
if !ctx.IsSet(flags.NotificationStateFlag.Name) {
|
||||||
filter = []string{string(gitea.NotifyStatusUnread)}
|
filter = []string{string(gitea.NotifyStatusUnread)}
|
||||||
}
|
}
|
||||||
return markNotificationAs(ctx, filter, gitea.NotifyStatusRead)
|
return markNotificationAs(requestCtx, ctx, filter, gitea.NotifyStatusRead)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,8 +47,11 @@ var CmdNotificationsMarkUnread = cli.Command{
|
|||||||
Description: "Mark all filtered or a specific notification as unread",
|
Description: "Mark all filtered or a specific notification as unread",
|
||||||
ArgsUsage: "[all | <notification id>]",
|
ArgsUsage: "[all | <notification id>]",
|
||||||
Flags: flags.NotificationFlags,
|
Flags: flags.NotificationFlags,
|
||||||
Action: func(_ stdctx.Context, cmd *cli.Command) error {
|
Action: func(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
filter, err := flags.NotificationStateFlag.GetValues(cmd)
|
filter, err := flags.NotificationStateFlag.GetValues(cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -52,7 +59,7 @@ var CmdNotificationsMarkUnread = cli.Command{
|
|||||||
if !ctx.IsSet(flags.NotificationStateFlag.Name) {
|
if !ctx.IsSet(flags.NotificationStateFlag.Name) {
|
||||||
filter = []string{string(gitea.NotifyStatusRead)}
|
filter = []string{string(gitea.NotifyStatusRead)}
|
||||||
}
|
}
|
||||||
return markNotificationAs(ctx, filter, gitea.NotifyStatusUnread)
|
return markNotificationAs(requestCtx, ctx, filter, gitea.NotifyStatusUnread)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,8 +71,11 @@ var CmdNotificationsMarkPinned = cli.Command{
|
|||||||
Description: "Mark all filtered or a specific notification as pinned",
|
Description: "Mark all filtered or a specific notification as pinned",
|
||||||
ArgsUsage: "[all | <notification id>]",
|
ArgsUsage: "[all | <notification id>]",
|
||||||
Flags: flags.NotificationFlags,
|
Flags: flags.NotificationFlags,
|
||||||
Action: func(_ stdctx.Context, cmd *cli.Command) error {
|
Action: func(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
filter, err := flags.NotificationStateFlag.GetValues(cmd)
|
filter, err := flags.NotificationStateFlag.GetValues(cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -73,7 +83,7 @@ var CmdNotificationsMarkPinned = cli.Command{
|
|||||||
if !ctx.IsSet(flags.NotificationStateFlag.Name) {
|
if !ctx.IsSet(flags.NotificationStateFlag.Name) {
|
||||||
filter = []string{string(gitea.NotifyStatusUnread)}
|
filter = []string{string(gitea.NotifyStatusUnread)}
|
||||||
}
|
}
|
||||||
return markNotificationAs(ctx, filter, gitea.NotifyStatusPinned)
|
return markNotificationAs(requestCtx, ctx, filter, gitea.NotifyStatusPinned)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,15 +94,18 @@ var CmdNotificationsUnpin = cli.Command{
|
|||||||
Description: "Marks all pinned or a specific notification as read",
|
Description: "Marks all pinned or a specific notification as read",
|
||||||
ArgsUsage: "[all | <notification id>]",
|
ArgsUsage: "[all | <notification id>]",
|
||||||
Flags: flags.NotificationFlags,
|
Flags: flags.NotificationFlags,
|
||||||
Action: func(_ stdctx.Context, cmd *cli.Command) error {
|
Action: func(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
filter := []string{string(gitea.NotifyStatusPinned)}
|
filter := []string{string(gitea.NotifyStatusPinned)}
|
||||||
// NOTE: we implicitly mark it as read, to match web UI semantics. marking as unread might be more useful?
|
// NOTE: we implicitly mark it as read, to match web UI semantics. marking as unread might be more useful?
|
||||||
return markNotificationAs(ctx, filter, gitea.NotifyStatusRead)
|
return markNotificationAs(requestCtx, ctx, filter, gitea.NotifyStatusRead)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func markNotificationAs(cmd *context.TeaContext, filterStates []string, targetState gitea.NotifyStatus) (err error) {
|
func markNotificationAs(ctx stdctx.Context, cmd *context.TeaContext, filterStates []string, targetState gitea.NotifyStatus) (err error) {
|
||||||
client := cmd.Login.Client()
|
client := cmd.Login.Client()
|
||||||
subject := cmd.Args().First()
|
subject := cmd.Args().First()
|
||||||
allRepos := cmd.Bool("mine")
|
allRepos := cmd.Bool("mine")
|
||||||
@@ -107,10 +120,12 @@ func markNotificationAs(cmd *context.TeaContext, filterStates []string, targetSt
|
|||||||
opts := gitea.MarkNotificationOptions{Status: states, ToStatus: targetState}
|
opts := gitea.MarkNotificationOptions{Status: states, ToStatus: targetState}
|
||||||
|
|
||||||
if allRepos {
|
if allRepos {
|
||||||
_, _, err = client.ReadNotifications(opts)
|
_, _, err = client.Notifications.MarkRead(ctx, opts)
|
||||||
} else {
|
} else {
|
||||||
cmd.Ensure(context.CtxRequirement{RemoteRepo: true})
|
if err := cmd.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
_, _, err = client.ReadRepoNotifications(cmd.Owner, cmd.Repo, opts)
|
return err
|
||||||
|
}
|
||||||
|
_, _, err = client.Notifications.MarkReadByRepo(ctx, cmd.Owner, cmd.Repo, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: print all affected notification subject URLs
|
// TODO: print all affected notification subject URLs
|
||||||
@@ -121,12 +136,12 @@ func markNotificationAs(cmd *context.TeaContext, filterStates []string, targetSt
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, _, err = client.ReadNotification(id, targetState)
|
_, _, err = client.Notifications.MarkReadByID(ctx, id, targetState)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
n, _, err := client.GetNotification(id)
|
n, _, err := client.Notifications.GetByID(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-7
@@ -8,9 +8,9 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
"gitea.dev/tea/cmd/flags"
|
||||||
"code.gitea.io/tea/modules/context"
|
"gitea.dev/tea/modules/context"
|
||||||
local_git "code.gitea.io/tea/modules/git"
|
local_git "gitea.dev/tea/modules/git"
|
||||||
|
|
||||||
"github.com/skratchdot/open-golang/open"
|
"github.com/skratchdot/open-golang/open"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
@@ -27,9 +27,14 @@ var CmdOpen = cli.Command{
|
|||||||
Flags: append([]cli.Flag{}, flags.LoginRepoFlags...),
|
Flags: append([]cli.Flag{}, flags.LoginRepoFlags...),
|
||||||
}
|
}
|
||||||
|
|
||||||
func runOpen(_ stdctx.Context, cmd *cli.Command) error {
|
func runOpen(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
var suffix string
|
var suffix string
|
||||||
number := ctx.Args().Get(0)
|
number := ctx.Args().Get(0)
|
||||||
@@ -74,5 +79,10 @@ func runOpen(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
suffix = number
|
suffix = number
|
||||||
}
|
}
|
||||||
|
|
||||||
return open.Run(path.Join(ctx.GetRemoteRepoHTMLURL(), suffix))
|
repoURL, err := ctx.GetRemoteRepoHTMLURL()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return open.Run(path.Join(repoURL, suffix))
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-11
@@ -6,9 +6,9 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
stdctx "context"
|
stdctx "context"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/organizations"
|
"gitea.dev/tea/cmd/organizations"
|
||||||
"code.gitea.io/tea/modules/context"
|
"gitea.dev/tea/modules/context"
|
||||||
"code.gitea.io/tea/modules/print"
|
"gitea.dev/tea/modules/print"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
@@ -31,19 +31,18 @@ var CmdOrgs = cli.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func runOrganizations(ctx stdctx.Context, cmd *cli.Command) error {
|
func runOrganizations(ctx stdctx.Context, cmd *cli.Command) error {
|
||||||
teaCtx := context.InitCommand(cmd)
|
teaCtx, err := context.InitCommand(cmd)
|
||||||
if teaCtx.Args().Len() == 1 {
|
if err != nil {
|
||||||
return runOrganizationDetail(teaCtx)
|
return err
|
||||||
}
|
}
|
||||||
return organizations.RunOrganizationList(ctx, cmd)
|
if teaCtx.Args().Len() == 1 {
|
||||||
}
|
org, _, err := teaCtx.Login.Client().Organizations.GetOrg(ctx, teaCtx.Args().First())
|
||||||
|
|
||||||
func runOrganizationDetail(ctx *context.TeaContext) error {
|
|
||||||
org, _, err := ctx.Login.Client().GetOrg(ctx.Args().First())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
print.OrganizationDetails(org)
|
print.OrganizationDetails(org)
|
||||||
return nil
|
return nil
|
||||||
|
}
|
||||||
|
return organizations.RunOrganizationList(ctx, cmd)
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-10
@@ -4,14 +4,15 @@
|
|||||||
package organizations
|
package organizations
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
stdctx "context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
stdctx "context"
|
"gitea.dev/sdk"
|
||||||
|
|
||||||
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/print"
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
"code.gitea.io/tea/modules/print"
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -25,7 +26,7 @@ var CmdOrganizationCreate = cli.Command{
|
|||||||
ArgsUsage: "<organization name>",
|
ArgsUsage: "<organization name>",
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "name",
|
Name: "full-name",
|
||||||
Aliases: []string{"n"},
|
Aliases: []string{"n"},
|
||||||
},
|
},
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
@@ -52,8 +53,11 @@ var CmdOrganizationCreate = cli.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RunOrganizationCreate sets up a new organization
|
// RunOrganizationCreate sets up a new organization
|
||||||
func RunOrganizationCreate(_ stdctx.Context, cmd *cli.Command) error {
|
func RunOrganizationCreate(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if ctx.Args().Len() < 1 {
|
if ctx.Args().Len() < 1 {
|
||||||
return fmt.Errorf("organization name is required")
|
return fmt.Errorf("organization name is required")
|
||||||
@@ -71,9 +75,9 @@ func RunOrganizationCreate(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
return fmt.Errorf("unknown visibility '%s'", ctx.String("visibility"))
|
return fmt.Errorf("unknown visibility '%s'", ctx.String("visibility"))
|
||||||
}
|
}
|
||||||
|
|
||||||
org, _, err := ctx.Login.Client().CreateOrg(gitea.CreateOrgOption{
|
org, _, err := ctx.Login.Client().Organizations.CreateOrg(requestCtx, gitea.CreateOrgOption{
|
||||||
Name: ctx.Args().First(),
|
Name: ctx.Args().First(),
|
||||||
// FullName: , // not really meaningful for orgs (not displayed in webui, use description instead?)
|
FullName: ctx.String("full-name"),
|
||||||
Description: ctx.String("description"),
|
Description: ctx.String("description"),
|
||||||
Website: ctx.String("website"),
|
Website: ctx.String("website"),
|
||||||
Location: ctx.String("location"),
|
Location: ctx.String("location"),
|
||||||
|
|||||||
@@ -7,9 +7,10 @@ import (
|
|||||||
stdctx "context"
|
stdctx "context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"code.gitea.io/tea/cmd/flags"
|
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
|
|
||||||
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CmdOrganizationDelete represents a sub command of organizations to delete a given user organization
|
// CmdOrganizationDelete represents a sub command of organizations to delete a given user organization
|
||||||
@@ -27,8 +28,11 @@ var CmdOrganizationDelete = cli.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RunOrganizationDelete delete user organization
|
// RunOrganizationDelete delete user organization
|
||||||
func RunOrganizationDelete(_ stdctx.Context, cmd *cli.Command) error {
|
func RunOrganizationDelete(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
client := ctx.Login.Client()
|
client := ctx.Login.Client()
|
||||||
|
|
||||||
@@ -36,7 +40,7 @@ func RunOrganizationDelete(_ stdctx.Context, cmd *cli.Command) error {
|
|||||||
return fmt.Errorf("organization name is required")
|
return fmt.Errorf("organization name is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
response, err := client.DeleteOrg(ctx.Args().First())
|
response, err := client.Organizations.DeleteOrg(requestCtx, ctx.Args().First())
|
||||||
if response != nil && response.StatusCode == 404 {
|
if response != nil && response.StatusCode == 404 {
|
||||||
return fmt.Errorf("organization not found: %s", ctx.Args().First())
|
return fmt.Errorf("organization not found: %s", ctx.Args().First())
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-11
@@ -6,11 +6,12 @@ package organizations
|
|||||||
import (
|
import (
|
||||||
stdctx "context"
|
stdctx "context"
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/cmd/flags"
|
|
||||||
"code.gitea.io/tea/modules/context"
|
|
||||||
"code.gitea.io/tea/modules/print"
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
|
|
||||||
|
"gitea.dev/tea/cmd/flags"
|
||||||
|
"gitea.dev/tea/modules/context"
|
||||||
|
"gitea.dev/tea/modules/print"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CmdOrganizationList represents a sub command of organizations to list users organizations
|
// CmdOrganizationList represents a sub command of organizations to list users organizations
|
||||||
@@ -28,18 +29,19 @@ var CmdOrganizationList = cli.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RunOrganizationList list user organizations
|
// RunOrganizationList list user organizations
|
||||||
func RunOrganizationList(_ stdctx.Context, cmd *cli.Command) error {
|
func RunOrganizationList(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
client := ctx.Login.Client()
|
client := ctx.Login.Client()
|
||||||
|
|
||||||
userOrganizations, _, err := client.ListUserOrgs(ctx.Login.User, gitea.ListOrgsOptions{
|
userOrganizations, _, err := client.Organizations.ListUserOrgs(requestCtx, ctx.Login.User, gitea.ListOrgsOptions{
|
||||||
ListOptions: flags.GetListOptions(),
|
ListOptions: flags.GetListOptions(cmd),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
print.OrganizationsList(userOrganizations, ctx.Output)
|
return print.OrganizationsList(userOrganizations, ctx.Output)
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
+47
-80
@@ -5,41 +5,26 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
stdctx "context"
|
stdctx "context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
gitea "gitea.dev/sdk"
|
||||||
"code.gitea.io/tea/cmd/flags"
|
|
||||||
"code.gitea.io/tea/cmd/pulls"
|
"gitea.dev/tea/cmd/flags"
|
||||||
"code.gitea.io/tea/modules/context"
|
"gitea.dev/tea/cmd/pulls"
|
||||||
"code.gitea.io/tea/modules/interact"
|
"gitea.dev/tea/modules/context"
|
||||||
"code.gitea.io/tea/modules/print"
|
"gitea.dev/tea/modules/interact"
|
||||||
"code.gitea.io/tea/modules/utils"
|
"gitea.dev/tea/modules/print"
|
||||||
|
"gitea.dev/tea/modules/utils"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
type pullLabelData struct {
|
type pullLabelData = detailLabelData
|
||||||
Name string `json:"name"`
|
|
||||||
Color string `json:"color"`
|
|
||||||
Description string `json:"description"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type pullReviewData struct {
|
type pullReviewData = detailReviewData
|
||||||
ID int64 `json:"id"`
|
|
||||||
Reviewer string `json:"reviewer"`
|
|
||||||
State gitea.ReviewStateType `json:"state"`
|
|
||||||
Body string `json:"body"`
|
|
||||||
Created time.Time `json:"created"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type pullCommentData struct {
|
type pullCommentData = detailCommentData
|
||||||
ID int64 `json:"id"`
|
|
||||||
Author string `json:"author"`
|
|
||||||
Created time.Time `json:"created"`
|
|
||||||
Body string `json:"body"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type pullData struct {
|
type pullData struct {
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
@@ -88,10 +73,14 @@ var CmdPulls = cli.Command{
|
|||||||
&pulls.CmdPullsCreate,
|
&pulls.CmdPullsCreate,
|
||||||
&pulls.CmdPullsClose,
|
&pulls.CmdPullsClose,
|
||||||
&pulls.CmdPullsReopen,
|
&pulls.CmdPullsReopen,
|
||||||
|
&pulls.CmdPullsEdit,
|
||||||
&pulls.CmdPullsReview,
|
&pulls.CmdPullsReview,
|
||||||
&pulls.CmdPullsApprove,
|
&pulls.CmdPullsApprove,
|
||||||
&pulls.CmdPullsReject,
|
&pulls.CmdPullsReject,
|
||||||
&pulls.CmdPullsMerge,
|
&pulls.CmdPullsMerge,
|
||||||
|
&pulls.CmdPullsReviewComments,
|
||||||
|
&pulls.CmdPullsResolve,
|
||||||
|
&pulls.CmdPullsUnresolve,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,35 +91,49 @@ func runPulls(ctx stdctx.Context, cmd *cli.Command) error {
|
|||||||
return pulls.RunPullsList(ctx, cmd)
|
return pulls.RunPullsList(ctx, cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func runPullDetail(_ stdctx.Context, cmd *cli.Command, index string) error {
|
func runPullDetail(requestCtx stdctx.Context, cmd *cli.Command, index string) error {
|
||||||
ctx := context.InitCommand(cmd)
|
ctx, err := context.InitCommand(cmd)
|
||||||
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
idx, err := utils.ArgToIndex(index)
|
idx, err := utils.ArgToIndex(index)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
client := ctx.Login.Client()
|
client := ctx.Login.Client()
|
||||||
pr, _, err := client.GetPullRequest(ctx.Owner, ctx.Repo, idx)
|
pr, _, err := client.PullRequests.GetPullRequest(requestCtx, ctx.Owner, ctx.Repo, idx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
reviews, _, err := client.ListPullReviews(ctx.Owner, ctx.Repo, idx, gitea.ListPullReviewsOptions{
|
var reviews []*gitea.PullReview
|
||||||
ListOptions: gitea.ListOptions{Page: -1},
|
for page := 1; ; {
|
||||||
|
page_reviews, resp, err := client.PullRequests.ListPullReviews(requestCtx, ctx.Owner, ctx.Repo, idx, gitea.ListPullReviewsOptions{
|
||||||
|
ListOptions: gitea.ListOptions{Page: page, PageSize: 50},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("error while loading reviews: %v\n", err)
|
fmt.Printf("error while loading reviews: %v\n", err)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
reviews = append(reviews, page_reviews...)
|
||||||
|
if resp == nil || resp.NextPage == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
page = resp.NextPage
|
||||||
}
|
}
|
||||||
|
|
||||||
if ctx.IsSet("output") {
|
if ctx.IsSet("output") {
|
||||||
switch ctx.String("output") {
|
switch ctx.String("output") {
|
||||||
case "json":
|
case "json":
|
||||||
return runPullDetailAsJSON(ctx, pr, reviews)
|
return runPullDetailAsJSON(requestCtx, ctx, pr, reviews)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ci, _, err := client.GetCombinedStatus(ctx.Owner, ctx.Repo, pr.Head.Sha)
|
ci, _, err := client.Repositories.GetCombinedStatus(requestCtx, ctx.Owner, ctx.Repo, pr.Head.Sha)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("error while loading CI: %v\n", err)
|
fmt.Printf("error while loading CI: %v\n", err)
|
||||||
}
|
}
|
||||||
@@ -138,7 +141,7 @@ func runPullDetail(_ stdctx.Context, cmd *cli.Command, index string) error {
|
|||||||
print.PullDetails(pr, reviews, ci)
|
print.PullDetails(pr, reviews, ci)
|
||||||
|
|
||||||
if pr.Comments > 0 {
|
if pr.Comments > 0 {
|
||||||
err = interact.ShowCommentsMaybeInteractive(ctx, idx, pr.Comments)
|
err = interact.ShowCommentsMaybeInteractive(requestCtx, ctx, idx, pr.Comments)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("error loading comments: %v\n", err)
|
fmt.Printf("error loading comments: %v\n", err)
|
||||||
}
|
}
|
||||||
@@ -147,30 +150,9 @@ func runPullDetail(_ stdctx.Context, cmd *cli.Command, index string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func runPullDetailAsJSON(ctx *context.TeaContext, pr *gitea.PullRequest, reviews []*gitea.PullReview) error {
|
func runPullDetailAsJSON(requestCtx stdctx.Context, ctx *context.TeaContext, pr *gitea.PullRequest, reviews []*gitea.PullReview) error {
|
||||||
c := ctx.Login.Client()
|
c := ctx.Login.Client()
|
||||||
opts := gitea.ListIssueCommentOptions{ListOptions: flags.GetListOptions()}
|
opts := gitea.ListIssueCommentOptions{ListOptions: flags.GetListOptions(ctx.Command)}
|
||||||
|
|
||||||
labelSlice := make([]pullLabelData, 0, len(pr.Labels))
|
|
||||||
for _, label := range pr.Labels {
|
|
||||||
labelSlice = append(labelSlice, pullLabelData{label.Name, label.Color, label.Description})
|
|
||||||
}
|
|
||||||
|
|
||||||
assigneesSlice := make([]string, 0, len(pr.Assignees))
|
|
||||||
for _, assignee := range pr.Assignees {
|
|
||||||
assigneesSlice = append(assigneesSlice, assignee.UserName)
|
|
||||||
}
|
|
||||||
|
|
||||||
reviewsSlice := make([]pullReviewData, 0, len(reviews))
|
|
||||||
for _, review := range reviews {
|
|
||||||
reviewsSlice = append(reviewsSlice, pullReviewData{
|
|
||||||
ID: review.ID,
|
|
||||||
Reviewer: review.Reviewer.UserName,
|
|
||||||
State: review.State,
|
|
||||||
Body: review.Body,
|
|
||||||
Created: review.Submitted,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
mergedBy := ""
|
mergedBy := ""
|
||||||
if pr.MergedBy != nil {
|
if pr.MergedBy != nil {
|
||||||
@@ -184,10 +166,10 @@ func runPullDetailAsJSON(ctx *context.TeaContext, pr *gitea.PullRequest, reviews
|
|||||||
State: pr.State,
|
State: pr.State,
|
||||||
Created: pr.Created,
|
Created: pr.Created,
|
||||||
Updated: pr.Updated,
|
Updated: pr.Updated,
|
||||||
User: pr.Poster.UserName,
|
User: username(pr.Poster),
|
||||||
Body: pr.Body,
|
Body: pr.Body,
|
||||||
Labels: labelSlice,
|
Labels: buildDetailLabels(pr.Labels),
|
||||||
Assignees: assigneesSlice,
|
Assignees: buildDetailAssignees(pr.Assignees),
|
||||||
URL: pr.HTMLURL,
|
URL: pr.HTMLURL,
|
||||||
Base: pr.Base.Ref,
|
Base: pr.Base.Ref,
|
||||||
Head: pr.Head.Ref,
|
Head: pr.Head.Ref,
|
||||||
@@ -198,33 +180,18 @@ func runPullDetailAsJSON(ctx *context.TeaContext, pr *gitea.PullRequest, reviews
|
|||||||
MergedAt: pr.Merged,
|
MergedAt: pr.Merged,
|
||||||
MergedBy: mergedBy,
|
MergedBy: mergedBy,
|
||||||
ClosedAt: pr.Closed,
|
ClosedAt: pr.Closed,
|
||||||
Reviews: reviewsSlice,
|
Reviews: buildDetailReviews(reviews),
|
||||||
Comments: make([]pullCommentData, 0),
|
Comments: make([]pullCommentData, 0),
|
||||||
}
|
}
|
||||||
|
|
||||||
if ctx.Bool("comments") {
|
if ctx.Bool("comments") {
|
||||||
comments, _, err := c.ListIssueComments(ctx.Owner, ctx.Repo, pr.Index, opts)
|
comments, _, err := c.Issues.ListIssueComments(requestCtx, ctx.Owner, ctx.Repo, pr.Index, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
pullSlice.Comments = make([]pullCommentData, 0, len(comments))
|
pullSlice.Comments = buildDetailComments(comments)
|
||||||
for _, comment := range comments {
|
|
||||||
pullSlice.Comments = append(pullSlice.Comments, pullCommentData{
|
|
||||||
ID: comment.ID,
|
|
||||||
Author: comment.Poster.UserName,
|
|
||||||
Body: comment.Body,
|
|
||||||
Created: comment.Created,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonData, err := json.MarshalIndent(pullSlice, "", "\t")
|
return writeIndentedJSON(ctx.Writer, pullSlice)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = fmt.Fprintf(ctx.Writer, "%s\n", jsonData)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user