From b950e28dd3c6f097a4503f6f9b90b805b728fc6a Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Wed, 18 Sep 2019 15:06:38 -0700 Subject: [PATCH] closes #2835 and #2781 --- CHANGELOG.md | 6 ++ handler/api/api.go | 1 + handler/api/repos/crons/exec.go | 101 +++++++++++++++++++++++++++ handler/api/repos/crons/exec_test.go | 7 ++ handler/api/repos/crons/none.go | 5 ++ trigger/skip.go | 2 + trigger/skip_test.go | 10 +++ trigger/trigger.go | 1 + 8 files changed, 133 insertions(+) create mode 100644 handler/api/repos/crons/exec.go create mode 100644 handler/api/repos/crons/exec_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index a2ca1777..3b3b87f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## Unreleased +### Added +- endpoint point to execute a cron pipeline on-demand, by [@bradrydzewski](https://github.com/bradrydzewski). [#2781](https://github.com/drone/drone/issues/2781). +- ignore skip comments when cron event, by [@bradrydzewski](https://github.com/bradrydzewski). [#2835](https://github.com/drone/drone/issues/2835). + +### Fixed +- fixed issue with missing cron job name in user interface, by [@bradrydzewski](https://github.com/bradrydzewski). ## [1.4.0] - 2019-09-12 ### Added diff --git a/handler/api/api.go b/handler/api/api.go index f9ebb1c9..62c7db9e 100644 --- a/handler/api/api.go +++ b/handler/api/api.go @@ -236,6 +236,7 @@ func (s Server) Handler() http.Handler { r.Post("/", crons.HandleCreate(s.Repos, s.Cron)) r.Get("/", crons.HandleList(s.Repos, s.Cron)) r.Get("/{cron}", crons.HandleFind(s.Repos, s.Cron)) + r.Post("/{cron}", crons.HandleExec(s.Users, s.Repos, s.Cron, s.Commits, s.Triggerer)) r.Patch("/{cron}", crons.HandleUpdate(s.Repos, s.Cron)) r.Delete("/{cron}", crons.HandleDelete(s.Repos, s.Cron)) }) diff --git a/handler/api/repos/crons/exec.go b/handler/api/repos/crons/exec.go new file mode 100644 index 00000000..b533ec72 --- /dev/null +++ b/handler/api/repos/crons/exec.go @@ -0,0 +1,101 @@ +// Copyright 2019 Drone.IO Inc. All rights reserved. +// Use of this source code is governed by the Drone Non-Commercial License +// that can be found in the LICENSE file. + +// +build !oss + +package crons + +import ( + "context" + "fmt" + "net/http" + + "github.com/drone/drone/core" + "github.com/drone/drone/handler/api/render" + "github.com/sirupsen/logrus" + + "github.com/go-chi/chi" +) + +// HandleExec returns an http.HandlerFunc that processes http +// requests to execute a cronjob on-demand. +func HandleExec( + users core.UserStore, + repos core.RepositoryStore, + crons core.CronStore, + commits core.CommitService, + trigger core.Triggerer, +) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var ( + ctx = r.Context() + namespace = chi.URLParam(r, "owner") + name = chi.URLParam(r, "name") + cron = chi.URLParam(r, "cron") + ) + + repo, err := repos.FindName(ctx, namespace, name) + if err != nil { + render.NotFound(w, err) + return + } + + cronjob, err := crons.FindName(ctx, repo.ID, cron) + if err != nil { + render.NotFound(w, err) + logger := logrus.WithError(err) + logger.Debugln("api: cannot find cron") + return + } + + user, err := users.Find(ctx, repo.UserID) + if err != nil { + logger := logrus.WithError(err) + logger.Debugln("api: cannot find repository owner") + render.NotFound(w, err) + return + } + + commit, err := commits.FindRef(ctx, user, repo.Slug, cronjob.Branch) + if err != nil { + logger := logrus.WithError(err). + WithField("namespace", repo.Namespace). + WithField("name", repo.Name). + WithField("cron", cronjob.Name) + logger.Debugln("api: cannot find commit") + render.NotFound(w, err) + return + } + + hook := &core.Hook{ + Trigger: core.TriggerCron, + Event: core.EventCron, + Link: commit.Link, + Timestamp: commit.Author.Date, + Message: commit.Message, + After: commit.Sha, + Ref: fmt.Sprintf("refs/heads/%s", cronjob.Branch), + Target: cronjob.Branch, + Author: commit.Author.Login, + AuthorName: commit.Author.Name, + AuthorEmail: commit.Author.Email, + AuthorAvatar: commit.Author.Avatar, + Cron: cronjob.Name, + Sender: commit.Author.Login, + } + + build, err := trigger.Trigger(context.Background(), repo, hook) + if err != nil { + logger := logrus.WithError(err). + WithField("namespace", repo.Namespace). + WithField("name", repo.Name). + WithField("cron", cronjob.Name) + logger.Debugln("api: cannot trigger cron") + render.InternalError(w, err) + return + } + + render.JSON(w, build, 200) + } +} diff --git a/handler/api/repos/crons/exec_test.go b/handler/api/repos/crons/exec_test.go new file mode 100644 index 00000000..403d96f1 --- /dev/null +++ b/handler/api/repos/crons/exec_test.go @@ -0,0 +1,7 @@ +// Copyright 2019 Drone.IO Inc. All rights reserved. +// Use of this source code is governed by the Drone Non-Commercial License +// that can be found in the LICENSE file. + +// +build !oss + +package crons diff --git a/handler/api/repos/crons/none.go b/handler/api/repos/crons/none.go index 25093991..fe375541 100644 --- a/handler/api/repos/crons/none.go +++ b/handler/api/repos/crons/none.go @@ -46,3 +46,8 @@ func HandleFind(core.RepositoryStore, core.CronStore) http.HandlerFunc { func HandleList(core.RepositoryStore, core.CronStore) http.HandlerFunc { return notImplemented } + +func HandleExec(core.UserStore, core.RepositoryStore, core.CronStore, + core.CommitService, core.Triggerer) http.HandlerFunc { + return notImplemented +} diff --git a/trigger/skip.go b/trigger/skip.go index eedd8e24..84cdfddf 100644 --- a/trigger/skip.go +++ b/trigger/skip.go @@ -57,6 +57,8 @@ func skipMessage(hook *core.Hook) bool { switch { case hook.Event == core.EventTag: return false + case hook.Event == core.EventCron: + return false case skipMessageEval(hook.Message): return true case skipMessageEval(hook.Title): diff --git a/trigger/skip_test.go b/trigger/skip_test.go index 8d77e22a..3d26cfb1 100644 --- a/trigger/skip_test.go +++ b/trigger/skip_test.go @@ -180,6 +180,16 @@ func Test_skipMessage(t *testing.T) { title: "update readme [CI SKIP]", want: false, }, + { + event: "cron", + title: "update readme [CI SKIP]", + want: false, + }, + { + event: "cron", + title: "update readme [CI SKIP]", + want: false, + }, } for _, test := range tests { hook := &core.Hook{ diff --git a/trigger/trigger.go b/trigger/trigger.go index 826b1510..19b2c0d9 100644 --- a/trigger/trigger.go +++ b/trigger/trigger.go @@ -351,6 +351,7 @@ func (t *triggerer) Trigger(ctx context.Context, repo *core.Repository, base *co Deploy: base.Deployment, DeployID: base.DeploymentID, Sender: base.Sender, + Cron: base.Cron, Created: time.Now().Unix(), Updated: time.Now().Unix(), }