88 "regexp"
99 "strings"
1010 texttemplate "text/template"
11+ "time"
1112 "unicode/utf8"
1213
1314 "github.com/bradleyfalzon/ghinstallation/v2"
@@ -39,6 +40,7 @@ type GitHubNotification struct {
3940 PullRequestComment * GitHubPullRequestComment `json:"pullRequestComment,omitempty"`
4041 RepoURLPath string `json:"repoURLPath,omitempty"`
4142 RevisionPath string `json:"revisionPath,omitempty"`
43+ CheckRun * GitHubCheckRun `json:"checkRun,omitempty"`
4244}
4345
4446type GitHubStatus struct {
@@ -47,6 +49,23 @@ type GitHubStatus struct {
4749 TargetURL string `json:"targetURL,omitempty"`
4850}
4951
52+ type GitHubCheckRun struct {
53+ // head_sha - this will be the revision path
54+ // external_id - this should have the details of argocd server
55+ Name string `json:"name,omitempty"`
56+ DetailsURL string `json:"details_url,omitempty"`
57+ Status string `json:"status,omitempty"`
58+ Conclusion string `json:"conclusion,omitempty"`
59+ StartedAt string `json:"started_at,omitempty"`
60+ CompletedAt string `json:"completed_at,omitempty"`
61+ Output * GitHubCheckRunOutput `json:"output,omitempty"`
62+ }
63+ type GitHubCheckRunOutput struct {
64+ Title string `json:"title,omitempty"`
65+ Summary string `json:"summary,omitempty"`
66+ Text string `json:"text,omitempty"`
67+ }
68+
5069type GitHubDeployment struct {
5170 State string `json:"state,omitempty"`
5271 Environment string `json:"environment,omitempty"`
@@ -139,6 +158,49 @@ func (g *GitHubNotification) GetTemplater(name string, f texttemplate.FuncMap) (
139158 }
140159 }
141160
161+ var checkRunName , detailsURL , status , conclusion , startedAt , completedAt * texttemplate.Template
162+ if g .CheckRun != nil {
163+ checkRunName , err = texttemplate .New (name ).Funcs (f ).Parse (g .CheckRun .Name )
164+ if err != nil {
165+ return nil , err
166+ }
167+ detailsURL , err = texttemplate .New (name ).Funcs (f ).Parse (g .CheckRun .DetailsURL )
168+ if err != nil {
169+ return nil , err
170+ }
171+ status , err = texttemplate .New (name ).Funcs (f ).Parse (g .CheckRun .Status )
172+ if err != nil {
173+ return nil , err
174+ }
175+ conclusion , err = texttemplate .New (name ).Funcs (f ).Parse (g .CheckRun .Conclusion )
176+ if err != nil {
177+ return nil , err
178+ }
179+ startedAt , err = texttemplate .New (name ).Funcs (f ).Parse (g .CheckRun .StartedAt )
180+ if err != nil {
181+ return nil , err
182+ }
183+ completedAt , err = texttemplate .New (name ).Funcs (f ).Parse (g .CheckRun .CompletedAt )
184+ if err != nil {
185+ return nil , err
186+ }
187+ }
188+ var checkRunTitle , summary , text * texttemplate.Template
189+ if g .CheckRun != nil && g .CheckRun .Output != nil {
190+ checkRunTitle , err = texttemplate .New (name ).Funcs (f ).Parse (g .CheckRun .Output .Title )
191+ if err != nil {
192+ return nil , err
193+ }
194+ summary , err = texttemplate .New (name ).Funcs (f ).Parse (g .CheckRun .Output .Summary )
195+ if err != nil {
196+ return nil , err
197+ }
198+ text , err = texttemplate .New (name ).Funcs (f ).Parse (g .CheckRun .Output .Text )
199+ if err != nil {
200+ return nil , err
201+ }
202+ }
203+
142204 return func (notification * Notification , vars map [string ]interface {}) error {
143205 if notification .GitHub == nil {
144206 notification .GitHub = & GitHubNotification {
@@ -246,6 +308,63 @@ func (g *GitHubNotification) GetTemplater(name string, f texttemplate.FuncMap) (
246308 notification .GitHub .PullRequestComment .Content = contentData .String ()
247309 }
248310
311+ if g .CheckRun != nil {
312+ if notification .GitHub .CheckRun == nil {
313+ notification .GitHub .CheckRun = & GitHubCheckRun {}
314+ }
315+ var checkRunNameData bytes.Buffer
316+ if err := checkRunName .Execute (& checkRunNameData , vars ); err != nil {
317+ return err
318+ }
319+ notification .GitHub .CheckRun .Name = checkRunNameData .String ()
320+ var detailsURLData bytes.Buffer
321+ if err := detailsURL .Execute (& detailsURLData , vars ); err != nil {
322+ return err
323+ }
324+ notification .GitHub .CheckRun .DetailsURL = detailsURLData .String ()
325+
326+ var statusData bytes.Buffer
327+ if err := status .Execute (& statusData , vars ); err != nil {
328+ return err
329+ }
330+ notification .GitHub .CheckRun .Status = statusData .String ()
331+ var conclusionData bytes.Buffer
332+ if err := conclusion .Execute (& conclusionData , vars ); err != nil {
333+ return err
334+ }
335+ notification .GitHub .CheckRun .Conclusion = conclusionData .String ()
336+ var startedAtData bytes.Buffer
337+ if err := startedAt .Execute (& startedAtData , vars ); err != nil {
338+ return err
339+ }
340+ notification .GitHub .CheckRun .StartedAt = startedAtData .String ()
341+ var completedAtData bytes.Buffer
342+ if err := completedAt .Execute (& completedAtData , vars ); err != nil {
343+ return err
344+ }
345+ notification .GitHub .CheckRun .CompletedAt = completedAtData .String ()
346+ }
347+ if g .CheckRun != nil && g .CheckRun .Output != nil {
348+ if notification .GitHub .CheckRun .Output == nil {
349+ notification .GitHub .CheckRun .Output = & GitHubCheckRunOutput {}
350+ }
351+ var checkRunTitleData bytes.Buffer
352+ if err := checkRunTitle .Execute (& checkRunTitleData , vars ); err != nil {
353+ return err
354+ }
355+ notification .GitHub .CheckRun .Output .Title = checkRunTitleData .String ()
356+ var summaryData bytes.Buffer
357+ if err := summary .Execute (& summaryData , vars ); err != nil {
358+ return err
359+ }
360+ notification .GitHub .CheckRun .Output .Summary = summaryData .String ()
361+ var textData bytes.Buffer
362+ if err := text .Execute (& textData , vars ); err != nil {
363+ return err
364+ }
365+ notification .GitHub .CheckRun .Output .Text = textData .String ()
366+ }
367+
249368 return nil
250369 }, nil
251370}
@@ -438,5 +557,44 @@ func (g gitHubService) Send(notification Notification, _ Destination) error {
438557 }
439558 }
440559
560+ if notification .GitHub .CheckRun != nil {
561+ startedTime , err := time .Parse ("YYYY-MM-DDTHH:MM:SSZ" , notification .GitHub .CheckRun .StartedAt )
562+ if err != nil {
563+ return err
564+ }
565+ completedTime , err := time .Parse ("YYYY-MM-DDTHH:MM:SSZ" , notification .GitHub .CheckRun .CompletedAt )
566+ if err != nil {
567+ return err
568+ }
569+ externalID := "argocd-notifications"
570+ checkRunOutput := & github.CheckRunOutput {}
571+ if notification .GitHub .CheckRun .Output != nil {
572+ checkRunOutput = & github.CheckRunOutput {
573+ Title : & notification .GitHub .CheckRun .Output .Title ,
574+ Text : & notification .GitHub .CheckRun .Output .Text ,
575+ Summary : & notification .GitHub .CheckRun .Output .Summary ,
576+ }
577+ }
578+
579+ _ , _ , err = g .client .Checks .CreateCheckRun (
580+ context .Background (),
581+ u [0 ],
582+ u [1 ],
583+ github.CreateCheckRunOptions {
584+ HeadSHA : notification .GitHub .revision ,
585+ ExternalID : & externalID ,
586+ Name : notification .GitHub .CheckRun .Name ,
587+ DetailsURL : & notification .GitHub .CheckRun .DetailsURL ,
588+ StartedAt : & github.Timestamp {Time : startedTime },
589+ CompletedAt : & github.Timestamp {Time : completedTime },
590+ Output : checkRunOutput ,
591+ },
592+ )
593+
594+ if err != nil {
595+ return err
596+ }
597+ }
598+
441599 return nil
442600}
0 commit comments