MCPcopy Create free account
hub / github.com/cli/cli / populateLogSegments

Function populateLogSegments

pkg/cmd/run/view/logs.go:93–156  ·  view source on GitHub ↗

populateLogSegments populates log segments from the provided jobs and data available in the given ZIP archive map. Any missing logs will be assigned a log fetcher that retrieves logs from the API. For example, if there's no step log available in the ZIP archive, the entire job log will be selected

(httpClient *http.Client, repo ghrepo.Interface, jobs []shared.Job, zlm *zipLogMap, onlyFailed bool)

Source from the content-addressed store, hash-verified

91// fetchers to be assigned. This is to avoid overwhelming the API with too many
92// requests.
93func populateLogSegments(httpClient *http.Client, repo ghrepo.Interface, jobs []shared.Job, zlm *zipLogMap, onlyFailed bool) ([]logSegment, error) {
94 segments := make([]logSegment, 0, len(jobs))
95
96 apiLogFetcherCount := 0
97 for _, job := range jobs {
98 if shared.IsSkipped(job.Conclusion) {
99 continue
100 }
101
102 if onlyFailed && !shared.IsFailureState(job.Conclusion) {
103 continue
104 }
105
106 stepLogAvailable := slices.ContainsFunc(job.Steps, func(step shared.Step) bool {
107 _, ok := zlm.forStep(job.ID, step.Number)
108 return ok
109 })
110
111 // If at least one step log is available, we populate the segments with
112 // them and don't use the entire job log.
113 if stepLogAvailable {
114 steps := slices.Clone(job.Steps)
115 sort.Sort(steps)
116 for _, step := range steps {
117 if onlyFailed && !shared.IsFailureState(step.Conclusion) {
118 continue
119 }
120
121 zf, ok := zlm.forStep(job.ID, step.Number)
122 if !ok {
123 // We have no step log in the zip archive, but there's nothing we can do
124 // about that because there is no API endpoint to fetch step logs.
125 continue
126 }
127
128 segments = append(segments, logSegment{
129 job: &job,
130 step: &step,
131 fetcher: &zipLogFetcher{File: zf},
132 })
133 }
134 continue
135 }
136
137 segment := logSegment{job: &job}
138 if zf, ok := zlm.forJob(job.ID); ok {
139 segment.fetcher = &zipLogFetcher{File: zf}
140 } else {
141 segment.fetcher = &apiLogFetcher{
142 httpClient: httpClient,
143 repo: repo,
144 jobID: job.ID,
145 }
146 apiLogFetcherCount++
147 }
148 segments = append(segments, segment)
149
150 if apiLogFetcherCount > maxAPILogFetchers {

Callers 1

runViewFunction · 0.85

Calls 5

IsSkippedFunction · 0.92
IsFailureStateFunction · 0.92
forStepMethod · 0.80
forJobMethod · 0.80
CloneMethod · 0.65

Tested by

no test coverage detected