MCPcopy
hub / github.com/ossf/scorecard / GetOSesForJob

Function GetOSesForJob

checks/fileparser/github_workflow.go:173–222  ·  view source on GitHub ↗

GetOSesForJob returns the OSes this job runs on.

(job *actionlint.Job)

Source from the content-addressed store, hash-verified

171
172// GetOSesForJob returns the OSes this job runs on.
173func GetOSesForJob(job *actionlint.Job) ([]string, error) {
174 // The 'runs-on' field either lists the OS'es directly, or it can have an expression '${{ matrix.os }}' which
175 // is where the OS'es are actually listed.
176 jobOSes := make([]string, 0)
177 jobRunsOnLabels := getJobRunsOnLabels(job)
178 getFromMatrix := len(jobRunsOnLabels) == 1 && strings.Contains(jobRunsOnLabels[0].Value, matrixos)
179 if !getFromMatrix {
180 // We can get the OSes straight from 'runs-on'.
181 for _, os := range jobRunsOnLabels {
182 jobOSes = append(jobOSes, os.Value)
183 }
184 return jobOSes, nil
185 }
186
187 jobStrategyMatrixRows := getJobStrategyMatrixRows(job)
188 for rowKey, rowValue := range jobStrategyMatrixRows {
189 if rowKey != os {
190 continue
191 }
192 for _, os := range rowValue.Values {
193 jobOSes = append(jobOSes, strings.Trim(os.String(), "'\""))
194 }
195 }
196
197 matrixCombinations := getJobStrategyMatrixIncludeCombinations(job)
198 for _, combination := range matrixCombinations {
199 if combination.Assigns == nil {
200 continue
201 }
202 for _, assign := range combination.Assigns {
203 if assign.Key == nil || assign.Key.Value != os || assign.Value == nil {
204 continue
205 }
206 jobOSes = append(jobOSes, strings.Trim(assign.Value.String(), "'\""))
207 }
208 }
209
210 if len(jobOSes) == 0 {
211 // This error is caught by the caller, which is responsible for adding more
212 // precise location information
213 jobName := GetJobName(job)
214 return jobOSes, &checker.ElementError{
215 Location: finding.Location{
216 Snippet: &jobName,
217 },
218 Err: sce.ErrJobOSParsing,
219 }
220 }
221 return jobOSes, nil
222}
223
224// JobAlwaysRunsOnWindows returns true if the only OS that this job runs on is Windows.
225func JobAlwaysRunsOnWindows(job *actionlint.Job) (bool, error) {

Callers 1

JobAlwaysRunsOnWindowsFunction · 0.85

Calls 5

getJobRunsOnLabelsFunction · 0.85
getJobStrategyMatrixRowsFunction · 0.85
GetJobNameFunction · 0.85
StringMethod · 0.65

Tested by

no test coverage detected