| Conditions | 7 |
| Total Lines | 53 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* Vuls - Vulnerability Scanner |
||
| 35 | func FillGitHubSecurityAlerts(r *models.ScanResult, owner, repo, token string) (nCVEs int, err error) { |
||
| 36 | src := oauth2.StaticTokenSource( |
||
| 37 | &oauth2.Token{AccessToken: token}, |
||
| 38 | ) |
||
| 39 | httpClient := oauth2.NewClient(context.Background(), src) |
||
| 40 | const jsonfmt = `{"query": |
||
| 41 | "query { repository(owner:\"%s\", name:\"%s\") { name, vulnerabilityAlerts(first: %d, %s) { pageInfo{ endCursor, hasNextPage, startCursor}, edges { node { id, externalIdentifier, externalReference, fixedIn, packageName } } } } }"}` |
||
| 42 | |||
| 43 | after := "" |
||
| 44 | for { |
||
| 45 | jsonStr := fmt.Sprintf(jsonfmt, owner, repo, 100, after) |
||
| 46 | req, err := http.NewRequest("POST", |
||
| 47 | "https://api.github.com/graphql", |
||
| 48 | bytes.NewBuffer([]byte(jsonStr)), |
||
| 49 | ) |
||
| 50 | if err != nil { |
||
| 51 | return 0, err |
||
| 52 | } |
||
| 53 | req.Header.Set("Content-Type", "application/json") |
||
| 54 | |||
| 55 | // https://developer.github.com/v4/previews/#repository-vulnerability-alerts |
||
| 56 | // To toggle this preview and access data, need to provide a custom media type in the Accept header: |
||
| 57 | // TODO remove this header if it is no longer preview status in the future. |
||
| 58 | req.Header.Set("Accept", "application/vnd.github.vixen-preview+json") |
||
| 59 | |||
| 60 | resp, err := httpClient.Do(req) |
||
| 61 | if err != nil { |
||
| 62 | return 0, err |
||
| 63 | } |
||
| 64 | defer resp.Body.Close() |
||
| 65 | bodyBytes, err := ioutil.ReadAll(resp.Body) |
||
| 66 | if err != nil { |
||
| 67 | return 0, err |
||
| 68 | } |
||
| 69 | |||
| 70 | alerts := SecurityAlerts{} |
||
| 71 | if err = json.Unmarshal(bodyBytes, &alerts); err != nil { |
||
| 72 | return 0, err |
||
| 73 | } |
||
| 74 | // TODO add type field to models.Pakcage. |
||
| 75 | // OS Packages ... osPkg |
||
| 76 | // CPE ... CPE |
||
| 77 | // GitHub ... GitHub |
||
| 78 | // WordPress theme ... wpTheme |
||
| 79 | // WordPress plugin ... wpPlugin |
||
| 80 | // WordPress core ... wpCore |
||
| 81 | pp.Println(alerts) |
||
| 82 | if !alerts.Data.Repository.VulnerabilityAlerts.PageInfo.HasNextPage { |
||
| 83 | break |
||
| 84 | } |
||
| 85 | after = fmt.Sprintf(`after: \"%s\"`, alerts.Data.Repository.VulnerabilityAlerts.PageInfo.EndCursor) |
||
| 86 | } |
||
| 87 | return 0, err |
||
| 88 | } |
||
| 113 |