From 309da8b0f5d3d426a5de6e90dcc0cd4635218abf Mon Sep 17 00:00:00 2001 From: pputman-clabs Date: Wed, 12 Feb 2025 02:54:27 -0600 Subject: [PATCH 01/15] add helper function to parse DeploymentCallBackURL to get RunID --- github/github.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/github/github.go b/github/github.go index 9c5ca1f9241..39d708fb567 100644 --- a/github/github.go +++ b/github/github.go @@ -1770,3 +1770,14 @@ type roundTripperFunc func(*http.Request) (*http.Response, error) func (fn roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) { return fn(r) } + +func (e *DeploymentProtectionRuleEvent) GetRunID() int64 { + r, _ := regexp.MustCompile(`^repos\/.*\/.*\/actions\/runs\/(?P[0-9].*)\/deployment_protection_rule$`) + + match = r.FindStringSubmatch(e.DeploymentCallbackURL) + runID, err := strconv.ParseInt(match, 10, 64) + if err != nil { + panic(err) + } + return returnID +} From 19449c007ef677e4ca1c150ab969cbef15499c94 Mon Sep 17 00:00:00 2001 From: pputman-clabs Date: Wed, 12 Feb 2025 03:33:52 -0600 Subject: [PATCH 02/15] added a test to make sure it got the runID properly --- github/github.go | 14 ++++++++------ github/github_test.go | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/github/github.go b/github/github.go index 39d708fb567..e6122a4e894 100644 --- a/github/github.go +++ b/github/github.go @@ -19,6 +19,7 @@ import ( "net/http" "net/url" "reflect" + "regexp" "strconv" "strings" "sync" @@ -1771,13 +1772,14 @@ func (fn roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) { return fn(r) } -func (e *DeploymentProtectionRuleEvent) GetRunID() int64 { - r, _ := regexp.MustCompile(`^repos\/.*\/.*\/actions\/runs\/(?P[0-9].*)\/deployment_protection_rule$`) +// Helper Function to extract the workflow RunID from the *DeploymentProtectionRuleEvent.DeploymentCallBackURL +func (e *DeploymentProtectionRuleEvent) GetRunID() (int64, error) { + r := regexp.MustCompile(`^repos\/.*\/.*\/actions\/runs\/(?P[0-9].*)\/deployment_protection_rule$`) + match := r.FindStringSubmatch(*e.DeploymentCallbackURL) - match = r.FindStringSubmatch(e.DeploymentCallbackURL) - runID, err := strconv.ParseInt(match, 10, 64) + runID, err := strconv.ParseInt(match[1], 10, 64) if err != nil { - panic(err) + return -1, err } - return returnID + return runID, nil } diff --git a/github/github_test.go b/github/github_test.go index b402f098413..24ee544803f 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -3101,3 +3101,20 @@ func TestPtr(t *testing.T) { equal(t, int64(-10), *Ptr(int64(-10))) equal(t, "str", *Ptr("str")) } + +func TestDeploymentProtectionRuleEvent_GetRunID(t *testing.T) { + t.Parallel() + var want int64 = 123456789 + testURL := fmt.Sprintf("repos/dummy-org/dummy-repo/actions/runs/%v/deployment_protection_rule", want) + + e := DeploymentProtectionRuleEvent{ + DeploymentCallbackURL: &testURL, + } + got, err := e.GetRunID() + if err != nil { + t.Errorf("error %v", err) + } + if got != want { + t.Errorf("want %#v, got %#v", want, got) + } +} From 52886c45f75067e0eec7782ea48d68d2b299c3e8 Mon Sep 17 00:00:00 2001 From: pputman-clabs Date: Wed, 12 Feb 2025 10:38:09 -0600 Subject: [PATCH 03/15] Add check for regex match --- github/github.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/github/github.go b/github/github.go index e6122a4e894..a3fd9e4270d 100644 --- a/github/github.go +++ b/github/github.go @@ -1772,11 +1772,13 @@ func (fn roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) { return fn(r) } -// Helper Function to extract the workflow RunID from the *DeploymentProtectionRuleEvent.DeploymentCallBackURL +// Helper Function to extract the workflow RunID from the *DeploymentProtectionRuleEvent.DeploymentCallBackURL. func (e *DeploymentProtectionRuleEvent) GetRunID() (int64, error) { r := regexp.MustCompile(`^repos\/.*\/.*\/actions\/runs\/(?P[0-9].*)\/deployment_protection_rule$`) match := r.FindStringSubmatch(*e.DeploymentCallbackURL) - + if len(match) != 2 { + return -1, errors.New("no match") + } runID, err := strconv.ParseInt(match[1], 10, 64) if err != nil { return -1, err From 4d2ad3172f2a588c068e13f286fd217c5d98dde5 Mon Sep 17 00:00:00 2001 From: pputman-clabs Date: Wed, 12 Feb 2025 11:05:46 -0600 Subject: [PATCH 04/15] fix runID regex to only match on integers --- github/github.go | 2 +- github/github_test.go | 35 ++++++++++++++++++++++++----------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/github/github.go b/github/github.go index a3fd9e4270d..beee4036e43 100644 --- a/github/github.go +++ b/github/github.go @@ -1774,7 +1774,7 @@ func (fn roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) { // Helper Function to extract the workflow RunID from the *DeploymentProtectionRuleEvent.DeploymentCallBackURL. func (e *DeploymentProtectionRuleEvent) GetRunID() (int64, error) { - r := regexp.MustCompile(`^repos\/.*\/.*\/actions\/runs\/(?P[0-9].*)\/deployment_protection_rule$`) + r := regexp.MustCompile(`^repos\/.*\/.*\/actions\/runs\/(?P[0-9]*)\/deployment_protection_rule$`) match := r.FindStringSubmatch(*e.DeploymentCallbackURL) if len(match) != 2 { return -1, errors.New("no match") diff --git a/github/github_test.go b/github/github_test.go index 24ee544803f..172c07d996a 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -3104,17 +3104,30 @@ func TestPtr(t *testing.T) { func TestDeploymentProtectionRuleEvent_GetRunID(t *testing.T) { t.Parallel() - var want int64 = 123456789 - testURL := fmt.Sprintf("repos/dummy-org/dummy-repo/actions/runs/%v/deployment_protection_rule", want) - - e := DeploymentProtectionRuleEvent{ - DeploymentCallbackURL: &testURL, - } - got, err := e.GetRunID() - if err != nil { - t.Errorf("error %v", err) + tests := []struct { + want int64 + URL string + }{ + { + want: 123456789, + URL: fmt.Sprintf("repos/dummy-org/dummy-repo/actions/runs/%v/deployment_protection_rule", 123456789), + }, + { + want: , + URL: fmt.Sprintf("repos/dummy-org/dummy-repo/actions/runs/%v/deployment_protection_rule", "abc123"), + }, } - if got != want { - t.Errorf("want %#v, got %#v", want, got) + for _, tt := range tests { + + e := DeploymentProtectionRuleEvent{ + DeploymentCallbackURL: &tt.URL, + } + got, err := e.GetRunID() + if err != nil { + t.Errorf("error %v", err) + } + if got != tt.want { + t.Errorf("want %#v, got %#v", tt.want, got) + } } } From fa18394db3773850f5124a9fd7766b7f1267598f Mon Sep 17 00:00:00 2001 From: pputman-clabs Date: Wed, 12 Feb 2025 16:41:31 -0600 Subject: [PATCH 05/15] add test for incorrect runID string --- github/github.go | 4 +++- github/github_test.go | 46 +++++++++++++++++++++---------------------- go.mod | 7 +++++++ go.sum | 9 +++++++++ 4 files changed, 42 insertions(+), 24 deletions(-) diff --git a/github/github.go b/github/github.go index beee4036e43..31e0086319e 100644 --- a/github/github.go +++ b/github/github.go @@ -1772,9 +1772,11 @@ func (fn roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) { return fn(r) } + +var r *regexp.Regexp = regexp.MustCompile(`^repos\/.*\/.*\/actions\/runs\/(\d+)\/deployment_protection_rule$`) + // Helper Function to extract the workflow RunID from the *DeploymentProtectionRuleEvent.DeploymentCallBackURL. func (e *DeploymentProtectionRuleEvent) GetRunID() (int64, error) { - r := regexp.MustCompile(`^repos\/.*\/.*\/actions\/runs\/(?P[0-9]*)\/deployment_protection_rule$`) match := r.FindStringSubmatch(*e.DeploymentCallbackURL) if len(match) != 2 { return -1, errors.New("no match") diff --git a/github/github_test.go b/github/github_test.go index 172c07d996a..1b4df2f0398 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -22,6 +22,7 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" "github.com/google/go-cmp/cmp" ) @@ -3104,30 +3105,29 @@ func TestPtr(t *testing.T) { func TestDeploymentProtectionRuleEvent_GetRunID(t *testing.T) { t.Parallel() - tests := []struct { - want int64 - URL string - }{ - { - want: 123456789, - URL: fmt.Sprintf("repos/dummy-org/dummy-repo/actions/runs/%v/deployment_protection_rule", 123456789), - }, - { - want: , - URL: fmt.Sprintf("repos/dummy-org/dummy-repo/actions/runs/%v/deployment_protection_rule", "abc123"), - }, + + var want int64 = 123456789 + URL := "repos/dummy-org/dummy-repo/actions/runs/123456789/deployment_protection_rule" + + e := DeploymentProtectionRuleEvent{ + DeploymentCallbackURL: &URL, } - for _, tt := range tests { - e := DeploymentProtectionRuleEvent{ - DeploymentCallbackURL: &tt.URL, - } - got, err := e.GetRunID() - if err != nil { - t.Errorf("error %v", err) - } - if got != tt.want { - t.Errorf("want %#v, got %#v", tt.want, got) - } + got, _ := e.GetRunID() + if got != want { + t.Errorf("want %#v, got %#v", want, got) + } + + want = -1 + URL = "repos/dummy-org/dummy-repo/actions/runs/abc123/deployment_protection_rule" + + var err error + expectedErrorMsg := "no match" + got, err = e.GetRunID() + + assert.EqualErrorf(t, err, expectedErrorMsg, "Error should be: %v, got: %v", expectedErrorMsg, err) + + if got != want { + t.Errorf("want %#v, got %#v", want, got) } } diff --git a/go.mod b/go.mod index b08ff98195a..4cd0856d4fd 100644 --- a/go.mod +++ b/go.mod @@ -6,3 +6,10 @@ require ( github.com/google/go-cmp v0.6.0 github.com/google/go-querystring v1.1.0 ) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/testify v1.10.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum index b2abdf11c21..6ce5f3fc9c9 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,15 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 918be154d593ff7a0a6611e43c94a5f746cede0e Mon Sep 17 00:00:00 2001 From: pputman-clabs Date: Wed, 12 Feb 2025 23:01:59 -0600 Subject: [PATCH 06/15] revert go.mod and go.sum --- go.mod | 7 ------- go.sum | 9 --------- 2 files changed, 16 deletions(-) diff --git a/go.mod b/go.mod index 4cd0856d4fd..b08ff98195a 100644 --- a/go.mod +++ b/go.mod @@ -6,10 +6,3 @@ require ( github.com/google/go-cmp v0.6.0 github.com/google/go-querystring v1.1.0 ) - -require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/stretchr/testify v1.10.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect -) diff --git a/go.sum b/go.sum index 6ce5f3fc9c9..b2abdf11c21 100644 --- a/go.sum +++ b/go.sum @@ -1,15 +1,6 @@ -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= -github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 20afbb09062423a035d5c7cdef0bb33e6e586664 Mon Sep 17 00:00:00 2001 From: pputman-clabs Date: Wed, 12 Feb 2025 23:02:54 -0600 Subject: [PATCH 07/15] go format --- github/github.go | 1 - github/github_test.go | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/github/github.go b/github/github.go index 31e0086319e..75f79eaa7ec 100644 --- a/github/github.go +++ b/github/github.go @@ -1772,7 +1772,6 @@ func (fn roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) { return fn(r) } - var r *regexp.Regexp = regexp.MustCompile(`^repos\/.*\/.*\/actions\/runs\/(\d+)\/deployment_protection_rule$`) // Helper Function to extract the workflow RunID from the *DeploymentProtectionRuleEvent.DeploymentCallBackURL. diff --git a/github/github_test.go b/github/github_test.go index 1b4df2f0398..e11bf008df9 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -22,8 +22,8 @@ import ( "testing" "time" - "github.com/stretchr/testify/assert" "github.com/google/go-cmp/cmp" + "github.com/stretchr/testify/assert" ) const ( @@ -3107,7 +3107,7 @@ func TestDeploymentProtectionRuleEvent_GetRunID(t *testing.T) { t.Parallel() var want int64 = 123456789 - URL := "repos/dummy-org/dummy-repo/actions/runs/123456789/deployment_protection_rule" + URL := "repos/dummy-org/dummy-repo/actions/runs/123456789/deployment_protection_rule" e := DeploymentProtectionRuleEvent{ DeploymentCallbackURL: &URL, @@ -3119,7 +3119,7 @@ func TestDeploymentProtectionRuleEvent_GetRunID(t *testing.T) { } want = -1 - URL = "repos/dummy-org/dummy-repo/actions/runs/abc123/deployment_protection_rule" + URL = "repos/dummy-org/dummy-repo/actions/runs/abc123/deployment_protection_rule" var err error expectedErrorMsg := "no match" From 8564c962adb3d9a224d7cc309363f2a1dabbd008 Mon Sep 17 00:00:00 2001 From: pputman-clabs Date: Thu, 13 Feb 2025 02:02:22 -0600 Subject: [PATCH 08/15] remove error type checking as we only need to see if an error is returned --- github/github.go | 3 ++- github/github_test.go | 12 +++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/github/github.go b/github/github.go index 75f79eaa7ec..44eeef9ee3e 100644 --- a/github/github.go +++ b/github/github.go @@ -1777,8 +1777,9 @@ var r *regexp.Regexp = regexp.MustCompile(`^repos\/.*\/.*\/actions\/runs\/(\d+)\ // Helper Function to extract the workflow RunID from the *DeploymentProtectionRuleEvent.DeploymentCallBackURL. func (e *DeploymentProtectionRuleEvent) GetRunID() (int64, error) { match := r.FindStringSubmatch(*e.DeploymentCallbackURL) + var ErrNoMatch = errors.New("no match found") if len(match) != 2 { - return -1, errors.New("no match") + return -1, ErrNoMatch } runID, err := strconv.ParseInt(match[1], 10, 64) if err != nil { diff --git a/github/github_test.go b/github/github_test.go index e11bf008df9..8419b1cae5e 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -23,7 +23,6 @@ import ( "time" "github.com/google/go-cmp/cmp" - "github.com/stretchr/testify/assert" ) const ( @@ -3120,12 +3119,11 @@ func TestDeploymentProtectionRuleEvent_GetRunID(t *testing.T) { want = -1 URL = "repos/dummy-org/dummy-repo/actions/runs/abc123/deployment_protection_rule" - - var err error - expectedErrorMsg := "no match" - got, err = e.GetRunID() - - assert.EqualErrorf(t, err, expectedErrorMsg, "Error should be: %v, got: %v", expectedErrorMsg, err) + + got, err := e.GetRunID() + if err == nil { + t.Errorf("Expected error to be returned") + } if got != want { t.Errorf("want %#v, got %#v", want, got) From 84cdc289db2096cfa06ec00f5896ecd032a094db Mon Sep 17 00:00:00 2001 From: pputman-clabs Date: Thu, 13 Feb 2025 02:04:15 -0600 Subject: [PATCH 09/15] go format --- github/github_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/github/github_test.go b/github/github_test.go index 8419b1cae5e..5c4cbfe2280 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -3119,11 +3119,11 @@ func TestDeploymentProtectionRuleEvent_GetRunID(t *testing.T) { want = -1 URL = "repos/dummy-org/dummy-repo/actions/runs/abc123/deployment_protection_rule" - + got, err := e.GetRunID() - if err == nil { - t.Errorf("Expected error to be returned") - } + if err == nil { + t.Errorf("Expected error to be returned") + } if got != want { t.Errorf("want %#v, got %#v", want, got) From 3e68eccd9735d2725c9ee0a5cd176604dabd96d9 Mon Sep 17 00:00:00 2001 From: pputman-clabs Date: Thu, 13 Feb 2025 08:38:23 -0600 Subject: [PATCH 10/15] touch up the regex --- github/github.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/github.go b/github/github.go index 44eeef9ee3e..a57aa2e70f9 100644 --- a/github/github.go +++ b/github/github.go @@ -1772,7 +1772,7 @@ func (fn roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) { return fn(r) } -var r *regexp.Regexp = regexp.MustCompile(`^repos\/.*\/.*\/actions\/runs\/(\d+)\/deployment_protection_rule$`) +var r *regexp.Regexp = regexp.MustCompile(`^repos/.*/actions/runs/(\d+)/deployment_protection_rule$`) // Helper Function to extract the workflow RunID from the *DeploymentProtectionRuleEvent.DeploymentCallBackURL. func (e *DeploymentProtectionRuleEvent) GetRunID() (int64, error) { From 421a6521f141d7ae5ccbf6dce4454216bdc3effd Mon Sep 17 00:00:00 2001 From: pputman-clabs Date: Thu, 13 Feb 2025 10:01:02 -0600 Subject: [PATCH 11/15] remove new custom error type since it provides no real benefit --- github/github.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/github/github.go b/github/github.go index a57aa2e70f9..81c7a5026b3 100644 --- a/github/github.go +++ b/github/github.go @@ -1777,9 +1777,8 @@ var r *regexp.Regexp = regexp.MustCompile(`^repos/.*/actions/runs/(\d+)/deployme // Helper Function to extract the workflow RunID from the *DeploymentProtectionRuleEvent.DeploymentCallBackURL. func (e *DeploymentProtectionRuleEvent) GetRunID() (int64, error) { match := r.FindStringSubmatch(*e.DeploymentCallbackURL) - var ErrNoMatch = errors.New("no match found") if len(match) != 2 { - return -1, ErrNoMatch + return -1, errors.New("no match") } runID, err := strconv.ParseInt(match[1], 10, 64) if err != nil { From 4fb5ed1c1060fb77bb6597da121b5818df4fc4eb Mon Sep 17 00:00:00 2001 From: pputman-clabs Date: Thu, 13 Feb 2025 10:15:48 -0600 Subject: [PATCH 12/15] add comments to actions_worklow_runs.go methods that use RunID about the use of the new helper function --- github/actions_workflow_runs.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/github/actions_workflow_runs.go b/github/actions_workflow_runs.go index dddc56d2327..70fa5ce8b31 100644 --- a/github/actions_workflow_runs.go +++ b/github/actions_workflow_runs.go @@ -203,7 +203,8 @@ func (s *ActionsService) ListRepositoryWorkflowRuns(ctx context.Context, owner, return runs, resp, nil } -// GetWorkflowRunByID gets a specific workflow run by ID. +// GetWorkflowRunByID gets a specific workflow run by ID. +// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent. // // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#get-a-workflow-run // @@ -226,6 +227,7 @@ func (s *ActionsService) GetWorkflowRunByID(ctx context.Context, owner, repo str } // GetWorkflowRunAttempt gets a specific workflow run attempt. +// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent. // // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#get-a-workflow-run-attempt // @@ -252,6 +254,7 @@ func (s *ActionsService) GetWorkflowRunAttempt(ctx context.Context, owner, repo } // GetWorkflowRunAttemptLogs gets a redirect URL to download a plain text file of logs for a workflow run for attempt number. +// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve a workflow run ID from the DeploymentProtectionRuleEvent. // // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#download-workflow-run-attempt-logs // @@ -302,6 +305,7 @@ func (s *ActionsService) getWorkflowRunAttemptLogsWithRateLimit(ctx context.Cont } // RerunWorkflowByID re-runs a workflow by ID. +// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID a the DeploymentProtectionRuleEvent. // // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#re-run-a-workflow // @@ -318,6 +322,7 @@ func (s *ActionsService) RerunWorkflowByID(ctx context.Context, owner, repo stri } // RerunFailedJobsByID re-runs all of the failed jobs and their dependent jobs in a workflow run by ID. +// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent. // // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#re-run-failed-jobs-from-a-workflow-run // @@ -335,6 +340,7 @@ func (s *ActionsService) RerunFailedJobsByID(ctx context.Context, owner, repo st // RerunJobByID re-runs a job and its dependent jobs in a workflow run by ID. // +// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent. // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#re-run-a-job-from-a-workflow-run // //meta:operation POST /repos/{owner}/{repo}/actions/jobs/{job_id}/rerun @@ -350,6 +356,7 @@ func (s *ActionsService) RerunJobByID(ctx context.Context, owner, repo string, j } // CancelWorkflowRunByID cancels a workflow run by ID. +// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent. // // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#cancel-a-workflow-run // @@ -366,6 +373,7 @@ func (s *ActionsService) CancelWorkflowRunByID(ctx context.Context, owner, repo } // GetWorkflowRunLogs gets a redirect URL to download a plain text file of logs for a workflow run. +// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent. // // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#download-workflow-run-logs // @@ -416,6 +424,7 @@ func (s *ActionsService) getWorkflowRunLogsWithRateLimit(ctx context.Context, u } // DeleteWorkflowRun deletes a workflow run by ID. +// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent. // // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#delete-a-workflow-run // @@ -432,6 +441,7 @@ func (s *ActionsService) DeleteWorkflowRun(ctx context.Context, owner, repo stri } // DeleteWorkflowRunLogs deletes all logs for a workflow run. +// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent. // // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#delete-workflow-run-logs // @@ -448,6 +458,7 @@ func (s *ActionsService) DeleteWorkflowRunLogs(ctx context.Context, owner, repo } // GetWorkflowRunUsageByID gets a specific workflow usage run by run ID in the unit of billable milliseconds. +// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent. // // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#get-workflow-run-usage // @@ -470,6 +481,7 @@ func (s *ActionsService) GetWorkflowRunUsageByID(ctx context.Context, owner, rep } // GetPendingDeployments get all deployment environments for a workflow run that are waiting for protection rules to pass. +// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent. // // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#get-pending-deployments-for-a-workflow-run // @@ -492,6 +504,7 @@ func (s *ActionsService) GetPendingDeployments(ctx context.Context, owner, repo } // PendingDeployments approve or reject pending deployments that are waiting on approval by a required reviewer. +// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent. // // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#review-pending-deployments-for-a-workflow-run // @@ -514,6 +527,7 @@ func (s *ActionsService) PendingDeployments(ctx context.Context, owner, repo str } // ReviewCustomDeploymentProtectionRule approves or rejects custom deployment protection rules provided by a GitHub App for a workflow run. +// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent. // // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#review-custom-deployment-protection-rules-for-a-workflow-run // From d0396e511b85964e16631960f9a39bb5bf1ab2b6 Mon Sep 17 00:00:00 2001 From: pputman-clabs Date: Thu, 13 Feb 2025 10:29:10 -0600 Subject: [PATCH 13/15] fix linting issues --- github/github.go | 4 ++-- github/github_test.go | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/github/github.go b/github/github.go index 81c7a5026b3..5fd0dc9e850 100644 --- a/github/github.go +++ b/github/github.go @@ -1772,9 +1772,9 @@ func (fn roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) { return fn(r) } -var r *regexp.Regexp = regexp.MustCompile(`^repos/.*/actions/runs/(\d+)/deployment_protection_rule$`) +var r = regexp.MustCompile(`^repos/.*/actions/runs/(\d+)/deployment_protection_rule$`) -// Helper Function to extract the workflow RunID from the *DeploymentProtectionRuleEvent.DeploymentCallBackURL. +// GetRunID is a Helper Function used to extract the workflow RunID from the *DeploymentProtectionRuleEvent.DeploymentCallBackURL. func (e *DeploymentProtectionRuleEvent) GetRunID() (int64, error) { match := r.FindStringSubmatch(*e.DeploymentCallbackURL) if len(match) != 2 { diff --git a/github/github_test.go b/github/github_test.go index 5c4cbfe2280..683012adc53 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -3106,10 +3106,10 @@ func TestDeploymentProtectionRuleEvent_GetRunID(t *testing.T) { t.Parallel() var want int64 = 123456789 - URL := "repos/dummy-org/dummy-repo/actions/runs/123456789/deployment_protection_rule" + url := "repos/dummy-org/dummy-repo/actions/runs/123456789/deployment_protection_rule" e := DeploymentProtectionRuleEvent{ - DeploymentCallbackURL: &URL, + DeploymentCallbackURL: &url, } got, _ := e.GetRunID() @@ -3118,7 +3118,7 @@ func TestDeploymentProtectionRuleEvent_GetRunID(t *testing.T) { } want = -1 - URL = "repos/dummy-org/dummy-repo/actions/runs/abc123/deployment_protection_rule" + url = "repos/dummy-org/dummy-repo/actions/runs/abc123/deployment_protection_rule" got, err := e.GetRunID() if err == nil { From 5211c386706a9105b81ade75ba96a2021e8e8981 Mon Sep 17 00:00:00 2001 From: pputman-clabs Date: Thu, 13 Feb 2025 11:13:28 -0600 Subject: [PATCH 14/15] Rename regex to something more descriptive --- github/github.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/github/github.go b/github/github.go index 165fd638f5d..47bcf08f684 100644 --- a/github/github.go +++ b/github/github.go @@ -1772,11 +1772,11 @@ func (fn roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) { return fn(r) } -var r = regexp.MustCompile(`^repos/.*/actions/runs/(\d+)/deployment_protection_rule$`) +var runIDFromURLRE = regexp.MustCompile(`^repos/.*/actions/runs/(\d+)/deployment_protection_rule$`) // GetRunID is a Helper Function used to extract the workflow RunID from the *DeploymentProtectionRuleEvent.DeploymentCallBackURL. func (e *DeploymentProtectionRuleEvent) GetRunID() (int64, error) { - match := r.FindStringSubmatch(*e.DeploymentCallbackURL) + match := runIDFromURLRE.FindStringSubmatch(*e.DeploymentCallbackURL) if len(match) != 2 { return -1, errors.New("no match") } From af36bac76dd5cc8275225bb2afab720cbdeda997 Mon Sep 17 00:00:00 2001 From: pputman-clabs Date: Thu, 13 Feb 2025 13:58:04 -0600 Subject: [PATCH 15/15] run script/generate.sh for linter --- github/actions_workflow_runs.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/github/actions_workflow_runs.go b/github/actions_workflow_runs.go index 70fa5ce8b31..20b9cfcd500 100644 --- a/github/actions_workflow_runs.go +++ b/github/actions_workflow_runs.go @@ -203,7 +203,7 @@ func (s *ActionsService) ListRepositoryWorkflowRuns(ctx context.Context, owner, return runs, resp, nil } -// GetWorkflowRunByID gets a specific workflow run by ID. +// GetWorkflowRunByID gets a specific workflow run by ID. // You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent. // // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#get-a-workflow-run @@ -341,6 +341,7 @@ func (s *ActionsService) RerunFailedJobsByID(ctx context.Context, owner, repo st // RerunJobByID re-runs a job and its dependent jobs in a workflow run by ID. // // You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent. +// // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#re-run-a-job-from-a-workflow-run // //meta:operation POST /repos/{owner}/{repo}/actions/jobs/{job_id}/rerun