Skip to content

Commit 7f6fd51

Browse files
committed
fix: remove wip when delte repo
1 parent 765c368 commit 7f6fd51

File tree

9 files changed

+164
-137
lines changed

9 files changed

+164
-137
lines changed

api/jiaozifs.gen.go

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

api/swagger.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ components:
202202
properties:
203203
description:
204204
type: string
205+
head:
206+
type: string
205207
RepositoryList:
206208
type: object
207209
required:

controller/repository_ctl.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,9 @@ func (repositoryCtl RepositoryController) DeleteRepository(ctx context.Context,
312312
if err != nil {
313313
return err
314314
}
315+
316+
//delete wip
317+
_, err = repositoryCtl.Repo.WipRepo().Delete(ctx, models.NewDeleteWipParams().SetRepositoryID(repository.ID))
315318
return err
316319
})
317320
if err != nil {
@@ -373,7 +376,22 @@ func (repositoryCtl RepositoryController) UpdateRepository(ctx context.Context,
373376
return
374377
}
375378

376-
err = repositoryCtl.Repo.RepositoryRepo().UpdateByID(ctx, models.NewUpdateRepoParams(repo.ID).SetDescription(utils.StringValue(body.Description)))
379+
params := models.NewUpdateRepoParams(repo.ID)
380+
if body.Head != nil {
381+
_, err = repositoryCtl.Repo.BranchRepo().Get(ctx, models.NewGetBranchParams().SetRepositoryID(repo.ID).SetName(utils.StringValue(body.Head)))
382+
if err != nil {
383+
w.Error(err)
384+
return
385+
}
386+
387+
params.SetHead(utils.StringValue(body.Head))
388+
}
389+
390+
if body.Description != nil {
391+
params.SetDescription(utils.StringValue(body.Description))
392+
}
393+
394+
err = repositoryCtl.Repo.RepositoryRepo().UpdateByID(ctx, params)
377395
if err != nil {
378396
w.Error(err)
379397
return

integrationtest/repo_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,23 @@ func RepoSpec(ctx context.Context, urlStr string) func(c convey.C) {
342342
convey.So(err, convey.ShouldBeNil)
343343
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusForbidden)
344344
})
345+
346+
c.Convey("update head to not exit", func() {
347+
resp, err := client.UpdateRepository(ctx, userName, repoName, api.UpdateRepositoryJSONRequestBody{
348+
Head: utils.String("xxx"),
349+
})
350+
convey.So(err, convey.ShouldBeNil)
351+
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusNotFound)
352+
})
353+
354+
createBranch(ctx, c, client, userName, repoName, "main", "feat/ano_branch")
355+
c.Convey("update default head success", func() {
356+
resp, err := client.UpdateRepository(ctx, userName, repoName, api.UpdateRepositoryJSONRequestBody{
357+
Head: utils.String("feat/ano_branch"),
358+
})
359+
convey.So(err, convey.ShouldBeNil)
360+
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)
361+
})
345362
})
346363

347364
c.Convey("get commits in repository", func(c convey.C) {

integrationtest/wip_test.go

Lines changed: 31 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -32,69 +32,75 @@ func WipSpec(ctx context.Context, urlStr string) func(c convey.C) {
3232
convey.So(respResult.JSON200, convey.ShouldHaveLength, 0)
3333
})
3434

35-
c.Convey("create wip", func(c convey.C) {
35+
createWip(ctx, c, client, "create main wip", userName, repoName, "main")
36+
37+
c.Convey("get wip", func(c convey.C) {
3638
c.Convey("no auth", func() {
3739
re := client.RequestEditors
3840
client.RequestEditors = nil
39-
resp, err := client.CreateWip(ctx, userName, repoName, &api.CreateWipParams{
41+
resp, err := client.GetWip(ctx, userName, repoName, &api.GetWipParams{
4042
RefName: branchName,
4143
})
4244
client.RequestEditors = re
4345
convey.So(err, convey.ShouldBeNil)
4446
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusUnauthorized)
4547
})
4648

47-
c.Convey("fail to create branch in non exit repo", func() {
48-
resp, err := client.CreateWip(ctx, userName, "fakerepo", &api.CreateWipParams{
49+
c.Convey("auto create a wip", func() {
50+
resp, err := client.GetWip(ctx, userName, repoName, &api.GetWipParams{
4951
RefName: branchName,
5052
})
5153
convey.So(err, convey.ShouldBeNil)
52-
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusNotFound)
54+
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusCreated)
55+
56+
_, err = api.ParseGetWipResponse(resp)
57+
convey.So(err, convey.ShouldBeNil)
5358
})
5459

55-
c.Convey("fail to create branch in non exit user", func() {
56-
resp, err := client.CreateWip(ctx, "mock_user", "main", &api.CreateWipParams{
60+
c.Convey("success get wip", func() {
61+
resp, err := client.GetWip(ctx, userName, repoName, &api.GetWipParams{
5762
RefName: branchName,
5863
})
5964
convey.So(err, convey.ShouldBeNil)
60-
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusNotFound)
65+
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)
66+
67+
_, err = api.ParseGetWipResponse(resp)
68+
convey.So(err, convey.ShouldBeNil)
6169
})
6270

63-
c.Convey("fail to create branch in non exit ref", func() {
64-
resp, err := client.CreateWip(ctx, userName, repoName, &api.CreateWipParams{
65-
RefName: "mock ref",
71+
c.Convey("fail to get wip in non exit ref", func() {
72+
resp, err := client.GetWip(ctx, userName, repoName, &api.GetWipParams{
73+
RefName: "mock_ref",
6674
})
6775
convey.So(err, convey.ShouldBeNil)
6876
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusNotFound)
6977
})
7078

71-
c.Convey("forbidden create branch in others", func() {
72-
resp, err := client.CreateWip(ctx, "jimmy", "happygo", &api.CreateWipParams{
73-
RefName: "main",
79+
c.Convey("fail to get wip from non exit user", func() {
80+
resp, err := client.GetWip(ctx, "mock_owner", repoName, &api.GetWipParams{
81+
RefName: branchName,
7482
})
7583
convey.So(err, convey.ShouldBeNil)
76-
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusForbidden)
84+
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusNotFound)
7785
})
7886

79-
c.Convey("success create branch", func() {
80-
resp, err := client.CreateWip(ctx, userName, repoName, &api.CreateWipParams{
87+
c.Convey("fail to get non exit branch", func() {
88+
resp, err := client.GetWip(ctx, userName, "mock_repo", &api.GetWipParams{
8189
RefName: branchName,
8290
})
8391
convey.So(err, convey.ShouldBeNil)
84-
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusCreated)
92+
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusNotFound)
8593
})
8694

87-
c.Convey("user only have one wip for one userName", func() {
88-
resp, err := client.CreateWip(ctx, userName, repoName, &api.CreateWipParams{
89-
RefName: branchName,
95+
c.Convey("fail to others repo's wips", func() {
96+
resp, err := client.GetWip(ctx, "jimmy", "happygo", &api.GetWipParams{
97+
RefName: "main",
9098
})
9199
convey.So(err, convey.ShouldBeNil)
92-
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusBadRequest)
100+
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusForbidden)
93101
})
94102
})
95103

96-
createWip(ctx, c, client, "create main wip", userName, repoName, "main")
97-
98104
c.Convey("list wip", func(c convey.C) {
99105
c.Convey("no auth", func() {
100106
re := client.RequestEditors
@@ -134,62 +140,6 @@ func WipSpec(ctx context.Context, urlStr string) func(c convey.C) {
134140
})
135141
})
136142

137-
c.Convey("get wip", func(c convey.C) {
138-
c.Convey("no auth", func() {
139-
re := client.RequestEditors
140-
client.RequestEditors = nil
141-
resp, err := client.GetWip(ctx, userName, repoName, &api.GetWipParams{
142-
RefName: branchName,
143-
})
144-
client.RequestEditors = re
145-
convey.So(err, convey.ShouldBeNil)
146-
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusUnauthorized)
147-
})
148-
149-
c.Convey("success get branch", func() {
150-
resp, err := client.GetWip(ctx, userName, repoName, &api.GetWipParams{
151-
RefName: branchName,
152-
})
153-
convey.So(err, convey.ShouldBeNil)
154-
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)
155-
156-
_, err = api.ParseGetWipResponse(resp)
157-
convey.So(err, convey.ShouldBeNil)
158-
})
159-
160-
c.Convey("fail to get wip in non exit ref", func() {
161-
resp, err := client.GetWip(ctx, userName, repoName, &api.GetWipParams{
162-
RefName: "mock_ref",
163-
})
164-
convey.So(err, convey.ShouldBeNil)
165-
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusNotFound)
166-
})
167-
168-
c.Convey("fail to get wip from non exit user", func() {
169-
resp, err := client.GetWip(ctx, "mock_owner", repoName, &api.GetWipParams{
170-
RefName: branchName,
171-
})
172-
convey.So(err, convey.ShouldBeNil)
173-
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusNotFound)
174-
})
175-
176-
c.Convey("fail to get non exit branch", func() {
177-
resp, err := client.GetWip(ctx, userName, "mock_repo", &api.GetWipParams{
178-
RefName: branchName,
179-
})
180-
convey.So(err, convey.ShouldBeNil)
181-
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusNotFound)
182-
})
183-
184-
c.Convey("fail to others repo's wips", func() {
185-
resp, err := client.GetWip(ctx, "jimmy", "happygo", &api.GetWipParams{
186-
RefName: "main",
187-
})
188-
convey.So(err, convey.ShouldBeNil)
189-
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusForbidden)
190-
})
191-
})
192-
193143
c.Convey("delete wip", func(c convey.C) {
194144
c.Convey("no auth", func() {
195145
re := client.RequestEditors
@@ -234,7 +184,7 @@ func WipSpec(ctx context.Context, urlStr string) func(c convey.C) {
234184
//ensure delete work
235185
getResp, err := client.GetWip(ctx, userName, repoName, &api.GetWipParams{RefName: branchNameForDelete})
236186
convey.So(err, convey.ShouldBeNil)
237-
convey.So(getResp.StatusCode, convey.ShouldEqual, http.StatusNotFound)
187+
convey.So(getResp.StatusCode, convey.ShouldEqual, http.StatusCreated)
238188
})
239189
})
240190
}

models/commit_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,14 @@ func TestCommitRepo(t *testing.T) {
3939
})
4040
}
4141

42-
func TestDeleteCOmmit(t *testing.T) {
42+
func TestDeleteCommit(t *testing.T) {
4343
ctx := context.Background()
4444
postgres, _, db := testhelper.SetupDatabase(ctx, t)
4545
defer postgres.Stop() //nolint
4646
t.Run("delete commit", func(t *testing.T) {
4747
repoID := uuid.New()
4848
commitRepo := models.NewCommitRepo(db, repoID)
4949
require.Equal(t, commitRepo.RepositoryID(), repoID)
50-
5150
toDeleteModel := &models.Commit{}
5251
require.NoError(t, gofakeit.Struct(toDeleteModel))
5352
toDeleteModel.RepositoryID = repoID
@@ -63,12 +62,11 @@ func TestDeleteCOmmit(t *testing.T) {
6362
repoID := uuid.New()
6463
commitRepo := models.NewCommitRepo(db, repoID)
6564
require.Equal(t, commitRepo.RepositoryID(), repoID)
66-
6765
for i := 0; i < 5; i++ {
6866
toDeleteModel := &models.Commit{}
6967
require.NoError(t, gofakeit.Struct(toDeleteModel))
7068
toDeleteModel.RepositoryID = repoID
71-
toDeleteModel, err := commitRepo.Insert(ctx, toDeleteModel)
69+
_, err := commitRepo.Insert(ctx, toDeleteModel)
7270
require.NoError(t, err)
7371
}
7472

0 commit comments

Comments
 (0)