Skip to content

Commit

Permalink
feat: store repo meta data in repo directory
Browse files Browse the repository at this point in the history
- Store auth'd user in context.
- Write description, owner, and git-daemon-export-ok files.

Fixes: #255
Fixes: #256
  • Loading branch information
aymanbagabas committed Jul 17, 2023
1 parent e398b4d commit 2f21bcd
Showing 1 changed file with 43 additions and 8 deletions.
51 changes: 43 additions & 8 deletions server/backend/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -53,6 +54,18 @@ func (d *Backend) CreateRepository(ctx context.Context, name string, opts proto.
return err
}

if err := os.WriteFile(filepath.Join(rp, "description"), []byte(opts.Description), fs.ModePerm); err != nil {
d.logger.Error("failed to write description", "repo", name, "err", err)
return err

Check failure on line 59 in server/backend/repo.go

View workflow job for this annotation

GitHub Actions / lint-soft

error returned from external package is unwrapped: sig: func os.WriteFile(name string, data []byte, perm io/fs.FileMode) error (wrapcheck)
}

Check warning on line 60 in server/backend/repo.go

View check run for this annotation

Codecov / codecov/patch

server/backend/repo.go#L58-L60

Added lines #L58 - L60 were not covered by tests

if !opts.Private {
if err := os.WriteFile(filepath.Join(rp, "git-daemon-export-ok"), []byte{}, fs.ModePerm); err != nil {
d.logger.Error("failed to write git-daemon-export-ok", "repo", name, "err", err)
return err

Check failure on line 65 in server/backend/repo.go

View workflow job for this annotation

GitHub Actions / lint-soft

error returned from external package is unwrapped: sig: func os.WriteFile(name string, data []byte, perm io/fs.FileMode) error (wrapcheck)
}

Check warning on line 66 in server/backend/repo.go

View check run for this annotation

Codecov / codecov/patch

server/backend/repo.go#L64-L66

Added lines #L64 - L66 were not covered by tests
}

return hooks.GenerateHooks(ctx, d.cfg, repo)
}); err != nil {
d.logger.Debug("failed to create repository in database", "err", err)
Expand Down Expand Up @@ -341,29 +354,51 @@ func (d *Backend) SetHidden(ctx context.Context, name string, hidden bool) error
// SetDescription sets the description of a repository.
//
// It implements backend.Backend.
func (d *Backend) SetDescription(ctx context.Context, repo string, desc string) error {
repo = utils.SanitizeRepo(repo)
func (d *Backend) SetDescription(ctx context.Context, name string, desc string) error {
name = utils.SanitizeRepo(name)
rp := filepath.Join(d.reposPath(), name+".git")

// Delete cache
d.cache.Delete(repo)
d.cache.Delete(name)

return d.db.TransactionContext(ctx, func(tx *db.Tx) error {
return d.store.SetRepoDescriptionByName(ctx, tx, repo, desc)
if err := os.WriteFile(filepath.Join(rp, "description"), []byte(desc), fs.ModePerm); err != nil {
d.logger.Error("failed to write description", "repo", name, "err", err)
return err

Check failure on line 367 in server/backend/repo.go

View workflow job for this annotation

GitHub Actions / lint-soft

error returned from external package is unwrapped: sig: func os.WriteFile(name string, data []byte, perm io/fs.FileMode) error (wrapcheck)
}

Check warning on line 368 in server/backend/repo.go

View check run for this annotation

Codecov / codecov/patch

server/backend/repo.go#L366-L368

Added lines #L366 - L368 were not covered by tests

return d.store.SetRepoDescriptionByName(ctx, tx, name, desc)

Check failure on line 370 in server/backend/repo.go

View workflow job for this annotation

GitHub Actions / lint-soft

error returned from interface method should be wrapped: sig: func (github.com/charmbracelet/soft-serve/server/store.RepositoryStore).SetRepoDescriptionByName(ctx context.Context, tx *github.com/charmbracelet/soft-serve/server/db.Tx, name string, description string) error (wrapcheck)
})
}

// SetPrivate sets the private flag of a repository.
//
// It implements backend.Backend.
func (d *Backend) SetPrivate(ctx context.Context, repo string, private bool) error {
repo = utils.SanitizeRepo(repo)
func (d *Backend) SetPrivate(ctx context.Context, name string, private bool) error {
name = utils.SanitizeRepo(name)
rp := filepath.Join(d.reposPath(), name+".git")

// Delete cache
d.cache.Delete(repo)
d.cache.Delete(name)

return db.WrapError(
d.db.TransactionContext(ctx, func(tx *db.Tx) error {
return d.store.SetRepoIsPrivateByName(ctx, tx, repo, private)
fp := filepath.Join(rp, "git-daemon-export-ok")
if !private {

Check failure on line 387 in server/backend/repo.go

View workflow job for this annotation

GitHub Actions / lint-soft

`if !private` has complex nested blocks (complexity: 5) (nestif)
if err := os.WriteFile(fp, []byte{}, fs.ModePerm); err != nil {
d.logger.Error("failed to write git-daemon-export-ok", "repo", name, "err", err)
return err

Check failure on line 390 in server/backend/repo.go

View workflow job for this annotation

GitHub Actions / lint-soft

error returned from external package is unwrapped: sig: func os.WriteFile(name string, data []byte, perm io/fs.FileMode) error (wrapcheck)
}

Check warning on line 391 in server/backend/repo.go

View check run for this annotation

Codecov / codecov/patch

server/backend/repo.go#L388-L391

Added lines #L388 - L391 were not covered by tests
} else {
if _, err := os.Stat(fp); err == nil {
if err := os.Remove(fp); err != nil {
d.logger.Error("failed to remove git-daemon-export-ok", "repo", name, "err", err)
return err

Check failure on line 396 in server/backend/repo.go

View workflow job for this annotation

GitHub Actions / lint-soft

error returned from external package is unwrapped: sig: func os.Remove(name string) error (wrapcheck)
}

Check warning on line 397 in server/backend/repo.go

View check run for this annotation

Codecov / codecov/patch

server/backend/repo.go#L395-L397

Added lines #L395 - L397 were not covered by tests
}
}

return d.store.SetRepoIsPrivateByName(ctx, tx, name, private)

Check failure on line 401 in server/backend/repo.go

View workflow job for this annotation

GitHub Actions / lint-soft

error returned from interface method should be wrapped: sig: func (github.com/charmbracelet/soft-serve/server/store.RepositoryStore).SetRepoIsPrivateByName(ctx context.Context, tx *github.com/charmbracelet/soft-serve/server/db.Tx, name string, isPrivate bool) error (wrapcheck)
}),
)
}
Expand Down

0 comments on commit 2f21bcd

Please sign in to comment.