Skip to content

Commit

Permalink
feat(backend): hide repository from ui
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed May 2, 2023
1 parent 2a0d05a commit 43ff02c
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 2 deletions.
7 changes: 7 additions & 0 deletions server/backend/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type RepositoryOptions struct {
Description string
ProjectName string
Mirror bool
Hidden bool
}

// RepositoryStore is an interface for managing repositories.
Expand Down Expand Up @@ -46,6 +47,10 @@ type RepositoryMetadata interface {
SetPrivate(repo string, private bool) error
// IsMirror returns whether the repository is a mirror.
IsMirror(repo string) bool
// IsHidden returns whether the repository is hidden.
IsHidden(repo string) bool
// SetHidden sets whether the repository is hidden.
SetHidden(repo string, hidden bool) error
}

// RepositoryAccess is an interface for managing repository access.
Expand All @@ -71,6 +76,8 @@ type Repository interface {
IsPrivate() bool
// IsMirror returns whether the repository is a mirror.
IsMirror() bool
// IsHidden returns whether the repository is hidden.
IsHidden() bool
// Open returns the underlying git.Repository.
Open() (*git.Repository, error)
}
14 changes: 14 additions & 0 deletions server/backend/sqlite/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,17 @@ func (r *Repo) ProjectName() string {

return name
}

// IsHidden returns whether the repository is hidden.
//
// It implements backend.Repository.
func (r *Repo) IsHidden() bool {
var hidden bool
if err := wrapTx(r.db, context.Background(), func(tx *sqlx.Tx) error {
return tx.Get(&hidden, "SELECT hidden FROM repo WHERE name = ?", r.name)
}); err != nil {
return false
}

return hidden
}
1 change: 1 addition & 0 deletions server/backend/sqlite/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var (
description TEXT NOT NULL,
private BOOLEAN NOT NULL,
mirror BOOLEAN NOT NULL,
hidden BOOLEAN NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL
);`
Expand Down
30 changes: 28 additions & 2 deletions server/backend/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ func (d *SqliteBackend) CreateRepository(name string, opts backend.RepositoryOpt
}

if err := wrapTx(d.db, context.Background(), func(tx *sqlx.Tx) error {
_, err := tx.Exec(`INSERT INTO repo (name, project_name, description, private, mirror, updated_at)
_, err := tx.Exec(`INSERT INTO repo (name, project_name, description, private, mirror, hidden, updated_at)
VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP);`,
name, opts.ProjectName, opts.Description, opts.Private, opts.Mirror)
name, opts.ProjectName, opts.Description, opts.Private, opts.Mirror, opts.Hidden)
return err
}); err != nil {
logger.Debug("failed to create repository in database", "err", err)
Expand Down Expand Up @@ -324,6 +324,32 @@ func (d *SqliteBackend) IsPrivate(repo string) bool {
return private
}

// IsHidden returns true if the repository is hidden.
//
// It implements backend.Backend.
func (d *SqliteBackend) IsHidden(repo string) bool {
repo = utils.SanitizeRepo(repo)
var hidden bool
if err := wrapTx(d.db, context.Background(), func(tx *sqlx.Tx) error {
return tx.Get(&hidden, "SELECT hidden FROM repo WHERE name = ?", repo)
}); err != nil {
return false
}

return hidden
}

// SetHidden sets the hidden flag of a repository.
//
// It implements backend.Backend.
func (d *SqliteBackend) SetHidden(repo string, hidden bool) error {
repo = utils.SanitizeRepo(repo)
return wrapDbErr(wrapTx(d.db, context.Background(), func(tx *sqlx.Tx) error {
_, err := tx.Exec("UPDATE repo SET hidden = ?, updated_at = CURRENT_TIMESTAMP WHERE name = ?;", hidden, repo)
return err
}))
}

// ProjectName returns the project name of a repository.
//
// It implements backend.Backend.
Expand Down
37 changes: 37 additions & 0 deletions server/cmd/hidden.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cmd

import "github.com/spf13/cobra"

func hiddenCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "hidden REPOSITORY [TRUE|FALSE]",
Short: "Hide or unhide a repository",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cfg, _ := fromContext(cmd)
repo := args[0]
switch len(args) {
case 1:
if err := checkIfReadable(cmd, args); err != nil {
return err
}

hidden := cfg.Backend.IsHidden(repo)
cmd.Println(hidden)
case 2:
if err := checkIfCollab(cmd, args); err != nil {
return err
}

hidden := args[1] == "true"
if err := cfg.Backend.SetHidden(repo, hidden); err != nil {
return err
}
}

return nil
},
}

return cmd
}
1 change: 1 addition & 0 deletions server/cmd/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func repoCommand() *cobra.Command {
createCommand(),
deleteCommand(),
descriptionCommand(),
hiddenCommand(),
importCommand(),
listCommand(),
privateCommand(),
Expand Down
3 changes: 3 additions & 0 deletions ui/pages/selection/selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ func (s *Selection) Init() tea.Cmd {
}
sortedItems := make(Items, 0)
for _, r := range repos {
if r.IsHidden() {
continue
}
al := cfg.Backend.AccessLevelByPublicKey(r.Name(), pk)
if al >= backend.ReadOnlyAccess {
item, err := NewItem(r, cfg)
Expand Down

0 comments on commit 43ff02c

Please sign in to comment.