1
0
mirror of https://github.com/go-gitea/gitea.git synced 2024-11-04 08:17:24 -05:00

Replace placeholders in licenses (#24354)

Replace #22117. Implement it in a more maintainable way.

Some licenses have placeholders e.g. the BSD licenses start with this
line:
```
Copyright (c) <year> <owner>. 
```
This PR replaces the placeholders with the correct value when initialize
a new repo.

### FAQ

- Why not use a regex?
It will be a pretty complicated regex which could be hard to maintain.

- There're still missing placeholders.
There are over 500 licenses, it's impossible for anyone to inspect all
of them alone. Please help to add them if you find any, and it is also
OK to leave them for the future.

---------

Co-authored-by: Giteabot <teabot@gitea.io>
This commit is contained in:
Jason Song 2023-05-05 21:46:17 +08:00 committed by GitHub
parent a866cb0cb9
commit ea1afb945d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 330 additions and 3 deletions

View File

@ -35,12 +35,40 @@ func main() {
base, out := os.Args[1], os.Args[2]
// Add ext for excluded files because license_test.go will be included for some reason.
// And there are more files that should be excluded, check with:
//
// go run github.com/google/go-licenses@v1.6.0 save . --force --save_path=.go-licenses 2>/dev/null
// find .go-licenses -type f | while read FILE; do echo "${$(basename $FILE)##*.}"; done | sort -u
// AUTHORS
// COPYING
// LICENSE
// Makefile
// NOTICE
// gitignore
// go
// md
// mod
// sum
// toml
// txt
// yml
//
// It could be removed once we have a better regex.
excludedExt := map[string]bool{
".gitignore": true,
".go": true,
".mod": true,
".sum": true,
".toml": true,
".yml": true,
}
var paths []string
err := filepath.WalkDir(base, func(path string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
if entry.IsDir() || !licenseRe.MatchString(entry.Name()) {
if entry.IsDir() || !licenseRe.MatchString(entry.Name()) || excludedExt[filepath.Ext(entry.Name())] {
return nil
}
paths = append(paths, path)

View File

@ -195,9 +195,14 @@ func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir,
// LICENSE
if len(opts.License) > 0 {
data, err = options.License(opts.License)
data, err = getLicense(opts.License, &licenseValues{
Owner: repo.OwnerName,
Email: authorSig.Email,
Repo: repo.Name,
Year: time.Now().Format("2006"),
})
if err != nil {
return fmt.Errorf("GetRepoInitFile[%s]: %w", opts.License, err)
return fmt.Errorf("getLicense[%s]: %w", opts.License, err)
}
if err = os.WriteFile(filepath.Join(tmpDir, "LICENSE"), data, 0o644); err != nil {

View File

@ -0,0 +1,113 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repository
import (
"bufio"
"bytes"
"fmt"
"regexp"
"strings"
"code.gitea.io/gitea/modules/options"
)
type licenseValues struct {
Owner string
Email string
Repo string
Year string
}
func getLicense(name string, values *licenseValues) ([]byte, error) {
data, err := options.License(name)
if err != nil {
return nil, fmt.Errorf("GetRepoInitFile[%s]: %w", name, err)
}
return fillLicensePlaceholder(name, values, data), nil
}
func fillLicensePlaceholder(name string, values *licenseValues, origin []byte) []byte {
placeholder := getLicensePlaceholder(name)
scanner := bufio.NewScanner(bytes.NewReader(origin))
output := bytes.NewBuffer(nil)
for scanner.Scan() {
line := scanner.Text()
if placeholder.MatchLine == nil || placeholder.MatchLine.MatchString(line) {
for _, v := range placeholder.Owner {
line = strings.ReplaceAll(line, v, values.Owner)
}
for _, v := range placeholder.Email {
line = strings.ReplaceAll(line, v, values.Email)
}
for _, v := range placeholder.Repo {
line = strings.ReplaceAll(line, v, values.Repo)
}
for _, v := range placeholder.Year {
line = strings.ReplaceAll(line, v, values.Year)
}
}
output.WriteString(line + "\n")
}
return output.Bytes()
}
type licensePlaceholder struct {
Owner []string
Email []string
Repo []string
Year []string
MatchLine *regexp.Regexp
}
func getLicensePlaceholder(name string) *licensePlaceholder {
// Some universal placeholders.
// If you want to add a new one, make sure you have check it by `grep -r 'NEW_WORD' options/license` and all of them are placeholders.
ret := &licensePlaceholder{
Owner: []string{
"<name of author>",
"<owner>",
"[NAME]",
"[name of copyright owner]",
"[name of copyright holder]",
"<COPYRIGHT HOLDERS>",
"<copyright holders>",
"<AUTHOR>",
"<author's name or designee>",
"[one or more legally recognised persons or entities offering the Work under the terms and conditions of this Licence]",
},
Email: []string{
"[EMAIL]",
},
Repo: []string{
"<program>",
"<one line to give the program's name and a brief idea of what it does.>",
},
Year: []string{
"<year>",
"[YEAR]",
"{YEAR}",
"[yyyy]",
"[Year]",
"[year]",
},
}
// Some special placeholders for specific licenses.
// It's unsafe to apply them to all licenses.
switch name {
case "0BSD":
return &licensePlaceholder{
Owner: []string{"AUTHOR"},
Email: []string{"EMAIL"},
Year: []string{"YEAR"},
MatchLine: regexp.MustCompile(`Copyright \(C\) YEAR by AUTHOR EMAIL`), // there is another AUTHOR in the file, but it's not a placeholder
}
// Other special placeholders can be added here.
}
return ret
}

View File

@ -0,0 +1,181 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repository
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_getLicense(t *testing.T) {
type args struct {
name string
values *licenseValues
}
tests := []struct {
name string
args args
want string
wantErr assert.ErrorAssertionFunc
}{
{
name: "regular",
args: args{
name: "MIT",
values: &licenseValues{Owner: "Gitea", Year: "2023"},
},
want: `MIT License
Copyright (c) 2023 Gitea
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
`,
wantErr: assert.NoError,
},
{
name: "license not found",
args: args{
name: "notfound",
},
wantErr: assert.Error,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := getLicense(tt.args.name, tt.args.values)
if !tt.wantErr(t, err, fmt.Sprintf("getLicense(%v, %v)", tt.args.name, tt.args.values)) {
return
}
assert.Equalf(t, tt.want, string(got), "getLicense(%v, %v)", tt.args.name, tt.args.values)
})
}
}
func Test_fillLicensePlaceholder(t *testing.T) {
type args struct {
name string
values *licenseValues
origin string
}
tests := []struct {
name string
args args
want string
}{
{
name: "owner",
args: args{
name: "regular",
values: &licenseValues{Year: "2023", Owner: "Gitea", Email: "teabot@gitea.io", Repo: "gitea"},
origin: `
<name of author>
<owner>
[NAME]
[name of copyright owner]
[name of copyright holder]
<COPYRIGHT HOLDERS>
<copyright holders>
<AUTHOR>
<author's name or designee>
[one or more legally recognised persons or entities offering the Work under the terms and conditions of this Licence]
`,
},
want: `
Gitea
Gitea
Gitea
Gitea
Gitea
Gitea
Gitea
Gitea
Gitea
Gitea
`,
},
{
name: "email",
args: args{
name: "regular",
values: &licenseValues{Year: "2023", Owner: "Gitea", Email: "teabot@gitea.io", Repo: "gitea"},
origin: `
[EMAIL]
`,
},
want: `
teabot@gitea.io
`,
},
{
name: "repo",
args: args{
name: "regular",
values: &licenseValues{Year: "2023", Owner: "Gitea", Email: "teabot@gitea.io", Repo: "gitea"},
origin: `
<program>
<one line to give the program's name and a brief idea of what it does.>
`,
},
want: `
gitea
gitea
`,
},
{
name: "year",
args: args{
name: "regular",
values: &licenseValues{Year: "2023", Owner: "Gitea", Email: "teabot@gitea.io", Repo: "gitea"},
origin: `
<year>
[YEAR]
{YEAR}
[yyyy]
[Year]
[year]
`,
},
want: `
2023
2023
2023
2023
2023
2023
`,
},
{
name: "0BSD",
args: args{
name: "0BSD",
values: &licenseValues{Year: "2023", Owner: "Gitea", Email: "teabot@gitea.io", Repo: "gitea"},
origin: `
Copyright (C) YEAR by AUTHOR EMAIL
...
... THE AUTHOR BE LIABLE FOR ...
`,
},
want: `
Copyright (C) 2023 by Gitea teabot@gitea.io
...
... THE AUTHOR BE LIABLE FOR ...
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, string(fillLicensePlaceholder(tt.args.name, tt.args.values, []byte(tt.args.origin))), "fillLicensePlaceholder(%v, %v, %v)", tt.args.name, tt.args.values, tt.args.origin)
})
}
}