aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authoryp05327 <576951401@qq.com>2024-10-02 04:25:08 +0900
committerGitHub <noreply@github.com>2024-10-01 15:25:08 -0400
commit70b7df0e5e5cce5a3561fa5a6a8abd4ebc902e68 (patch)
tree6532a12e349fe59e4c9eb66161246360aa6350d2 /tests
parentf4b8f6fc40ce2869135372a5c6ec6418d27ebfba (diff)
downloadgitea-70b7df0e5e5cce5a3561fa5a6a8abd4ebc902e68.tar.gz
gitea-70b7df0e5e5cce5a3561fa5a6a8abd4ebc902e68.zip
Support repo license (#24872)
Close #278 Close #24076 ## Solutions: - Use [google/licenseclassifier](https://github.com/google/licenseclassifier/) Test result between [google/licensecheck](https://github.com/google/licensecheck) and [go-license-detector](https://github.com/go-enry/go-license-detector): https://github.com/go-gitea/gitea/pull/24872#issuecomment-1560361167 Test result between [google/licensecheck](https://github.com/google/licensecheck) and [google/licenseclassifier](https://github.com/google/licenseclassifier/): https://github.com/go-gitea/gitea/pull/24872#issuecomment-1576092178 - Generate License Convert Name List to avoid import license templates with same contents Gitea automatically get latest license data from[ spdx/license-list-data](https://github.com/spdx/license-list-data). But unfortunately, some license templates have same contents. #20915 [click here to see the list](https://github.com/go-gitea/gitea/pull/24872#issuecomment-1584141684) So we will generate a list of these license templates with same contents and create a new file to save the result when using `make generate-license`. (Need to decide the save path) - Save License info into a new table `repo_license` Can easily support searching repo by license in the future. ## Screen shot Single License: ![image](https://github.com/go-gitea/gitea/assets/18380374/41260bd7-0b4c-4038-8592-508706cffa9f) Multiple Licenses: ![image](https://github.com/go-gitea/gitea/assets/18380374/34ce2f73-7e18-446b-9b96-ecc4fb61bd70) Triggers: - [x] Push commit to default branch - [x] Create repo - [x] Mirror repo - [x] When Default Branch is changed, licenses should be updated Todo: - [x] Save Licenses info in to DB when there's a change to license file in the commit - [x] DB Migration - [x] A nominal test? - [x] Select which library to use(https://github.com/go-gitea/gitea/pull/24872#issuecomment-1560361167) - [x] API Support - [x] Add repo license table - ~Select license in settings if there are several licenses(Not recommended)~ - License board(later, not in this PR) ![image](https://github.com/go-gitea/gitea/assets/18380374/2c3c3bf8-bcc2-4c6d-8ce0-81d1a9733878) --------- Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: Denys Konovalov <kontakt@denyskon.de> Co-authored-by: delvh <dev.lh@web.de> Co-authored-by: KN4CK3R <admin@oldschoolhack.me> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: 6543 <m.huber@kithara.com> Co-authored-by: a1012112796 <1012112796@qq.com> Co-authored-by: techknowlogick <techknowlogick@gitea.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/gitea-repositories-meta/user2/repo1.git/objects/08/51b61d9f8ca0e9e63617e11907988ee88b1ca6bin0 -> 85 bytes
-rw-r--r--tests/gitea-repositories-meta/user2/repo1.git/objects/12/8105ae73669ac2a4cb42751538f0c65c54e28abin0 -> 643 bytes
-rw-r--r--tests/gitea-repositories-meta/user2/repo1.git/objects/90/c1019714259b24fb81711d4416ac0f18667dfa2
-rw-r--r--tests/gitea-repositories-meta/user2/repo1.git/refs/heads/DefaultBranch2
-rw-r--r--tests/integration/api_admin_test.go4
-rw-r--r--tests/integration/api_repo_license_test.go80
6 files changed, 85 insertions, 3 deletions
diff --git a/tests/gitea-repositories-meta/user2/repo1.git/objects/08/51b61d9f8ca0e9e63617e11907988ee88b1ca6 b/tests/gitea-repositories-meta/user2/repo1.git/objects/08/51b61d9f8ca0e9e63617e11907988ee88b1ca6
new file mode 100644
index 0000000000..69b1e0310b
--- /dev/null
+++ b/tests/gitea-repositories-meta/user2/repo1.git/objects/08/51b61d9f8ca0e9e63617e11907988ee88b1ca6
Binary files differ
diff --git a/tests/gitea-repositories-meta/user2/repo1.git/objects/12/8105ae73669ac2a4cb42751538f0c65c54e28a b/tests/gitea-repositories-meta/user2/repo1.git/objects/12/8105ae73669ac2a4cb42751538f0c65c54e28a
new file mode 100644
index 0000000000..4c925560cb
--- /dev/null
+++ b/tests/gitea-repositories-meta/user2/repo1.git/objects/12/8105ae73669ac2a4cb42751538f0c65c54e28a
Binary files differ
diff --git a/tests/gitea-repositories-meta/user2/repo1.git/objects/90/c1019714259b24fb81711d4416ac0f18667dfa b/tests/gitea-repositories-meta/user2/repo1.git/objects/90/c1019714259b24fb81711d4416ac0f18667dfa
new file mode 100644
index 0000000000..bbd3bf7f30
--- /dev/null
+++ b/tests/gitea-repositories-meta/user2/repo1.git/objects/90/c1019714259b24fb81711d4416ac0f18667dfa
@@ -0,0 +1,2 @@
+xM
+0F] 2d&*`ƅW[>ރ< НƮj!&= *ʞ1*@TS*X3Wuc.K90EĞ$h+fg<~_@E{=,! muY Bg8fz|_erk9U]j~2]< \ No newline at end of file
diff --git a/tests/gitea-repositories-meta/user2/repo1.git/refs/heads/DefaultBranch b/tests/gitea-repositories-meta/user2/repo1.git/refs/heads/DefaultBranch
index f98a263be6..5abf667b61 100644
--- a/tests/gitea-repositories-meta/user2/repo1.git/refs/heads/DefaultBranch
+++ b/tests/gitea-repositories-meta/user2/repo1.git/refs/heads/DefaultBranch
@@ -1 +1 @@
-65f1bf27bc3bf70f64657658635e66094edbcb4d
+90c1019714259b24fb81711d4416ac0f18667dfa
diff --git a/tests/integration/api_admin_test.go b/tests/integration/api_admin_test.go
index 92da7ce041..66209ee4e0 100644
--- a/tests/integration/api_admin_test.go
+++ b/tests/integration/api_admin_test.go
@@ -304,11 +304,11 @@ func TestAPICron(t *testing.T) {
AddTokenAuth(token)
resp := MakeRequest(t, req, http.StatusOK)
- assert.Equal(t, "28", resp.Header().Get("X-Total-Count"))
+ assert.Equal(t, "29", resp.Header().Get("X-Total-Count"))
var crons []api.Cron
DecodeJSON(t, resp, &crons)
- assert.Len(t, crons, 28)
+ assert.Len(t, crons, 29)
})
t.Run("Execute", func(t *testing.T) {
diff --git a/tests/integration/api_repo_license_test.go b/tests/integration/api_repo_license_test.go
new file mode 100644
index 0000000000..52d3085694
--- /dev/null
+++ b/tests/integration/api_repo_license_test.go
@@ -0,0 +1,80 @@
+// Copyright 2024 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package integration
+
+import (
+ "fmt"
+ "net/http"
+ "net/url"
+ "testing"
+ "time"
+
+ auth_model "code.gitea.io/gitea/models/auth"
+ api "code.gitea.io/gitea/modules/structs"
+
+ "github.com/stretchr/testify/assert"
+)
+
+var testLicenseContent = `
+Copyright (c) 2024 Gitea
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+`
+
+func TestAPIRepoLicense(t *testing.T) {
+ onGiteaRun(t, func(t *testing.T, u *url.URL) {
+ session := loginUser(t, "user2")
+
+ // Request editor page
+ req := NewRequest(t, "GET", "/user2/repo1/_new/master/")
+ resp := session.MakeRequest(t, req, http.StatusOK)
+
+ doc := NewHTMLParser(t, resp.Body)
+ lastCommit := doc.GetInputValueByName("last_commit")
+ assert.NotEmpty(t, lastCommit)
+
+ // Save new file to master branch
+ req = NewRequestWithValues(t, "POST", "/user2/repo1/_new/master/", map[string]string{
+ "_csrf": doc.GetCSRF(),
+ "last_commit": lastCommit,
+ "tree_path": "LICENSE",
+ "content": testLicenseContent,
+ "commit_choice": "direct",
+ })
+ session.MakeRequest(t, req, http.StatusSeeOther)
+
+ // let gitea update repo license
+ time.Sleep(time.Second)
+ checkRepoLicense(t, "user2", "repo1", []string{"BSD-2-Clause"})
+
+ // Change default branch
+ token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
+ branchName := "DefaultBranch"
+ req = NewRequestWithJSON(t, "PATCH", "/api/v1/repos/user2/repo1", api.EditRepoOption{
+ DefaultBranch: &branchName,
+ }).AddTokenAuth(token)
+ session.MakeRequest(t, req, http.StatusOK)
+
+ // let gitea update repo license
+ time.Sleep(time.Second)
+ checkRepoLicense(t, "user2", "repo1", []string{"MIT"})
+ })
+}
+
+func checkRepoLicense(t *testing.T, owner, repo string, expected []string) {
+ reqURL := fmt.Sprintf("/api/v1/repos/%s/%s/licenses", owner, repo)
+ req := NewRequest(t, "GET", reqURL)
+ resp := MakeRequest(t, req, http.StatusOK)
+
+ var licenses []string
+ DecodeJSON(t, resp, &licenses)
+
+ assert.ElementsMatch(t, expected, licenses, 0)
+}