Skip to content

Commit

Permalink
Merge pull request #226 from fluxcd/soule-gitea
Browse files Browse the repository at this point in the history
Adding gitea provider
  • Loading branch information
souleb authored Jun 6, 2023
2 parents f046b17 + 9cea5a7 commit 251cafb
Show file tree
Hide file tree
Showing 39 changed files with 4,357 additions and 3 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/e2e-gitea.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
name: e2e-gitea

"on":
workflow_dispatch:
pull_request:
push:
branches:
- main
tags-ignore: ["*"]

jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
- name: Restore Go cache
uses: actions/cache@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8 # v3.3.1
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Setup Go
uses: actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753 # v4.0.1
with:
go-version: 1.20.x
- name: Start Provider instances
run: make start-provider-instances-gitea GITEA_VERSION=latest
- name: Run tests [gitea]
run: |
export GITEA_TOKEN=$(cat /tmp/gitea-token)
make test-e2e-gitea
- name: Upload coverage to Codecov
uses: codecov/codecov-action@eaaf4bedf32dbdc6b720b63067d99c4d77d6047d # v3.1.4
with:
files: ./coverage.txt
2 changes: 1 addition & 1 deletion .github/workflows/e2e-gitlab.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
with:
go-version: 1.20.x
- name: Start Provider instances
run: make start-provider-instances GITLAB_VERSION=latest
run: make start-provider-instances-gitlab GITLAB_VERSION=latest
- name: Run tests [gitlab]
run: make test-e2e-gitlab
- name: Upload coverage to Codecov
Expand Down
39 changes: 39 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ the following variables to your needs:
| Test team (this is an ordinary group in GitLab) | fluxcd-testing-2 | `GITLAB_TEST_TEAM_NAME` |
| Test user | fluxcd-gitprovider-bot | `GIT_PROVIDER_USER` |

After the tests have finished, you can stop the ephemeral GitLab instance with:

```
make stop-provider-instances
```

#### Stash

```
Expand All @@ -86,6 +92,39 @@ make test-e2e-stash
| Test organization | go-git-provider-testing | `GIT_PROVIDER_ORGANIZATION` |
| Test team | fluxcd-test-team | `STASH_TEST_TEAM_NAME` |


#### Gitea

For the Gitea tests there is automation in place to spin up an ephemeral Gitea instance to run the test suite against:

```
make start-provider-instances-gitea
```

As soon as the containers are up and Gitea is running, execute the tests:

```
make test-e2e-gitea
```

The Gitea token is stored in `/tmp/gitea-token` by default.

The Make target automatically runs the tests against the ephemeral instance. To change the test configuration, adjust
the following variables to your needs:

| Setting | Default value | Environment variable |
| ----------------------------------------------- | ----------------------------- | --------------------------- |
| Access token | read from `/tmp/gitea-token` | `GITEA_TOKEN` |
| Test group | fluxcd-testing | `GIT_PROVIDER_ORGANIZATION` |
| Test team (this is an ordinary group in GitLab) | fluxcd-testing-2 | `GITEA_TEST_TEAM_NAME` |
| Test user | fluxcd-gitprovider-bot | `GITEA_USER` |

After the tests have finished, you can stop the ephemeral Gitea instance with:

```
make stop-provider-instances
```

## Continuous Integration

The e2e test suite runs in GitHub Actions on each commit to the main branch and on branches pushed to the repository, i.e. on PRs created from people with write access.
Expand Down
13 changes: 12 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@ GITLAB_TEST_REPO_NAME ?= fluxcd-testing-repo
GITLAB_TEST_SUBGROUP ?= fluxcd-testing-sub-group
GITLAB_TEST_TEAM_NAME ?= fluxcd-testing-2

# Gitea specific testing variables
GITEA_BASE_URL ?= http://127.0.0.1:3000
GITEA_TOKEN ?=
GITEA_TEST_TEAM_NAME ?= fluxcd-testing-2

start-provider-instances-gitlab:
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
GITLAB_BASE_URL=$(GITLAB_BASE_URL) GITLAB_TOKEN=$(GITLAB_TOKEN) ./tests/gitlab/await-healthy.sh

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

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

test-e2e-gitea: tidy fmt vet
GITEA_BASE_URL=$(GITEA_BASE_URL) GITEA_TOKEN=$(GITEA_TOKEN) GITEA_TEST_TEAM_NAME=$(GITEA_TEST_TEAM_NAME) \
GIT_PROVIDER_ORGANIZATION=$(GIT_PROVIDER_ORGANIZATION) GITEA_USER=$(GIT_PROVIDER_USER) \
go test ${TEST_FLAGS} -race -coverprofile=coverage.txt -covermode=atomic -tags=e2e ./gitea/...
22 changes: 22 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,30 @@ services:
test: /healthcheck-and-setup.sh
interval: 60s
timeout: 10m
gitea:
image: gitea/gitea:${GITEA_VERSION:-latest}
platform: linux/amd64
ports:
- 3000:3000
environment:
- USER_UID=1000
- USER_GID=1000
- GITEA__security__INSTALL_LOCK=${GITEA_INSTALL_LOCK:-true}
- GIT_PROVIDER_ORGANIZATION=$GIT_PROVIDER_ORGANIZATION
- GITEA_USER=$GITEA_USER
- GITEA_TEST_TEAM_NAME=$GITEA_TEST_TEAM_NAME
labels:
go-git-provider-gitea/owned: ""
volumes:
- gitea:/data
- ${PWD}/tests/gitea/healthcheck-and-setup.sh:/healthcheck-and-setup.sh:Z
healthcheck:
test: /healthcheck-and-setup.sh
interval: 60s
timeout: 10m

volumes:
gitlab-config:
gitlab-logs:
gitlab-data:
gitea:
145 changes: 145 additions & 0 deletions gitea/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
Copyright 2023 The Flux CD contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package gitea

import (
"context"
"fmt"
"strings"

"code.gitea.io/sdk/gitea"
"github.com/fluxcd/go-git-providers/gitprovider"
)

const (
// DefaultDomain specifies the default domain used as the backend.
DefaultDomain = "gitea.com"
// ProviderID is the provider ID for Gitea.
ProviderID = gitprovider.ProviderID("gitea")
)

// NewClient creates a new gitprovider.Client instance for Gitea API endpoints.
//
// Gitea Selfhosted can be used if you specify the domain using WithDomain.
func NewClient(token string, optFns ...gitprovider.ClientOption) (gitprovider.Client, error) {
// Complete the options struct
opts, err := gitprovider.MakeClientOptions(optFns...)
if err != nil {
return nil, err
}

// Create a *http.Client using the transport chain
httpClient, err := gitprovider.BuildClientFromTransportChain(opts.GetTransportChain())
if err != nil {
return nil, err
}

domain := DefaultDomain
if opts.Domain != nil {
domain = *opts.Domain
}
baseURL := domain
if !strings.Contains(domain, "://") {
baseURL = fmt.Sprintf("https://%s/", domain)
}

gt, err := gitea.NewClient(baseURL, gitea.SetHTTPClient(httpClient), gitea.SetToken(token))
if err != nil {
return nil, err
}
// By default, turn destructive actions off. But allow overrides.
destructiveActions := false
if opts.EnableDestructiveAPICalls != nil {
destructiveActions = *opts.EnableDestructiveAPICalls
}

return newClient(gt, domain, destructiveActions), nil
}

func newClient(c *gitea.Client, domain string, destructiveActions bool) *Client {
ctx := &clientContext{c, domain, destructiveActions}
return &Client{
clientContext: ctx,
orgs: &OrganizationsClient{
clientContext: ctx,
},
orgRepos: &OrgRepositoriesClient{
clientContext: ctx,
},
userRepos: &UserRepositoriesClient{
clientContext: ctx,
},
}
}

type clientContext struct {
c *gitea.Client
domain string
destructiveActions bool
}

// Client implements the gitprovider.Client interface.
var _ gitprovider.Client = &Client{}

// Client is an interface that allows talking to a Git provider.
type Client struct {
*clientContext

orgs *OrganizationsClient
orgRepos *OrgRepositoriesClient
userRepos *UserRepositoriesClient
}

// SupportedDomain returns the domain endpoint for this client, e.g. "gitea.com", "gitea.dev.com" or
// "my-custom-git-server.com:6443". This allows a higher-level user to know what Client to use for
// what endpoints.
// This field is set at client creation time, and can't be changed.
func (c *Client) SupportedDomain() string {
return c.domain
}

// ProviderID returns the provider ID "gitea".
// This field is set at client creation time, and can't be changed.
func (c *Client) ProviderID() gitprovider.ProviderID {
return ProviderID
}

// Raw returns the Gitea client (code.gitea.io/sdk/gitea *Client)
// used under the hood for accessing Gitea.
func (c *Client) Raw() interface{} {
return c.c
}

// Organizations returns the OrganizationsClient handling sets of organizations.
func (c *Client) Organizations() gitprovider.OrganizationsClient {
return c.orgs
}

// OrgRepositories returns the OrgRepositoriesClient handling sets of repositories in an organization.
func (c *Client) OrgRepositories() gitprovider.OrgRepositoriesClient {
return c.orgRepos
}

// UserRepositories returns the UserRepositoriesClient handling sets of repositories for a user.
func (c *Client) UserRepositories() gitprovider.UserRepositoriesClient {
return c.userRepos
}

// HasTokenPermission returns true if the given token has the given permissions.
func (c *Client) HasTokenPermission(ctx context.Context, permission gitprovider.TokenPermission) (bool, error) {
return false, gitprovider.ErrNoProviderSupport
}
Loading

0 comments on commit 251cafb

Please sign in to comment.