| 1 | + | // Copyright 2021 OpenSSF Scorecard Authors |
| 2 | + | // |
| 3 | + | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + | // you may not use this file except in compliance with the License. |
| 5 | + | // You may obtain a copy of the License at |
| 6 | + | // |
| 7 | + | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + | // |
| 9 | + | // Unless required by applicable law or agreed to in writing, software |
| 10 | + | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + | // See the License for the specific language governing permissions and |
| 13 | + | // limitations under the License. |
| 14 | + | |
| 15 | + | package git |
| 16 | + | |
| 17 | + | import ( |
| 18 | + | . "github.com/onsi/ginkgo/v2" |
| 19 | + | . "github.com/onsi/gomega" |
| 20 | + | |
| 21 | + | "github.com/ossf/scorecard/v4/clients" |
| 22 | + | ) |
| 23 | + | |
| 24 | + | var _ = DescribeTable("Test ListCommits commit-depth for HEAD", |
| 25 | + | func(uri string) { |
| 26 | + | const commitSHA = clients.HeadSHA |
| 27 | + | const commitDepth = 1 |
| 28 | + | client := &Client{} |
| 29 | + | Expect(client.InitRepo(uri, commitSHA, commitDepth)).To(BeNil()) |
| 30 | + | commits, err := client.ListCommits() |
| 31 | + | Expect(err).To(BeNil()) |
| 32 | + | Expect(len(commits)).Should(BeEquivalentTo(commitDepth)) |
| 33 | + | Expect(client.Close()).To(BeNil()) |
| 34 | + | }, |
| 35 | + | Entry("GitHub", "https://github.com/ossf/scorecard"), |
| 36 | + | Entry("Local", "file://../../"), |
| 37 | + | Entry("GitLab", "https://gitlab.haskell.org/haskell/filepath"), |
| 38 | + | ) |
| 39 | + | |
| 40 | + | var _ = DescribeTable("Test ListCommits commit-depth and latest commit at [0]", |
| 41 | + | func(uri, commitSHA string) { |
| 42 | + | const commitDepth = 10 |
| 43 | + | client := &Client{} |
| 44 | + | Expect(client.InitRepo(uri, commitSHA, commitDepth)).To(BeNil()) |
| 45 | + | commits, err := client.ListCommits() |
| 46 | + | Expect(err).To(BeNil()) |
| 47 | + | Expect(len(commits)).Should(BeEquivalentTo(commitDepth)) |
| 48 | + | Expect(commits[0].SHA).Should(BeEquivalentTo(commitSHA)) |
| 49 | + | Expect(client.Close()).To(BeNil()) |
| 50 | + | }, |
| 51 | + | Entry("GitHub", "https://github.com/ossf/scorecard", "c06ac740cc49fea404c54c036000731d5ea6ebe3"), |
| 52 | + | Entry("Local", "file://../../", "c06ac740cc49fea404c54c036000731d5ea6ebe3"), |
| 53 | + | Entry("GitLab", "https://gitlab.haskell.org/haskell/filepath", "98f8bba9eac8c7183143d290d319be7df76c258b"), |
| 54 | + | ) |
| 55 | + | |
| 56 | + | var _ = DescribeTable("Test ListCommits without enough commits", |
| 57 | + | func(uri string) { |
| 58 | + | const commitSHA = "dc1835b7ffe526969d65436b621e171e3386771e" |
| 59 | + | const commitDepth = 10 |
| 60 | + | client := &Client{} |
| 61 | + | Expect(client.InitRepo(uri, commitSHA, commitDepth)).To(BeNil()) |
| 62 | + | commits, err := client.ListCommits() |
| 63 | + | Expect(err).To(BeNil()) |
| 64 | + | Expect(len(commits)).Should(BeEquivalentTo(3)) |
| 65 | + | Expect(commits[0].SHA).Should(BeEquivalentTo(commitSHA)) |
| 66 | + | Expect(client.Close()).To(BeNil()) |
| 67 | + | }, |
| 68 | + | Entry("GitHub", "https://github.com/ossf/scorecard"), |
| 69 | + | Entry("Local", "file://../../"), |
| 70 | + | // TODO(#1709): Add equivalent test for GitLab. |
| 71 | + | ) |
| 72 | + | |
| 73 | + | var _ = DescribeTable("Test Search across a repo", |
| 74 | + | func(uri string) { |
| 75 | + | const ( |
| 76 | + | commitSHA = "c06ac740cc49fea404c54c036000731d5ea6ebe3" |
| 77 | + | commitDepth = 10 |
| 78 | + | ) |
| 79 | + | client := &Client{} |
| 80 | + | Expect(client.InitRepo(uri, commitSHA, commitDepth)).To(BeNil()) |
| 81 | + | resp, err := client.Search(clients.SearchRequest{ |
| 82 | + | Query: "github/codeql-action/analyze", |
| 83 | + | }) |
| 84 | + | Expect(err).To(BeNil()) |
| 85 | + | Expect(resp.Hits).Should(BeNumerically(">=", 1)) |
| 86 | + | Expect(client.Close()).To(BeNil()) |
| 87 | + | }, |
| 88 | + | Entry("GitHub", "https://github.com/ossf/scorecard"), |
| 89 | + | Entry("Local", "file://../../"), |
| 90 | + | // TODO(#1709): Add equivalent test for GitLab. |
| 91 | + | ) |
| 92 | + | |
| 93 | + | var _ = DescribeTable("Test Search within a path", |
| 94 | + | func(uri string) { |
| 95 | + | const ( |
| 96 | + | commitSHA = "c06ac740cc49fea404c54c036000731d5ea6ebe3" |
| 97 | + | commitDepth = 10 |
| 98 | + | ) |
| 99 | + | client := &Client{} |
| 100 | + | Expect(client.InitRepo(uri, commitSHA, commitDepth)).To(BeNil()) |
| 101 | + | resp, err := client.Search(clients.SearchRequest{ |
| 102 | + | Query: "github/codeql-action/analyze", |
| 103 | + | Path: ".github/workflows", |
| 104 | + | }) |
| 105 | + | Expect(err).To(BeNil()) |
| 106 | + | Expect(resp.Hits).Should(BeEquivalentTo(1)) |
| 107 | + | Expect(client.Close()).To(BeNil()) |
| 108 | + | }, |
| 109 | + | Entry("GitHub", "https://github.com/ossf/scorecard"), |
| 110 | + | Entry("Local", "file://../../"), |
| 111 | + | // TODO(#1709): Add equivalent test for GitLab. |
| 112 | + | ) |
| 113 | + | |
| 114 | + | var _ = DescribeTable("Test Search within a filename", |
| 115 | + | func(uri string) { |
| 116 | + | const ( |
| 117 | + | commitSHA = "c06ac740cc49fea404c54c036000731d5ea6ebe3" |
| 118 | + | commitDepth = 10 |
| 119 | + | ) |
| 120 | + | client := &Client{} |
| 121 | + | Expect(client.InitRepo(uri, commitSHA, commitDepth)).To(BeNil()) |
| 122 | + | resp, err := client.Search(clients.SearchRequest{ |
| 123 | + | Query: "github/codeql-action/analyze", |
| 124 | + | Filename: "codeql-analysis.yml", |
| 125 | + | }) |
| 126 | + | Expect(err).To(BeNil()) |
| 127 | + | Expect(resp.Hits).Should(BeEquivalentTo(1)) |
| 128 | + | Expect(client.Close()).To(BeNil()) |
| 129 | + | }, |
| 130 | + | Entry("GitHub", "https://github.com/ossf/scorecard"), |
| 131 | + | Entry("Local", "file://../../"), |
| 132 | + | // TODO(#1709): Add equivalent test for GitLab. |
| 133 | + | ) |
| 134 | + | |
| 135 | + | var _ = DescribeTable("Test Search within path and filename", |
| 136 | + | func(uri string) { |
| 137 | + | const ( |
| 138 | + | commitSHA = "c06ac740cc49fea404c54c036000731d5ea6ebe3" |
| 139 | + | commitDepth = 10 |
| 140 | + | ) |
| 141 | + | client := &Client{} |
| 142 | + | Expect(client.InitRepo(uri, commitSHA, commitDepth)).To(BeNil()) |
| 143 | + | resp, err := client.Search(clients.SearchRequest{ |
| 144 | + | Query: "github/codeql-action/analyze", |
| 145 | + | Path: ".github/workflows", |
| 146 | + | Filename: "codeql-analysis.yml", |
| 147 | + | }) |
| 148 | + | Expect(err).To(BeNil()) |
| 149 | + | Expect(resp.Hits).Should(BeEquivalentTo(1)) |
| 150 | + | Expect(client.Close()).To(BeNil()) |
| 151 | + | }, |
| 152 | + | Entry("GitHub", "https://github.com/ossf/scorecard"), |
| 153 | + | Entry("Local", "file://../../"), |
| 154 | + | // TODO(#1709): Add equivalent test for GitLab. |
| 155 | + | ) |
| 156 | + | |