Skip to content

Move LFSLock APIFormat into convert package #13808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions models/lfs_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ package models
import (
"fmt"
"path"
"strconv"
"strings"
"time"

"code.gitea.io/gitea/modules/log"
api "code.gitea.io/gitea/modules/structs"

"xorm.io/xorm"
)
Expand Down Expand Up @@ -52,18 +50,6 @@ func cleanPath(p string) string {
return path.Clean("/" + p)[1:]
}

// APIFormat convert a Release to lfs.LFSLock
func (l *LFSLock) APIFormat() *api.LFSLock {
return &api.LFSLock{
ID: strconv.FormatInt(l.ID, 10),
Path: l.Path,
LockedAt: l.Created.Round(time.Second),
Owner: &api.LFSLockOwner{
Name: l.Owner.DisplayName(),
},
}
}

// CreateLFSLock creates a new lock.
func CreateLFSLock(lock *LFSLock) (*LFSLock, error) {
err := CheckLFSAccessForRepo(lock.Owner, lock.Repo, AccessModeWrite)
Expand Down
14 changes: 14 additions & 0 deletions modules/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package convert

import (
"fmt"
"strconv"
"time"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
Expand Down Expand Up @@ -365,3 +367,15 @@ func ToCommitStatus(status *models.CommitStatus) *api.Status {

return apiStatus
}

// ToLFSLock convert a LFSLock to api.LFSLock
func ToLFSLock(l *models.LFSLock) *api.LFSLock {
return &api.LFSLock{
ID: strconv.FormatInt(l.ID, 10),
Path: l.Path,
LockedAt: l.Created.Round(time.Second),
Owner: &api.LFSLockOwner{
Name: l.Owner.DisplayName(),
},
}
}
15 changes: 8 additions & 7 deletions modules/lfs/locks.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
Expand Down Expand Up @@ -60,7 +61,7 @@ func handleLockListOut(ctx *context.Context, repo *models.Repository, lock *mode
return
}
ctx.JSON(200, api.LFSLockList{
Locks: []*api.LFSLock{lock.APIFormat()},
Locks: []*api.LFSLock{convert.ToLFSLock(lock)},
})
}

Expand Down Expand Up @@ -140,7 +141,7 @@ func GetListLockHandler(ctx *context.Context) {
lockListAPI := make([]*api.LFSLock, len(lockList))
next := ""
for i, l := range lockList {
lockListAPI[i] = l.APIFormat()
lockListAPI[i] = convert.ToLFSLock(l)
}
if limit > 0 && len(lockList) == limit {
next = strconv.Itoa(cursor + 1)
Expand Down Expand Up @@ -198,7 +199,7 @@ func PostLockHandler(ctx *context.Context) {
if err != nil {
if models.IsErrLFSLockAlreadyExist(err) {
ctx.JSON(409, api.LFSLockError{
Lock: lock.APIFormat(),
Lock: convert.ToLFSLock(lock),
Message: "already created lock",
})
return
Expand All @@ -216,7 +217,7 @@ func PostLockHandler(ctx *context.Context) {
})
return
}
ctx.JSON(201, api.LFSLockResponse{Lock: lock.APIFormat()})
ctx.JSON(201, api.LFSLockResponse{Lock: convert.ToLFSLock(lock)})
}

// VerifyLockHandler list locks for verification
Expand Down Expand Up @@ -274,9 +275,9 @@ func VerifyLockHandler(ctx *context.Context) {
lockTheirsListAPI := make([]*api.LFSLock, 0, len(lockList))
for _, l := range lockList {
if l.Owner.ID == ctx.User.ID {
lockOursListAPI = append(lockOursListAPI, l.APIFormat())
lockOursListAPI = append(lockOursListAPI, convert.ToLFSLock(l))
} else {
lockTheirsListAPI = append(lockTheirsListAPI, l.APIFormat())
lockTheirsListAPI = append(lockTheirsListAPI, convert.ToLFSLock(l))
}
}
ctx.JSON(200, api.LFSLockListVerify{
Expand Down Expand Up @@ -340,5 +341,5 @@ func UnLockHandler(ctx *context.Context) {
})
return
}
ctx.JSON(200, api.LFSLockResponse{Lock: lock.APIFormat()})
ctx.JSON(200, api.LFSLockResponse{Lock: convert.ToLFSLock(lock)})
}