Skip to content

Package registry: add description from UI #20628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
4 changes: 3 additions & 1 deletion models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ var migrations = []Migration{
// v222 -> v223
NewMigration("Drop old CredentialID column", v1_17.DropOldCredentialIDColumn),
// v223 -> v224
NewMigration("Rename CredentialIDBytes column to CredentialID", v1_17.RenameCredentialIDBytes),
NewMigration("Rename CredentialIDBytes column to CredentialID", renameCredentialIDBytes),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


// Gitea 1.17.0 ends at v224

Expand Down Expand Up @@ -442,6 +442,8 @@ var migrations = []Migration{
NewMigration("Add package cleanup rule table", v1_19.CreatePackageCleanupRuleTable),
// v235 -> v236
NewMigration("Add index for access_token", v1_19.AddIndexForAccessToken),

NewMigration("Add container repository property", addDescriptionAndReadmeColsForPackage),
}

// GetCurrentDBVersion returns the current db version
Expand Down
18 changes: 18 additions & 0 deletions models/migrations/v224.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move this migration into a versioned directory?
Also, could you update the copyright heading to match the new format?

// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package migrations

import (
"xorm.io/xorm"
)

func addDescriptionAndReadmeColsForPackage(x *xorm.Engine) error {
type Package struct {
Description string `xorm:"TEXT"`
Readme string `xorm:"LONGBLOB"`
}

return x.Sync(new(Package))
}
11 changes: 11 additions & 0 deletions models/packages/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ type Package struct {
Name string `xorm:"NOT NULL"`
LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
SemverCompatible bool `xorm:"NOT NULL DEFAULT false"`
Description string `xorm:"VARCHAR(255)"`
Readme string `xorm:"LONGBLOB"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not LONGTEXT?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe TEXT is more suitable for this column, as xorm should translate it into the appropriate type for each database

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LONGTEXT (for MySQL)

}

// TryInsertPackage inserts a package. If a package exists already, ErrDuplicatePackage is returned
Expand Down Expand Up @@ -167,6 +169,15 @@ func SetRepositoryLink(ctx context.Context, packageID, repoID int64) error {
return err
}

// SetDescriptions sets descriptions
func SetDescriptions(ctx context.Context, packageID int64, description, readme string) error {
_, err := db.GetEngine(ctx).ID(packageID).Update(&Package{
Description: description,
Readme: readme,
})
return err
}

// UnlinkRepositoryFromAllPackages unlinks every package from the repository
func UnlinkRepositoryFromAllPackages(ctx context.Context, repoID int64) error {
_, err := db.GetEngine(ctx).Where("repo_id = ?", repoID).Cols("repo_id").Update(&Package{})
Expand Down
7 changes: 7 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3192,6 +3192,13 @@ settings.delete.description = Deleting a package is permanent and cannot be undo
settings.delete.notice = You are about to delete %s (%s). This operation is irreversible, are you sure?
settings.delete.success = The package has been deleted.
settings.delete.error = Failed to delete the package.
settings.descriptions.success = Descriptions were successfully updated
settings.descriptions.error = Failed to update descriptions
settings.descriptions = Descriptions
settings.description.description = Description
settings.readme.description = Readme
settings.descriptions.button = Update descriptions
readme = Readme
owner.settings.cleanuprules.title = Manage Cleanup Rules
owner.settings.cleanuprules.add = Add Cleanup Rule
owner.settings.cleanuprules.edit = Edit Cleanup Rule
Expand Down
8 changes: 7 additions & 1 deletion options/locale/locale_fr-FR.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2824,7 +2824,13 @@ error.unit_not_allowed=Vous n'êtes pas autorisé à accéder à cette section d

[packages]
empty.repo=Avez-vous téléchargé un paquet, mais il n'est pas affiché ici? Allez dans les <a href="%[1]s">paramètres du paquet</a> et liez le à ce dépôt.
settings.descriptions.success = Les descriptifs ont été mises à jour
settings.descriptions.error = Échec de la mise à jour des descriptifs
settings.descriptions = Descriptifs
settings.description.description = Description
settings.readme.description = Readme
settings.descriptions.button = Mettre à jour les descriptifs
readme = Readme
Comment on lines +2827 to +2833
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be overridden by the CI again

published_by_in=%[1]s publié par <a href="%[2]s">%[3]s</a> en <a href="%[4]s"><strong>%[5]s</strong></a>
rubygems.required.ruby=Nécessite la version de Ruby
rubygems.required.rubygems=Nécessite la version de RubyGem

13 changes: 13 additions & 0 deletions routers/web/user/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,19 @@ func PackageSettingsPost(ctx *context.Context) {
ctx.Flash.Error(ctx.Tr("packages.settings.link.error"))
}

ctx.Redirect(ctx.Link)
return
case "update":
description := form.Description
readme := form.Readme

if err := packages_model.SetDescriptions(ctx, pd.Package.ID, description, readme); err != nil {
log.Error("Error updating package: %v", err)
ctx.Flash.Error(ctx.Tr("packages.settings.descriptions.error"))
} else {
ctx.Flash.Success(ctx.Tr("packages.settings.descriptions.success"))
}

ctx.Redirect(ctx.Link)
return
case "delete":
Expand Down
1 change: 1 addition & 0 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,7 @@ func RegisterRoutes(m *web.Route) {
m.Group("/{type}/{name}", func() {
m.Get("", user.RedirectToLastVersion)
m.Get("/versions", user.ListPackageVersions)
m.Post("/markdown", bindIgnErr(structs.MarkdownOption{}), misc.Markdown)
m.Group("/{version}", func() {
m.Get("", user.ViewPackageVersion)
m.Get("/files/{fileid}", user.DownloadPackageFile)
Expand Down
6 changes: 4 additions & 2 deletions services/forms/user_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,10 @@ func (f *WebauthnDeleteForm) Validate(req *http.Request, errs binding.Errors) bi

// PackageSettingForm form for package settings
type PackageSettingForm struct {
Action string
RepoID int64 `form:"repo_id"`
Action string
RepoID int64 `form:"repo_id"`
Description string `form:"description"`
Readme string `form:"readme"`
}

// Validate validates the fields
Expand Down
2 changes: 2 additions & 0 deletions templates/package/content/composer.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
</div>
</div>

{{template "package/content/readme" .}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few more registries have been merged in the mean time.
Those should get this call too.


{{if .PackageDescriptor.Metadata.Description}}
<h4 class="ui top attached header">{{.locale.Tr "packages.about"}}</h4>
<div class="ui attached segment">
Expand Down
2 changes: 2 additions & 0 deletions templates/package/content/conan.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
</div>
</div>

{{template "package/content/readme" .}}

{{if .PackageDescriptor.Metadata.Description}}
<h4 class="ui top attached header">{{.locale.Tr "packages.about"}}</h4>
<div class="ui attached segment">
Expand Down
3 changes: 3 additions & 0 deletions templates/package/content/container.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
</div>
</div>
</div>

{{template "package/content/readme" .}}

{{if .PackageDescriptor.Metadata.MultiArch}}
<h4 class="ui top attached header">{{.locale.Tr "packages.container.multi_arch"}}</h4>
<div class="ui attached segment">
Expand Down
2 changes: 2 additions & 0 deletions templates/package/content/generic.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
</div>
</div>
</div>

{{template "package/content/readme" .}}
{{end}}
2 changes: 2 additions & 0 deletions templates/package/content/helm.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ helm repo update</code></pre></div>
</div>
</div>

{{template "package/content/readme" .}}

{{if .PackageDescriptor.Metadata.Description}}
<h4 class="ui top attached header">{{.locale.Tr "packages.about"}}</h4>
<div class="ui attached segment">
Expand Down
2 changes: 2 additions & 0 deletions templates/package/content/maven.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
</div>
</div>

{{template "package/content/readme" .}}

{{if .PackageDescriptor.Metadata.Description}}
<h4 class="ui top attached header">{{.locale.Tr "packages.about"}}</h4>
<div class="ui attached segment">
Expand Down
2 changes: 2 additions & 0 deletions templates/package/content/npm.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
</div>
</div>

{{template "package/content/readme" .}}

{{if or .PackageDescriptor.Metadata.Description .PackageDescriptor.Metadata.Readme}}
<h4 class="ui top attached header">{{.locale.Tr "packages.about"}}</h4>
<div class="ui attached segment">
Expand Down
2 changes: 2 additions & 0 deletions templates/package/content/nuget.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
</div>
</div>

{{template "package/content/readme" .}}

{{if or .PackageDescriptor.Metadata.Description .PackageDescriptor.Metadata.ReleaseNotes}}
<h4 class="ui top attached header">{{.locale.Tr "packages.about"}}</h4>
<div class="ui attached segment">
Expand Down
6 changes: 6 additions & 0 deletions templates/package/content/readme.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{if .PackageDescriptor.Package.Readme}}
<h4 class="ui top attached header">{{.locale.Tr "packages.readme"}}</h4>
<div class="ui attached segment">
{{RenderMarkdownToHtml .PackageDescriptor.Package.Readme}}
</div>
{{end}}
3 changes: 3 additions & 0 deletions templates/package/content/rubygems.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ end</code></pre></div>
</div>
</div>
</div>

{{template "package/content/readme" .}}

{{if .PackageDescriptor.Metadata.Description}}
<h4 class="ui top attached header">{{.locale.Tr "packages.about"}}</h4>
<div class="ui attached segment">{{.PackageDescriptor.Metadata.Description}}</div>
Expand Down
14 changes: 14 additions & 0 deletions templates/package/readme_tab.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div class="ui top tabular menu" data-write="write" data-preview="preview">
<a class="active item" data-tab="write">{{.locale.Tr "write"}}</a>
<a class="item" data-tab="preview" data-url="{{.PackageDescriptor.PackageWebLink}}/markdown" data-context="{{.PackageDescriptor.Owner.Name}}/{{.PackageDescriptor.Package.Name}}">{{.locale.Tr "preview"}}</a>
</div>
<div class="field">
<div class="ui bottom active tab" data-tab="write">
<textarea id="content" class="edit_area js-quick-submit" name="readme" tabindex="4" data-id="-{{.PackageDescriptor.Package.ID}}" data-url="{{.PackageDescriptor.PackageWebLink}}/markdown" data-context="{{.PackageDescriptor.Owner.Name}}/{{.PackageDescriptor.Package.Name}}">
{{- .PackageDescriptor.Package.Readme -}}
</textarea>
</div>
<div class="ui bottom tab markup" data-tab="preview">
{{.locale.Tr "loading"}}
</div>
</div>
24 changes: 23 additions & 1 deletion templates/package/settings.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{template "base/head" .}}
<div class="page-content repository settings options">
<div class="page-content repository package settings options">
{{template "user/overview/header" .}}
<div class="ui container">
{{template "base/alert" .}}
Expand Down Expand Up @@ -34,6 +34,28 @@
</div>
</form>
</div>
<h4 class="ui top attached header">
{{.locale.Tr "packages.settings.descriptions"}}
</h4>
<div class="ui attached segment">
<form class="ui descriptions form" action="{{.Link}}" method="post">
{{template "base/disable_form_autofill"}}
{{.CsrfTokenHtml}}
<input type="hidden" name="action" value="update">

<div class="field {{if .Err_Description}}error{{end}}">
<label for="description">{{$.locale.Tr "packages.settings.description.description"}}</label>
<textarea id="description" name="description" rows="2" maxlength="255">{{.PackageDescriptor.Package.Description}}</textarea>
</div>

<label for="readme">{{$.locale.Tr "packages.settings.readme.description"}}</label>
{{template "package/readme_tab" .}}

<div class="field">
<button class="ui green button">{{.locale.Tr "packages.settings.descriptions.button"}}</button>
</div>
</form>
</div>
<h4 class="ui top attached error header">
{{.locale.Tr "repo.settings.danger_zone"}}
</h4>
Expand Down
4 changes: 4 additions & 0 deletions templates/package/shared/list.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
<span class="ui label">{{svg .Package.Type.SVGName 16}} {{.Package.Type.Name}}</span>
</div>
<div class="desc issue-item-bottom-row df ac fw my-1">
{{if .Package.Description}}
<p>{{- .Package.Description -}}</p>
{{end}}

{{$timeStr := TimeSinceUnix .Version.CreatedUnix $.locale}}
{{$hasRepositoryAccess := false}}
{{if .Repository}}
Expand Down
7 changes: 7 additions & 0 deletions templates/package/view.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
<div class="sixteen wide column title">
<div class="issue-title">
<h1>{{.PackageDescriptor.Package.Name}} ({{.PackageDescriptor.Version.Version}})</h1>

<p class="description">
{{if .PackageDescriptor.Package.Description}}
{{- .PackageDescriptor.Package.Description -}}
{{end}}
</p>
</div>
<div>
{{$timeStr := TimeSinceUnix .PackageDescriptor.Version.CreatedUnix $.locale}}
Expand All @@ -18,6 +24,7 @@
</div>
<div class="ui divider"></div>
</div>

<div class="twelve wide column">
{{template "package/content/composer" .}}
{{template "package/content/conan" .}}
Expand Down
31 changes: 31 additions & 0 deletions web_src/js/features/package-editor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import $ from 'jquery';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linting rules should prevent the addition of new jQuery code, could you utilize vanilla JS instead?

Cc: @silverwind who has more insight into linting

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no linting for it, I don't think it's possible for the linter to distinguish old and new code. But yes, new code should prefer not to use jQuery, we want to eventually remove it.


const {csrfToken} = window.config;

function initEditPreviewTab($form) {
const $tabMenu = $form.find('.tabular.menu');
$tabMenu.find('.item').tab();
$tabMenu.find(`.item[data-tab="${$tabMenu.data('preview')}"]`).on('click', function () {
Comment on lines +6 to +8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This selector looks really fragile.
Please create a specific class instead and query for that class.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's just the old code, after the "editor" refactoring, no such code is needed.

Actually, this PR has been stale for too long time, I guess the author might have lost interest on it?

If it is still stale and inactive, it needs the maintainers who have writer permission to help.

const $this = $(this);
const context = `/${$this.data('context')}`;
const mode = $this.data('markdown-mode') || 'comment';

$.post($this.data('url'), {
_csrf: csrfToken,
mode,
context,
text: $form.find(`.tab[data-tab="${$tabMenu.data('write')}"] textarea`).val(),
}, (data) => {
const $diffPreviewPanel = $form.find(`.tab[data-tab="${$tabMenu.data('preview')}"]`);
$diffPreviewPanel.html(data);
});
});
}

export function initPackageEditor() {
if ($('.package.settings .descriptions').length === 0) {
return;
}

initEditPreviewTab($('.package.settings .descriptions'));
}
3 changes: 3 additions & 0 deletions web_src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ import {initOrgTeamSearchRepoBox, initOrgTeamSettings} from './features/org-team
import {initUserAuthWebAuthn, initUserAuthWebAuthnRegister} from './features/user-auth-webauthn.js';
import {initRepoRelease, initRepoReleaseEditor} from './features/repo-release.js';
import {initRepoEditor} from './features/repo-editor.js';
import {initPackageEditor} from './features/package-editor.js';
import {initCompSearchUserBox} from './features/comp/SearchUserBox.js';
import {initInstall} from './features/install.js';
import {initCompWebHookEditor} from './features/comp/WebHookEditor.js';
Expand Down Expand Up @@ -188,6 +189,8 @@ $(document).ready(() => {
initRepoWikiForm();
initRepository();

initPackageEditor();

initCommitStatuses();
initMcaptcha();

Expand Down