Skip to content

Commit

Permalink
Merge remote-tracking branch 'giteaofficial/main'
Browse files Browse the repository at this point in the history
* giteaofficial/main:
  When dumping trim the standard suffices instead of a random suffix (go-gitea#19440)
  Add uploadpack.allowAnySHA1InWant to allow --filter=blob:none with older git clients (go-gitea#19430)
  Don't allow merging PR's which are being conflict checked (go-gitea#19357)
  doc: add brief intro on using traefik as reverse-proxy (go-gitea#19432)
  Fix panic in team repos API (go-gitea#19431)
  When updating mirror repo intervals by API reschedule next update too (go-gitea#19429)
  Fix nil error when some pages are rendered outside request context (go-gitea#19427)
  Add Helm Chart registry (go-gitea#19406)
  • Loading branch information
zjjhot committed Apr 20, 2022
2 parents 6c13a25 + 1e319ba commit a6b983f
Show file tree
Hide file tree
Showing 36 changed files with 787 additions and 55 deletions.
9 changes: 7 additions & 2 deletions cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (o outputType) String() string {
}

var outputTypeEnum = &outputType{
Enum: []string{"zip", "rar", "tar", "sz", "tar.gz", "tar.xz", "tar.bz2", "tar.br", "tar.lz4"},
Enum: []string{"zip", "tar", "tar.sz", "tar.gz", "tar.xz", "tar.bz2", "tar.br", "tar.lz4"},
Default: "zip",
}

Expand Down Expand Up @@ -160,7 +160,12 @@ func runDump(ctx *cli.Context) error {
fatal("Deleting default logger failed. Can not write to stdout: %v", err)
}
} else {
fileName = strings.TrimSuffix(fileName, path.Ext(fileName))
for _, suffix := range outputTypeEnum.Enum {
if strings.HasSuffix(fileName, "."+suffix) {
fileName = strings.TrimSuffix(fileName, "."+suffix)
break
}
}
fileName += "." + outType
}
setting.LoadFromExisting()
Expand Down
67 changes: 67 additions & 0 deletions docs/content/doc/packages/helm.en-us.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
date: "2022-04-14T00:00:00+00:00"
title: "Helm Chart Registry"
slug: "packages/helm"
draft: false
toc: false
menu:
sidebar:
parent: "packages"
name: "Helm"
weight: 50
identifier: "helm"
---

# Helm Chart Registry

Publish [Helm](https://helm.sh/) charts for your user or organization.

**Table of Contents**

{{< toc >}}

## Requirements

To work with the Helm Chart registry use a simple HTTP client like `curl` or the [`helm cm-push`](https://github.com/chartmuseum/helm-push/) plugin.

## Publish a package

Publish a package by running the following command:

```shell
curl --user {username}:{password} -X POST --upload-file ./{chart_file}.tgz https://gitea.example.com/api/packages/{owner}/helm/api/charts
```

or with the `helm cm-push` plugin:

```shell
helm repo add --username {username} --password {password} {repo} https://gitea.example.com/api/packages/{owner}/helm
helm cm-push ./{chart_file}.tgz {repo}
```

| Parameter | Description |
| ------------ | ----------- |
| `username` | Your Gitea username. |
| `password` | Your Gitea password or a personal access token. |
| `repo` | The name for the repository. |
| `chart_file` | The Helm Chart archive. |
| `owner` | The owner of the package. |

## Install a package

To install a Helm char from the registry, execute the following command:

```shell
helm repo add --username {username} --password {password} {repo} https://gitea.example.com/api/packages/{owner}/helm
helm repo update
helm install {name} {repo}/{chart}
```

| Parameter | Description |
| ---------- | ----------- |
| `username` | Your Gitea username. |
| `password` | Your Gitea password or a personal access token. |
| `repo` | The name for the repository. |
| `owner` | The owner of the package. |
| `name` | The local name. |
| `chart` | The name Helm Chart. |
2 changes: 1 addition & 1 deletion docs/content/doc/packages/maven.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ menu:
sidebar:
parent: "packages"
name: "Maven"
weight: 50
weight: 60
identifier: "maven"
---

Expand Down
2 changes: 1 addition & 1 deletion docs/content/doc/packages/npm.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ menu:
sidebar:
parent: "packages"
name: "npm"
weight: 60
weight: 70
identifier: "npm"
---

Expand Down
2 changes: 1 addition & 1 deletion docs/content/doc/packages/nuget.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ menu:
sidebar:
parent: "packages"
name: "NuGet"
weight: 70
weight: 80
identifier: "nuget"
---

Expand Down
1 change: 1 addition & 0 deletions docs/content/doc/packages/overview.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ The following package managers are currently supported:
| [Conan]({{< relref "doc/packages/conan.en-us.md" >}}) | C++ | `conan` |
| [Container]({{< relref "doc/packages/container.en-us.md" >}}) | - | any OCI compliant client |
| [Generic]({{< relref "doc/packages/generic.en-us.md" >}}) | - | any HTTP client |
| [Helm]({{< relref "doc/packages/helm.en-us.md" >}}) | - | any HTTP client, `cm-push` |
| [Maven]({{< relref "doc/packages/maven.en-us.md" >}}) | Java | `mvn`, `gradle` |
| [npm]({{< relref "doc/packages/npm.en-us.md" >}}) | JavaScript | `npm`, `yarn` |
| [NuGet]({{< relref "doc/packages/nuget.en-us.md" >}}) | .NET | `nuget` |
Expand Down
2 changes: 1 addition & 1 deletion docs/content/doc/packages/pypi.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ menu:
sidebar:
parent: "packages"
name: "PyPI"
weight: 80
weight: 90
identifier: "pypi"
---

Expand Down
2 changes: 1 addition & 1 deletion docs/content/doc/packages/rubygems.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ menu:
sidebar:
parent: "packages"
name: "RubyGems"
weight: 90
weight: 100
identifier: "rubygems"
---

Expand Down
15 changes: 15 additions & 0 deletions docs/content/doc/usage/reverse-proxies.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,18 @@ The added http-request will automatically add a trailing slash if needed and int

Then you **MUST** set something like `[server] ROOT_URL = http://example.com/gitea/` correctly in your configuration.

## Traefik

If you want traefik to serve your Gitea instance, you can add the following label section to your `docker-compose.yaml` (Assuming the provider is docker).

```yaml
gitea:
image: gitea/gitea
...
labels:
- "traefik.enable=true"
- "traefik.http.routers.gitea.rule=Host(`example.com`)"
- "traefik.http.services.gitea-websecure.loadbalancer.server.port=3000"
```
This config assumes that you are handling HTTPS on the traefik side and using HTTP between Gitea and traefik.
16 changes: 16 additions & 0 deletions docs/content/doc/usage/reverse-proxies.zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,19 @@ git.example.com {
```

然后您**必须**在 Gitea 的配置文件中正确的添加类似 `[server] ROOT_URL = http://git.example.com/git/` 的配置项。

## 使用 Traefik 作为反向代理服务

如果您想使用 traefik 作为 Gitea 的反向代理服务,您可以在 `docker-compose.yaml` 中添加 label 部分(假设使用 docker 作为 traefik 的 provider):

```yaml
gitea:
image: gitea/gitea
...
labels:
- "traefik.enable=true"
- "traefik.http.routers.gitea.rule=Host(`example.com`)"
- "traefik.http.services.gitea-websecure.loadbalancer.server.port=3000"
```
这份配置假设您使用 traefik 来处理 HTTPS 服务,并在其和 Gitea 之间使用 HTTP 进行通信。
166 changes: 166 additions & 0 deletions integrations/api_packages_helm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package integrations

import (
"archive/tar"
"bytes"
"compress/gzip"
"fmt"
"net/http"
"testing"
"time"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/packages"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
helm_module "code.gitea.io/gitea/modules/packages/helm"
"code.gitea.io/gitea/modules/setting"

"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
)

func TestPackageHelm(t *testing.T) {
defer prepareTestEnv(t)()
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)

packageName := "test-chart"
packageVersion := "1.0.3"
packageAuthor := "KN4CK3R"
packageDescription := "Gitea Test Package"

filename := fmt.Sprintf("%s-%s.tgz", packageName, packageVersion)

chartContent := `apiVersion: v2
description: ` + packageDescription + `
name: ` + packageName + `
type: application
version: ` + packageVersion + `
maintainers:
- name: ` + packageAuthor + `
dependencies:
- name: dep1
repository: https://example.com/
version: 1.0.0`

var buf bytes.Buffer
zw := gzip.NewWriter(&buf)
archive := tar.NewWriter(zw)
archive.WriteHeader(&tar.Header{
Name: fmt.Sprintf("%s/Chart.yaml", packageName),
Mode: 0o600,
Size: int64(len(chartContent)),
})
archive.Write([]byte(chartContent))
archive.Close()
zw.Close()
content := buf.Bytes()

url := fmt.Sprintf("/api/packages/%s/helm", user.Name)

t.Run("Upload", func(t *testing.T) {
defer PrintCurrentTest(t)()

uploadURL := url + "/api/charts"

req := NewRequestWithBody(t, "POST", uploadURL, bytes.NewReader(content))
req = AddBasicAuthHeader(req, user.Name)
MakeRequest(t, req, http.StatusCreated)

pvs, err := packages.GetVersionsByPackageType(db.DefaultContext, user.ID, packages.TypeHelm)
assert.NoError(t, err)
assert.Len(t, pvs, 1)

pd, err := packages.GetPackageDescriptor(db.DefaultContext, pvs[0])
assert.NoError(t, err)
assert.NotNil(t, pd.SemVer)
assert.IsType(t, &helm_module.Metadata{}, pd.Metadata)
assert.Equal(t, packageName, pd.Package.Name)
assert.Equal(t, packageVersion, pd.Version.Version)

pfs, err := packages.GetFilesByVersionID(db.DefaultContext, pvs[0].ID)
assert.NoError(t, err)
assert.Len(t, pfs, 1)
assert.Equal(t, filename, pfs[0].Name)
assert.True(t, pfs[0].IsLead)

pb, err := packages.GetBlobByID(db.DefaultContext, pfs[0].BlobID)
assert.NoError(t, err)
assert.Equal(t, int64(len(content)), pb.Size)

req = NewRequestWithBody(t, "POST", uploadURL, bytes.NewReader(content))
req = AddBasicAuthHeader(req, user.Name)
MakeRequest(t, req, http.StatusCreated)
})

t.Run("Download", func(t *testing.T) {
defer PrintCurrentTest(t)()

checkDownloadCount := func(count int64) {
pvs, err := packages.GetVersionsByPackageType(db.DefaultContext, user.ID, packages.TypeHelm)
assert.NoError(t, err)
assert.Len(t, pvs, 1)
assert.Equal(t, count, pvs[0].DownloadCount)
}

checkDownloadCount(0)

req := NewRequest(t, "GET", fmt.Sprintf("%s/%s", url, filename))
req = AddBasicAuthHeader(req, user.Name)
resp := MakeRequest(t, req, http.StatusOK)

assert.Equal(t, content, resp.Body.Bytes())

checkDownloadCount(1)
})

t.Run("Index", func(t *testing.T) {
defer PrintCurrentTest(t)()

req := NewRequest(t, "GET", fmt.Sprintf("%s/index.yaml", url))
req = AddBasicAuthHeader(req, user.Name)
resp := MakeRequest(t, req, http.StatusOK)

type ChartVersion struct {
helm_module.Metadata `yaml:",inline"`
URLs []string `yaml:"urls"`
Created time.Time `yaml:"created,omitempty"`
Removed bool `yaml:"removed,omitempty"`
Digest string `yaml:"digest,omitempty"`
}

type ServerInfo struct {
ContextPath string `yaml:"contextPath,omitempty"`
}

type Index struct {
APIVersion string `yaml:"apiVersion"`
Entries map[string][]*ChartVersion `yaml:"entries"`
Generated time.Time `yaml:"generated,omitempty"`
ServerInfo *ServerInfo `yaml:"serverInfo,omitempty"`
}

var result Index
assert.NoError(t, yaml.NewDecoder(resp.Body).Decode(&result))
assert.NotEmpty(t, result.Entries)
assert.Contains(t, result.Entries, packageName)

cvs := result.Entries[packageName]
assert.Len(t, cvs, 1)

cv := cvs[0]
assert.Equal(t, packageName, cv.Name)
assert.Equal(t, packageVersion, cv.Version)
assert.Equal(t, packageDescription, cv.Description)
assert.Len(t, cv.Maintainers, 1)
assert.Equal(t, packageAuthor, cv.Maintainers[0].Name)
assert.Len(t, cv.Dependencies, 1)
assert.ElementsMatch(t, []string{fmt.Sprintf("%s%s/%s", setting.AppURL, url[1:], filename)}, cv.URLs)

assert.Equal(t, url, result.ServerInfo.ContextPath)
})
}
2 changes: 1 addition & 1 deletion models/organization/team_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func GetTeamRepositories(ctx context.Context, opts *SearchTeamRepoOptions) ([]*r
)
}
if opts.PageSize > 0 {
sess.Limit(opts.PageSize, opts.Page*opts.PageSize)
sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
}
var repos []*repo_model.Repository
return repos, sess.OrderBy("repository.name").
Expand Down
3 changes: 3 additions & 0 deletions models/packages/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"code.gitea.io/gitea/modules/packages/composer"
"code.gitea.io/gitea/modules/packages/conan"
"code.gitea.io/gitea/modules/packages/container"
"code.gitea.io/gitea/modules/packages/helm"
"code.gitea.io/gitea/modules/packages/maven"
"code.gitea.io/gitea/modules/packages/npm"
"code.gitea.io/gitea/modules/packages/nuget"
Expand Down Expand Up @@ -129,6 +130,8 @@ func GetPackageDescriptor(ctx context.Context, pv *PackageVersion) (*PackageDesc
metadata = &container.Metadata{}
case TypeGeneric:
// generic packages have no metadata
case TypeHelm:
metadata = &helm.Metadata{}
case TypeNuGet:
metadata = &nuget.Metadata{}
case TypeNpm:
Expand Down
Loading

0 comments on commit a6b983f

Please sign in to comment.