Skip to content

Commit

Permalink
Merge pull request #135 from hasheddan/lower
Browse files Browse the repository at this point in the history
Avoid duplicating indexed repos due to case sensitivity
  • Loading branch information
hasheddan authored Jan 24, 2021
2 parents c5372f6 + f39f46d commit dd3c4f1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
17 changes: 10 additions & 7 deletions cmd/doc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,13 @@ func raw(w http.ResponseWriter, r *http.Request) {
repo := parameters["repo"]
tag := parameters["tag"]

fullRepo := fmt.Sprintf("%s/%s/%s", "github.com", org, repo)
var rows pgx.Rows
var err error
if tag == "" {
rows, err = db.Query(context.Background(), "SELECT c.data::jsonb FROM tags t INNER JOIN crds c ON (c.tag_id = t.id) WHERE t.repo=$1 AND t.id = (SELECT id FROM tags WHERE repo = $1 ORDER BY time DESC LIMIT 1);", "github.com"+"/"+org+"/"+repo)
rows, err = db.Query(context.Background(), "SELECT c.data::jsonb FROM tags t INNER JOIN crds c ON (c.tag_id = t.id) WHERE LOWER(t.repo)=LOWER($1) AND t.id = (SELECT id FROM tags WHERE LOWER(repo) = LOWER($1) ORDER BY time DESC LIMIT 1);", fullRepo)
} else {
rows, err = db.Query(context.Background(), "SELECT c.data::jsonb FROM tags t INNER JOIN crds c ON (c.tag_id = t.id) WHERE t.repo=$1 AND t.name=$2;", "github.com"+"/"+org+"/"+repo, tag)
rows, err = db.Query(context.Background(), "SELECT c.data::jsonb FROM tags t INNER JOIN crds c ON (c.tag_id = t.id) WHERE LOWER(t.repo)=LOWER($1) AND t.name=$2;", fullRepo, tag)
}

var res []byte
Expand Down Expand Up @@ -288,14 +289,15 @@ func org(w http.ResponseWriter, r *http.Request) {
repo := parameters["repo"]
tag := parameters["tag"]
pageData := getPageData(r, fmt.Sprintf("%s/%s", org, repo), false)
fullRepo := fmt.Sprintf("%s/%s/%s", "github.com", org, repo)
b := &pgx.Batch{}
if tag == "" {
b.Queue("SELECT t.name, c.group, c.version, c.kind FROM tags t INNER JOIN crds c ON (c.tag_id = t.id) WHERE t.repo=$1 AND t.id = (SELECT id FROM tags WHERE repo = $1 ORDER BY time DESC LIMIT 1);", "github.com"+"/"+org+"/"+repo)
b.Queue("SELECT t.name, c.group, c.version, c.kind FROM tags t INNER JOIN crds c ON (c.tag_id = t.id) WHERE LOWER(t.repo)=LOWER($1) AND t.id = (SELECT id FROM tags WHERE LOWER(repo) = LOWER($1) ORDER BY time DESC LIMIT 1);", fullRepo)
} else {
pageData.Title += fmt.Sprintf("@%s", tag)
b.Queue("SELECT t.name, c.group, c.version, c.kind FROM tags t INNER JOIN crds c ON (c.tag_id = t.id) WHERE t.repo=$1 AND t.name=$2;", "github.com"+"/"+org+"/"+repo, tag)
b.Queue("SELECT t.name, c.group, c.version, c.kind FROM tags t INNER JOIN crds c ON (c.tag_id = t.id) WHERE LOWER(t.repo)=LOWER($1) AND t.name=$2;", fullRepo, tag)
}
b.Queue("SELECT name FROM tags WHERE repo=$1 ORDER BY time DESC;", "github.com"+"/"+org+"/"+repo)
b.Queue("SELECT name FROM tags WHERE LOWER(repo)=LOWER($1) ORDER BY time DESC;", fullRepo)
br := db.SendBatch(context.Background(), b)
defer br.Close()
c, err := br.Query()
Expand Down Expand Up @@ -385,11 +387,12 @@ func doc(w http.ResponseWriter, r *http.Request) {
return
}
pageData := getPageData(r, fmt.Sprintf("%s.%s/%s", kind, group, version), false)
fullRepo := fmt.Sprintf("%s/%s/%s", "github.com", org, repo)
var c pgx.Row
if tag == "" {
c = db.QueryRow(context.Background(), "SELECT t.name, c.data::jsonb FROM tags t INNER JOIN crds c ON (c.tag_id = t.id) WHERE t.repo=$1 AND t.id = (SELECT id FROM tags WHERE repo = $1 ORDER BY time DESC LIMIT 1) AND c.group=$2 AND c.version=$3 AND c.kind=$4;", "github.com"+"/"+org+"/"+repo, group, version, kind)
c = db.QueryRow(context.Background(), "SELECT t.name, c.data::jsonb FROM tags t INNER JOIN crds c ON (c.tag_id = t.id) WHERE LOWER(t.repo)=LOWER($1) AND t.id = (SELECT id FROM tags WHERE repo = $1 ORDER BY time DESC LIMIT 1) AND c.group=$2 AND c.version=$3 AND c.kind=$4;", fullRepo, group, version, kind)
} else {
c = db.QueryRow(context.Background(), "SELECT t.name, c.data::jsonb FROM tags t INNER JOIN crds c ON (c.tag_id = t.id) WHERE t.repo=$1 AND t.name=$2 AND c.group=$3 AND c.version=$4 AND c.kind=$5;", "github.com"+"/"+org+"/"+repo, tag, group, version, kind)
c = db.QueryRow(context.Background(), "SELECT t.name, c.data::jsonb FROM tags t INNER JOIN crds c ON (c.tag_id = t.id) WHERE LOWER(t.repo)=LOWER($1) AND t.name=$2 AND c.group=$3 AND c.version=$4 AND c.kind=$5;", fullRepo, tag, group, version, kind)
}
foundTag := tag
if err := c.Scan(&foundTag, crd); err != nil {
Expand Down
12 changes: 7 additions & 5 deletions cmd/gitter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"os"
"path"
"regexp"
"strings"
"time"

"github.com/crdsdev/doc/pkg/crd"
Expand Down Expand Up @@ -93,8 +94,9 @@ func (g *Gitter) Index(gRepo models.GitterRepo, reply *string) error {
return err
}
defer os.RemoveAll(dir)
fullRepo := fmt.Sprintf("%s/%s/%s", "github.com", strings.ToLower(gRepo.Org), strings.ToLower(gRepo.Repo))
cloneOpts := &git.CloneOptions{
URL: fmt.Sprintf("https://github.com/%s/%s", gRepo.Org, gRepo.Repo),
URL: fmt.Sprintf("https://%s", fullRepo),
Depth: 1,
Progress: os.Stdout,
RecurseSubmodules: git.NoRecurseSubmodules,
Expand Down Expand Up @@ -147,18 +149,18 @@ func (g *Gitter) Index(gRepo models.GitterRepo, reply *string) error {
log.Printf("Unable to resolve revision: %s (%v)", t.hash.String(), err)
continue
}
r := g.conn.QueryRow(context.Background(), "SELECT id FROM tags WHERE name=$1 AND repo=$2", t.name, "github.com/"+gRepo.Org+"/"+gRepo.Repo)
r := g.conn.QueryRow(context.Background(), "SELECT id FROM tags WHERE name=$1 AND repo=$2", t.name, fullRepo)
var tagID int
if err := r.Scan(&tagID); err != nil {
if !errors.Is(err, pgx.ErrNoRows) {
return err
}
r := g.conn.QueryRow(context.Background(), "INSERT INTO tags(name, repo, time) VALUES ($1, $2, $3) RETURNING id", t.name, "github.com/"+gRepo.Org+"/"+gRepo.Repo, c.Committer.When)
r := g.conn.QueryRow(context.Background(), "INSERT INTO tags(name, repo, time) VALUES ($1, $2, $3) RETURNING id", t.name, fullRepo, c.Committer.When)
if err := r.Scan(&tagID); err != nil {
return err
}
}
repoCRDs, err := getCRDsFromTag(gRepo.Org+"/"+gRepo.Repo, dir, t.name, h, w)
repoCRDs, err := getCRDsFromTag(dir, t.name, h, w)
if err != nil {
log.Printf("Unable to get CRDs: %s@%s (%v)", repo, t.name, err)
continue
Expand All @@ -179,7 +181,7 @@ func (g *Gitter) Index(gRepo models.GitterRepo, reply *string) error {
return nil
}

func getCRDsFromTag(repo string, dir string, tag string, hash *plumbing.Hash, w *git.Worktree) (map[string]models.RepoCRD, error) {
func getCRDsFromTag(dir string, tag string, hash *plumbing.Hash, w *git.Worktree) (map[string]models.RepoCRD, error) {
err := w.Checkout(&git.CheckoutOptions{
Hash: *hash,
Force: true,
Expand Down

0 comments on commit dd3c4f1

Please sign in to comment.