@@ -0,0 +1,123 @@ |
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | + |
| 3 | +package repo |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/xml" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | + "time" |
| 13 | + |
| 14 | + actionsdb "github.com/tenseleyFlow/shithub/internal/actions/sqlc" |
| 15 | + "github.com/tenseleyFlow/shithub/internal/auth/policy" |
| 16 | +) |
| 17 | + |
| 18 | +const actionsAtomRunLimit = int32(50) |
| 19 | + |
| 20 | +func (h *Handlers) repoActionsAtom(w http.ResponseWriter, r *http.Request) { |
| 21 | + row, owner, ok := h.loadRepoAndAuthorize(w, r, policy.ActionRepoRead) |
| 22 | + if !ok { |
| 23 | + return |
| 24 | + } |
| 25 | + runs, err := actionsdb.New().ListWorkflowRunsForRepo(r.Context(), h.d.Pool, actionsdb.ListWorkflowRunsForRepoParams{ |
| 26 | + RepoID: row.ID, |
| 27 | + PageLimit: actionsAtomRunLimit, |
| 28 | + PageOffset: 0, |
| 29 | + }) |
| 30 | + if err != nil { |
| 31 | + h.d.Logger.WarnContext(r.Context(), "repo actions atom: list workflow runs", "repo_id", row.ID, "error", err) |
| 32 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 33 | + return |
| 34 | + } |
| 35 | + w.Header().Set("Content-Type", "application/atom+xml; charset=utf-8") |
| 36 | + writeActionsAtom(w, owner.Username, row.Name, runs, time.Now()) |
| 37 | +} |
| 38 | + |
| 39 | +func writeActionsAtom(w io.Writer, owner, repoName string, runs []actionsdb.ListWorkflowRunsForRepoRow, now time.Time) { |
| 40 | + type atomAuthor struct { |
| 41 | + Name string `xml:"name"` |
| 42 | + } |
| 43 | + type atomEntry struct { |
| 44 | + ID string `xml:"id"` |
| 45 | + Title string `xml:"title"` |
| 46 | + Updated string `xml:"updated"` |
| 47 | + Author atomAuthor `xml:"author"` |
| 48 | + Summary string `xml:"summary"` |
| 49 | + Link struct { |
| 50 | + Href string `xml:"href,attr"` |
| 51 | + Rel string `xml:"rel,attr,omitempty"` |
| 52 | + } `xml:"link"` |
| 53 | + } |
| 54 | + type atomFeed struct { |
| 55 | + XMLName xml.Name `xml:"http://www.w3.org/2005/Atom feed"` |
| 56 | + Title string `xml:"title"` |
| 57 | + ID string `xml:"id"` |
| 58 | + Updated string `xml:"updated"` |
| 59 | + Link struct { |
| 60 | + Href string `xml:"href,attr"` |
| 61 | + Rel string `xml:"rel,attr,omitempty"` |
| 62 | + } `xml:"link"` |
| 63 | + Entries []atomEntry `xml:"entry"` |
| 64 | + } |
| 65 | + |
| 66 | + feedUpdated := now.UTC() |
| 67 | + if len(runs) > 0 { |
| 68 | + feedUpdated = pgTime(runs[0].UpdatedAt, runs[0].CreatedAt.Time).UTC() |
| 69 | + } |
| 70 | + feed := atomFeed{ |
| 71 | + Title: fmt.Sprintf("%s/%s Actions runs", owner, repoName), |
| 72 | + ID: fmt.Sprintf("urn:shithub:actions:%s:%s", owner, repoName), |
| 73 | + Updated: feedUpdated.Format(time.RFC3339), |
| 74 | + } |
| 75 | + feed.Link.Href = fmt.Sprintf("/%s/%s/actions.atom", owner, repoName) |
| 76 | + feed.Link.Rel = "self" |
| 77 | + |
| 78 | + for _, run := range runs { |
| 79 | + var e atomEntry |
| 80 | + e.ID = fmt.Sprintf("urn:shithub:workflow_run:%d", run.ID) |
| 81 | + e.Title = actionsAtomRunTitle(run) |
| 82 | + e.Updated = pgTime(run.UpdatedAt, run.CreatedAt.Time).UTC().Format(time.RFC3339) |
| 83 | + e.Author.Name = actionsAtomActor(run.ActorUsername) |
| 84 | + e.Summary = actionsAtomRunSummary(run) |
| 85 | + e.Link.Href = fmt.Sprintf("/%s/%s/actions/runs/%d", owner, repoName, run.RunIndex) |
| 86 | + feed.Entries = append(feed.Entries, e) |
| 87 | + } |
| 88 | + |
| 89 | + enc := xml.NewEncoder(w) |
| 90 | + enc.Indent("", " ") |
| 91 | + _, _ = io.WriteString(w, xml.Header) |
| 92 | + _ = enc.Encode(feed) |
| 93 | + _ = enc.Flush() |
| 94 | +} |
| 95 | + |
| 96 | +func actionsAtomRunTitle(run actionsdb.ListWorkflowRunsForRepoRow) string { |
| 97 | + title := workflowDisplayName(run.WorkflowName, run.WorkflowFile) |
| 98 | + state, _, _ := workflowRunState(run.Status, run.Conclusion) |
| 99 | + return fmt.Sprintf("%s #%d %s", title, run.RunIndex, strings.ToLower(state)) |
| 100 | +} |
| 101 | + |
| 102 | +func actionsAtomRunSummary(run actionsdb.ListWorkflowRunsForRepoRow) string { |
| 103 | + parts := []string{ |
| 104 | + "Workflow: " + workflowDisplayName(run.WorkflowName, run.WorkflowFile), |
| 105 | + "Event: " + workflowRunEventLabel(string(run.Event)), |
| 106 | + "Status: " + string(run.Status), |
| 107 | + "Branch: " + run.HeadRef, |
| 108 | + "Commit: " + shortSHA(run.HeadSha), |
| 109 | + } |
| 110 | + if run.Conclusion.Valid { |
| 111 | + parts = append(parts, "Conclusion: "+string(run.Conclusion.CheckConclusion)) |
| 112 | + } |
| 113 | + parts = append(parts, "Run: #"+strconv.FormatInt(run.RunIndex, 10)) |
| 114 | + return strings.Join(parts, "\n") |
| 115 | +} |
| 116 | + |
| 117 | +func actionsAtomActor(username string) string { |
| 118 | + username = strings.TrimSpace(username) |
| 119 | + if username == "" { |
| 120 | + return "shithub" |
| 121 | + } |
| 122 | + return username |
| 123 | +} |