GetOSesForJob returns the OSes this job runs on.
(job *actionlint.Job)
| 171 | |
| 172 | // GetOSesForJob returns the OSes this job runs on. |
| 173 | func 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. |
| 225 | func JobAlwaysRunsOnWindows(job *actionlint.Job) (bool, error) { |
no test coverage detected