Skip to content

Commit 74b0d28

Browse files
committed
fix(github-webhook): clear pending check on ok-to-test
When using GitHub Webhook, the pending check run created for unauthorized PRs was never resolved after an admin commented /ok-to-test, leaving it stuck indefinitely. https://redhat.atlassian.net/browse/SRVKP-11789 Signed-off-by: Zaki Shaikh <zashaikh@redhat.com>
1 parent eda5331 commit 74b0d28

2 files changed

Lines changed: 46 additions & 3 deletions

File tree

pkg/pipelineascode/match.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ func (p *PacRun) verifyRepoAndUser(ctx context.Context) (*v1alpha1.Repository, e
124124
}
125125
// When /ok-to-test is approved, update the parent "Pipelines as Code CI" status to success
126126
// to indicate the approval was successful before pipelines start running.
127-
if p.event.EventType == opscomments.OkToTestCommentEventType.String() && !strings.Contains(p.vcx.GetConfig().Name, "github") {
127+
// we only do this for all providers except github apps since github apps use the checkRun API to update the status
128+
// checking installationID is <= 0 because sometime it's set to -1
129+
if p.event.EventType == opscomments.OkToTestCommentEventType.String() && p.event.InstallationID <= 0 {
128130
approvalStatus := providerstatus.StatusOpts{
129131
Status: CompletedStatus,
130132
Title: "Approved",

pkg/pipelineascode/match_test.go

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"crypto/hmac"
55
"crypto/sha256"
66
"encoding/hex"
7+
"encoding/json"
78
"fmt"
9+
"io"
810
"net/http"
911
"strings"
1012
"testing"
@@ -752,6 +754,36 @@ func TestVerifyRepoAndUser(t *testing.T) {
752754
wantRepoNil: false,
753755
wantErr: false,
754756
},
757+
{
758+
name: "happy path with ok-to-test comment status reporting",
759+
runevent: info.Event{
760+
Organization: "owner",
761+
Repository: "repo",
762+
URL: "https://example.com/owner/repo",
763+
SHA: "123abc",
764+
EventType: opscomments.OkToTestCommentEventType.String(),
765+
TriggerTarget: triggertype.PullRequest,
766+
Sender: "owner",
767+
Request: request,
768+
},
769+
repositories: []*v1alpha1.Repository{{
770+
ObjectMeta: metav1.ObjectMeta{Name: "repo", Namespace: "ns"},
771+
Spec: v1alpha1.RepositorySpec{
772+
URL: "https://example.com/owner/repo",
773+
GitProvider: &v1alpha1.GitProvider{
774+
Secret: &v1alpha1.Secret{
775+
Name: "secret",
776+
},
777+
WebhookSecret: &v1alpha1.Secret{
778+
Name: "webhook-secret",
779+
},
780+
},
781+
},
782+
}},
783+
webhookSecret: "secret",
784+
wantRepoNil: false,
785+
wantErr: false,
786+
},
755787
}
756788

757789
pacInfo := &info.PacOpts{Settings: settings.DefaultSettings()}
@@ -786,14 +818,23 @@ func TestVerifyRepoAndUser(t *testing.T) {
786818
// status endpoint stub (used when CreateStatus is called)
787819
mux.HandleFunc(
788820
fmt.Sprintf("/repos/%s/%s/statuses/%s", tt.runevent.Organization, tt.runevent.Repository, tt.runevent.SHA),
789-
func(rw http.ResponseWriter, _ *http.Request) { fmt.Fprint(rw, `{}`) },
821+
func(rw http.ResponseWriter, r *http.Request) {
822+
body, _ := io.ReadAll(r.Body)
823+
a := struct {
824+
State string `json:"state"`
825+
}{}
826+
err := json.Unmarshal(body, &a)
827+
assert.NilError(t, err)
828+
assert.Equal(t, a.State, "success")
829+
fmt.Fprint(rw, `{}`)
830+
},
790831
)
791832

792833
vcx := &ghprovider.Provider{Token: github.Ptr("token"), Logger: logger}
793834
vcx.SetGithubClient(ghClient)
794835
vcx.SetPacInfo(pacInfo)
795836

796-
k8int := &kitesthelper.KinterfaceTest{GetSecretResult: map[string]string{"pipelines-as-code-secret": tt.webhookSecret}}
837+
k8int := &kitesthelper.KinterfaceTest{GetSecretResult: map[string]string{"pipelines-as-code-secret": tt.webhookSecret, "secret": "token", "webhook-secret": "secret"}}
797838

798839
stdata, _ := testclient.SeedTestData(t, ctx, testclient.Data{Repositories: tt.repositories /*Secret: []*corev1.Secret{secret}*/})
799840
in := info.NewInfo()

0 commit comments

Comments
 (0)