Skip to content

Commit

Permalink
closes #2835 and #2781
Browse files Browse the repository at this point in the history
  • Loading branch information
bradrydzewski committed Sep 18, 2019
1 parent ab81e18 commit b950e28
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions handler/api/api.go
Expand Up @@ -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))
})
Expand Down
101 changes: 101 additions & 0 deletions 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)
}
}
7 changes: 7 additions & 0 deletions 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
5 changes: 5 additions & 0 deletions handler/api/repos/crons/none.go
Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions trigger/skip.go
Expand Up @@ -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):
Expand Down
10 changes: 10 additions & 0 deletions trigger/skip_test.go
Expand Up @@ -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{
Expand Down
1 change: 1 addition & 0 deletions trigger/trigger.go
Expand Up @@ -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(),
}
Expand Down

0 comments on commit b950e28

Please sign in to comment.