Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support repo license #24872

Merged
merged 209 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
209 commits
Select commit Hold shift + click to select a range
43129a1
add repo license check
yp05327 May 23, 2023
33ec600
fix incorrect location of {{end}}
yp05327 May 23, 2023
7d3f1d5
fix test
yp05327 May 23, 2023
8acb46e
remove debug code
yp05327 May 23, 2023
a4e0deb
use make tidy to add licensecheck
yp05327 May 24, 2023
5a75aab
use statistics in db
yp05327 May 24, 2023
973f995
check license change in HookPostReceive
yp05327 May 25, 2023
a876b2d
remove debug code
yp05327 May 25, 2023
a2ef8f6
update license when create a repo
yp05327 May 25, 2023
c4c6da5
add todo
yp05327 May 25, 2023
0278775
add license to fork repo
yp05327 May 25, 2023
c30bca1
fix lint
yp05327 May 25, 2023
aa91765
add license to mirror repo
yp05327 May 25, 2023
84801d5
move findFileInEntries to repo modules
yp05327 May 25, 2023
0421e98
update license when sync mirror repo
yp05327 May 25, 2023
cf02888
support search license file
yp05327 May 25, 2023
10b2a90
imporve debug info
yp05327 May 25, 2023
f285720
fix test
yp05327 May 25, 2023
7eb4584
fix test
yp05327 May 25, 2023
8ba16c0
fix
yp05327 May 25, 2023
0637b87
fix
yp05327 May 25, 2023
facd698
improve
yp05327 May 26, 2023
af1b031
fix fork repo
yp05327 May 26, 2023
30c25ba
remove check iswiki
yp05327 May 26, 2023
bc005dd
fix no commitID
yp05327 May 26, 2023
2f870f7
Merge branch 'main' into add-repo-license-display
yp05327 May 26, 2023
3518a63
fix lint
yp05327 May 26, 2023
0a9e4e8
Merge branch 'main' into add-repo-license-display
yp05327 May 26, 2023
763f104
improve
yp05327 May 29, 2023
f481624
add db migration
yp05327 May 29, 2023
07e91bf
fix migration
yp05327 May 29, 2023
744a032
add licensecheck test
yp05327 May 29, 2023
93d2c7f
fix lint
yp05327 May 30, 2023
ea2f23c
Merge branch 'main' into add-repo-license-display
silverwind May 30, 2023
92d8df7
move the position of license
yp05327 May 31, 2023
ac84422
fix
yp05327 May 31, 2023
0dc4c3e
Merge branch 'main' into add-repo-license-display
yp05327 May 31, 2023
ef340f6
only use defaultbranch to display license file
yp05327 May 31, 2023
63a6da8
add licenses to api.Repository
yp05327 May 31, 2023
b9c2329
update licenses info when update default branch
yp05327 May 31, 2023
48da7a6
convert use licenseclassifier
yp05327 Jun 5, 2023
a000c38
Merge branch 'main' into add-repo-license-display
yp05327 Jun 5, 2023
fa8e347
fix
yp05327 Jun 5, 2023
7a01d3b
remove unnecessary code
yp05327 Jun 6, 2023
1cb2020
init licenseclassifier when service start
yp05327 Jun 6, 2023
58b4cc2
remove unnecessary code
yp05327 Jun 6, 2023
c58162e
fix test
yp05327 Jun 6, 2023
f2933cc
fix migration
yp05327 Jun 6, 2023
388bc26
add tooltips
yp05327 Jun 8, 2023
7f2b5d0
improve
yp05327 Jun 9, 2023
4cf79be
fix
yp05327 Jun 9, 2023
f45d592
Merge branch 'main' into add-repo-license-display
yp05327 Jun 13, 2023
84c7b98
generate same file list
yp05327 Jun 14, 2023
3da5a80
generate licenses
yp05327 Jun 14, 2023
d0e5676
Merge branch 'main' into add-repo-license-display
yp05327 Jun 14, 2023
1b0783c
fix conflict
yp05327 Jun 14, 2023
25966eb
fix lint
yp05327 Jun 14, 2023
59198d3
fix lint
yp05327 Jun 14, 2023
4a838ad
allow not existed git repo
yp05327 Jun 14, 2023
866de8d
make generate-swagger
yp05327 Jun 14, 2023
27cb436
Merge branch 'main' into add-repo-license-display
yp05327 Jun 14, 2023
e46fd13
fix
yp05327 Jun 14, 2023
c831ee3
improve
yp05327 Jun 14, 2023
ae27d02
remove GetBlobAll
yp05327 Jun 14, 2023
a79cb78
fix
yp05327 Jun 15, 2023
c6cc8b4
fix lint
yp05327 Jun 15, 2023
ea2c0be
convert name
yp05327 Jun 15, 2023
d193276
fix lint
yp05327 Jun 15, 2023
e67a771
improve test
yp05327 Jun 19, 2023
6f25294
convert license col into table
yp05327 Jun 19, 2023
44a8ece
fix lint
yp05327 Jun 19, 2023
376b91a
remove todo
yp05327 Jun 19, 2023
4ec4a0f
finish todo
yp05327 Jun 19, 2023
50150b8
Merge branch 'main' into add-repo-license-display
yp05327 Jun 20, 2023
9cbde72
avoid return same licenses
yp05327 Jun 26, 2023
f7da797
fix conflicts
yp05327 Jun 26, 2023
3a99e01
Merge branch 'main' into add-repo-license-display
yp05327 Jun 26, 2023
2904f34
fix conflicts
yp05327 Jul 5, 2023
e38161b
fix conflicts
yp05327 Jul 5, 2023
7c7bdea
Merge branch 'main' into add-repo-license-display
yp05327 Jul 5, 2023
2b56a5a
fix
yp05327 Jul 5, 2023
b168d1b
improve
yp05327 Jul 5, 2023
7640268
remove licenses when remove repo
yp05327 Jul 6, 2023
7a9d605
add fixtures
yp05327 Jul 7, 2023
7e2589c
Merge branch 'main' into add-repo-license-display
yp05327 Jul 20, 2023
16822cd
make tidy
yp05327 Jul 20, 2023
fc93550
rename to v270
yp05327 Jul 27, 2023
d6d2524
Merge branch 'main' into add-repo-license-display
yp05327 Jul 27, 2023
d57592f
fix lint
yp05327 Jul 27, 2023
d6f7006
rename to v271
yp05327 Aug 2, 2023
8369e93
Merge branch 'main' into add-repo-license-display
yp05327 Aug 2, 2023
3c554a5
fix lint
yp05327 Aug 7, 2023
f546aae
rename to v273
yp05327 Aug 22, 2023
05c844d
Merge branch 'main' into add-repo-license-display
yp05327 Aug 22, 2023
87ae3a6
rename to v274
yp05327 Aug 29, 2023
24b1c13
Merge branch 'main' into add-repo-license-display
yp05327 Aug 29, 2023
cbe1515
rename to v278
yp05327 Sep 27, 2023
f65eeec
Merge branch 'main' into add-repo-license-display
yp05327 Sep 27, 2023
e70bb25
recover changes in DeleteRepository
yp05327 Sep 27, 2023
498ea78
recover changes in CreateRepository
yp05327 Sep 27, 2023
83945ed
recover changes in SetDefaultBranchPost
yp05327 Sep 27, 2023
bae33d5
recover changes in templates/repo/sub_menu.tmpl
yp05327 Sep 27, 2023
ba60195
fix
yp05327 Sep 27, 2023
71ad15e
Merge branch 'main' into add-repo-license-display
lunny Sep 27, 2023
91dda74
rename to v282
yp05327 Oct 23, 2023
fe1f087
Merge branch 'main' into add-repo-license-display
yp05327 Oct 23, 2023
462f7f5
fix package name of migration
yp05327 Oct 23, 2023
a335732
Merge branch 'main' into add-repo-license-display
GiteaBot Oct 27, 2023
df3e168
Update models/migrations/migrations.go
yp05327 Nov 1, 2023
3abe77e
rename to v283
yp05327 Nov 1, 2023
7a6c3b3
Merge branch 'main' into add-repo-license-display
yp05327 Nov 1, 2023
640a5e4
Update models/migrations/v1_22/v283.go
yp05327 Nov 6, 2023
d6ba4af
use slices.Contains
yp05327 Nov 6, 2023
8a1c767
fix
yp05327 Nov 6, 2023
7781e7d
sync funcs in migration from origin
yp05327 Nov 6, 2023
cd0b44c
fix typo
yp05327 Nov 24, 2023
c57d542
do not init classifier in init()
yp05327 Nov 24, 2023
44cf8e4
add return
yp05327 Nov 24, 2023
648d0a0
improve the logic of copy license info
yp05327 Nov 24, 2023
b45f907
improve variable name
yp05327 Nov 24, 2023
dac32ce
improve detect license
yp05327 Nov 24, 2023
21b1a7a
fix
yp05327 Nov 24, 2023
cd06857
fix test
yp05327 Nov 24, 2023
5ec4b50
add data-tooltip-placement
yp05327 Nov 24, 2023
a5d9763
fix bug
yp05327 Nov 30, 2023
9178d90
convert sameLicenses to license aliases
yp05327 Nov 30, 2023
093e71d
Update modules/repository/license.go
yp05327 Nov 30, 2023
1386780
remove generate licenses in migrations
yp05327 Dec 1, 2023
c5296f4
improve initClassifier
yp05327 Dec 1, 2023
121f2b2
fix
yp05327 Dec 1, 2023
c97caf8
improve
yp05327 Dec 1, 2023
bb46e01
Merge branch 'main' into add-repo-license-display
yp05327 Dec 4, 2023
727d5cc
add cron task
yp05327 Dec 4, 2023
da36554
use ctx.Locale.Tr
yp05327 Dec 4, 2023
3e45a9a
Update models/repo/license.go
yp05327 Dec 4, 2023
7621d98
fix lint
yp05327 Dec 6, 2023
d501891
fix test
yp05327 Dec 6, 2023
8e0929e
fix test
yp05327 Dec 8, 2023
db2e9eb
remove add update repo license notify todo
yp05327 Jan 17, 2024
030c0dd
rename to v285
yp05327 Jan 17, 2024
91c5454
Merge branch 'main' into add-repo-license-display
yp05327 Jan 17, 2024
5e72897
fix changes from 28691
yp05327 Jan 17, 2024
3ef4aa8
improve
yp05327 Jan 18, 2024
80934c3
Merge branch 'main' into add-repo-license-display
yp05327 Jan 18, 2024
cc3cf07
fix comment
yp05327 Jan 18, 2024
aabb200
fix
yp05327 Jan 18, 2024
a0e8a5d
Update build/generate-licenses.go
yp05327 Feb 22, 2024
86f8bec
Update build/generate-licenses.go
yp05327 Feb 22, 2024
841453d
convert v285 to v287
yp05327 Feb 22, 2024
d46b98e
Merge branch 'main' into add-repo-license-display
yp05327 Feb 22, 2024
baa29d4
update license
yp05327 Feb 22, 2024
d92bbab
Merge branch 'main' into add-repo-license-display
6543 Feb 25, 2024
7d92e47
use queue
yp05327 Feb 26, 2024
c79fbaa
fix not remove old records when license file not detected after branc…
yp05327 Feb 26, 2024
fe77ff5
remove unnecessary enter
yp05327 Feb 26, 2024
882767c
fix ci error
yp05327 Feb 27, 2024
a0a7e9a
add api and test
yp05327 Mar 1, 2024
11b5e09
convert v287 to v288
yp05327 Mar 1, 2024
2ec2083
Merge remote-tracking branch 'upstream/main' into add-repo-license-di…
yp05327 Mar 1, 2024
ce75061
convert code.gitea.io/gitea/modules/context into code.gitea.io/gitea/…
yp05327 Mar 1, 2024
51a5bce
Merge branch 'main' into add-repo-license-display
6543 Mar 1, 2024
7582a66
Merge branch 'main' into add-repo-license-display
delvh Mar 3, 2024
60bfdea
Merge branch 'main' into add-repo-license-display
6543 Mar 4, 2024
49ab83b
Update tests/integration/api_repo_license_test.go
6543 Mar 4, 2024
46e1246
Apply suggestions from code review
6543 Mar 4, 2024
f434251
do not use time.Now(9 in test
yp05327 Mar 5, 2024
c218be0
remove commit
yp05327 Mar 5, 2024
00b182c
Update models/repo/license.go
yp05327 Mar 5, 2024
ccde201
fix lint
yp05327 Mar 5, 2024
5059a0f
Update modules/repository/license.go
yp05327 Mar 5, 2024
75cf5a2
convert v288 into v289
yp05327 Mar 5, 2024
0eb8545
Merge branch 'main' into add-repo-license-display
yp05327 Mar 5, 2024
0e3d512
import gitrepo
yp05327 Mar 5, 2024
8dc1c3b
fix test
yp05327 Mar 5, 2024
be50529
improve
yp05327 Mar 5, 2024
2f1af68
fix lint
yp05327 Mar 5, 2024
f87f857
move to services
yp05327 Mar 5, 2024
2910c9c
fix year
yp05327 Mar 5, 2024
60aba0c
fix typo
yp05327 Mar 6, 2024
d5ca326
renqme v289 to v305
yp05327 Sep 12, 2024
a938795
Merge branch 'main' into add-repo-license-display
yp05327 Sep 12, 2024
0f00895
fix migration
yp05327 Sep 12, 2024
fa0119b
go mod tidy
yp05327 Sep 12, 2024
cfe8cb5
fix repository size
yp05327 Sep 12, 2024
0717cb6
make generate-swagger
yp05327 Sep 12, 2024
051fdff
fix lint
yp05327 Sep 12, 2024
1043d39
fix lint
yp05327 Sep 17, 2024
da313ff
fix lint
yp05327 Sep 17, 2024
875daf8
fix swagger
yp05327 Sep 17, 2024
4e18872
fix test
yp05327 Sep 17, 2024
8991c24
Merge branch 'main' into add-repo-license-display
techknowlogick Sep 17, 2024
f1ddccf
Merge branch 'main' into add-repo-license-display
techknowlogick Sep 18, 2024
47e7c58
Merge branch 'main' into add-repo-license-display
yp05327 Sep 19, 2024
a1d98a3
Merge branch 'main' into add-repo-license-display
yp05327 Sep 26, 2024
8f5a70d
adjust the length of license in db
yp05327 Sep 26, 2024
ae61ccd
remove INDEX
yp05327 Sep 26, 2024
286a889
remove INDEX for RepoID
yp05327 Sep 26, 2024
dd95d1b
remove DefaultBranch
yp05327 Sep 26, 2024
d26f2a0
move to license/etc/
yp05327 Sep 26, 2024
cdbdb3e
add copyright
yp05327 Sep 26, 2024
45fcea6
add return
yp05327 Sep 26, 2024
da6a1cf
remove returns
yp05327 Sep 26, 2024
bd2b334
use filepath.Join
yp05327 Sep 26, 2024
35f0800
detect license file by const filename
yp05327 Oct 1, 2024
2403c80
init license classifier when boot
yp05327 Oct 1, 2024
4e76dbf
improve
yp05327 Oct 1, 2024
22f7c41
fix lint
yp05327 Oct 1, 2024
317eef9
fix migration
yp05327 Oct 1, 2024
eca31c7
Merge branch 'main' into add-repo-license-display
yp05327 Oct 1, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions assets/go-licenses.json

Large diffs are not rendered by default.

64 changes: 59 additions & 5 deletions build/generate-licenses.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

//go:build ignore

package main

import (
"archive/tar"
"compress/gzip"
"crypto/md5"
"encoding/hex"
"flag"
"fmt"
"io"
Expand All @@ -15,6 +20,8 @@ import (
"path/filepath"
"strings"

"code.gitea.io/gitea/build/license"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/util"
)

Expand Down Expand Up @@ -77,7 +84,7 @@ func main() {
}

tr := tar.NewReader(gz)

aliasesFiles := make(map[string][]string)
for {
hdr, err := tr.Next()

Expand All @@ -97,26 +104,73 @@ func main() {
continue
}

if strings.HasPrefix(filepath.Base(hdr.Name), "README") {
fileBaseName := filepath.Base(hdr.Name)
licenseName := strings.TrimSuffix(fileBaseName, ".txt")

if strings.HasPrefix(fileBaseName, "README") {
continue
}

if strings.HasPrefix(filepath.Base(hdr.Name), "deprecated_") {
if strings.HasPrefix(fileBaseName, "deprecated_") {
continue
}
out, err := os.Create(path.Join(destination, strings.TrimSuffix(filepath.Base(hdr.Name), ".txt")))
out, err := os.Create(path.Join(destination, licenseName))
if err != nil {
log.Fatalf("Failed to create new file. %s", err)
}

defer out.Close()

if _, err := io.Copy(out, tr); err != nil {
// some license files have same content, so we need to detect these files and create a convert map into a json file
// Later we use this convert map to avoid adding same license content with different license name
h := md5.New()
// calculate md5 and write file in the same time
r := io.TeeReader(tr, h)
if _, err := io.Copy(out, r); err != nil {
log.Fatalf("Failed to write new file. %s", err)
} else {
fmt.Printf("Written %s\n", out.Name())

md5 := hex.EncodeToString(h.Sum(nil))
aliasesFiles[md5] = append(aliasesFiles[md5], licenseName)
}
}

// generate convert license name map
licenseAliases := make(map[string]string)
for _, fileNames := range aliasesFiles {
if len(fileNames) > 1 {
licenseName := license.GetLicenseNameFromAliases(fileNames)
if licenseName == "" {
// license name should not be empty as expected
// if it is empty, we need to rewrite the logic of GetLicenseNameFromAliases
log.Fatalf("GetLicenseNameFromAliases: license name is empty")
}
for _, fileName := range fileNames {
licenseAliases[fileName] = licenseName
}
}
}
// save convert license name map to file
b, err := json.Marshal(licenseAliases)
if err != nil {
log.Fatalf("Failed to create json bytes. %s", err)
}

licenseAliasesDestination := filepath.Join(destination, "etc", "license-aliases.json")
if err := os.MkdirAll(filepath.Dir(licenseAliasesDestination), 0o755); err != nil {
log.Fatalf("Failed to create directory for license aliases json file. %s", err)
}

f, err := os.Create(licenseAliasesDestination)
if err != nil {
log.Fatalf("Failed to create license aliases json file. %s", err)
}
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
defer f.Close()

if _, err = f.Write(b); err != nil {
log.Fatalf("Failed to write license aliases json file. %s", err)
}

fmt.Println("Done")
}
41 changes: 41 additions & 0 deletions build/license/aliasgenerator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package license

import "strings"

func GetLicenseNameFromAliases(fnl []string) string {
if len(fnl) == 0 {
return ""
}

shortestItem := func(list []string) string {
s := list[0]
for _, l := range list[1:] {
if len(l) < len(s) {
s = l
}
}
return s
}
allHasPrefix := func(list []string, s string) bool {
for _, l := range list {
if !strings.HasPrefix(l, s) {
return false
}
}
return true
}

sl := shortestItem(fnl)
slv := strings.Split(sl, "-")
var result string
for i := len(slv); i >= 0; i-- {
result = strings.Join(slv[:i], "-")
if allHasPrefix(fnl, result) {
return result
}
}
return ""
}
39 changes: 39 additions & 0 deletions build/license/aliasgenerator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package license

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetLicenseNameFromAliases(t *testing.T) {
tests := []struct {
target string
inputs []string
}{
{
// real case which you can find in license-aliases.json
target: "AGPL-1.0",
inputs: []string{
"AGPL-1.0-only",
"AGPL-1.0-or-late",
},
},
{
target: "",
inputs: []string{
"APSL-1.0",
"AGPL-1.0-only",
"AGPL-1.0-or-late",
},
},
}

for _, tt := range tests {
result := GetLicenseNameFromAliases(tt.inputs)
assert.Equal(t, result, tt.target)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ require (
github.com/gogs/go-gogs-client v0.0.0-20210131175652-1d7215cd8d85
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/go-github/v61 v61.0.0
github.com/google/licenseclassifier/v2 v2.0.0
github.com/google/pprof v0.0.0-20240618054019-d3b898a103f8
github.com/google/uuid v1.6.0
github.com/gorilla/feeds v1.2.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,8 @@ github.com/google/go-tpm v0.9.0/go.mod h1:FkNVkc6C+IsvDI9Jw1OveJmxGZUUaKxtrpOS47
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/licenseclassifier/v2 v2.0.0 h1:1Y57HHILNf4m0ABuMVb6xk4vAJYEUO0gDxNpog0pyeA=
github.com/google/licenseclassifier/v2 v2.0.0/go.mod h1:cOjbdH0kyC9R22sdQbYsFkto4NGCAc+ZSwbeThazEtM=
github.com/google/pprof v0.0.0-20240227163752-401108e1b7e7/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik=
github.com/google/pprof v0.0.0-20240618054019-d3b898a103f8 h1:ASJ/LAqdCHOyMYI+dwNxn7Rd8FscNkMyTr1KZU1JI/M=
github.com/google/pprof v0.0.0-20240618054019-d3b898a103f8/go.mod h1:K1liHPHnj73Fdn/EKuT8nrFqBihUSKXoLYU0BuatOYo=
Expand Down Expand Up @@ -735,6 +737,7 @@ github.com/sassoftware/go-rpmutils v0.4.0/go.mod h1:3goNWi7PGAT3/dlql2lv3+MSN5jN
github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys=
github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
github.com/serenize/snaker v0.0.0-20171204205717-a683aaf2d516/go.mod h1:Yow6lPLSAXx2ifx470yD/nUe22Dv5vBvxK/UK9UUTVs=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
6543 marked this conversation as resolved.
Show resolved Hide resolved
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
Expand Down
1 change: 1 addition & 0 deletions models/fixtures/repo_license.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[] # empty
2 changes: 1 addition & 1 deletion models/fixtures/repository.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
fork_id: 0
is_template: false
template_id: 0
size: 7597
size: 8478
is_fsck_enabled: true
close_issues_via_commit_in_any_branch: false

Expand Down
4 changes: 3 additions & 1 deletion models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ var migrations = []Migration{
// v259 -> v260
NewMigration("Convert scoped access tokens", v1_20.ConvertScopedAccessTokens),

// Gitea 1.20.0 ends at 260
// Gitea 1.20.0 ends at v260

// v260 -> v261
NewMigration("Drop custom_labels column of action_runner table", v1_21.DropCustomLabelsColumnOfActionRunner),
Expand Down Expand Up @@ -601,6 +601,8 @@ var migrations = []Migration{
NewMigration("Add metadata column for comment table", v1_23.AddCommentMetaDataColumn),
// v304 -> v305
NewMigration("Add index for release sha1", v1_23.AddIndexForReleaseSha1),
// v305 -> v306
NewMigration("Add Repository Licenses", v1_23.AddRepositoryLicenses),
}

// GetCurrentDBVersion returns the current db version
Expand Down
23 changes: 23 additions & 0 deletions models/migrations/v1_23/v305.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package v1_23 //nolint

import (
"code.gitea.io/gitea/modules/timeutil"

"xorm.io/xorm"
)

func AddRepositoryLicenses(x *xorm.Engine) error {
type RepoLicense struct {
ID int64 `xorm:"pk autoincr"`
RepoID int64 `xorm:"UNIQUE(s) NOT NULL"`
CommitID string
License string `xorm:"VARCHAR(255) UNIQUE(s) NOT NULL"`
CreatedUnix timeutil.TimeStamp `xorm:"INDEX CREATED"`
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX UPDATED"`
}

return x.Sync(new(RepoLicense))
}
120 changes: 120 additions & 0 deletions models/repo/license.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package repo

import (
"context"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/timeutil"
)

func init() {
db.RegisterModel(new(RepoLicense))
}

type RepoLicense struct { //revive:disable-line:exported
ID int64 `xorm:"pk autoincr"`
RepoID int64 `xorm:"UNIQUE(s) NOT NULL"`
CommitID string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the commit_id for? Why not a branch name here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is used for checking existing licenses when updating licenses:
If the newly detected licenses already exists in DB, we will update the commit ID.
And branch name can not correctly detect it, as same branch name may have different commit id in sometimes?
Maybe both are ok, but commit id is safer I think.

License string `xorm:"VARCHAR(255) UNIQUE(s) NOT NULL"`
CreatedUnix timeutil.TimeStamp `xorm:"INDEX CREATED"`
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX UPDATED"`
}

// RepoLicenseList defines a list of repo licenses
type RepoLicenseList []*RepoLicense //revive:disable-line:exported

func (rll RepoLicenseList) StringList() []string {
var licenses []string
for _, rl := range rll {
licenses = append(licenses, rl.License)
}
return licenses
}

// GetRepoLicenses returns the license statistics for a repository
func GetRepoLicenses(ctx context.Context, repo *Repository) (RepoLicenseList, error) {
licenses := make(RepoLicenseList, 0)
if err := db.GetEngine(ctx).Where("`repo_id` = ?", repo.ID).Asc("`license`").Find(&licenses); err != nil {
return nil, err
}
return licenses, nil
}

// UpdateRepoLicenses updates the license statistics for repository
func UpdateRepoLicenses(ctx context.Context, repo *Repository, commitID string, licenses []string) error {
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
oldLicenses, err := GetRepoLicenses(ctx, repo)
if err != nil {
return err
}
for _, license := range licenses {
upd := false
for _, o := range oldLicenses {
// Update already existing license
if o.License == license {
if _, err := db.GetEngine(ctx).ID(o.ID).Cols("`commit_id`").Update(o); err != nil {
return err
}
upd = true
break
}
}
// Insert new license
if !upd {
if err := db.Insert(ctx, &RepoLicense{
RepoID: repo.ID,
CommitID: commitID,
License: license,
}); err != nil {
return err
}
}
}
delvh marked this conversation as resolved.
Show resolved Hide resolved
// Delete old licenses
licenseToDelete := make([]int64, 0, len(oldLicenses))
for _, o := range oldLicenses {
if o.CommitID != commitID {
licenseToDelete = append(licenseToDelete, o.ID)
}
}
if len(licenseToDelete) > 0 {
if _, err := db.GetEngine(ctx).In("`id`", licenseToDelete).Delete(&RepoLicense{}); err != nil {
return err
}
}

return nil
}

// CopyLicense Copy originalRepo license information to destRepo (use for forked repo)
func CopyLicense(ctx context.Context, originalRepo, destRepo *Repository) error {
repoLicenses, err := GetRepoLicenses(ctx, originalRepo)
if err != nil {
return err
}
if len(repoLicenses) > 0 {
newRepoLicenses := make(RepoLicenseList, 0, len(repoLicenses))

for _, rl := range repoLicenses {
newRepoLicense := &RepoLicense{
RepoID: destRepo.ID,
CommitID: rl.CommitID,
License: rl.License,
}
newRepoLicenses = append(newRepoLicenses, newRepoLicense)
}
if err := db.Insert(ctx, &newRepoLicenses); err != nil {
return err
}
}
return nil
}

// CleanRepoLicenses will remove all license record of the repo
func CleanRepoLicenses(ctx context.Context, repo *Repository) error {
return db.DeleteBeans(ctx, &RepoLicense{
RepoID: repo.ID,
})
}
Loading
Loading