From 870e967ee87685c478ede10f8701563b80c6465b Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Tue, 3 Sep 2019 21:11:49 -0700 Subject: [PATCH] added support for rollback --- CHANGELOG.md | 2 + cmd/drone-server/config/config.go | 1 + go.mod | 2 +- go.sum | 2 + handler/api/api.go | 6 +- handler/api/repos/builds/promote.go | 1 + handler/api/repos/builds/rollback.go | 96 ++++++++++++++++++++++++ handler/api/repos/builds/rollback_oss.go | 37 +++++++++ server/server.go | 2 + 9 files changed, 145 insertions(+), 4 deletions(-) create mode 100644 handler/api/repos/builds/rollback_oss.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 58b51b4ac..78714f543 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - support for validation plugins, by [@bradrydzewski](https://github.com/bradrydzewski). [#2266](https://github.com/drone/drone/issues/2266). - support for conversion plugins, by [@bradrydzewski](https://github.com/bradrydzewski). - support for cron event type, by [@bradrydzewski](https://github.com/bradrydzewski). [#2705](https://github.com/drone/drone/issues/2705). +- support for rollback event, by [@bradrydzewski](https://github.com/bradrydzewski). [#2695](https://github.com/drone/drone/issues/2695). +- support for lets encrypt email, by [@bradrydzewski](https://github.com/bradrydzewski). [#2505](https://github.com/drone/drone/issues/2505). ### Removed - Support for basic auth as an option for Gitea, by [@techknowlogick](https://giteahub.com/techknowlogick). [#2721](https://github.com/drone/drone/issues/2721) diff --git a/cmd/drone-server/config/config.go b/cmd/drone-server/config/config.go index 3eb75fda6..006af0247 100644 --- a/cmd/drone-server/config/config.go +++ b/cmd/drone-server/config/config.go @@ -238,6 +238,7 @@ type ( Port string `envconfig:"DRONE_SERVER_PORT" default:":8080"` Proto string `envconfig:"DRONE_SERVER_PROTO" default:"http"` Acme bool `envconfig:"DRONE_TLS_AUTOCERT"` + Email string `envconfig:"DRONE_TLS_EMAIL"` Cert string `envconfig:"DRONE_TLS_CERT"` Key string `envconfig:"DRONE_TLS_KEY"` } diff --git a/go.mod b/go.mod index 49b851f4e..99b861bf5 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,7 @@ require ( github.com/docker/go-units v0.3.3 github.com/drone/drone-go v1.0.6 github.com/drone/drone-runtime v1.1.0 - github.com/drone/drone-ui v0.0.0-20190826173727-43d81f243ef3 + github.com/drone/drone-ui v0.0.0-20190904041021-b4bb5da2b193 github.com/drone/drone-yaml v1.2.3-0.20190902155851-ad8ad9816fbf github.com/drone/envsubst v1.0.1 github.com/drone/go-license v1.0.2 diff --git a/go.sum b/go.sum index 55df3d475..837309c1b 100644 --- a/go.sum +++ b/go.sum @@ -79,6 +79,8 @@ github.com/drone/drone-ui v0.0.0-20190821232913-65807c0b23a3 h1:imiIbuxHbMjvN1r2 github.com/drone/drone-ui v0.0.0-20190821232913-65807c0b23a3/go.mod h1:NBtVWW7NNJpD9+huMD/5TAE1db2nrEh0i35/9Rf1MPI= github.com/drone/drone-ui v0.0.0-20190826173727-43d81f243ef3 h1:HZl9aANRu3LqhO5Adm2kaccMUH8yvbNP+0fd0mXR5YI= github.com/drone/drone-ui v0.0.0-20190826173727-43d81f243ef3/go.mod h1:NBtVWW7NNJpD9+huMD/5TAE1db2nrEh0i35/9Rf1MPI= +github.com/drone/drone-ui v0.0.0-20190904041021-b4bb5da2b193 h1:T9pGkdg6kKcfF0VKXQ6+kTDBWhMOMm1BD5NbwsvDiQE= +github.com/drone/drone-ui v0.0.0-20190904041021-b4bb5da2b193/go.mod h1:NBtVWW7NNJpD9+huMD/5TAE1db2nrEh0i35/9Rf1MPI= github.com/drone/drone-ui v0.8.1 h1:I4WBAlnk/YQzCggQy/Qegb8Nu2T2R9KVzxso/lL98so= github.com/drone/drone-yaml v1.0.4 h1:NYTEGhf/XJMiJT8CwGy+pMOxWC8C2vhhzEo6/gbT4tU= github.com/drone/drone-yaml v1.0.4/go.mod h1:eM365p3g9M5sroFBTR/najiGrZnd/GiIpWHC2UW8PoI= diff --git a/handler/api/api.go b/handler/api/api.go index 5016dc507..5972b9d15 100644 --- a/handler/api/api.go +++ b/handler/api/api.go @@ -188,9 +188,9 @@ func (s Server) Handler() http.Handler { acl.CheckWriteAccess(), ).Post("/{number}/promote", builds.HandlePromote(s.Repos, s.Builds, s.Triggerer)) - // r.With( - // acl.CheckAdminAccess(), - // ).Post("/{number}/rollback", builds.HandleRollback(s.Repos, s.Builds, s.Triggerer)) + r.With( + acl.CheckAdminAccess(), + ).Post("/{number}/rollback", builds.HandleRollback(s.Repos, s.Builds, s.Triggerer)) r.With( acl.CheckAdminAccess(), diff --git a/handler/api/repos/builds/promote.go b/handler/api/repos/builds/promote.go index 6b5a6362e..790ad62e5 100644 --- a/handler/api/repos/builds/promote.go +++ b/handler/api/repos/builds/promote.go @@ -71,6 +71,7 @@ func HandlePromote( AuthorEmail: prev.AuthorEmail, AuthorAvatar: prev.AuthorAvatar, Deployment: environ, + Cron: prev.Cron, Sender: prev.Sender, Params: map[string]string{}, } diff --git a/handler/api/repos/builds/rollback.go b/handler/api/repos/builds/rollback.go index 3047a80e9..794df1320 100644 --- a/handler/api/repos/builds/rollback.go +++ b/handler/api/repos/builds/rollback.go @@ -5,3 +5,99 @@ // +build !oss package builds + +import ( + "net/http" + "strconv" + + "github.com/drone/drone/core" + "github.com/drone/drone/handler/api/render" + "github.com/drone/drone/handler/api/request" + + "github.com/go-chi/chi" +) + +// HandleRollback returns an http.HandlerFunc that processes http +// requests to rollback and re-execute a build. +func HandleRollback( + repos core.RepositoryStore, + builds core.BuildStore, + triggerer core.Triggerer, +) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var ( + environ = r.FormValue("target") + namespace = chi.URLParam(r, "owner") + name = chi.URLParam(r, "name") + user, _ = request.UserFrom(r.Context()) + ) + number, err := strconv.ParseInt(chi.URLParam(r, "number"), 10, 64) + if err != nil { + render.BadRequest(w, err) + return + } + repo, err := repos.FindName(r.Context(), namespace, name) + if err != nil { + render.NotFound(w, err) + return + } + prev, err := builds.FindNumber(r.Context(), repo.ID, number) + if err != nil { + render.NotFound(w, err) + return + } + if environ == "" { + render.BadRequestf(w, "Missing target environment") + return + } + + hook := &core.Hook{ + Parent: prev.Number, + Trigger: user.Login, + Event: core.EventRollback, + Action: prev.Action, + Link: prev.Link, + Timestamp: prev.Timestamp, + Title: prev.Title, + Message: prev.Message, + Before: prev.Before, + After: prev.After, + Ref: prev.Ref, + Fork: prev.Fork, + Source: prev.Source, + Target: prev.Target, + Author: prev.Author, + AuthorName: prev.AuthorName, + AuthorEmail: prev.AuthorEmail, + AuthorAvatar: prev.AuthorAvatar, + Deployment: environ, + Cron: prev.Cron, + Sender: prev.Sender, + Params: map[string]string{}, + } + + for k, v := range prev.Params { + hook.Params[k] = v + } + + for key, value := range r.URL.Query() { + if key == "access_token" { + continue + } + if key == "target" { + continue + } + if len(value) == 0 { + continue + } + hook.Params[key] = value[0] + } + + result, err := triggerer.Trigger(r.Context(), repo, hook) + if err != nil { + render.InternalError(w, err) + } else { + render.JSON(w, result, 200) + } + } +} diff --git a/handler/api/repos/builds/rollback_oss.go b/handler/api/repos/builds/rollback_oss.go new file mode 100644 index 000000000..b7065331d --- /dev/null +++ b/handler/api/repos/builds/rollback_oss.go @@ -0,0 +1,37 @@ +// Copyright 2019 Drone IO, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build oss + +package builds + +import ( + "net/http" + + "github.com/drone/drone/core" + "github.com/drone/drone/handler/api/render" +) + +var notImplemented = func(w http.ResponseWriter, r *http.Request) { + render.NotImplemented(w, render.ErrNotImplemented) +} + +// HandleRollback returns a non-op http.HandlerFunc. +func HandleRollback( + core.RepositoryStore, + core.BuildStore, + core.Triggerer, +) http.HandlerFunc { + return notImplemented +} diff --git a/server/server.go b/server/server.go index d1da662b1..bb126f2bb 100644 --- a/server/server.go +++ b/server/server.go @@ -20,6 +20,7 @@ import ( // A Server defines parameters for running an HTTP server. type Server struct { Acme bool + Email string Addr string Cert string Key string @@ -90,6 +91,7 @@ func (s Server) listenAndServeAcme(ctx context.Context) error { c := cacheDir() m := &autocert.Manager{ + Email: s.Email, Cache: autocert.DirCache(c), Prompt: autocert.AcceptTOS, HostPolicy: autocert.HostWhitelist(s.Host),