forked from go-vela/types
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.go
70 lines (60 loc) · 1.83 KB
/
webhook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.
package types
import (
"strings"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
)
var (
skipDeleteEventMsg = "tag/branch delete event"
skipDirectiveMsg = "skip ci directive found in commit title/message"
)
// Webhook defines a struct that is used to return
// the required data when processing webhook event
// a for a source provider event.
type Webhook struct {
Comment string
PRNumber int
Hook *library.Hook
Repo *library.Repo
Build *library.Build
}
// ShouldSkip uses the build information
// associated with the given hook to determine
// whether the hook should be skipped.
func (w *Webhook) ShouldSkip() (bool, string) {
// push or tag event
if strings.EqualFold(constants.EventPush, w.Build.GetEvent()) || strings.EqualFold(constants.EventTag, w.Build.GetEvent()) {
// the head commit will return null in the hook
// payload from the scm when the event is
// associated with a branch/tag delete
if len(w.Build.GetCommit()) == 0 {
return true, skipDeleteEventMsg
}
// check for skip ci directive in message or title
if hasSkipDirective(w.Build.GetMessage()) ||
hasSkipDirective(w.Build.GetTitle()) {
return true, skipDirectiveMsg
}
}
return false, ""
}
// hasSkipDirective is a small helper function
// to check a string for a number of patterns
// that signal to vela that the hook should
// be skipped from processing.
func hasSkipDirective(s string) bool {
sl := strings.ToLower(s)
switch {
case strings.Contains(sl, "[skip ci]"),
strings.Contains(sl, "[ci skip]"),
strings.Contains(sl, "[skip vela]"),
strings.Contains(sl, "[vela skip]"),
strings.Contains(sl, "***no_ci***"):
return true
default:
return false
}
}