Skip to content

Commit 4e76dbf

Browse files
committed
improve
1 parent 2403c80 commit 4e76dbf

File tree

3 files changed

+87
-36
lines changed

3 files changed

+87
-36
lines changed

build/generate-licenses.go

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"path/filepath"
2121
"strings"
2222

23+
"code.gitea.io/gitea/build/license"
2324
"code.gitea.io/gitea/modules/json"
2425
"code.gitea.io/gitea/modules/util"
2526
)
@@ -139,7 +140,12 @@ func main() {
139140
licenseAliases := make(map[string]string)
140141
for _, fileNames := range aliasesFiles {
141142
if len(fileNames) > 1 {
142-
licenseName := getLicenseNameFromAliases(fileNames)
143+
licenseName := license.GetLicenseNameFromAliases(fileNames)
144+
if licenseName == "" {
145+
// license name should not be empty as expected
146+
// if it is empty, we need to rewrite the logic of GetLicenseNameFromAliases
147+
log.Fatalf("GetLicenseNameFromAliases: license name is empty")
148+
}
143149
for _, fileName := range fileNames {
144150
licenseAliases[fileName] = licenseName
145151
}
@@ -168,38 +174,3 @@ func main() {
168174

169175
fmt.Println("Done")
170176
}
171-
172-
func getLicenseNameFromAliases(fnl []string) string {
173-
if len(fnl) == 0 {
174-
return ""
175-
}
176-
177-
shortestItem := func(list []string) string {
178-
s := list[0]
179-
for _, l := range list[1:] {
180-
if len(l) < len(s) {
181-
s = l
182-
}
183-
}
184-
return s
185-
}
186-
allHasPrefix := func(list []string, s string) bool {
187-
for _, l := range list {
188-
if !strings.HasPrefix(l, s) {
189-
return false
190-
}
191-
}
192-
return true
193-
}
194-
195-
sl := shortestItem(fnl)
196-
slv := strings.Split(sl, "-")
197-
var result string
198-
for i := len(slv); i >= 0; i-- {
199-
result = strings.Join(slv[:i], "-")
200-
if allHasPrefix(fnl, result) {
201-
return result
202-
}
203-
}
204-
return ""
205-
}

build/license/aliasgenerator.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package license
5+
6+
import "strings"
7+
8+
func GetLicenseNameFromAliases(fnl []string) string {
9+
if len(fnl) == 0 {
10+
return ""
11+
}
12+
13+
shortestItem := func(list []string) string {
14+
s := list[0]
15+
for _, l := range list[1:] {
16+
if len(l) < len(s) {
17+
s = l
18+
}
19+
}
20+
return s
21+
}
22+
allHasPrefix := func(list []string, s string) bool {
23+
for _, l := range list {
24+
if !strings.HasPrefix(l, s) {
25+
return false
26+
}
27+
}
28+
return true
29+
}
30+
31+
sl := shortestItem(fnl)
32+
slv := strings.Split(sl, "-")
33+
var result string
34+
for i := len(slv); i >= 0; i-- {
35+
result = strings.Join(slv[:i], "-")
36+
if allHasPrefix(fnl, result) {
37+
return result
38+
}
39+
}
40+
return ""
41+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package license
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestGetLicenseNameFromAliases(t *testing.T) {
13+
tests := []struct {
14+
target string
15+
inputs []string
16+
}{
17+
{
18+
// real case which you can find in license-aliases.json
19+
target: "AGPL-1.0",
20+
inputs: []string{
21+
"AGPL-1.0-only",
22+
"AGPL-1.0-or-late",
23+
},
24+
},
25+
{
26+
target: "",
27+
inputs: []string{
28+
"APSL-1.0",
29+
"AGPL-1.0-only",
30+
"AGPL-1.0-or-late",
31+
},
32+
},
33+
}
34+
35+
for _, tt := range tests {
36+
result := GetLicenseNameFromAliases(tt.inputs)
37+
assert.Equal(t, result, tt.target)
38+
}
39+
}

0 commit comments

Comments
 (0)