Skip to content

Commit 68c3c23

Browse files
committed
feat(ci): utilize file_not_deleted predicate
When a workflow file that a policy rule depends on is deleted in a PR, the rule should be skipped rather than fail. This is achieved by adding the file_not_deleted predicate to each workflow-based policy rule.
1 parent 8fdb839 commit 68c3c23

File tree

5 files changed

+68
-16
lines changed

5 files changed

+68
-16
lines changed

cmd/generate-policy-bot-config/main_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ on:
209209
ChangedFiles: &predicate.ChangedFiles{
210210
Paths: mustRegexpsFromGlobs(t, []string{"src/**"}),
211211
},
212+
FileNotDeleted: &predicate.FileNotDeleted{
213+
Paths: mustRegexpsFromGlobs(t, []string{".github/workflows/workflow.yml"}),
214+
},
212215
},
213216
Requires: approval.Requires{
214217
Conditions: predicate.Predicates{
@@ -385,6 +388,9 @@ func expectedConfig(t *testing.T) policy.Config {
385388
ChangedFiles: &predicate.ChangedFiles{
386389
Paths: mustRegexpsFromGlobs(t, []string{"src/**"}),
387390
},
391+
FileNotDeleted: &predicate.FileNotDeleted{
392+
Paths: mustRegexpsFromGlobs(t, []string{".github/workflows/workflow.yml"}),
393+
},
388394
},
389395
Requires: approval.Requires{
390396
Conditions: predicate.Predicates{

go.mod

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
module github.com/grafana/generate-policy-bot-config
22

3-
go 1.23.0
3+
go 1.24.0
4+
5+
toolchain go1.24.1
46

57
require (
68
github.com/jessevdk/go-flags v1.6.1
79
github.com/lmittmann/tint v1.0.7
8-
github.com/palantir/policy-bot v1.35.0
10+
github.com/palantir/policy-bot v1.36.6-0.20250320223329-6245c9d9b3af
911
github.com/redmatter/go-globre v1.2.0
1012
github.com/stretchr/testify v1.10.0
1113
github.com/willabides/actionslog v0.5.1
@@ -18,23 +20,24 @@ require (
1820
github.com/davecgh/go-spew v1.1.1 // indirect
1921
github.com/fatih/color v1.10.0 // indirect
2022
github.com/goccy/go-yaml v1.11.0 // indirect
21-
github.com/google/go-github/v63 v63.0.0 // indirect
23+
github.com/google/go-github/v69 v69.2.0 // indirect
2224
github.com/google/go-querystring v1.1.0 // indirect
2325
github.com/hashicorp/golang-lru v1.0.2 // indirect
26+
github.com/kr/pretty v0.3.1 // indirect
2427
github.com/mattn/go-colorable v0.1.13 // indirect
2528
github.com/mattn/go-isatty v0.0.19 // indirect
2629
github.com/pkg/errors v0.9.1 // indirect
2730
github.com/pmezard/go-difflib v1.0.0 // indirect
2831
github.com/rs/zerolog v1.33.0 // indirect
2932
github.com/shurcooL/githubv4 v0.0.0-20240727222349-48295856cce7 // indirect
3033
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f // indirect
31-
golang.org/x/net v0.36.0 // indirect
34+
golang.org/x/net v0.37.0 // indirect
3235
golang.org/x/sys v0.31.0 // indirect
3336
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
37+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
3438
)
3539

3640
// Includes PRs to test:
3741
// - https://github.com/palantir/policy-bot/pull/796
3842
// - https://github.com/palantir/policy-bot/pull/794
3943
// - https://github.com/palantir/policy-bot/pull/789
40-
replace github.com/palantir/policy-bot => github.com/iainlane/policy-bot v1.35.1-0.20240904124510-b6b6121c33c8

go.sum

+23-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
2+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
23
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
34
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
45
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
@@ -15,16 +16,21 @@ github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5x
1516
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
1617
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
1718
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
18-
github.com/google/go-github/v63 v63.0.0 h1:13xwK/wk9alSokujB9lJkuzdmQuVn2QCPeck76wR3nE=
19-
github.com/google/go-github/v63 v63.0.0/go.mod h1:IqbcrgUmIcEaioWrGYei/09o+ge5vhffGOcxrO0AfmA=
19+
github.com/google/go-github/v69 v69.2.0 h1:wR+Wi/fN2zdUx9YxSmYE0ktiX9IAR/BeePzeaUUbEHE=
20+
github.com/google/go-github/v69 v69.2.0/go.mod h1:xne4jymxLR6Uj9b7J7PyTpkMYstEMMwGZa0Aehh1azM=
2021
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
2122
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
2223
github.com/hashicorp/golang-lru v1.0.2 h1:dV3g9Z/unq5DpblPpw+Oqcv4dU/1omnb4Ok8iPY6p1c=
2324
github.com/hashicorp/golang-lru v1.0.2/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
24-
github.com/iainlane/policy-bot v1.35.1-0.20240904124510-b6b6121c33c8 h1:BegqDJQDSs+S51TZp2Pm06bh4aKyzk+PsDWQ/A7r2wE=
25-
github.com/iainlane/policy-bot v1.35.1-0.20240904124510-b6b6121c33c8/go.mod h1:zQuaWUKRIO+qc5qYErd/raGB4Pok/6H1S7kIfulFcpw=
2625
github.com/jessevdk/go-flags v1.6.1 h1:Cvu5U8UGrLay1rZfv/zP7iLpSHGUZ/Ou68T0iX1bBK4=
2726
github.com/jessevdk/go-flags v1.6.1/go.mod h1:Mk8T1hIAWpOiJiHa9rJASDK2UGWji0EuPGBnNLMooyc=
27+
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
28+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
29+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
30+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
31+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
32+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
33+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
2834
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
2935
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
3036
github.com/lmittmann/tint v1.0.7 h1:D/0OqWZ0YOGZ6AyC+5Y2kD8PBEzBk6rFHVSfOqCkF9Y=
@@ -36,12 +42,17 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
3642
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
3743
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
3844
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
45+
github.com/palantir/policy-bot v1.36.6-0.20250320223329-6245c9d9b3af h1:H9XC0mT1IBwTkDQNZj0tOerSpFB0cLAsjJ/ZkuS625c=
46+
github.com/palantir/policy-bot v1.36.6-0.20250320223329-6245c9d9b3af/go.mod h1:zJKYkrRCN0lpZ9N/RYEeSnKF5LCvmuxc4AyYMiRnirg=
47+
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
3948
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
4049
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
4150
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4251
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
4352
github.com/redmatter/go-globre v1.2.0 h1:Ru6C0wf8ORayShgpEM8l2RCE738rkO7BzaV4ZhSRF/A=
4453
github.com/redmatter/go-globre v1.2.0/go.mod h1:6wjSGhVB4cpmL+nJgfqJ6QYOzkhKZ0xj0h63N0vl9vI=
54+
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
55+
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
4556
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
4657
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
4758
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
@@ -55,14 +66,14 @@ github.com/vektah/gqlparser v1.3.1 h1:8b0IcD3qZKWJQHSzynbDlrtP3IxVydZ2DZepCGofqf
5566
github.com/vektah/gqlparser v1.3.1/go.mod h1:bkVf0FX+Stjg/MHnm8mEyubuaArhNEqfQhF+OTiAL74=
5667
github.com/willabides/actionslog v0.5.1 h1:dJ/Cxg8vO1pEohgC2O4CW1tCWFKJrYJXTZDWYJQK0+E=
5768
github.com/willabides/actionslog v0.5.1/go.mod h1:WDufDP3XZUMBOmau2BvfVCGYuUcVRZI6Eqy8ZRw4pJ8=
58-
golang.org/x/crypto v0.35.0 h1:b15kiHdrGCHrP6LvwaQ3c03kgNhhiMgvlhxHQhmg2Xs=
59-
golang.org/x/crypto v0.35.0/go.mod h1:dy7dXNW32cAb/6/PRuTNsix8T+vJAqvuIy5Bli/x0YQ=
69+
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
70+
golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
6071
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
6172
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
62-
golang.org/x/net v0.36.0 h1:vWF2fRbw4qslQsQzgFqZff+BItCvGFQqKzKIzx1rmoA=
63-
golang.org/x/net v0.36.0/go.mod h1:bFmbeoIPfrw4sMHNhb4J9f6+tPziuGjq7Jk/38fxi1I=
64-
golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs=
65-
golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
73+
golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c=
74+
golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
75+
golang.org/x/oauth2 v0.28.0 h1:CrgCKl8PPAVtLnU3c+EDw6x11699EWlsDeWNWKdIOkc=
76+
golang.org/x/oauth2 v0.28.0/go.mod h1:onh5ek6nERTohokkhCD/y2cV4Do3fxFHFuAejCkRWT8=
6677
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
6778
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
6879
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@@ -75,8 +86,9 @@ golang.org/x/term v0.30.0/go.mod h1:NYYFdzHoI5wRh/h5tDMdMqCqPJZEuNqVR5xJLd/n67g=
7586
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
7687
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
7788
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
78-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
7989
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
90+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
91+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
8092
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
8193
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
8294
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

internal/policybot.go

+9
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ func makeApprovalRule(path string, wf GitHubWorkflow) (*approval.Rule, error) {
115115
}
116116
}
117117

118+
regexPath, err := RegexpsFromGlobs([]string{path})
119+
if err != nil {
120+
return nil, fmt.Errorf("couldn't convert path to regex: %w", err)
121+
}
122+
123+
preds.FileNotDeleted = &predicate.FileNotDeleted{
124+
Paths: regexPath,
125+
}
126+
118127
requires := approval.Requires{
119128
Conditions: predicate.Predicates{
120129
HasWorkflowResult: &predicate.HasWorkflowResult{

internal/policybot_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ func TestMakeApprovalRule(t *testing.T) {
104104
Paths: mustRegexpsFromGlobs(t, []string{"src/**"}),
105105
IgnorePaths: mustRegexpsFromGlobs(t, []string{"docs/**"}),
106106
},
107+
FileNotDeleted: &predicate.FileNotDeleted{
108+
Paths: mustRegexpsFromGlobs(t, []string{".github/workflows/test.yml"}),
109+
},
107110
},
108111
Requires: approval.Requires{
109112
Conditions: predicate.Predicates{
@@ -125,6 +128,11 @@ func TestMakeApprovalRule(t *testing.T) {
125128
},
126129
expected: &approval.Rule{
127130
Name: "Workflow .github/workflows/build.yml succeeded or skipped",
131+
Predicates: predicate.Predicates{
132+
FileNotDeleted: &predicate.FileNotDeleted{
133+
Paths: mustRegexpsFromGlobs(t, []string{".github/workflows/build.yml"}),
134+
},
135+
},
128136
Requires: approval.Requires{
129137
Conditions: predicate.Predicates{
130138
HasWorkflowResult: &predicate.HasWorkflowResult{
@@ -151,6 +159,9 @@ func TestMakeApprovalRule(t *testing.T) {
151159
TargetsBranch: &predicate.TargetsBranch{
152160
Pattern: mustRegexp(t, "(^main$|^develop$)"),
153161
},
162+
FileNotDeleted: &predicate.FileNotDeleted{
163+
Paths: mustRegexpsFromGlobs(t, []string{".github/workflows/test.yml"}),
164+
},
154165
},
155166
Requires: approval.Requires{
156167
Conditions: predicate.Predicates{
@@ -184,6 +195,9 @@ func TestMakeApprovalRule(t *testing.T) {
184195
TargetsBranch: &predicate.TargetsBranch{
185196
Pattern: mustRegexp(t, "(^main$|^develop$)"),
186197
},
198+
FileNotDeleted: &predicate.FileNotDeleted{
199+
Paths: mustRegexpsFromGlobs(t, []string{".github/workflows/test.yml"}),
200+
},
187201
},
188202
Requires: approval.Requires{
189203
Conditions: predicate.Predicates{
@@ -283,6 +297,11 @@ func TestGitHubWorkflowCollectionPolicyBotConfig(t *testing.T) {
283297
ApprovalRules: []*approval.Rule{
284298
{
285299
Name: "Workflow .github/workflows/build.yml succeeded or skipped",
300+
Predicates: predicate.Predicates{
301+
FileNotDeleted: &predicate.FileNotDeleted{
302+
Paths: mustRegexpsFromGlobs(t, []string{".github/workflows/build.yml"}),
303+
},
304+
},
286305
Requires: approval.Requires{
287306
Conditions: predicate.Predicates{
288307
HasWorkflowResult: &predicate.HasWorkflowResult{
@@ -298,6 +317,9 @@ func TestGitHubWorkflowCollectionPolicyBotConfig(t *testing.T) {
298317
ChangedFiles: &predicate.ChangedFiles{
299318
Paths: mustRegexpsFromGlobs(t, []string{"src/**"}),
300319
},
320+
FileNotDeleted: &predicate.FileNotDeleted{
321+
Paths: mustRegexpsFromGlobs(t, []string{".github/workflows/test.yml"}),
322+
},
301323
},
302324
Requires: approval.Requires{
303325
Conditions: predicate.Predicates{

0 commit comments

Comments
 (0)