Conditions | 10 |
Total Lines | 77 |
Code Lines | 44 |
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:
Complex classes like github.FillGitHubSecurityAlerts often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /* Vuls - Vulnerability Scanner |
||
36 | func FillGitHubSecurityAlerts(r *models.ScanResult, owner, repo, token string) (nCVEs int, err error) { |
||
37 | src := oauth2.StaticTokenSource( |
||
38 | &oauth2.Token{AccessToken: token}, |
||
39 | ) |
||
40 | httpClient := oauth2.NewClient(context.Background(), src) |
||
41 | const jsonfmt = `{"query": |
||
42 | "query { repository(owner:\"%s\", name:\"%s\") { name, vulnerabilityAlerts(first: %d, %s) { pageInfo{ endCursor, hasNextPage, startCursor}, edges { node { id, externalIdentifier, externalReference, fixedIn, packageName } } } } }"}` |
||
43 | |||
44 | after := "" |
||
45 | for { |
||
46 | jsonStr := fmt.Sprintf(jsonfmt, owner, repo, 100, after) |
||
47 | req, err := http.NewRequest("POST", |
||
48 | "https://api.github.com/graphql", |
||
49 | bytes.NewBuffer([]byte(jsonStr)), |
||
50 | ) |
||
51 | if err != nil { |
||
52 | return 0, err |
||
53 | } |
||
54 | req.Header.Set("Content-Type", "application/json") |
||
55 | |||
56 | // https://developer.github.com/v4/previews/#repository-vulnerability-alerts |
||
57 | // To toggle this preview and access data, need to provide a custom media type in the Accept header: |
||
58 | // TODO remove this header if it is no longer preview status in the future. |
||
59 | req.Header.Set("Accept", "application/vnd.github.vixen-preview+json") |
||
60 | |||
61 | resp, err := httpClient.Do(req) |
||
62 | if err != nil { |
||
63 | return 0, err |
||
64 | } |
||
65 | defer resp.Body.Close() |
||
66 | bodyBytes, err := ioutil.ReadAll(resp.Body) |
||
67 | if err != nil { |
||
68 | return 0, err |
||
69 | } |
||
70 | |||
71 | alerts := SecurityAlerts{} |
||
72 | if err = json.Unmarshal(bodyBytes, &alerts); err != nil { |
||
73 | return 0, err |
||
74 | } |
||
75 | |||
76 | // TODO remove |
||
77 | util.Log.Debugf("%s", pp.Sprintf("%s", alerts)) |
||
|
|||
78 | |||
79 | // TODO add type field to models.Pakcage. |
||
80 | // OS Packages ... osPkg |
||
81 | // CPE ... CPE |
||
82 | // GitHub ... GitHub |
||
83 | // WordPress theme ... wpTheme |
||
84 | // WordPress plugin ... wpPlugin |
||
85 | // WordPress core ... wpCore |
||
86 | for _, v := range alerts.Data.Repository.VulnerabilityAlerts.Edges { |
||
87 | cveID := v.Node.ExternalIdentifier |
||
88 | affectedPkg := models.PackageStatus{Name: v.Node.PackageName} |
||
89 | if val, ok := r.ScannedCves[cveID]; ok { |
||
90 | val.AffectedPackages = append(val.AffectedPackages, affectedPkg) |
||
91 | r.ScannedCves[cveID] = val |
||
92 | // TODO add package information to r.Packages |
||
93 | // TODO get current version via github API if possible |
||
94 | nCVEs++ |
||
95 | } else { |
||
96 | v := models.VulnInfo{ |
||
97 | CveID: cveID, |
||
98 | Confidences: models.Confidences{models.GitHubMatch}, |
||
99 | AffectedPackages: models.PackageStatuses{affectedPkg}, |
||
100 | } |
||
101 | r.ScannedCves[cveID] = v |
||
102 | // TODO add package information to r.Packages |
||
103 | // TODO get current version via github API if possible |
||
104 | nCVEs++ |
||
105 | } |
||
106 | } |
||
107 | if !alerts.Data.Repository.VulnerabilityAlerts.PageInfo.HasNextPage { |
||
108 | break |
||
109 | } |
||
110 | after = fmt.Sprintf(`after: \"%s\"`, alerts.Data.Repository.VulnerabilityAlerts.PageInfo.EndCursor) |
||
111 | } |
||
112 | return nCVEs, err |
||
113 | } |
||
138 |