Skip to content

Commit

Permalink
Merge pull request agola-io#90 from camandel/ci_skip
Browse files Browse the repository at this point in the history
skip run with special commit
  • Loading branch information
sgotti authored Sep 20, 2019
2 parents 562ba6e + 180dd2f commit 30d5117
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
10 changes: 10 additions & 0 deletions internal/services/gateway/action/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/json"
"net/http"
"path"
"regexp"

"agola.io/agola/internal/config"
gitsource "agola.io/agola/internal/gitsources"
Expand Down Expand Up @@ -66,6 +67,10 @@ const (
AnnotationPullRequestLink = "pull_request_link"
)

var (
SkipRunMessage = regexp.MustCompile(`.*\[ci skip\].*`)
)

func (h *ActionHandler) GetRun(ctx context.Context, runID string) (*rsapitypes.RunResponse, error) {
runResp, resp, err := h.runserviceClient.GetRun(ctx, runID, nil)
if err != nil {
Expand Down Expand Up @@ -483,6 +488,11 @@ func (h *ActionHandler) CreateRuns(ctx context.Context, req *CreateRunRequest) e
}

for _, run := range config.Runs {
if SkipRunMessage.MatchString(req.Message) {
h.log.Debugf("skipping run since special commit message")
continue
}

if match := types.MatchWhen(run.When.ToWhen(), req.Branch, req.Tag, req.Ref); !match {
h.log.Debugf("skipping run since when condition doesn't match")
continue
Expand Down
71 changes: 67 additions & 4 deletions tests/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ func createProject(ctx context.Context, t *testing.T, giteaClient *gitea.Client,
return giteaRepo, project
}

func push(t *testing.T, config, cloneURL, remoteToken string) {
func push(t *testing.T, config, cloneURL, remoteToken, message string) {
gitfs := memfs.New()
f, err := gitfs.Create(".agola/config.jsonnet")
if err != nil {
Expand Down Expand Up @@ -480,7 +480,7 @@ func push(t *testing.T, config, cloneURL, remoteToken string) {
if _, err := wt.Add(".agola/config.jsonnet"); err != nil {
t.Fatalf("unexpected err: %v", err)
}
_, err = wt.Commit("commit", &git.CommitOptions{
_, err = wt.Commit(message, &git.CommitOptions{
Author: &object.Signature{
Name: "user01",
Email: "user01@example.com",
Expand Down Expand Up @@ -510,6 +510,7 @@ func TestPush(t *testing.T) {
config string
num int
annotations map[string]string
message string
}{
{
name: "test push",
Expand Down Expand Up @@ -544,6 +545,7 @@ func TestPush(t *testing.T) {
"ref": "refs/heads/master",
"ref_type": "branch",
},
message: "commit",
},
{
name: "test push with unmatched branch",
Expand Down Expand Up @@ -575,7 +577,68 @@ func TestPush(t *testing.T) {
],
}
`,
num: 0,
num: 0,
message: "commit",
},
{
name: "test push with [ci skip] in subject",
config: `
{
runs: [
{
name: 'run01',
tasks: [
{
name: 'task01',
runtime: {
containers: [
{
image: 'alpine/git',
},
],
},
steps: [
{ type: 'clone' },
{ type: 'run', command: 'env' },
],
},
],
},
],
}
`,
num: 0,
message: "[ci skip] commit",
},
{
name: "test push with [ci skip] in body",
config: `
{
runs: [
{
name: 'run01',
tasks: [
{
name: 'task01',
runtime: {
containers: [
{
image: 'alpine/git',
},
],
},
steps: [
{ type: 'clone' },
{ type: 'run', command: 'env' },
],
},
],
},
],
}
`,
num: 0,
message: "commit\n\n[ci skip] body",
},
}

Expand Down Expand Up @@ -604,7 +667,7 @@ func TestPush(t *testing.T) {

giteaRepo, project := createProject(ctx, t, giteaClient, gwClient)

push(t, tt.config, giteaRepo.CloneURL, giteaToken)
push(t, tt.config, giteaRepo.CloneURL, giteaToken, tt.message)

_ = testutil.Wait(30*time.Second, func() (bool, error) {
runs, _, err := gwClient.GetRuns(ctx, nil, nil, []string{path.Join("/project", project.ID)}, nil, "", 0, false)
Expand Down

0 comments on commit 30d5117

Please # to comment.