-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ implement
Search
for Azure DevOps (#4428)
* ✨ implement `Search` for Azure DevOps Signed-off-by: Jamie Magee <[email protected]> * Add E2E search test for Azure DevOps Signed-off-by: Jamie Magee <[email protected]> * PR comments Signed-off-by: Jamie Magee <[email protected]> * PR comments Signed-off-by: Jamie Magee <[email protected]> --------- Signed-off-by: Jamie Magee <[email protected]>
- Loading branch information
1 parent
bf3432d
commit 86f46b1
Showing
6 changed files
with
408 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2024 OpenSSF 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 azuredevopsrepo | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestAzureDevOpsRepoE2E(t *testing.T) { | ||
if val, exists := os.LookupEnv("SKIP_GINKGO"); exists && val == "1" { | ||
t.Skip() | ||
} | ||
if val, exists := os.LookupEnv("TEST_AZURE_DEVOPS_EXTERNAL"); !exists || val != "1" { | ||
t.Skip() | ||
} | ||
t.Parallel() | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Azure DevOps Repo Suite") | ||
} |
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,105 @@ | ||
// Copyright 2024 OpenSSF 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 azuredevopsrepo | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/microsoft/azure-devops-go-api/azuredevops/v7/search" | ||
|
||
"github.com/ossf/scorecard/v5/clients" | ||
) | ||
|
||
var errEmptyQuery = errors.New("search query is empty") | ||
|
||
type searchHandler struct { | ||
searchClient search.Client | ||
once *sync.Once | ||
ctx context.Context | ||
repourl *Repo | ||
searchCode fnSearchCode | ||
} | ||
|
||
func (s *searchHandler) init(ctx context.Context, repourl *Repo) { | ||
s.ctx = ctx | ||
s.once = new(sync.Once) | ||
s.repourl = repourl | ||
s.searchCode = s.searchClient.FetchCodeSearchResults | ||
} | ||
|
||
type ( | ||
fnSearchCode func(ctx context.Context, args search.FetchCodeSearchResultsArgs) (*search.CodeSearchResponse, error) | ||
) | ||
|
||
func (s *searchHandler) search(request clients.SearchRequest) (clients.SearchResponse, error) { | ||
filters, query, err := s.buildFilters(request) | ||
if err != nil { | ||
return clients.SearchResponse{}, fmt.Errorf("handler.buildQuery: %w", err) | ||
} | ||
|
||
searchResultsPageSize := 1000 | ||
args := search.FetchCodeSearchResultsArgs{ | ||
Request: &search.CodeSearchRequest{ | ||
Filters: &filters, | ||
SearchText: &query, | ||
Top: &searchResultsPageSize, | ||
}, | ||
} | ||
searchResults, err := s.searchCode(s.ctx, args) | ||
if err != nil { | ||
return clients.SearchResponse{}, fmt.Errorf("FetchCodeSearchResults: %w", err) | ||
} | ||
|
||
return searchResponseFrom(searchResults), nil | ||
} | ||
|
||
func (s *searchHandler) buildFilters(request clients.SearchRequest) (map[string][]string, string, error) { | ||
filters := make(map[string][]string) | ||
query := strings.Builder{} | ||
if request.Query == "" { | ||
return filters, query.String(), errEmptyQuery | ||
} | ||
query.WriteString(request.Query) | ||
query.WriteString(" ") | ||
|
||
filters["Project"] = []string{s.repourl.project} | ||
filters["Repository"] = []string{s.repourl.name} | ||
|
||
if request.Path != "" { | ||
filters["Path"] = []string{request.Path} | ||
} | ||
if request.Filename != "" { | ||
query.WriteString(fmt.Sprintf("file:%s", request.Filename)) | ||
} | ||
|
||
return filters, query.String(), nil | ||
} | ||
|
||
func searchResponseFrom(searchResults *search.CodeSearchResponse) clients.SearchResponse { | ||
var results []clients.SearchResult | ||
for _, result := range *searchResults.Results { | ||
results = append(results, clients.SearchResult{ | ||
Path: *result.Path, | ||
}) | ||
} | ||
return clients.SearchResponse{ | ||
Results: results, | ||
Hits: *searchResults.Count, | ||
} | ||
} |
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,48 @@ | ||
// Copyright 2024 OpenSSF 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 azuredevopsrepo | ||
|
||
import ( | ||
"context" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/ossf/scorecard/v5/clients" | ||
) | ||
|
||
var _ = Describe("E2E TEST: azuredevopsrepo.searchHandler", func() { | ||
Context("Search - Azure DevOps", func() { | ||
It("Should return search results", func() { | ||
repo, err := MakeAzureDevOpsRepo("https://dev.azure.com/jamiemagee/jamiemagee/_git/scorecard") | ||
Expect(err).Should(BeNil()) | ||
|
||
repoClient, err := CreateAzureDevOpsClient(context.Background(), repo) | ||
Expect(err).Should(BeNil()) | ||
|
||
err = repoClient.InitRepo(repo, clients.HeadSHA, 0) | ||
Expect(err).Should(BeNil()) | ||
|
||
request := clients.SearchRequest{ | ||
Query: "scorecard", | ||
Filename: "README.md", | ||
} | ||
results, err := repoClient.Search(request) | ||
Expect(err).Should(BeNil()) | ||
Expect(results.Hits).Should(BeNumerically(">", 0)) | ||
Expect(len(results.Results)).Should(BeNumerically(">", 0)) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.