Skip to content

Commit 59071c6

Browse files
committed
adding gitea test suite
Signed-off-by: Soule BA <[email protected]>
1 parent 41a9ae7 commit 59071c6

19 files changed

+382
-61
lines changed

.github/workflows/e2e-gitea.yaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
name: e2e-gitea
3+
4+
"on":
5+
workflow_dispatch:
6+
pull_request:
7+
push:
8+
branches:
9+
- main
10+
tags-ignore: ["*"]
11+
12+
jobs:
13+
test:
14+
timeout-minutes: 60
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
19+
- name: Restore Go cache
20+
uses: actions/cache@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8 # v3.3.1
21+
with:
22+
path: ~/go/pkg/mod
23+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
24+
restore-keys: |
25+
${{ runner.os }}-go-
26+
- name: Setup Go
27+
uses: actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753 # v4.0.1
28+
with:
29+
go-version: 1.20.x
30+
- name: Start Provider instances
31+
run: make start-provider-instances-gitea GITEA_VERSION=latest
32+
- name: Run tests [gitea]
33+
run: |
34+
export GITEA_TOKEN=$(cat /tmp/gitea-token)
35+
make test-e2e-gitea
36+
- name: Upload coverage to Codecov
37+
uses: codecov/codecov-action@eaaf4bedf32dbdc6b720b63067d99c4d77d6047d # v3.1.4
38+
with:
39+
files: ./coverage.txt

.github/workflows/e2e-gitlab.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
with:
2929
go-version: 1.20.x
3030
- name: Start Provider instances
31-
run: make start-provider-instances GITLAB_VERSION=latest
31+
run: make start-provider-instances-gitlab GITLAB_VERSION=latest
3232
- name: Run tests [gitlab]
3333
run: make test-e2e-gitlab
3434
- name: Upload coverage to Codecov

Makefile

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,19 @@ GITLAB_TEST_REPO_NAME ?= fluxcd-testing-repo
2222
GITLAB_TEST_SUBGROUP ?= fluxcd-testing-sub-group
2323
GITLAB_TEST_TEAM_NAME ?= fluxcd-testing-2
2424

25+
# Gitea specific testing variables
26+
GITEA_USER_UID ?= 1000
27+
GITEA_BASE_URL ?= http://127.0.0.1:3000
28+
GITEA_TOKEN ?= not-set
29+
GITEA_TEST_TEAM_NAME ?= fluxcd-testing-2
30+
2531
start-provider-instances-gitlab:
2632
GITLAB_TOKEN=$(GITLAB_TOKEN) GIT_PROVIDER_USER=$(GIT_PROVIDER_USER) GIT_PROVIDER_ORGANIZATION=$(GIT_PROVIDER_ORGANIZATION) GITLAB_TEST_REPO_NAME=$(GITLAB_TEST_REPO_NAME) GITLAB_TEST_SUBGROUP=$(GITLAB_TEST_SUBGROUP) GITLAB_TEST_TEAM_NAME=$(GITLAB_TEST_TEAM_NAME) docker compose up -d gitlab
2733
GITLAB_BASE_URL=$(GITLAB_BASE_URL) GITLAB_TOKEN=$(GITLAB_TOKEN) ./tests/gitlab/await-healthy.sh
2834

29-
start-provider-instances: start-provider-instances-gitlab
35+
start-provider-instances-gitea:
36+
GITEA_TEST_TEAM_NAME=$(GITEA_TEST_TEAM_NAME) GIT_PROVIDER_ORGANIZATION=$(GIT_PROVIDER_ORGANIZATION) GITEA_USER=$(GIT_PROVIDER_USER) docker compose up -d gitea
37+
GITEA_USER=$(GIT_PROVIDER_USER) GITEA_BASE_URL=$(GITEA_BASE_URL) ./tests/gitea/await-healthy.sh
3038

3139
stop-provider-instances:
3240
docker compose down --volumes
@@ -43,3 +51,7 @@ test-e2e-gitlab: tidy fmt vet
4351
test-e2e-stash: tidy fmt vet
4452
go test ${TEST_FLAGS} -race -coverprofile=coverage.txt -covermode=atomic -tags=e2e ./stash/...
4553

54+
test-e2e-gitea: tidy fmt vet
55+
GITEA_BASE_URL=$(GITEA_BASE_URL) GITEA_TOKEN=$(GITEA_TOKEN) GITEA_TEST_TEAM_NAME=$(GITEA_TEST_TEAM_NAME) \
56+
GIT_PROVIDER_ORGANIZATION=$(GIT_PROVIDER_ORGANIZATION) GITEA_USER=$(GIT_PROVIDER_USER) \
57+
go test ${TEST_FLAGS} -race -coverprofile=coverage.txt -covermode=atomic -tags=e2e ./gitea/...

docker-compose.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,30 @@ services:
2727
test: /healthcheck-and-setup.sh
2828
interval: 60s
2929
timeout: 10m
30+
gitea:
31+
image: gitea/gitea:${GITEA_VERSION:-latest}
32+
platform: linux/amd64
33+
ports:
34+
- 3000:3000
35+
environment:
36+
- USER_UID=${GITEA_USER_UID:-1000}
37+
- USER_GID=1000
38+
- GITEA__security__INSTALL_LOCK=${GITEA_INSTALL_LOCK:-true}
39+
- GIT_PROVIDER_ORGANIZATION=$GIT_PROVIDER_ORGANIZATION
40+
- GITEA_USER=$GITEA_USER
41+
- GITEA_TEST_TEAM_NAME=$GITEA_TEST_TEAM_NAME
42+
labels:
43+
go-git-provider-gitea/owned: ""
44+
volumes:
45+
- gitea:/data
46+
- ${PWD}/tests/gitea/healthcheck-and-setup.sh:/healthcheck-and-setup.sh:Z
47+
healthcheck:
48+
test: /healthcheck-and-setup.sh
49+
interval: 60s
50+
timeout: 10m
3051

3152
volumes:
3253
gitlab-config:
3354
gitlab-logs:
3455
gitlab-data:
56+
gitea:

gitea/auth_test.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,32 @@ func Test_DomainVariations(t *testing.T) {
3030
expectedErrs []error
3131
}{
3232
{
33-
name: "gitea.com domain",
34-
opts: gitprovider.WithDomain("gitea.com"),
35-
want: "gitea.com",
33+
name: "try.gitea.io domain",
34+
opts: gitprovider.WithDomain("try.gitea.io"),
35+
want: "try.gitea.io",
3636
},
3737
{
3838
name: "custom domain without protocol",
39-
opts: gitprovider.WithDomain("my-gitea.dev.com"),
40-
want: "https://my-gitea.dev.com",
39+
opts: gitprovider.WithDomain("try.gitea.io"),
40+
want: "try.gitea.io",
4141
},
4242
{
4343
name: "custom domain with https protocol",
44-
opts: gitprovider.WithDomain("https://my-gitea.dev.com"),
45-
want: "https://my-gitea.dev.com",
44+
opts: gitprovider.WithDomain("https://try.gitea.io"),
45+
want: "https://try.gitea.io",
4646
},
4747
{
4848
name: "custom domain with http protocol",
49-
opts: gitprovider.WithDomain("http://my-gitea.dev.com"),
50-
want: "http://my-gitea.dev.com",
49+
opts: gitprovider.WithDomain("http://try.gitea.io"),
50+
want: "http://try.gitea.io",
5151
},
5252
}
5353
for _, tt := range tests {
5454
t.Run(tt.name, func(t *testing.T) {
55-
c1, _ := NewClient("token", tt.opts)
55+
c1, err := NewClient("token", tt.opts)
56+
if err != nil {
57+
t.Fatal(err)
58+
}
5659
assertEqual(t, tt.want, c1.SupportedDomain())
5760

5861
c2, _ := NewClient("token", tt.opts)

gitea/client_repository_branch.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ type BranchClient struct {
3333
}
3434

3535
// Create creates a branch with the given specifications.
36+
// Creating a branch from a commit is noy supported by Gitea.
37+
// see: https://github.com/go-gitea/gitea/issues/22139
3638
func (c *BranchClient) Create(ctx context.Context, branch, sha string) error {
3739

3840
// Doesn't seem to support specific sha?
3941
opts := gitea.CreateBranchOption{
40-
BranchName: branch,
41-
// TODO: Explore branching from given SHA
42-
// OldBranchName: sha,
42+
BranchName: branch,
43+
OldBranchName: sha,
4344
}
4445
err := opts.Validate()
4546
if err != nil {

gitea/client_repository_commit.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ func (c *CommitClient) Create(ctx context.Context, branch string, message string
7878

7979
resp, err := c.c.CreateCommits(ctx, c.ref.GetIdentity(), c.ref.GetRepository(), *files[0].Path, &gitea.CreateFileOptions{
8080
Content: *files[0].Content,
81+
FileOptions: gitea.FileOptions{
82+
Message: message,
83+
BranchName: branch,
84+
},
8185
})
8286
if err != nil {
8387
return nil, fmt.Errorf("failed to create commit: %w", err)

gitea/client_repository_pullrequest.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ func (c *PullRequestClient) List(ctx context.Context) ([]gitprovider.PullRequest
5353
// Create creates a pull request with the given specifications.
5454
func (c *PullRequestClient) Create(ctx context.Context, title, branch, baseBranch, description string) (gitprovider.PullRequest, error) {
5555
prOpts := gitea.CreatePullRequestOption{
56+
Base: baseBranch,
5657
Title: title,
58+
Head: branch,
59+
Body: description,
5760
}
5861
pr, _, err := c.c.Client().CreatePullRequest(c.ref.GetIdentity(), c.ref.GetRepository(), prOpts)
5962

@@ -86,6 +89,7 @@ func (c *PullRequestClient) Edit(ctx context.Context, number int, opts gitprovid
8689
}
8790

8891
// Merge merges a pull request with the given specifications.
92+
// Supported merge methods are: MergeMethodMerge, MergeMethodRebase, MergeMethodSquash
8993
func (c *PullRequestClient) Merge(ctx context.Context, number int, mergeMethod gitprovider.MergeMethod, message string) error {
9094
mergeOpts := gitea.MergePullRequestOption{
9195
Style: gitea.MergeStyle(mergeMethod),

gitea/giteaclient.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ type giteaClient interface {
8989
// RemoveTeam is a wrapper for "DELETE /repos/{owner}/{repo}/teams/{team_slug}".
9090
// This function handles HTTP error wrapping.
9191
RemoveTeam(ctx context.Context, orgName, repo, teamName string) error
92-
93-
//
9492
}
9593

9694
type giteaClientImpl struct {
@@ -324,10 +322,20 @@ func (c *giteaClientImpl) ListCommits(_ context.Context, owner, repo, branch str
324322
},
325323
SHA: branch,
326324
}
327-
apiObjs, _, listErr := c.c.ListRepoCommits(owner, repo, opts)
328-
if listErr != nil {
329-
return nil, listErr
325+
apiObjs := []*gitea.Commit{}
326+
err := allPages(&opts.ListOptions, func() (*gitea.Response, error) {
327+
pageObjs, resp, listErr := c.c.ListRepoCommits(owner, repo, opts)
328+
if len(pageObjs) > 0 {
329+
apiObjs = append(apiObjs, pageObjs...)
330+
return resp, listErr
331+
}
332+
return nil, nil
333+
})
334+
335+
if err != nil {
336+
return nil, err
330337
}
338+
331339
return apiObjs, nil
332340
}
333341

@@ -365,6 +373,9 @@ func (c *giteaClientImpl) GetTeamPermissions(_ context.Context, orgName, repo, t
365373
if err != nil {
366374
return nil, handleHTTPError(resp, err)
367375
}
376+
if apiObj == nil {
377+
return nil, fmt.Errorf("team %s not found in repository %s/%s", teamName, orgName, repo)
378+
}
368379

369380
return &apiObj.Permission, nil
370381
}
@@ -379,6 +390,8 @@ func (c *giteaClientImpl) ListRepoTeams(ctx context.Context, orgName, repo strin
379390
}
380391

381392
// AddTeam adds the given team to the given repository.
393+
// We don't support setting permissions for Gitea, so we ignore the permission parameter.
394+
// see https://github.com/go-gitea/gitea/issues/14717
382395
func (c *giteaClientImpl) AddTeam(_ context.Context, orgName, repo, teamName string, permission gitprovider.RepositoryPermission) error {
383396
res, err := c.c.AddRepoTeam(orgName, repo, teamName)
384397
return handleHTTPError(res, err)

0 commit comments

Comments
 (0)