Change gist init url to /init (#109)

This commit is contained in:
Thomas Miceli 2023-09-25 18:43:55 +02:00 committed by GitHub
parent 6c450c6f3b
commit 5b278e2e86
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 26 additions and 29 deletions

View file

@ -266,8 +266,8 @@ func (gist *Gist) InitRepository() error {
return git.InitRepository(gist.User.Username, gist.Uuid) return git.InitRepository(gist.User.Username, gist.Uuid)
} }
func (gist *Gist) InitRepositoryViaNewPush(ctx echo.Context) error { func (gist *Gist) InitRepositoryViaInit(ctx echo.Context) error {
return git.InitRepositoryViaNewPush(gist.User.Username, gist.Uuid, ctx) return git.InitRepositoryViaInit(gist.User.Username, gist.Uuid, ctx)
} }
func (gist *Gist) DeleteRepository() error { func (gist *Gist) DeleteRepository() error {

View file

@ -67,7 +67,7 @@ func InitRepository(user string, gist string) error {
return createDotGitFiles(repositoryPath) return createDotGitFiles(repositoryPath)
} }
func InitRepositoryViaNewPush(user string, gist string, ctx echo.Context) error { func InitRepositoryViaInit(user string, gist string, ctx echo.Context) error {
repositoryPath := RepositoryPath(user, gist) repositoryPath := RepositoryPath(user, gist)
if err := InitRepository(user, gist); err != nil { if err := InitRepository(user, gist); err != nil {

View file

@ -2,7 +2,6 @@ package git
import ( import (
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/thomiceli/opengist/internal/config" "github.com/thomiceli/opengist/internal/config"
"net/http" "net/http"
@ -248,7 +247,7 @@ func TestTruncate(t *testing.T) {
require.Equal(t, 2, len(content), "Content size is not correct") require.Equal(t, 2, len(content), "Content size is not correct")
} }
func TestInitViaNewPush(t *testing.T) { func TestInitViaGitInit(t *testing.T) {
setup(t) setup(t)
defer teardown(t) defer teardown(t)
@ -267,11 +266,9 @@ func TestInitViaNewPush(t *testing.T) {
user := "testUser" user := "testUser"
gist := "testGist" gist := "testGist"
// Call InitRepositoryViaNewPush err := InitRepositoryViaInit(user, gist, c)
err := InitRepositoryViaNewPush(user, gist, c)
// Perform assertions require.NoError(t, err)
assert.NoError(t, err)
} }
func commitToBare(t *testing.T, user string, gist string, files map[string]string) { func commitToBare(t *testing.T, user string, gist string, files map[string]string) {

View file

@ -5,7 +5,7 @@ import ogdb "github.com/thomiceli/opengist/internal/db"
var db *memdb.MemDB var db *memdb.MemDB
type GistPush struct { type GistInit struct {
UserID uint UserID uint
Gist *ogdb.Gist Gist *ogdb.Gist
} }
@ -14,8 +14,8 @@ func Setup() error {
var err error var err error
schema := &memdb.DBSchema{ schema := &memdb.DBSchema{
Tables: map[string]*memdb.TableSchema{ Tables: map[string]*memdb.TableSchema{
"gist_push": { "gist_init": {
Name: "gist_push", Name: "gist_init",
Indexes: map[string]*memdb.IndexSchema{ Indexes: map[string]*memdb.IndexSchema{
"id": { "id": {
Name: "id", Name: "id",
@ -35,9 +35,9 @@ func Setup() error {
return nil return nil
} }
func InsertGistPush(userId uint, gist *ogdb.Gist) error { func InsertGistInit(userId uint, gist *ogdb.Gist) error {
txn := db.Txn(true) txn := db.Txn(true)
if err := txn.Insert("gist_push", &GistPush{ if err := txn.Insert("gist_init", &GistInit{
UserID: userId, UserID: userId,
Gist: gist, Gist: gist,
}); err != nil { }); err != nil {
@ -49,11 +49,11 @@ func InsertGistPush(userId uint, gist *ogdb.Gist) error {
return nil return nil
} }
func GetGistPushAndDelete(userId uint) (*GistPush, error) { func GetGistInitAndDelete(userId uint) (*GistInit, error) {
txn := db.Txn(true) txn := db.Txn(true)
defer txn.Abort() defer txn.Abort()
raw, err := txn.First("gist_push", "id", userId) raw, err := txn.First("gist_init", "id", userId)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -62,11 +62,11 @@ func GetGistPushAndDelete(userId uint) (*GistPush, error) {
return nil, nil return nil, nil
} }
gistPush := raw.(*GistPush) gistInit := raw.(*GistInit)
if err := txn.Delete("gist_push", gistPush); err != nil { if err := txn.Delete("gist_init", gistInit); err != nil {
return nil, err return nil, err
} }
txn.Commit() txn.Commit()
return gistPush, nil return gistInit, nil
} }

View file

@ -51,8 +51,8 @@ func gitHttp(ctx echo.Context) error {
gist := getData(ctx, "gist").(*db.Gist) gist := getData(ctx, "gist").(*db.Gist)
isPushNew := strings.HasPrefix(ctx.Request().URL.Path, "/push/info/refs") isInit := strings.HasPrefix(ctx.Request().URL.Path, "/init/info/refs")
isPushReceive := strings.HasPrefix(ctx.Request().URL.Path, "/push/git-receive-pack") isInitReceive := strings.HasPrefix(ctx.Request().URL.Path, "/init/git-receive-pack")
isInfoRefs := strings.HasSuffix(route.gitUrl, "/info/refs$") isInfoRefs := strings.HasSuffix(route.gitUrl, "/info/refs$")
isPull := ctx.QueryParam("service") == "git-upload-pack" || isPull := ctx.QueryParam("service") == "git-upload-pack" ||
strings.HasSuffix(ctx.Request().URL.Path, "git-upload-pack") || strings.HasSuffix(ctx.Request().URL.Path, "git-upload-pack") ||
@ -92,7 +92,7 @@ func gitHttp(ctx echo.Context) error {
return basicAuth(ctx) return basicAuth(ctx)
} }
if !isPushNew && !isPushReceive { if !isInit && !isInitReceive {
if gist.ID == 0 { if gist.ID == 0 {
return plainText(ctx, 404, "Check your credentials or make sure you have access to the Gist") return plainText(ctx, 404, "Check your credentials or make sure you have access to the Gist")
} }
@ -122,7 +122,7 @@ func gitHttp(ctx echo.Context) error {
return errorRes(401, "Invalid credentials", nil) return errorRes(401, "Invalid credentials", nil)
} }
if isPushNew { if isInit {
gist = new(db.Gist) gist = new(db.Gist)
gist.UserID = user.ID gist.UserID = user.ID
gist.User = *user gist.User = *user
@ -133,7 +133,7 @@ func gitHttp(ctx echo.Context) error {
gist.Uuid = strings.Replace(uuidGist.String(), "-", "", -1) gist.Uuid = strings.Replace(uuidGist.String(), "-", "", -1)
gist.Title = "gist:" + gist.Uuid gist.Title = "gist:" + gist.Uuid
if err = gist.InitRepositoryViaNewPush(ctx); err != nil { if err = gist.InitRepositoryViaInit(ctx); err != nil {
return errorRes(500, "Cannot init repository in the file system", err) return errorRes(500, "Cannot init repository in the file system", err)
} }
@ -141,13 +141,13 @@ func gitHttp(ctx echo.Context) error {
return errorRes(500, "Cannot init repository in database", err) return errorRes(500, "Cannot init repository in database", err)
} }
if err := memdb.InsertGistPush(user.ID, gist); err != nil { if err := memdb.InsertGistInit(user.ID, gist); err != nil {
return errorRes(500, "Cannot save the URL for the new Gist", err) return errorRes(500, "Cannot save the URL for the new Gist", err)
} }
setData(ctx, "gist", gist) setData(ctx, "gist", gist)
} else { } else {
gistFromMemdb, err := memdb.GetGistPushAndDelete(user.ID) gistFromMemdb, err := memdb.GetGistInitAndDelete(user.ID)
if err != nil { if err != nil {
return errorRes(500, "Cannot get the gist link from the in memory database", err) return errorRes(500, "Cannot get the gist link from the in memory database", err)
} }

View file

@ -159,7 +159,7 @@ func NewServer(isDev bool) *Server {
return nil return nil
}, },
})) }))
e.Use(middleware.Recover()) //e.Use(middleware.Recover())
e.Use(middleware.Secure()) e.Use(middleware.Secure())
e.Renderer = &Template{ e.Renderer = &Template{
@ -235,7 +235,7 @@ func NewServer(isDev bool) *Server {
} }
if config.C.HttpGit { if config.C.HttpGit {
e.Any("/push/*", gitHttp, gistNewPushSoftInit) e.Any("/init/*", gitHttp, gistNewPushSoftInit)
} }
g1.GET("/all", allGists, checkRequireLogin) g1.GET("/all", allGists, checkRequireLogin)

View file

@ -168,7 +168,7 @@ func validateReservedKeywords(fl validator.FieldLevel) bool {
name := fl.Field().String() name := fl.Field().String()
restrictedNames := map[string]struct{}{} restrictedNames := map[string]struct{}{}
for _, restrictedName := range []string{"assets", "register", "login", "logout", "settings", "admin-panel", "all", "search", "push"} { for _, restrictedName := range []string{"assets", "register", "login", "logout", "settings", "admin-panel", "all", "search", "init"} {
restrictedNames[restrictedName] = struct{}{} restrictedNames[restrictedName] = struct{}{}
} }