Passed
Pull Request — master (#775)
by kota
11:09
created

github.FillGitHubSecurityAlerts   B

Complexity

Conditions 7

Size

Total Lines 53
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 30
dl 0
loc 53
rs 7.76
c 0
b 0
f 0
nop 4

How to fix   Long Method   

Long Method

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:

1
/* Vuls - Vulnerability Scanner
2
Copyright (C) 2016  Future Corporation , Japan.
3
4
This program is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
8
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
GNU General Public License for more details.
13
14
You should have received a copy of the GNU General Public License
15
along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
*/
17
18
package github
19
20
import (
21
	"bytes"
22
	"context"
23
	"encoding/json"
24
	"fmt"
25
	"io/ioutil"
26
	"net/http"
27
28
	"github.com/future-architect/vuls/models"
29
	"github.com/k0kubun/pp"
30
	"golang.org/x/oauth2"
31
)
32
33
// FillGitHubSecurityAlerts access to owner/repo on GitHub and fetch scurity alerts of the repository via GitHub API v4 GraphQL and then set to the given ScanResult.
34
// https://help.github.com/articles/about-security-alerts-for-vulnerable-dependencies/
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
}
89
90
//SecurityAlerts has detected CVE-IDs, PackageNames, Refs
91
type SecurityAlerts struct {
92
	Data struct {
93
		Repository struct {
94
			VulnerabilityAlerts struct {
95
				PageInfo struct {
96
					EndCursor   string `json:"endCursor"`
97
					HasNextPage bool   `json:"hasNextPage"`
98
					StartCursor string `json:"startCursor"`
99
				} `json:"pageInfo"`
100
				Edges []struct {
101
					Node struct {
102
						ID                 string `json:"id"`
103
						ExternalIdentifier string `json:"externalIdentifier"`
104
						ExternalReference  string `json:"externalReference"`
105
						FixedIn            string `json:"fixedIn"`
106
						PackageName        string `json:"packageName"`
107
					} `json:"node"`
108
				} `json:"edges"`
109
			} `json:"vulnerabilityAlerts"`
110
		} `json:"repository"`
111
	} `json:"data"`
112
}
113