Skip to content

Commit 4353cf9

Browse files
authored
Move LFSLock APIFormat into convert package (#13808)
1 parent e306c29 commit 4353cf9

File tree

3 files changed

+22
-21
lines changed

3 files changed

+22
-21
lines changed

models/lfs_lock.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ package models
77
import (
88
"fmt"
99
"path"
10-
"strconv"
1110
"strings"
1211
"time"
1312

1413
"code.gitea.io/gitea/modules/log"
15-
api "code.gitea.io/gitea/modules/structs"
1614

1715
"xorm.io/xorm"
1816
)
@@ -52,18 +50,6 @@ func cleanPath(p string) string {
5250
return path.Clean("/" + p)[1:]
5351
}
5452

55-
// APIFormat convert a Release to lfs.LFSLock
56-
func (l *LFSLock) APIFormat() *api.LFSLock {
57-
return &api.LFSLock{
58-
ID: strconv.FormatInt(l.ID, 10),
59-
Path: l.Path,
60-
LockedAt: l.Created.Round(time.Second),
61-
Owner: &api.LFSLockOwner{
62-
Name: l.Owner.DisplayName(),
63-
},
64-
}
65-
}
66-
6753
// CreateLFSLock creates a new lock.
6854
func CreateLFSLock(lock *LFSLock) (*LFSLock, error) {
6955
err := CheckLFSAccessForRepo(lock.Owner, lock.Repo, AccessModeWrite)

modules/convert/convert.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package convert
77

88
import (
99
"fmt"
10+
"strconv"
11+
"time"
1012

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

366368
return apiStatus
367369
}
370+
371+
// ToLFSLock convert a LFSLock to api.LFSLock
372+
func ToLFSLock(l *models.LFSLock) *api.LFSLock {
373+
return &api.LFSLock{
374+
ID: strconv.FormatInt(l.ID, 10),
375+
Path: l.Path,
376+
LockedAt: l.Created.Round(time.Second),
377+
Owner: &api.LFSLockOwner{
378+
Name: l.Owner.DisplayName(),
379+
},
380+
}
381+
}

modules/lfs/locks.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"code.gitea.io/gitea/models"
1313
"code.gitea.io/gitea/modules/context"
14+
"code.gitea.io/gitea/modules/convert"
1415
"code.gitea.io/gitea/modules/log"
1516
"code.gitea.io/gitea/modules/setting"
1617
api "code.gitea.io/gitea/modules/structs"
@@ -60,7 +61,7 @@ func handleLockListOut(ctx *context.Context, repo *models.Repository, lock *mode
6061
return
6162
}
6263
ctx.JSON(200, api.LFSLockList{
63-
Locks: []*api.LFSLock{lock.APIFormat()},
64+
Locks: []*api.LFSLock{convert.ToLFSLock(lock)},
6465
})
6566
}
6667

@@ -140,7 +141,7 @@ func GetListLockHandler(ctx *context.Context) {
140141
lockListAPI := make([]*api.LFSLock, len(lockList))
141142
next := ""
142143
for i, l := range lockList {
143-
lockListAPI[i] = l.APIFormat()
144+
lockListAPI[i] = convert.ToLFSLock(l)
144145
}
145146
if limit > 0 && len(lockList) == limit {
146147
next = strconv.Itoa(cursor + 1)
@@ -198,7 +199,7 @@ func PostLockHandler(ctx *context.Context) {
198199
if err != nil {
199200
if models.IsErrLFSLockAlreadyExist(err) {
200201
ctx.JSON(409, api.LFSLockError{
201-
Lock: lock.APIFormat(),
202+
Lock: convert.ToLFSLock(lock),
202203
Message: "already created lock",
203204
})
204205
return
@@ -216,7 +217,7 @@ func PostLockHandler(ctx *context.Context) {
216217
})
217218
return
218219
}
219-
ctx.JSON(201, api.LFSLockResponse{Lock: lock.APIFormat()})
220+
ctx.JSON(201, api.LFSLockResponse{Lock: convert.ToLFSLock(lock)})
220221
}
221222

222223
// VerifyLockHandler list locks for verification
@@ -274,9 +275,9 @@ func VerifyLockHandler(ctx *context.Context) {
274275
lockTheirsListAPI := make([]*api.LFSLock, 0, len(lockList))
275276
for _, l := range lockList {
276277
if l.Owner.ID == ctx.User.ID {
277-
lockOursListAPI = append(lockOursListAPI, l.APIFormat())
278+
lockOursListAPI = append(lockOursListAPI, convert.ToLFSLock(l))
278279
} else {
279-
lockTheirsListAPI = append(lockTheirsListAPI, l.APIFormat())
280+
lockTheirsListAPI = append(lockTheirsListAPI, convert.ToLFSLock(l))
280281
}
281282
}
282283
ctx.JSON(200, api.LFSLockListVerify{
@@ -340,5 +341,5 @@ func UnLockHandler(ctx *context.Context) {
340341
})
341342
return
342343
}
343-
ctx.JSON(200, api.LFSLockResponse{Lock: lock.APIFormat()})
344+
ctx.JSON(200, api.LFSLockResponse{Lock: convert.ToLFSLock(lock)})
344345
}

0 commit comments

Comments
 (0)