1// SPDX-License-Identifier: MIT
2
3package workflow
4
5import "strings"
6
7var skipMarkers = []string{"[skip ci]", "[ci skip]", "[no ci]", "[skip actions]", "[actions skip]"}
8
9// SkipCI reports whether the message of the head commit of a push (or of the
10// commit a tag points at) tells GitHub not to run workflows for it. As
11// recorded on github.com (scenarios skip-directives, skip-directives-placement,
12// skip-checks-trailer and skip-directive-tag):
13//
14// - the markers [skip ci], [ci skip], [no ci], [skip actions] and
15// [actions skip] skip anywhere in the subject or the body, and are
16// case-sensitive: [SKIP CI] and [Skip Ci] do not skip. Near misses do
17// not skip either: [skip-ci], "skip ci" without brackets, [skip ci please]
18// and the markers ***NO_CI*** and ***SKIP_CI***;
19// - the trailer "skip-checks: true" skips only as the end of the message,
20// preceded by two blank lines ("x\n\n\nskip-checks: true"). The space
21// after the colon is optional and trailing white space is ignored. It does
22// not skip with one blank line or none, with text after it, after another
23// trailer, with another value ("false", "yes"), or with any change of case
24// in the key or the value.
25//
26// Not recorded, so assumed: only the head commit of a push is looked at (the
27// scenarios push one commit at a time); more than two blank lines before the
28// trailer count like two; a line holding only spaces or tabs is not blank
29// (the two lines before the trailer must be empty); trailing blank lines and
30// CRLF line ends are ignored; more than one space after the colon is not
31// accepted; and a message made of the trailer alone does not skip.
32func SkipCI(headCommitMessage string) bool {
33 for _, m := range skipMarkers {
34 if strings.Contains(headCommitMessage, m) {
35 return true
36 }
37 }
38 return hasSkipChecksTrailer(headCommitMessage)
39}
40
41// hasSkipChecksTrailer reports whether the message ends with a
42// "skip-checks: true" line preceded by two empty lines and some text.
43func hasSkipChecksTrailer(msg string) bool {
44 msg = strings.ReplaceAll(msg, "\r\n", "\n")
45 msg = strings.TrimRight(msg, " \t\n")
46 lines := strings.Split(msg, "\n")
47 n := len(lines)
48 if n < 4 {
49 return false
50 }
51 val, ok := strings.CutPrefix(lines[n-1], "skip-checks:")
52 if !ok || strings.TrimPrefix(val, " ") != "true" {
53 return false
54 }
55 return lines[n-2] == "" && lines[n-3] == "" && strings.TrimSpace(strings.Join(lines[:n-3], "")) != ""
56}