@@ -24,11 +24,17 @@ import (
2424 "github.com/fionn/commit-signature-verifier/internal/xssh"
2525)
2626
27+ // GitHub encapsulates GitHub-related functionality.
28+ type GitHub struct {
29+ * github.Client
30+
31+ webhookSecret configuration.Secret
32+ }
33+
2734// Service provides the GitHub client, signature configuration and methods to
2835// handle commit payloads.
2936type Service struct {
30- github * github.Client
31- webhookSecret configuration.Secret
37+ github GitHub
3238 allowedSigners []xssh.AllowedSigner
3339}
3440
@@ -73,6 +79,8 @@ func VerifyCommit(commit *github.Commit, allowedSigners []xssh.AllowedSigner) (o
7379 return true , description
7480}
7581
82+ // statusFromEvent takes an event and returns a commit status object suitable
83+ // for posting.
7684func (s Service ) statusFromEvent (ctx context.Context , event * github.PushEvent ) (* github.RepoStatus , error ) {
7785 if strings .HasPrefix (* event .Ref , "refs/tags/" ) {
7886 slog .DebugContext (ctx , "Received tag so skipping status" , slog .String ("tag" , * event .Ref ))
@@ -101,7 +109,12 @@ func (s Service) statusFromEvent(ctx context.Context, event *github.PushEvent) (
101109 description := fmt .Sprintf ("Failed to get commit %s." , * event .After )
102110 slog .ErrorContext (ctx , "Failed to get commit" ,
103111 slog .String ("commit" , * event .After ), slog .String ("error" , err .Error ()))
104- return & github.RepoStatus {State : & state , Description : & description , Context : & context }, nil
112+
113+ return & github.RepoStatus {
114+ State : & state ,
115+ Description : & description ,
116+ Context : & context ,
117+ }, nil
105118 }
106119
107120 commit := repositoryCommit .Commit
@@ -112,10 +125,17 @@ func (s Service) statusFromEvent(ctx context.Context, event *github.PushEvent) (
112125 if ok {
113126 state = "success"
114127 }
115- return & github.RepoStatus {State : & state , Description : & description , Context : & context }, nil
128+
129+ return & github.RepoStatus {
130+ State : & state ,
131+ Description : & description ,
132+ Context : & context ,
133+ }, nil
116134}
117135
118- func (s Service ) postPushEventStatus (ctx context.Context , event * github.PushEvent ) error {
136+ // processPushEvent gets the commit signature state for the event and posts it
137+ // to the commit status, i.e. this is primarily a side-effect function.
138+ func (s Service ) processPushEvent (ctx context.Context , event * github.PushEvent ) error {
119139 status , err := s .statusFromEvent (ctx , event )
120140 if err != nil {
121141 return fmt .Errorf ("failed to create commit status: %w" , err )
@@ -137,10 +157,13 @@ func (s Service) postPushEventStatus(ctx context.Context, event *github.PushEven
137157 return nil
138158}
139159
160+ // handleWebhook receives webhook events posted to us and kicks off the
161+ // processing of these events. Its role is to sanity-check the incoming payloads
162+ // and then, if deemed acceptable, pass the event off to processPushEvent.
140163func (s Service ) handleWebhook (w http.ResponseWriter , r * http.Request ) {
141164 ctx := r .Context ()
142165
143- payload , err := github .ValidatePayload (r , s .webhookSecret )
166+ payload , err := github .ValidatePayload (r , s .github . webhookSecret )
144167 if err != nil {
145168 slog .WarnContext (ctx , "Failed to validate payload" , slog .String ("error" , err .Error ()))
146169 http .Error (w , "Failed to validate payload" , http .StatusForbidden )
@@ -160,9 +183,11 @@ func (s Service) handleWebhook(w http.ResponseWriter, r *http.Request) {
160183 slog .String ("repository" , * event .Repo .FullName ),
161184 slog .String ("ref" , * event .Ref ),
162185 slog .String ("commit" , * event .After ))
186+
163187 ctx = context .WithValue (ctx ,
164188 github .SleepUntilPrimaryRateLimitResetWhenRateLimited , true )
165- if err := s .postPushEventStatus (ctx , event ); err != nil {
189+
190+ if err := s .processPushEvent (ctx , event ); err != nil {
166191 slog .ErrorContext (ctx , "Failed to handle push event" ,
167192 slog .String ("error" , err .Error ()))
168193 http .Error (w , "Failed to handle push event" , http .StatusInternalServerError )
@@ -196,8 +221,7 @@ func Run() error {
196221 }
197222
198223 service := Service {
199- github : githubClient ,
200- webhookSecret : config .WebhookSecret ,
224+ github : GitHub {githubClient , config .WebhookSecret },
201225 allowedSigners : config .AllowedSigners ,
202226 }
203227
0 commit comments