v0.8.1 b8e893f

ci_convert.go RawHistoryFolder

1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package rpc
4
5import (
6	"fmt"
7
8	pb "bnema.dev/ermite-api/gen/ermite"
9	"bnema.dev/ermite/internal/domain"
10	"bnema.dev/ermite/internal/ports"
11)
12
13func statusToPB(s domain.CIStatus) pb.CiStatus {
14	switch s {
15	case domain.CIQueued:
16		return pb.CiStatus_CI_STATUS_QUEUED
17	case domain.CIInProgress:
18		return pb.CiStatus_CI_STATUS_IN_PROGRESS
19	case domain.CICompleted:
20		return pb.CiStatus_CI_STATUS_COMPLETED
21	}
22	return pb.CiStatus_CI_STATUS_UNSPECIFIED
23}
24
25func statusFromPB(s pb.CiStatus) (domain.CIStatus, error) {
26	switch s {
27	case pb.CiStatus_CI_STATUS_UNSPECIFIED:
28		return "", nil
29	case pb.CiStatus_CI_STATUS_QUEUED:
30		return domain.CIQueued, nil
31	case pb.CiStatus_CI_STATUS_IN_PROGRESS:
32		return domain.CIInProgress, nil
33	case pb.CiStatus_CI_STATUS_COMPLETED:
34		return domain.CICompleted, nil
35	}
36	return "", fmt.Errorf("%w: status %v", domain.ErrInvalid, s)
37}
38
39var conclusionPairs = []struct {
40	d domain.CIConclusion
41	p pb.CiConclusion
42}{
43	{domain.CISuccess, pb.CiConclusion_CI_CONCLUSION_SUCCESS},
44	{domain.CIFailure, pb.CiConclusion_CI_CONCLUSION_FAILURE},
45	{domain.CICancelled, pb.CiConclusion_CI_CONCLUSION_CANCELLED},
46	{domain.CISkipped, pb.CiConclusion_CI_CONCLUSION_SKIPPED},
47	{domain.CITimedOut, pb.CiConclusion_CI_CONCLUSION_TIMED_OUT},
48	{domain.CINeutral, pb.CiConclusion_CI_CONCLUSION_NEUTRAL},
49	{domain.CIActionRequired, pb.CiConclusion_CI_CONCLUSION_ACTION_REQUIRED},
50}
51
52func conclusionToPB(c domain.CIConclusion) pb.CiConclusion {
53	for _, e := range conclusionPairs {
54		if e.d == c {
55			return e.p
56		}
57	}
58	return pb.CiConclusion_CI_CONCLUSION_UNSPECIFIED
59}
60
61func conclusionFromPB(c pb.CiConclusion) (domain.CIConclusion, error) {
62	if c == pb.CiConclusion_CI_CONCLUSION_UNSPECIFIED {
63		return domain.CINoConclusion, nil
64	}
65	for _, e := range conclusionPairs {
66		if e.p == c {
67			return e.d, nil
68		}
69	}
70	return "", fmt.Errorf("%w: conclusion %v", domain.ErrInvalid, c)
71}
72
73func isolationToPB(i domain.CIIsolation) pb.RunnerIsolation {
74	switch i {
75	case domain.CIIsolationVM:
76		return pb.RunnerIsolation_RUNNER_ISOLATION_VM
77	case domain.CIIsolationContainerSysbox:
78		return pb.RunnerIsolation_RUNNER_ISOLATION_CONTAINER_SYSBOX
79	case domain.CIIsolationContainerPrivileged:
80		return pb.RunnerIsolation_RUNNER_ISOLATION_CONTAINER_PRIVILEGED
81	}
82	return pb.RunnerIsolation_RUNNER_ISOLATION_UNSPECIFIED
83}
84
85func isolationFromPB(i pb.RunnerIsolation) (domain.CIIsolation, error) {
86	switch i {
87	case pb.RunnerIsolation_RUNNER_ISOLATION_VM:
88		return domain.CIIsolationVM, nil
89	case pb.RunnerIsolation_RUNNER_ISOLATION_CONTAINER_SYSBOX:
90		return domain.CIIsolationContainerSysbox, nil
91	case pb.RunnerIsolation_RUNNER_ISOLATION_CONTAINER_PRIVILEGED:
92		return domain.CIIsolationContainerPrivileged, nil
93	}
94	return "", fmt.Errorf("%w: isolation is required", domain.ErrInvalid)
95}
96
97func stepStateToPB(s domain.CIStepState) pb.StepState {
98	switch s {
99	case domain.CIStepPending:
100		return pb.StepState_STEP_STATE_PENDING
101	case domain.CIStepInProgress:
102		return pb.StepState_STEP_STATE_IN_PROGRESS
103	case domain.CIStepCompleted:
104		return pb.StepState_STEP_STATE_COMPLETED
105	}
106	return pb.StepState_STEP_STATE_UNSPECIFIED
107}
108
109func stepStateFromPB(s pb.StepState) domain.CIStepState {
110	switch s {
111	case pb.StepState_STEP_STATE_PENDING:
112		return domain.CIStepPending
113	case pb.StepState_STEP_STATE_IN_PROGRESS:
114		return domain.CIStepInProgress
115	case pb.StepState_STEP_STATE_COMPLETED:
116		return domain.CIStepCompleted
117	}
118	return ""
119}
120
121func cancelReasonToPB(r domain.CICancelReason) pb.CancelReason {
122	switch r {
123	case domain.CICancelUser:
124		return pb.CancelReason_CANCEL_REASON_USER
125	case domain.CICancelFailFast:
126		return pb.CancelReason_CANCEL_REASON_FAIL_FAST
127	case domain.CICancelTimeout:
128		return pb.CancelReason_CANCEL_REASON_TIMEOUT
129	}
130	return pb.CancelReason_CANCEL_REASON_UNSPECIFIED
131}
132
133// runName is Run.name: the name of the run, the workflow name for a run that
134// has none of its own.
135func runName(r domain.CIRun) string {
136	if r.Name == "" {
137		return r.WorkflowName
138	}
139	return r.Name
140}
141
142func runToPB(r domain.CIRun) *pb.Run {
143	return &pb.Run{
144		Repo: r.Repo.String(), Id: r.ID, Number: r.Number, Attempt: r.Attempt,
145		WorkflowPath: r.WorkflowPath, WorkflowName: r.WorkflowName, Name: runName(r), Title: r.Title, Event: r.Event,
146		Ref: r.Ref, Sha: r.Sha, Actor: r.Actor, Status: statusToPB(r.Status), Conclusion: conclusionToPB(r.Conclusion),
147		Error: r.Error, CreatedAt: timestamp(r.CreatedAt), StartedAt: timestamp(r.StartedAt), CompletedAt: timestamp(r.CompletedAt),
148		Version: int64(r.Version), //nolint:gosec // a store sequence, far below 2^63
149	}
150}
151
152func stepsToPB(steps []domain.CIStep, lines int64) []*pb.Step {
153	ranges := domain.StepRanges(steps, lines)
154	out := make([]*pb.Step, 0, len(steps))
155	for i, s := range steps {
156		out = append(out, &pb.Step{
157			Number: s.Number, Name: s.Name, State: stepStateToPB(s.State), Conclusion: conclusionToPB(s.Conclusion),
158			StartedAt: timestamp(s.StartedAt), CompletedAt: timestamp(s.CompletedAt),
159			LogStart: ranges[i].Start, LogEnd: ranges[i].End,
160		})
161	}
162	return out
163}
164
165func jobToPB(j domain.CIJob, lines int64) *pb.RunJob {
166	return &pb.RunJob{
167		Id: j.ID, RunId: j.RunID, Attempt: j.Attempt, Repo: j.Repo.String(), Key: j.Key, Name: j.Name,
168		Status: statusToPB(j.Status()), Conclusion: conclusionToPB(j.Conclusion), Error: j.Error,
169		Labels: j.Labels, Needs: j.Needs, RunnerId: j.RunnerID, RunnerName: j.RunnerName, Isolation: isolationToPB(j.Isolation),
170		Steps: stepsToPB(j.Steps, lines), CreatedAt: timestamp(j.CreatedAt), StartedAt: timestamp(j.StartedAt),
171		CompletedAt: timestamp(j.CompletedAt),
172		Version:     int64(j.Version), //nolint:gosec // a store sequence, far below 2^63
173	}
174}
175
176func jobViewToPB(v ports.JobView) *pb.RunJob {
177	out := jobToPB(v.CIJob, v.Lines)
178	if v.RunnerGone {
179		out.RunnerId = ""
180	}
181	return out
182}
183
184func checkToPB(c domain.CICheck) *pb.Check {
185	return &pb.Check{
186		Id: c.ID, Repo: c.Repo.String(), Sha: c.Sha, Name: c.Name, Status: statusToPB(c.Status),
187		Conclusion: conclusionToPB(c.Conclusion), RunId: c.RunID, JobId: c.JobID, Attempt: c.Attempt,
188		WorkflowName: c.WorkflowName, StartedAt: timestamp(c.StartedAt), CompletedAt: timestamp(c.CompletedAt),
189	}
190}
191
192func runnerToPB(r ports.RunnerView) *pb.Runner {
193	return &pb.Runner{
194		Id: r.ID, Name: r.Name, Labels: r.Labels, Isolation: isolationToPB(r.Isolation), Version: r.Version,
195		Arch: r.Arch, Os: r.OS, RegisteredAt: timestamp(r.RegisteredAt), LastSeen: timestamp(r.LastSeen), Busy: r.Busy,
196	}
197}
198
199func logLineToPB(l ports.JobLogLine) *pb.JobLogLine {
200	return &pb.JobLogLine{Step: l.Step, Time: timestamp(l.Time), Text: l.Text, Continued: l.Continued}
201}
202
203func runnerJobToPB(j *ports.RunnerJob) *pb.Job {
204	return &pb.Job{
205		JobId: j.Job.ID, RunId: j.Job.RunID, Attempt: j.Job.RunAttempt, JobToken: j.Token,
206		WorkflowPath: j.WorkflowPath, WorkflowYaml: j.WorkflowYAML, JobKey: j.Job.Key, Matrix: j.Job.MatrixJSON,
207		Contexts: j.Job.ContextsJSON, Labels: j.Job.Labels, TimeoutMinutes: j.Job.TimeoutMinutes,
208		Repo: j.Repo.String(), Sha: j.Sha, Ref: j.Ref,
209	}
210}
211
212func stepUpdatesFromPB(steps []*pb.StepUpdate) ([]ports.StepUpdate, error) {
213	out := make([]ports.StepUpdate, 0, len(steps))
214	for _, s := range steps {
215		concl, err := conclusionFromPB(s.GetConclusion())
216		if err != nil {
217			return nil, err
218		}
219		out = append(out, ports.StepUpdate{
220			Number: s.GetNumber(), Name: s.GetName(), State: stepStateFromPB(s.GetState()), Conclusion: concl,
221			StartedAt: goTime(s.GetStartedAt()), CompletedAt: goTime(s.GetCompletedAt()),
222		})
223	}
224	return out, nil
225}
226
227func jobEndedToPB(e *domain.JobEndedError) *pb.JobEnded {
228	return &pb.JobEnded{Conclusion: conclusionToPB(e.Conclusion), Error: e.Message}
229}
230
231func runEventToPB(ev domain.RunUpdated) *pb.Event_RunUpdated {
232	return &pb.Event_RunUpdated{RunUpdated: &pb.RunUpdated{Run: runToPB(ev.Run)}}
233}
234
235func jobEventToPB(ev domain.JobUpdated) *pb.Event_JobUpdated {
236	return &pb.Event_JobUpdated{JobUpdated: &pb.JobUpdated{Job: jobToPB(ev.Job, ev.Lines)}}
237}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 3

Tags 44

↑↓ moveenter openesc close