Merge pull request #90 from camandel/ci_skip

skip run with special commit
This commit is contained in:
Simone Gotti 2019-09-20 14:33:27 +02:00 committed by GitHub
commit c3ca892f00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 77 additions and 4 deletions

View File

@ -19,6 +19,7 @@ import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"path" "path"
"regexp"
"agola.io/agola/internal/config" "agola.io/agola/internal/config"
gitsource "agola.io/agola/internal/gitsources" gitsource "agola.io/agola/internal/gitsources"
@ -66,6 +67,10 @@ const (
AnnotationPullRequestLink = "pull_request_link" AnnotationPullRequestLink = "pull_request_link"
) )
var (
SkipRunMessage = regexp.MustCompile(`.*\[ci skip\].*`)
)
func (h *ActionHandler) GetRun(ctx context.Context, runID string) (*rsapitypes.RunResponse, error) { func (h *ActionHandler) GetRun(ctx context.Context, runID string) (*rsapitypes.RunResponse, error) {
runResp, resp, err := h.runserviceClient.GetRun(ctx, runID, nil) runResp, resp, err := h.runserviceClient.GetRun(ctx, runID, nil)
if err != nil { if err != nil {
@ -483,6 +488,11 @@ func (h *ActionHandler) CreateRuns(ctx context.Context, req *CreateRunRequest) e
} }
for _, run := range config.Runs { 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 { 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") h.log.Debugf("skipping run since when condition doesn't match")
continue continue

View File

@ -451,7 +451,7 @@ func createProject(ctx context.Context, t *testing.T, giteaClient *gitea.Client,
return giteaRepo, project 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() gitfs := memfs.New()
f, err := gitfs.Create(".agola/config.jsonnet") f, err := gitfs.Create(".agola/config.jsonnet")
if err != nil { if err != nil {
@ -480,7 +480,7 @@ func push(t *testing.T, config, cloneURL, remoteToken string) {
if _, err := wt.Add(".agola/config.jsonnet"); err != nil { if _, err := wt.Add(".agola/config.jsonnet"); err != nil {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)
} }
_, err = wt.Commit("commit", &git.CommitOptions{ _, err = wt.Commit(message, &git.CommitOptions{
Author: &object.Signature{ Author: &object.Signature{
Name: "user01", Name: "user01",
Email: "user01@example.com", Email: "user01@example.com",
@ -510,6 +510,7 @@ func TestPush(t *testing.T) {
config string config string
num int num int
annotations map[string]string annotations map[string]string
message string
}{ }{
{ {
name: "test push", name: "test push",
@ -544,6 +545,7 @@ func TestPush(t *testing.T) {
"ref": "refs/heads/master", "ref": "refs/heads/master",
"ref_type": "branch", "ref_type": "branch",
}, },
message: "commit",
}, },
{ {
name: "test push with unmatched branch", name: "test push with unmatched branch",
@ -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",
}, },
} }
@ -604,7 +667,7 @@ func TestPush(t *testing.T) {
giteaRepo, project := createProject(ctx, t, giteaClient, gwClient) 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) { _ = testutil.Wait(30*time.Second, func() (bool, error) {
runs, _, err := gwClient.GetRuns(ctx, nil, nil, []string{path.Join("/project", project.ID)}, nil, "", 0, false) runs, _, err := gwClient.GetRuns(ctx, nil, nil, []string{path.Join("/project", project.ID)}, nil, "", 0, false)