Slugify: Replace gosimple/slug with a simple function (#59517)

This commit is contained in:
Ryan McKinley
2022-11-30 11:12:56 -05:00
committed by GitHub
parent 000de83eb4
commit 5b71a16acf
13 changed files with 443 additions and 56 deletions
+2 -1
View File
@@ -10,6 +10,7 @@ import (
"github.com/go-sql-driver/mysql"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/slugify"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/folder"
@@ -170,7 +171,7 @@ func (ss *sqlStore) Get(ctx context.Context, q folder.GetFolderQuery) (*folder.F
}
return nil
})
foldr.Url = models.GetFolderUrl(foldr.UID, models.SlugifyTitle(foldr.Title))
foldr.Url = models.GetFolderUrl(foldr.UID, slugify.Slugify(foldr.Title))
return foldr, err
}
+2 -1
View File
@@ -3,6 +3,7 @@ package folder
import (
"time"
"github.com/grafana/grafana/pkg/infra/slugify"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/util/errutil"
@@ -141,7 +142,7 @@ func (f *Folder) ToLegacyModel() *models.Folder {
Id: f.ID,
Uid: f.UID,
Title: f.Title,
Url: models.GetFolderUrl(f.UID, models.SlugifyTitle(f.Title)),
Url: models.GetFolderUrl(f.UID, slugify.Slugify(f.Title)),
Version: 0,
Created: f.Created,
Updated: f.Updated,
@@ -6,6 +6,7 @@ import (
"fmt"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/slugify"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/dashboards"
alert_models "github.com/grafana/grafana/pkg/services/ngalert/models"
@@ -99,7 +100,7 @@ func (prov *defaultAlertRuleProvisioner) provisionRule(
func (prov *defaultAlertRuleProvisioner) getOrCreateFolderUID(
ctx context.Context, folderName string, orgID int64) (string, error) {
cmd := &models.GetDashboardQuery{
Slug: models.SlugifyTitle(folderName),
Slug: slugify.Slugify(folderName),
OrgId: orgID,
}
err := prov.dashboardService.GetDashboard(ctx, cmd)
@@ -13,6 +13,7 @@ import (
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/slugify"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/dashboards"
@@ -299,7 +300,7 @@ func (fr *FileReader) getOrCreateFolderID(ctx context.Context, cfg *config, serv
return 0, ErrFolderNameMissing
}
cmd := &models.GetDashboardQuery{Slug: models.SlugifyTitle(folderName), OrgId: cfg.OrgID}
cmd := &models.GetDashboardQuery{Slug: slugify.Slugify(folderName), OrgId: cfg.OrgID}
err := fr.dashboardStore.GetDashboard(ctx, cmd)
if err != nil && !errors.Is(err, dashboards.ErrDashboardNotFound) {
@@ -1,13 +1,10 @@
package ualert
import (
"encoding/base64"
"strings"
"time"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/gosimple/slug"
"github.com/grafana/grafana/pkg/infra/slugify"
)
type dashboard struct {
@@ -45,22 +42,7 @@ func (d *dashboard) setVersion(version int) {
// UpdateSlug updates the slug
func (d *dashboard) updateSlug() {
title := d.Data.Get("title").MustString()
d.Slug = slugifyTitle(title)
}
func slugifyTitle(title string) string {
s := slug.Make(strings.ToLower(title))
if s == "" {
// If the dashboard name is only characters outside of the
// sluggable characters, the slug creation will return an
// empty string which will mess up URLs. This failsafe picks
// that up and creates the slug as a base64 identifier instead.
s = base64.RawURLEncoding.EncodeToString([]byte(title))
if slug.MaxLength != 0 && len(s) > slug.MaxLength {
s = s[:slug.MaxLength]
}
}
return s
d.Slug = slugify.Slugify(title)
}
func newDashboardFromJson(data *simplejson.Json) *dashboard {
+2 -1
View File
@@ -7,6 +7,7 @@ import (
"fmt"
"strconv"
"github.com/grafana/grafana/pkg/infra/slugify"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
)
@@ -57,7 +58,7 @@ func NewStaticDashboardSummaryBuilder(lookup DatasourceLookup, sanitize bool) mo
}
dashboardRefs := NewReferenceAccumulator()
url := fmt.Sprintf("/d/%s/%s", uid, models.SlugifyTitle(dash.Title))
url := fmt.Sprintf("/d/%s/%s", uid, slugify.Slugify(dash.Title))
summary.Name = dash.Title
summary.Description = dash.Description
summary.URL = url