-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use RepoClient.Search API in SAST check (#857)
Co-authored-by: Azeem Shaikh <azeems@google.com>
- Loading branch information
1 parent
23764f0
commit 13ef9dd
Showing
6 changed files
with
223 additions
and
24 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
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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2021 Security Scorecard Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package githubrepo | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/google/go-github/v38/github" | ||
|
||
"github.com/ossf/scorecard/v2/clients" | ||
) | ||
|
||
var errEmptyQuery = errors.New("search query is empty") | ||
|
||
type searchHandler struct { | ||
ghClient *github.Client | ||
ctx context.Context | ||
owner string | ||
repo string | ||
} | ||
|
||
func (handler *searchHandler) init(ctx context.Context, owner, repo string) { | ||
handler.ctx = ctx | ||
handler.owner = owner | ||
handler.repo = repo | ||
} | ||
|
||
func (handler *searchHandler) search(request clients.SearchRequest) (clients.SearchResponse, error) { | ||
query, err := handler.buildQuery(request) | ||
if err != nil { | ||
return clients.SearchResponse{}, fmt.Errorf("handler.buildQuery: %w", err) | ||
} | ||
|
||
resp, _, err := handler.ghClient.Search.Code(handler.ctx, query, &github.SearchOptions{}) | ||
if err != nil { | ||
return clients.SearchResponse{}, fmt.Errorf("Search.Code: %w", err) | ||
} | ||
return searchResponseFrom(resp), nil | ||
} | ||
|
||
func (handler *searchHandler) buildQuery(request clients.SearchRequest) (string, error) { | ||
if request.Query == "" { | ||
return "", fmt.Errorf("%w", errEmptyQuery) | ||
} | ||
var queryBuilder strings.Builder | ||
if _, err := queryBuilder.WriteString( | ||
fmt.Sprintf("%s repo:%s/%s", request.Query, handler.owner, handler.repo)); err != nil { | ||
return "", fmt.Errorf("WriteString: %w", err) | ||
} | ||
if request.Filename != "" { | ||
if _, err := queryBuilder.WriteString( | ||
fmt.Sprintf(" in:file filename:%s", request.Filename)); err != nil { | ||
return "", fmt.Errorf("WriteString: %w", err) | ||
} | ||
} | ||
if request.Path != "" { | ||
if _, err := queryBuilder.WriteString(fmt.Sprintf(" path:%s", request.Path)); err != nil { | ||
return "", fmt.Errorf("WriteString: %w", err) | ||
} | ||
} | ||
return queryBuilder.String(), nil | ||
} | ||
|
||
func searchResponseFrom(resp *github.CodeSearchResult) clients.SearchResponse { | ||
var ret clients.SearchResponse | ||
ret.Hits = resp.GetTotal() | ||
for _, result := range resp.CodeResults { | ||
ret.Results = append(ret.Results, clients.SearchResult{ | ||
Path: result.GetPath(), | ||
}) | ||
} | ||
return ret | ||
} |
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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright 2021 Security Scorecard Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package githubrepo | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/ossf/scorecard/v2/clients" | ||
) | ||
|
||
func TestBuildQuery(t *testing.T) { | ||
t.Parallel() | ||
testcases := []struct { | ||
searchReq clients.SearchRequest | ||
expectedErrType error | ||
name string | ||
owner string | ||
repo string | ||
expectedQuery string | ||
hasError bool | ||
}{ | ||
{ | ||
name: "Basic", | ||
owner: "testowner", | ||
repo: "testrepo", | ||
searchReq: clients.SearchRequest{ | ||
Query: "testquery", | ||
}, | ||
expectedQuery: "testquery repo:testowner/testrepo", | ||
}, | ||
{ | ||
name: "EmptyQuery", | ||
owner: "testowner", | ||
repo: "testrepo", | ||
searchReq: clients.SearchRequest{}, | ||
hasError: true, | ||
expectedErrType: errEmptyQuery, | ||
}, | ||
{ | ||
name: "WithFilename", | ||
owner: "testowner", | ||
repo: "testrepo", | ||
searchReq: clients.SearchRequest{ | ||
Query: "testquery", | ||
Filename: "filename1.txt", | ||
}, | ||
expectedQuery: "testquery repo:testowner/testrepo in:file filename:filename1.txt", | ||
}, | ||
{ | ||
name: "WithPath", | ||
owner: "testowner", | ||
repo: "testrepo", | ||
searchReq: clients.SearchRequest{ | ||
Query: "testquery", | ||
Path: "dir1/file1.txt", | ||
}, | ||
expectedQuery: "testquery repo:testowner/testrepo path:dir1/file1.txt", | ||
}, | ||
{ | ||
name: "WithFilenameAndPath", | ||
owner: "testowner", | ||
repo: "testrepo", | ||
searchReq: clients.SearchRequest{ | ||
Query: "testquery", | ||
Filename: "filename1.txt", | ||
Path: "dir1/dir2", | ||
}, | ||
expectedQuery: "testquery repo:testowner/testrepo in:file filename:filename1.txt path:dir1/dir2", | ||
}, | ||
} | ||
|
||
for _, testcase := range testcases { | ||
testcase := testcase | ||
t.Run(testcase.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
handler := searchHandler{ | ||
owner: testcase.owner, | ||
repo: testcase.repo, | ||
} | ||
|
||
query, err := handler.buildQuery(testcase.searchReq) | ||
if !testcase.hasError && err != nil { | ||
t.Fatalf("expected - no error, got: %v", err) | ||
} | ||
if testcase.hasError && !errors.Is(err, testcase.expectedErrType) { | ||
t.Fatalf("expectedErrType - %v, got - %v", | ||
testcase.expectedErrType, err) | ||
} else if query != testcase.expectedQuery { | ||
t.Fatalf("expectedQuery - %s, got - %s", | ||
testcase.expectedQuery, query) | ||
} | ||
}) | ||
} | ||
} |
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