Go · 2310 bytes Raw Blame History
1 // SPDX-License-Identifier: AGPL-3.0-or-later
2
3 package repo
4
5 import (
6 "context"
7 "fmt"
8
9 checksdb "github.com/tenseleyFlow/shithub/internal/checks/sqlc"
10 )
11
12 type codeCommitCheckSummary struct {
13 Show bool
14 Label string
15 StateClass string
16 StateIcon string
17 Href string
18 }
19
20 func (h *Handlers) codeCommitCheckSummary(ctx context.Context, owner, repoName string, repoID int64, headSHA string) codeCommitCheckSummary {
21 runs, err := h.cq.ListCheckRunsForCommit(ctx, h.d.Pool, checksdb.ListCheckRunsForCommitParams{
22 RepoID: repoID,
23 HeadSha: headSHA,
24 })
25 if err != nil {
26 h.d.Logger.WarnContext(ctx, "code: ListCheckRunsForCommit", "repo_id", repoID, "head_sha", headSHA, "error", err)
27 return codeCommitCheckSummary{}
28 }
29 summary := summarizeCodeCommitChecks(runs)
30 if !summary.Show {
31 return summary
32 }
33 summary.Href = fmt.Sprintf("/%s/%s/actions", owner, repoName)
34 return summary
35 }
36
37 func summarizeCodeCommitChecks(runs []checksdb.CheckRun) codeCommitCheckSummary {
38 if len(runs) == 0 {
39 return codeCommitCheckSummary{}
40 }
41 total := len(runs)
42 pending := 0
43 failing := 0
44 for _, run := range runs {
45 if run.Status != checksdb.CheckStatusCompleted {
46 pending++
47 continue
48 }
49 if !run.Conclusion.Valid {
50 pending++
51 continue
52 }
53 switch run.Conclusion.CheckConclusion {
54 case checksdb.CheckConclusionSuccess, checksdb.CheckConclusionSkipped, checksdb.CheckConclusionNeutral:
55 default:
56 failing++
57 }
58 }
59 switch {
60 case failing > 0:
61 return codeCommitCheckSummary{
62 Show: true,
63 Label: checkSummaryLabel(failing, total, "failed"),
64 StateClass: "failure",
65 StateIcon: "x-circle-fill",
66 }
67 case pending > 0:
68 return codeCommitCheckSummary{
69 Show: true,
70 Label: checkSummaryLabel(pending, total, "pending"),
71 StateClass: "pending",
72 StateIcon: "dot-fill",
73 }
74 default:
75 return codeCommitCheckSummary{
76 Show: true,
77 Label: checkSummaryLabel(total, total, "successful"),
78 StateClass: "success",
79 StateIcon: "check-circle-fill",
80 }
81 }
82 }
83
84 func checkSummaryLabel(count, total int, state string) string {
85 checkWord := "checks"
86 if total == 1 {
87 checkWord = "check"
88 }
89 if count == total {
90 return fmt.Sprintf("%d %s %s", total, checkWord, state)
91 }
92 return fmt.Sprintf("%d of %d checks %s", count, total, state)
93 }
94