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 2 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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ require (
github.com/gogs/go-gogs-client v0.0.0-20210131175652-1d7215cd8d85
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/google/go-github/v51 v51.0.0
github.com/google/licensecheck v0.3.1
github.com/google/pprof v0.0.0-20230502171905-255e3b9b56de
github.com/google/uuid v1.3.0
github.com/gorilla/feeds v1.1.1
Expand Down Expand Up @@ -203,7 +204,6 @@ require (
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/go-tpm v0.3.3 // indirect
github.com/google/licensecheck v0.3.1 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/gorilla/handlers v1.5.1 // indirect
github.com/gorilla/mux v1.8.0 // indirect
Expand Down
38 changes: 38 additions & 0 deletions models/repo/license.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
6543 marked this conversation as resolved.
Show resolved Hide resolved
// SPDX-License-Identifier: MIT

package repo

import (
"context"

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

// UpdateLanguageStats updates the license statistics for repository
func UpdateLicenseStats(repo *Repository, commitID string, licenses []string) error {
ctx, committer, err := db.TxContext(db.DefaultContext)
if err != nil {
return err
}
defer committer.Close()

if err = UpdateLicense(ctx, repo.ID, licenses); err != nil {
return err
}

// Update indexer status
if err = UpdateIndexerStatus(ctx, repo, RepoIndexerTypeStats, commitID); err != nil {
return err
}

return committer.Commit()
}

// UpdateRepoSize updates the repository size, calculating it using getDirectorySize
func UpdateLicense(ctx context.Context, repoID int64, licenses []string) error {
_, err := db.GetEngine(ctx).ID(repoID).Cols("licenses").NoAutoTime().Update(&Repository{
Licenses: licenses,
})
return err
}
2 changes: 2 additions & 0 deletions models/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ type Repository struct {
Units []*RepoUnit `xorm:"-"`
PrimaryLanguage *LanguageStat `xorm:"-"`

Licenses []string `xorm:"TEXT JSON"`
yp05327 marked this conversation as resolved.
Show resolved Hide resolved

IsFork bool `xorm:"INDEX NOT NULL DEFAULT false"`
ForkID int64 `xorm:"INDEX"`
BaseRepo *Repository `xorm:"-"`
Expand Down
3 changes: 3 additions & 0 deletions modules/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,9 @@ func repoAssignment(ctx *Context, repo *repo_model.Repository) {
ctx.Data["PushMirrors"] = pushMirrors
ctx.Data["RepoName"] = ctx.Repo.Repository.Name
ctx.Data["IsEmptyRepo"] = ctx.Repo.Repository.IsEmpty
ctx.Data["Licenses"] = ctx.Repo.Repository.Licenses
// TODO: support ext
ctx.Data["LicenseFileName"] = "LICENSE"
}

// RepoIDAssignment returns a handler which assigns the repo to the context.
Expand Down
68 changes: 68 additions & 0 deletions modules/git/repo_license_gogit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
yp05327 marked this conversation as resolved.
Show resolved Hide resolved
// SPDX-License-Identifier: MIT

//go:build gogit

package git

import (
"io"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/google/licensecheck"
)

// GetLicenseStats calculates license stats for git repository at specified commit
func (repo *Repository) GetLicenseStats(commitID string) ([]string, error) {
r, err := git.PlainOpen(repo.Path)
if err != nil {
return nil, err
}

rev, err := r.ResolveRevision(plumbing.Revision(commitID))
if err != nil {
return nil, err
}

commit, err := r.CommitObject(*rev)
if err != nil {
return nil, err
}

tree, err := commit.Tree()
if err != nil {
return nil, err
}

var licenses []string
// TODO: fix unnecessary check
err = tree.Files().ForEach(func(f *object.File) error {
if f.Size == 0 {
return nil
}

// TODO: support ext
if f.Name == "LICENSE" {
r, err := f.Reader()
if err != nil {
return err
}
defer r.Close()

content, err := io.ReadAll(r)
if err != nil {
return err
}

cov := licensecheck.Scan(content)
for _, m := range cov.Match {
licenses = append(licenses, m.ID)
}
}
return nil
})

return licenses, nil
}
90 changes: 90 additions & 0 deletions modules/git/repo_license_nogogit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

//go:build !gogit

package git

import (
"bytes"
"io"

"code.gitea.io/gitea/modules/log"
"github.com/google/licensecheck"
)

// GetLicenseStats calculates language stats for git repository at specified commit
func (repo *Repository) GetLicenseStats(commitID string) ([]string, error) {
// We will feed the commit IDs in order into cat-file --batch, followed by blobs as necessary.
// so let's create a batch stdin and stdout
batchStdinWriter, batchReader, cancel := repo.CatFileBatch(repo.Ctx)
defer cancel()

writeID := func(id string) error {
_, err := batchStdinWriter.Write([]byte(id + "\n"))
return err
}

if err := writeID(commitID); err != nil {
return nil, err
}
shaBytes, typ, size, err := ReadBatchLine(batchReader)
if typ != "commit" {
log.Debug("Unable to get commit for: %s. Err: %v", commitID, err)
return nil, ErrNotExist{commitID, ""}
}

sha, err := NewIDFromString(string(shaBytes))
if err != nil {
log.Debug("Unable to get commit for: %s. Err: %v", commitID, err)
return nil, ErrNotExist{commitID, ""}
}

commit, err := CommitFromReader(repo, sha, io.LimitReader(batchReader, size))
if err != nil {
log.Debug("Unable to get commit for: %s. Err: %v", commitID, err)
return nil, err
}
if _, err = batchReader.Discard(1); err != nil {
return nil, err
}

tree := commit.Tree

entries, err := tree.ListEntriesRecursiveWithSize()
if err != nil {
return nil, err
}

var licenses []string
for _, f := range entries {
if f.Size() == 0 {
continue
}

// TODO: support ext
if f.Name() == "LICENSE" {
// Need to read all of them or can not get the right result from licensecheck.Scan
if err := writeID(f.ID.String()); err != nil {
return nil, err
}
_, _, size, err := ReadBatchLine(batchReader)
if err != nil {
log.Debug("Error reading blob: %s Err: %v", f.ID.String(), err)
return nil, err
}
contentBuf := bytes.Buffer{}
_, err = contentBuf.ReadFrom(io.LimitReader(batchReader, size))
if err != nil {
return nil, err
}
cov := licensecheck.Scan(contentBuf.Bytes())
for _, m := range cov.Match {
licenses = append(licenses, m.ID)
}
break
}
}

return licenses, nil
}
Loading