ability to delete repository

This commit is contained in:
Brad Rydzewski 2019-04-14 14:01:47 -07:00
parent 9b5d07f843
commit 727177da13
4 changed files with 130 additions and 5 deletions

View file

@ -8,13 +8,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- specify a user for the pipeline step, by [@bradrydzewski](https://github.com/bradrydzewski). [#2651](https://github.com/drone/drone/issues/2651).
- update drone-yaml to version 1.0.7.
- update drone-runtime to version 1.0.5.
- support for Gitea oauth2, by [@techknowlogick](https://github.com/techknowlogick). [#2622](https://github.com/drone/drone/pull/2622).
- ping the docker daemon before starting the agent, by [@bradrydzewski](https://github.com/bradrydzewski). [#2495](https://github.com/drone/drone/issues/2495).
- support for Cron job name in Yaml trigger block, by [@bradrydzewski](https://github.com/bradrydzewski). [#2628](https://github.com/drone/drone/issues/2628).
- support for Cron job name in Yaml when block, by [@bradrydzewski](https://github.com/bradrydzewski). [#2628](https://github.com/drone/drone/issues/2628).
- sqlite username column changed to case-insensitive, by [@bradrydzewski](https://github.com/bradrydzewski).
- endpoint to purge repository from database, by [@bradrydzewski](https://github.com/bradrydzewski).
- update drone-yaml from version 1.0.6 to 1.0.8.
- update drone-runtime from version 1.0.4 to 1.0.6.
## [1.0.1] - 2019-04-10
### Added

View file

@ -58,9 +58,24 @@ func HandleDisable(
return
}
action := core.WebhookActionDisabled
if r.FormValue("remove") == "true" {
action = core.WebhookActionDeleted
err = repos.Delete(r.Context(), repo)
if err != nil {
render.InternalError(w, err)
logger.FromRequest(r).
WithError(err).
WithField("namespace", owner).
WithField("name", name).
Warnln("api: cannot delete repository")
return
}
}
err = sender.Send(r.Context(), &core.WebhookData{
Event: core.WebhookEventRepo,
Action: core.WebhookActionDisabled,
Action: action,
Repo: repo,
})
if err != nil {

View file

@ -121,3 +121,43 @@ func TestDisable_InternalError(t *testing.T) {
t.Errorf(diff)
}
}
func TestDelete(t *testing.T) {
controller := gomock.NewController(t)
defer controller.Finish()
repo := &core.Repository{
ID: 1,
Namespace: "octocat",
Name: "hello-world",
Slug: "octocat/hello-world",
Active: true,
}
repos := mock.NewMockRepositoryStore(controller)
repos.EXPECT().FindName(gomock.Any(), gomock.Any(), repo.Name).Return(repo, nil)
repos.EXPECT().Update(gomock.Any(), repo).Return(nil)
repos.EXPECT().Delete(gomock.Any(), repo).Return(nil)
// a failed webhook should result in a warning message in the
// logs, but should not cause the endpoint to error.
webhook := mock.NewMockWebhookSender(controller)
webhook.EXPECT().Send(gomock.Any(), gomock.Any()).Return(io.EOF)
w := httptest.NewRecorder()
r := httptest.NewRequest("DELETE", "/api/repos/octocat/hello-world?remove=true", nil)
router := chi.NewRouter()
router.Delete("/api/repos/{owner}/{name}", HandleDisable(repos, webhook))
router.ServeHTTP(w, r)
if got, want := w.Code, 200; want != got {
t.Errorf("Want response code %d, got %d", want, got)
}
got, want := new(core.Repository), repo
json.NewDecoder(w.Body).Decode(got)
if diff := cmp.Diff(got, want); len(diff) != 0 {
t.Errorf(diff)
}
}

View file

@ -9,11 +9,11 @@ import (
"database/sql"
"testing"
"github.com/drone/drone/store/shared/db"
"github.com/drone/drone/store/shared/db/dbtest"
"github.com/drone/drone/core"
"github.com/drone/drone/store/perm"
"github.com/drone/drone/store/repos"
"github.com/drone/drone/store/shared/db"
"github.com/drone/drone/store/shared/db/dbtest"
"github.com/drone/drone/store/user"
)
@ -42,6 +42,7 @@ func TestBatch(t *testing.T) {
t.Run("Insert", testBatchInsert(batcher, repos, perms, user))
t.Run("Update", testBatchUpdate(batcher, repos, perms, user))
t.Run("Delete", testBatchDelete(batcher, repos, perms, user))
t.Run("DuplicateID", testBatchDuplicateID(batcher, repos, perms, user))
}
func testBatchInsert(
@ -166,6 +167,74 @@ func testBatchDelete(
}
}
func testBatchDuplicateID(
batcher core.Batcher,
repos core.RepositoryStore,
perms core.PermStore,
user *core.User,
) func(t *testing.T) {
return func(t *testing.T) {
before, err := repos.FindName(noContext, "octocat", "hello-world")
if err != nil {
t.Errorf("Want repository, got error %q", err)
}
batch := &core.Batch{
Insert: []*core.Repository{
{
ID: 0,
UserID: 1,
UID: "43", // Updated ID
Namespace: "octocat",
Name: "hello-world",
Slug: "octocat/hello-world",
},
{
ID: 0,
UserID: 1,
UID: "43", // Updated ID
Namespace: "octocat",
Name: "hello-world",
Slug: "octocat/hello-world",
},
{
ID: 0,
UserID: 1,
UID: "64778136",
Namespace: "octocat",
Name: "linguist",
Slug: "octocat/linguist",
},
},
Update: []*core.Repository{
{
ID: before.ID,
UserID: 1,
UID: "44", // Updated ID
Namespace: "octocat",
Name: "hello-world",
Slug: "octocat/hello-world",
Private: true,
},
},
}
err = batcher.Batch(noContext, user, batch)
if err != nil {
t.Error(err)
}
added, err := repos.FindName(noContext, "octocat", "linguist")
if err != nil {
t.Errorf("Want repository, got error %q", err)
}
if got, want := added.UID, "64778136"; got != want {
t.Errorf("Want added repository UID %v, got %v", want, got)
}
}
}
func seedUser(db *db.DB) (*core.User, error) {
out := &core.User{Login: "octocat"}
err := user.New(db).Create(noContext, out)