GetEnabled returns the list of enabled checks.
( sp *ScorecardPolicy, argsChecks []string, requiredRequestTypes []checker.RequestType, repoType clients.RepoType, )
| 142 | |
| 143 | // GetEnabled returns the list of enabled checks. |
| 144 | func GetEnabled( |
| 145 | sp *ScorecardPolicy, |
| 146 | argsChecks []string, |
| 147 | requiredRequestTypes []checker.RequestType, |
| 148 | repoType clients.RepoType, |
| 149 | ) (checker.CheckNameToFnMap, error) { |
| 150 | enabledChecks := checker.CheckNameToFnMap{} |
| 151 | |
| 152 | // Build a case-insensitive repo-type lookup map once, only when needed. |
| 153 | var repoTypeLookup map[string][]string |
| 154 | if repoType != "" { |
| 155 | if d, err := docs.Read(); err == nil { |
| 156 | repoTypeLookup = make(map[string][]string) |
| 157 | for _, c := range d.GetChecks() { |
| 158 | repoTypeLookup[strings.ToLower(c.GetName())] = c.GetSupportedRepoTypes() |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | switch { |
| 164 | case len(argsChecks) != 0: |
| 165 | // Populate checks to run with the `--repo` CLI argument. |
| 166 | for _, checkName := range argsChecks { |
| 167 | if !isSupportedCheck(checkName, requiredRequestTypes) { |
| 168 | return enabledChecks, |
| 169 | sce.WithMessage(sce.ErrScorecardInternal, |
| 170 | fmt.Sprintf("Unsupported RequestType %s by check: %s", |
| 171 | fmt.Sprint(requiredRequestTypes), checkName)) |
| 172 | } |
| 173 | if !isSupportedRepoType(repoTypeLookup, checkName, repoType) { |
| 174 | continue |
| 175 | } |
| 176 | if !enableCheck(checkName, &enabledChecks) { |
| 177 | return enabledChecks, |
| 178 | sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("invalid check: %s", checkName)) |
| 179 | } |
| 180 | } |
| 181 | case sp != nil: |
| 182 | // Populate checks to run with policy file. |
| 183 | for checkName := range sp.GetPolicies() { |
| 184 | if !isSupportedCheck(checkName, requiredRequestTypes) { |
| 185 | continue |
| 186 | } |
| 187 | if !isSupportedRepoType(repoTypeLookup, checkName, repoType) { |
| 188 | continue |
| 189 | } |
| 190 | |
| 191 | if !enableCheck(checkName, &enabledChecks) { |
| 192 | return enabledChecks, |
| 193 | sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("invalid check: %s", checkName)) |
| 194 | } |
| 195 | } |
| 196 | default: |
| 197 | // Enable all checks that are supported. |
| 198 | for checkName := range checks.GetAll() { |
| 199 | if !isSupportedCheck(checkName, requiredRequestTypes) { |
| 200 | continue |
| 201 | } |