This commit is contained in:
Brad Rydzewski 2019-09-18 15:06:38 -07:00
parent ab81e18705
commit b950e28dd3
8 changed files with 133 additions and 0 deletions

View file

@ -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

View file

@ -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))
})

View file

@ -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)
}
}

View file

@ -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

View file

@ -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
}

View file

@ -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):

View file

@ -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{

View file

@ -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(),
}