Skip to content

Commit c94b8e9

Browse files
authored
Merge branch 'main' into feat/readme
2 parents a8a2b52 + f74faaf commit c94b8e9

File tree

7 files changed

+214
-125
lines changed

7 files changed

+214
-125
lines changed

api/jiaozifs.gen.go

Lines changed: 102 additions & 101 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/swagger.yml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,7 @@ paths:
11011101
name: path
11021102
description: path
11031103
required: false
1104+
allowEmptyValue: true
11041105
schema:
11051106
type: string
11061107
responses:
@@ -1220,17 +1221,19 @@ paths:
12201221
name: path
12211222
description: specific path, if not specific return entries in root
12221223
required: false
1224+
allowEmptyValue: true
12231225
schema:
12241226
type: string
12251227
- in: query
12261228
name: ref
1227-
description: specific branch, default to repostiory default branch(HEAD)
1229+
description: specific( ref name, tag name, commit hash), for wip and branchm, branch name default to repository default branch(HEAD),
12281230
required: false
1231+
allowEmptyValue: true
12291232
schema:
12301233
type: string
12311234
- in: query
12321235
name: type
1233-
description: type indicate to retrieve from wip/branch/tag, default branch
1236+
description: type indicate to retrieve from wip/branch/tag/commit, default branch
12341237
required: true
12351238
schema:
12361239
$ref: "#/components/schemas/RefType"
@@ -1279,6 +1282,7 @@ paths:
12791282
name: path
12801283
description: specific path, if not specific return entries in root
12811284
required: false
1285+
allowEmptyValue: true
12821286
schema:
12831287
type: string
12841288
responses:
@@ -1324,6 +1328,7 @@ paths:
13241328
name: path
13251329
description: specific path, if not specific return entries in root
13261330
required: false
1331+
allowEmptyValue: true
13271332
schema:
13281333
type: string
13291334
responses:
@@ -1356,15 +1361,16 @@ paths:
13561361
get:
13571362
tags:
13581363
- repo
1359-
operationId: getCommitsInRepository
1360-
summary: get commits in repository
1364+
operationId: getCommitsInRef
1365+
summary: get commits in ref
13611366
parameters:
13621367
- $ref: "#/components/parameters/PaginationCommitAfter"
13631368
- $ref: "#/components/parameters/PaginationAmount"
13641369
- in: query
13651370
name: refName
13661371
description: ref(branch/tag) name
13671372
required: false
1373+
allowEmptyValue: true
13681374
schema:
13691375
type: string
13701376
responses:

controller/commit_ctl.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,18 @@ func (commitCtl CommitController) GetEntriesInRef(ctx context.Context, w *api.Ji
4343
return
4444
}
4545

46-
refName := repository.HEAD
47-
if params.Ref != nil {
48-
refName = *params.Ref
49-
}
50-
5146
if operator.Name != ownerName { //todo check permission
5247
w.Forbidden()
5348
return
5449
}
5550

5651
treeHash := hash.EmptyHash
5752
if params.Type == api.RefTypeWip {
53+
refName := repository.HEAD
54+
if params.Ref != nil {
55+
refName = *params.Ref
56+
}
57+
5858
//todo maybe from tag reference
5959
ref, err := commitCtl.Repo.BranchRepo().Get(ctx, models.NewGetBranchParams().SetRepositoryID(repository.ID).SetName(refName))
6060
if err != nil {
@@ -68,6 +68,11 @@ func (commitCtl CommitController) GetEntriesInRef(ctx context.Context, w *api.Ji
6868
}
6969
treeHash = wip.CurrentTree
7070
} else if params.Type == api.RefTypeBranch {
71+
refName := repository.HEAD
72+
if params.Ref != nil {
73+
refName = *params.Ref
74+
}
75+
7176
ref, err := commitCtl.Repo.BranchRepo().Get(ctx, models.NewGetBranchParams().SetRepositoryID(repository.ID).SetName(refName))
7277
if err != nil {
7378
w.Error(err)
@@ -81,6 +86,21 @@ func (commitCtl CommitController) GetEntriesInRef(ctx context.Context, w *api.Ji
8186
}
8287
treeHash = commit.TreeHash
8388
}
89+
} else if params.Type == api.RefTypeCommit {
90+
commitHash, err := hash.FromHex(utils.StringValue(params.Ref))
91+
if err != nil {
92+
w.BadRequest(err.Error())
93+
return
94+
}
95+
96+
if !commitHash.IsEmpty() {
97+
commit, err := commitCtl.Repo.CommitRepo(repository.ID).Commit(ctx, commitHash)
98+
if err != nil {
99+
w.Error(err)
100+
return
101+
}
102+
treeHash = commit.TreeHash
103+
}
84104
} else {
85105
//check in validate middleware, test cant cover here, keep this check
86106
w.BadRequest("not support")

controller/repository_ctl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ func (repositoryCtl RepositoryController) UpdateRepository(ctx context.Context,
399399
w.OK()
400400
}
401401

402-
func (repositoryCtl RepositoryController) GetCommitsInRepository(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, ownerName string, repositoryName string, params api.GetCommitsInRepositoryParams) {
402+
func (repositoryCtl RepositoryController) GetCommitsInRef(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, ownerName string, repositoryName string, params api.GetCommitsInRefParams) {
403403
operator, err := auth.GetOperator(ctx)
404404
if err != nil {
405405
w.Error(err)

integrationtest/commit_test.go

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"net/http"
66

7+
"github.com/jiaozifs/jiaozifs/utils/hash"
8+
79
"github.com/jiaozifs/jiaozifs/api"
810
apiimpl "github.com/jiaozifs/jiaozifs/api/api_impl"
911
"github.com/jiaozifs/jiaozifs/utils"
@@ -227,6 +229,66 @@ func GetEntriesInRefSpec(ctx context.Context, urlStr string) func(c convey.C) {
227229
uploadObject(ctx, c, client, "update f2 to main branch", userName, repoName, "main", "g/m.dat") //modify
228230
commitWip(ctx, c, client, "commit branch change", userName, repoName, "main", "test")
229231

232+
c.Convey("get commit entries", func(c convey.C) {
233+
c.Convey("fail to get entries in uncorrected hash", func() {
234+
resp, err := client.GetEntriesInRef(ctx, userName, repoName, &api.GetEntriesInRefParams{
235+
Path: utils.String("/"),
236+
Ref: utils.String("123"),
237+
Type: api.RefTypeCommit,
238+
})
239+
convey.So(err, convey.ShouldBeNil)
240+
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusBadRequest)
241+
})
242+
243+
c.Convey("fail to get entries in not found", func() {
244+
resp, err := client.GetEntriesInRef(ctx, userName, repoName, &api.GetEntriesInRefParams{
245+
Path: utils.String("/"),
246+
Ref: utils.String("46780d412b4b3c71ba6cdfcb52105c7b"),
247+
Type: api.RefTypeCommit,
248+
})
249+
convey.So(err, convey.ShouldBeNil)
250+
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusNotFound)
251+
})
252+
253+
c.Convey("success to get entries in empty hash", func() {
254+
resp, err := client.GetEntriesInRef(ctx, userName, repoName, &api.GetEntriesInRefParams{
255+
Path: utils.String("/"),
256+
Ref: utils.String(hash.EmptyHash.Hex()),
257+
Type: api.RefTypeCommit,
258+
})
259+
convey.So(err, convey.ShouldBeNil)
260+
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)
261+
262+
result, err := api.ParseGetEntriesInRefResponse(resp)
263+
convey.So(err, convey.ShouldBeNil)
264+
convey.So(*result.JSON200, convey.ShouldHaveLength, 0)
265+
})
266+
267+
c.Convey("success to get entries in commit", func() {
268+
getCommitsResp, err := client.GetCommitsInRef(ctx, userName, repoName, &api.GetCommitsInRefParams{
269+
RefName: utils.String(branchName),
270+
})
271+
convey.So(err, convey.ShouldBeNil)
272+
getCommitsResult, err := api.ParseGetCommitsInRefResponse(getCommitsResp)
273+
convey.So(err, convey.ShouldBeNil)
274+
275+
commit := (*getCommitsResult.JSON200)[0]
276+
resp, err := client.GetEntriesInRef(ctx, userName, repoName, &api.GetEntriesInRefParams{
277+
Path: utils.String("/"),
278+
Ref: utils.String(commit.Hash),
279+
Type: api.RefTypeCommit,
280+
})
281+
convey.So(err, convey.ShouldBeNil)
282+
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)
283+
284+
result, err := api.ParseGetEntriesInRefResponse(resp)
285+
convey.So(err, convey.ShouldBeNil)
286+
convey.So(*result.JSON200, convey.ShouldHaveLength, 2)
287+
convey.So((*result.JSON200)[0].Name, convey.ShouldEqual, "g")
288+
convey.So((*result.JSON200)[1].Name, convey.ShouldEqual, "m.dat")
289+
})
290+
})
291+
230292
c.Convey("compare commit", func(c convey.C) {
231293
c.Convey("get base and head", func() {
232294
resp, err := client.GetBranch(ctx, userName, repoName, &api.GetBranchParams{RefName: "main"})
@@ -331,10 +393,10 @@ func GetCommitChangesSpec(ctx context.Context, urlStr string) func(c convey.C) {
331393

332394
c.Convey("get commit change", func(c convey.C) {
333395
c.Convey("list commit history", func() {
334-
resp, err := client.GetCommitsInRepository(ctx, userName, repoName, &api.GetCommitsInRepositoryParams{RefName: utils.String("main")})
396+
resp, err := client.GetCommitsInRef(ctx, userName, repoName, &api.GetCommitsInRefParams{RefName: utils.String("main")})
335397
convey.So(err, convey.ShouldBeNil)
336398
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)
337-
result, err := api.ParseGetCommitsInRepositoryResponse(resp)
399+
result, err := api.ParseGetCommitsInRefResponse(resp)
338400
convey.So(err, convey.ShouldBeNil)
339401

340402
commits = *result.JSON200

integrationtest/repo_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -365,31 +365,31 @@ func RepoSpec(ctx context.Context, urlStr string) func(c convey.C) {
365365
c.Convey("no auth", func() {
366366
re := client.RequestEditors
367367
client.RequestEditors = nil
368-
resp, err := client.GetCommitsInRepository(ctx, userName, repoName, &api.GetCommitsInRepositoryParams{
368+
resp, err := client.GetCommitsInRef(ctx, userName, repoName, &api.GetCommitsInRefParams{
369369
RefName: utils.String(controller.DefaultBranchName),
370370
})
371371
client.RequestEditors = re
372372
convey.So(err, convey.ShouldBeNil)
373373
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusUnauthorized)
374374
})
375375
c.Convey("update repository in not exit repo", func() {
376-
resp, err := client.GetCommitsInRepository(ctx, userName, "happyrunfake", &api.GetCommitsInRepositoryParams{
376+
resp, err := client.GetCommitsInRef(ctx, userName, "happyrunfake", &api.GetCommitsInRefParams{
377377
RefName: utils.String(controller.DefaultBranchName),
378378
})
379379
convey.So(err, convey.ShouldBeNil)
380380
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusNotFound)
381381
})
382382

383383
c.Convey("update repository in non exit user", func() {
384-
resp, err := client.GetCommitsInRepository(ctx, "telo", repoName, &api.GetCommitsInRepositoryParams{
384+
resp, err := client.GetCommitsInRef(ctx, "telo", repoName, &api.GetCommitsInRefParams{
385385
RefName: utils.String(controller.DefaultBranchName),
386386
})
387387
convey.So(err, convey.ShouldBeNil)
388388
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusNotFound)
389389
})
390390

391391
c.Convey("update repository in other's repo", func() {
392-
resp, err := client.GetCommitsInRepository(ctx, "admin", repoName, &api.GetCommitsInRepositoryParams{
392+
resp, err := client.GetCommitsInRef(ctx, "admin", repoName, &api.GetCommitsInRefParams{
393393
RefName: utils.String(controller.DefaultBranchName),
394394
})
395395
convey.So(err, convey.ShouldBeNil)
@@ -401,13 +401,13 @@ func RepoSpec(ctx context.Context, urlStr string) func(c convey.C) {
401401
uploadObject(ctx, c, client, "add rand object", userName, repoName, controller.DefaultBranchName, "a.txt")
402402
commitWip(ctx, c, client, "commit object", userName, repoName, controller.DefaultBranchName, "first commit")
403403
c.Convey("success get commits", func() {
404-
resp, err := client.GetCommitsInRepository(ctx, userName, repoName, &api.GetCommitsInRepositoryParams{
404+
resp, err := client.GetCommitsInRef(ctx, userName, repoName, &api.GetCommitsInRefParams{
405405
RefName: utils.String(controller.DefaultBranchName),
406406
})
407407
convey.So(err, convey.ShouldBeNil)
408408
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)
409409

410-
result, err := api.ParseGetCommitsInRepositoryResponse(resp)
410+
result, err := api.ParseGetCommitsInRefResponse(resp)
411411
convey.So(err, convey.ShouldBeNil)
412412
convey.So(*result.JSON200, convey.ShouldHaveLength, 1)
413413
convey.So((*result.JSON200)[0].Message, convey.ShouldEqual, "first commit")
@@ -418,33 +418,33 @@ func RepoSpec(ctx context.Context, urlStr string) func(c convey.C) {
418418
uploadObject(ctx, c, client, "add third object", userName, repoName, controller.DefaultBranchName, "c.txt")
419419
commitWip(ctx, c, client, "commit third object", userName, repoName, controller.DefaultBranchName, "third commit")
420420
c.Convey("success get commits by params", func() {
421-
resp, err := client.GetCommitsInRepository(ctx, userName, repoName, &api.GetCommitsInRepositoryParams{
421+
resp, err := client.GetCommitsInRef(ctx, userName, repoName, &api.GetCommitsInRefParams{
422422
RefName: utils.String(controller.DefaultBranchName),
423423
})
424424
convey.So(err, convey.ShouldBeNil)
425425
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)
426426

427-
result, err := api.ParseGetCommitsInRepositoryResponse(resp)
427+
result, err := api.ParseGetCommitsInRefResponse(resp)
428428
convey.So(err, convey.ShouldBeNil)
429429
convey.So(*result.JSON200, convey.ShouldHaveLength, 3)
430430
convey.So((*result.JSON200)[0].Message, convey.ShouldEqual, "third commit")
431431

432-
newResp, err := client.GetCommitsInRepository(ctx, userName, repoName, &api.GetCommitsInRepositoryParams{
432+
newResp, err := client.GetCommitsInRef(ctx, userName, repoName, &api.GetCommitsInRefParams{
433433
After: utils.String((*result.JSON200)[0].Committer.When.Format(time.RFC3339Nano)),
434434
Amount: utils.Int(1),
435435
RefName: utils.String(controller.DefaultBranchName),
436436
})
437437
convey.So(err, convey.ShouldBeNil)
438438
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)
439439

440-
newResult, err := api.ParseGetCommitsInRepositoryResponse(newResp)
440+
newResult, err := api.ParseGetCommitsInRefResponse(newResp)
441441
convey.So(err, convey.ShouldBeNil)
442442
convey.So(*newResult.JSON200, convey.ShouldHaveLength, 1)
443443
convey.So((*newResult.JSON200)[0].Message, convey.ShouldEqual, "second commit")
444444
})
445445

446446
c.Convey("failed get commits by wrong params", func() {
447-
resp, err := client.GetCommitsInRepository(ctx, userName, repoName, &api.GetCommitsInRepositoryParams{
447+
resp, err := client.GetCommitsInRef(ctx, userName, repoName, &api.GetCommitsInRefParams{
448448
After: utils.String("123"),
449449
RefName: utils.String(controller.DefaultBranchName),
450450
})

utils/hash/hash.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func FromHex(str string) (Hash, error) {
2525

2626
func (hash Hash) Hex() string {
2727
if hash == nil {
28-
hex.EncodeToString(EmptyHash)
28+
return hex.EncodeToString(EmptyHash)
2929
}
3030
return hex.EncodeToString(hash)
3131
}

0 commit comments

Comments
 (0)