From 298dcd5a50947cb598ac440d7b69b5ceffd0ec99 Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Sun, 31 Jul 2016 22:58:29 +0200 Subject: [PATCH] Integrated real API implementation for org secrets --- server/build.go | 2 +- server/hook.go | 2 +- server/{secret.go => repo_secret.go} | 23 +--------- server/team_secret.go | 67 ++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 23 deletions(-) rename server/{secret.go => repo_secret.go} (74%) create mode 100644 server/team_secret.go diff --git a/server/build.go b/server/build.go index d65cb1ae..e2cbb3c5 100644 --- a/server/build.go +++ b/server/build.go @@ -291,7 +291,7 @@ func PostBuild(c *gin.Context) { // get the previous build so that we can send // on status change notifications last, _ := store.GetBuildLastBefore(c, repo, build.Branch, build.ID) - secs, err := store.GetSecretList(c, repo) + secs, err := store.GetMergedSecretList(c, repo) if err != nil { log.Errorf("Error getting secrets for %s#%d. %s", repo.FullName, build.Number, err) } diff --git a/server/hook.go b/server/hook.go index 19e2d912..1c93ed86 100644 --- a/server/hook.go +++ b/server/hook.go @@ -206,7 +206,7 @@ func PostHook(c *gin.Context) { // get the previous build so that we can send // on status change notifications last, _ := store.GetBuildLastBefore(c, repo, build.Branch, build.ID) - secs, err := store.GetSecretList(c, repo) + secs, err := store.GetMergedSecretList(c, repo) if err != nil { log.Errorf("Error getting secrets for %s#%d. %s", repo.FullName, build.Number, err) } diff --git a/server/secret.go b/server/repo_secret.go similarity index 74% rename from server/secret.go rename to server/repo_secret.go index 39e5bd35..336adf00 100644 --- a/server/secret.go +++ b/server/repo_secret.go @@ -19,7 +19,7 @@ func GetSecrets(c *gin.Context) { return } - var list []*model.Secret + var list []*model.RepoSecret for _, s := range secrets { list = append(list, s.Clone()) @@ -28,19 +28,10 @@ func GetSecrets(c *gin.Context) { c.JSON(http.StatusOK, list) } -func GetTeamSecrets(c *gin.Context) { - var ( - list []*model.Secret - ) - - // TODO(must): Integrate a real implementation - c.JSON(http.StatusOK, list) -} - func PostSecret(c *gin.Context) { repo := session.Repo(c) - in := &model.Secret{} + in := &model.RepoSecret{} err := c.Bind(in) if err != nil { c.String(http.StatusBadRequest, "Invalid JSON input. %s", err.Error()) @@ -58,11 +49,6 @@ func PostSecret(c *gin.Context) { c.String(http.StatusOK, "") } -func PostTeamSecret(c *gin.Context) { - c.String(http.StatusOK, "") - // TODO(must): Integrate a real implementation -} - func DeleteSecret(c *gin.Context) { repo := session.Repo(c) name := c.Param("secret") @@ -80,8 +66,3 @@ func DeleteSecret(c *gin.Context) { c.String(http.StatusOK, "") } - -func DeleteTeamSecret(c *gin.Context) { - c.String(http.StatusOK, "") - // TODO(must): Integrate a real implementation -} diff --git a/server/team_secret.go b/server/team_secret.go new file mode 100644 index 00000000..ec508b9a --- /dev/null +++ b/server/team_secret.go @@ -0,0 +1,67 @@ +package server + +import ( + "net/http" + + "github.com/drone/drone/model" + "github.com/drone/drone/store" + + "github.com/gin-gonic/gin" +) + +func GetTeamSecrets(c *gin.Context) { + team := c.Param("team") + secrets, err := store.GetTeamSecretList(c, team) + + if err != nil { + c.AbortWithStatus(http.StatusInternalServerError) + return + } + + var list []*model.TeamSecret + + for _, s := range secrets { + list = append(list, s.Clone()) + } + + c.JSON(http.StatusOK, list) +} + +func PostTeamSecret(c *gin.Context) { + team := c.Param("team") + + in := &model.TeamSecret{} + err := c.Bind(in) + if err != nil { + c.String(http.StatusBadRequest, "Invalid JSON input. %s", err.Error()) + return + } + in.ID = 0 + in.Key = team + + err = store.SetTeamSecret(c, in) + if err != nil { + c.String(http.StatusInternalServerError, "Unable to persist team secret. %s", err.Error()) + return + } + + c.String(http.StatusOK, "") +} + +func DeleteTeamSecret(c *gin.Context) { + team := c.Param("team") + name := c.Param("secret") + + secret, err := store.GetTeamSecret(c, team, name) + if err != nil { + c.String(http.StatusNotFound, "Cannot find secret %s.", name) + return + } + err = store.DeleteTeamSecret(c, secret) + if err != nil { + c.String(http.StatusInternalServerError, "Unable to delete team secret. %s", err.Error()) + return + } + + c.String(http.StatusOK, "") +}