-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
avoid unnecessary rescans by checking Frogbot's latest build commit status
- Loading branch information
1 parent
f5e9749
commit fb6a8c3
Showing
6 changed files
with
223 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,135 @@ func TestScanAndFixRepos(t *testing.T) { | |
assert.NoError(t, cmd.Run(configAggregator, client)) | ||
} | ||
|
||
func TestShouldScanBranchByStatus(t *testing.T) { | ||
commitStatusTestCases := []struct { | ||
statuses []vcsclient.CommitStatusInfo | ||
description string | ||
expected bool | ||
}{ | ||
{ | ||
statuses: []vcsclient.CommitStatusInfo{}, | ||
description: "Empty statuses", | ||
expected: true, | ||
}, | ||
{ | ||
statuses: []vcsclient.CommitStatusInfo{ | ||
{ | ||
State: vcsclient.Fail, | ||
Description: utils.CommitStatusDescription, | ||
DetailsUrl: utils.CommitStatusDetailsUrl, | ||
Creator: utils.FrogbotCreatorName, | ||
LastUpdatedAt: time.Now().UTC(), | ||
}, { | ||
State: vcsclient.InProgress, | ||
Description: utils.CommitStatusDescription, | ||
DetailsUrl: "", | ||
Creator: "im not frogbot", | ||
LastUpdatedAt: time.Now().UTC(), | ||
}, | ||
}, | ||
description: "Frogbot failed statues should scan", | ||
expected: true, | ||
}, | ||
{ | ||
statuses: []vcsclient.CommitStatusInfo{ | ||
{ | ||
State: vcsclient.Fail, | ||
Description: "description", | ||
DetailsUrl: "some other url", | ||
Creator: "im not frogbot", | ||
LastUpdatedAt: time.Now().UTC(), | ||
}, { | ||
State: vcsclient.InProgress, | ||
Description: "this is the latest commit", | ||
DetailsUrl: "some other url", | ||
Creator: "im not frogbot", | ||
LastUpdatedAt: time.Now().UTC(), | ||
}, | ||
{ | ||
State: vcsclient.Pass, | ||
Description: "this is the latest commit", | ||
DetailsUrl: "some other url", | ||
Creator: "im not frogbot", | ||
LastUpdatedAt: time.Now().UTC(), | ||
}, | ||
}, | ||
description: "Non Frogbot statues", | ||
expected: true, | ||
}, { | ||
statuses: []vcsclient.CommitStatusInfo{ | ||
{ | ||
State: vcsclient.Pass, | ||
Description: utils.CommitStatusDescription, | ||
DetailsUrl: utils.CommitStatusDetailsUrl, | ||
Creator: utils.FrogbotCreatorName, | ||
LastUpdatedAt: time.Now().AddDate(0, -1, 0), | ||
}, | ||
}, | ||
description: "Old statuse should scan", | ||
expected: true, | ||
}, | ||
} | ||
for _, tt := range commitStatusTestCases { | ||
t.Run(tt.description, func(t *testing.T) { | ||
shouldScan := shouldScanCommitByStatus(tt.statuses) | ||
assert.Equal(t, tt.expected, shouldScan) | ||
}) | ||
} | ||
} | ||
|
||
func TestStatusTimestampElapsed(t *testing.T) { | ||
testCases := []struct { | ||
commitStatusInfo vcsclient.CommitStatusInfo | ||
description string | ||
expected bool | ||
}{ | ||
{ | ||
commitStatusInfo: vcsclient.CommitStatusInfo{ | ||
State: 0, | ||
Description: "", | ||
DetailsUrl: "", | ||
Creator: "", | ||
CreatedAt: time.Now().UTC().AddDate(0, -3, 0), | ||
LastUpdatedAt: time.Now().UTC().AddDate(0, 0, -utils.SkipRepoScanDays-1), | ||
}, | ||
expected: true, | ||
description: "Last Update time is priority", | ||
}, | ||
{ | ||
commitStatusInfo: vcsclient.CommitStatusInfo{ | ||
State: 0, | ||
Description: "", | ||
DetailsUrl: "", | ||
Creator: "", | ||
CreatedAt: time.Now(), | ||
LastUpdatedAt: time.Now(), | ||
}, | ||
expected: false, | ||
description: "No scan needed ", | ||
}, | ||
{ | ||
commitStatusInfo: vcsclient.CommitStatusInfo{ | ||
State: 0, | ||
Description: "", | ||
DetailsUrl: "", | ||
Creator: "", | ||
CreatedAt: time.Now().UTC(), | ||
LastUpdatedAt: time.Time{}, | ||
}, | ||
expected: false, | ||
description: "Creation time fallback", | ||
}, | ||
} | ||
|
||
for _, tt := range testCases { | ||
t.Run(tt.description, func(t *testing.T) { | ||
needScan := statusTimestampElapsed(tt.commitStatusInfo) | ||
assert.Equal(t, tt.expected, needScan) | ||
}) | ||
} | ||
} | ||
|
||
func createReposGitEnvironment(t *testing.T, wd, port string, repositories ...string) { | ||
for _, repository := range repositories { | ||
fullWdPath := filepath.Join(wd, repository) | ||
|
@@ -127,7 +256,19 @@ func createHttpHandler(t *testing.T, port *string, projectNames ...string) http. | |
return | ||
} | ||
if r.RequestURI == fmt.Sprintf("/repos/jfrog/%s/commits?page=1&per_page=1&sha=master", projectName) { | ||
w.WriteHeader(http.StatusNotFound) | ||
w.WriteHeader(http.StatusOK) | ||
rawJson := "[\n {\n \"url\": \"https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e\",\n \"sha\": \"6dcb09b5b57875f334f61aebed695e2e4193db5e\",\n \"node_id\": \"MDY6Q29tbWl0NmRjYjA5YjViNTc4NzVmMzM0ZjYxYWViZWQ2OTVlMmU0MTkzZGI1ZQ==\",\n \"html_url\": \"https://github.com/octocat/Hello-World/commit/6dcb09b5b57875f334f61aebed695e2e4193db5e\",\n \"comments_url\": \"https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e/comments\",\n \"commit\": {\n \"url\": \"https://api.github.com/repos/octocat/Hello-World/git/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e\",\n \"author\": {\n \"name\": \"Monalisa Octocat\",\n \"email\": \"[email protected]\",\n \"date\": \"2011-04-14T16:00:49Z\"\n },\n \"committer\": {\n \"name\": \"Monalisa Octocat\",\n \"email\": \"[email protected]\",\n \"date\": \"2011-04-14T16:00:49Z\"\n },\n \"message\": \"Fix all the bugs\",\n \"tree\": {\n \"url\": \"https://api.github.com/repos/octocat/Hello-World/tree/6dcb09b5b57875f334f61aebed695e2e4193db5e\",\n \"sha\": \"6dcb09b5b57875f334f61aebed695e2e4193db5e\"\n },\n \"comment_count\": 0,\n \"verification\": {\n \"verified\": false,\n \"reason\": \"unsigned\",\n \"signature\": null,\n \"payload\": null\n }\n },\n \"author\": {\n \"login\": \"octocat\",\n \"id\": 1,\n \"node_id\": \"MDQ6VXNlcjE=\",\n \"avatar_url\": \"https://github.com/images/error/octocat_happy.gif\",\n \"gravatar_id\": \"\",\n \"url\": \"https://api.github.com/users/octocat\",\n \"html_url\": \"https://github.com/octocat\",\n \"followers_url\": \"https://api.github.com/users/octocat/followers\",\n \"following_url\": \"https://api.github.com/users/octocat/following{/other_user}\",\n \"gists_url\": \"https://api.github.com/users/octocat/gists{/gist_id}\",\n \"starred_url\": \"https://api.github.com/users/octocat/starred{/owner}{/repo}\",\n \"subscriptions_url\": \"https://api.github.com/users/octocat/subscriptions\",\n \"organizations_url\": \"https://api.github.com/users/octocat/orgs\",\n \"repos_url\": \"https://api.github.com/users/octocat/repos\",\n \"events_url\": \"https://api.github.com/users/octocat/events{/privacy}\",\n \"received_events_url\": \"https://api.github.com/users/octocat/received_events\",\n \"type\": \"User\",\n \"site_admin\": false\n },\n \"committer\": {\n \"login\": \"octocat\",\n \"id\": 1,\n \"node_id\": \"MDQ6VXNlcjE=\",\n \"avatar_url\": \"https://github.com/images/error/octocat_happy.gif\",\n \"gravatar_id\": \"\",\n \"url\": \"https://api.github.com/users/octocat\",\n \"html_url\": \"https://github.com/octocat\",\n \"followers_url\": \"https://api.github.com/users/octocat/followers\",\n \"following_url\": \"https://api.github.com/users/octocat/following{/other_user}\",\n \"gists_url\": \"https://api.github.com/users/octocat/gists{/gist_id}\",\n \"starred_url\": \"https://api.github.com/users/octocat/starred{/owner}{/repo}\",\n \"subscriptions_url\": \"https://api.github.com/users/octocat/subscriptions\",\n \"organizations_url\": \"https://api.github.com/users/octocat/orgs\",\n \"repos_url\": \"https://api.github.com/users/octocat/repos\",\n \"events_url\": \"https://api.github.com/users/octocat/events{/privacy}\",\n \"received_events_url\": \"https://api.github.com/users/octocat/received_events\",\n \"type\": \"User\",\n \"site_admin\": false\n },\n \"parents\": [\n {\n \"url\": \"https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e\",\n \"sha\": \"6dcb09b5b57875f334f61aebed695e2e4193db5e\"\n }\n ]\n }\n]" | ||
b := []byte(rawJson) | ||
_, err := w.Write(b) | ||
assert.NoError(t, err) | ||
return | ||
} | ||
if r.RequestURI == fmt.Sprintf("/repos/jfrog/%v/code-scanning/sarifs", projectName) { | ||
w.WriteHeader(http.StatusAccepted) | ||
rawJson := "{\n \"id\": \"47177e22-5596-11eb-80a1-c1e54ef945c6\",\n \"url\": \"https://api.github.com/repos/octocat/hello-world/code-scanning/sarifs/47177e22-5596-11eb-80a1-c1e54ef945c6\"\n}" | ||
b := []byte(rawJson) | ||
_, err := w.Write(b) | ||
assert.NoError(t, err) | ||
return | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters