-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Support repo license #24872
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
Changes from 146 commits
43129a1
33ec600
7d3f1d5
8acb46e
a4e0deb
5a75aab
973f995
a876b2d
a2ef8f6
c4c6da5
0278775
c30bca1
aa91765
84801d5
0421e98
cf02888
10b2a90
f285720
7eb4584
8ba16c0
0637b87
facd698
af1b031
30c25ba
bc005dd
2f870f7
3518a63
0a9e4e8
763f104
f481624
07e91bf
744a032
93d2c7f
ea2f23c
92d8df7
ac84422
0dc4c3e
ef340f6
63a6da8
b9c2329
48da7a6
a000c38
fa8e347
7a01d3b
1cb2020
58b4cc2
c58162e
f2933cc
388bc26
7f2b5d0
4cf79be
f45d592
84c7b98
3da5a80
d0e5676
1b0783c
25966eb
59198d3
4a838ad
866de8d
27cb436
e46fd13
c831ee3
ae27d02
a79cb78
c6cc8b4
ea2c0be
d193276
e67a771
6f25294
44a8ece
376b91a
4ec4a0f
50150b8
9cbde72
f7da797
3a99e01
2904f34
e38161b
7c7bdea
2b56a5a
b168d1b
7640268
7a9d605
7e2589c
16822cd
fc93550
d6d2524
d57592f
d6f7006
8369e93
3c554a5
f546aae
05c844d
87ae3a6
24b1c13
cbe1515
f65eeec
e70bb25
498ea78
83945ed
bae33d5
ba60195
71ad15e
91dda74
fe1f087
462f7f5
a335732
df3e168
3abe77e
7a6c3b3
640a5e4
d6ba4af
8a1c767
7781e7d
cd0b44c
c57d542
44cf8e4
648d0a0
b45f907
dac32ce
21b1a7a
cd06857
5ec4b50
a5d9763
9178d90
093e71d
1386780
c5296f4
121f2b2
c97caf8
bb46e01
727d5cc
da36554
3e45a9a
7621d98
d501891
8e0929e
db2e9eb
030c0dd
91c5454
5e72897
3ef4aa8
80934c3
cc3cf07
aabb200
a0e8a5d
86f8bec
841453d
d46b98e
baa29d4
d92bbab
7d92e47
c79fbaa
fe77ff5
882767c
a0a7e9a
11b5e09
2ec2083
ce75061
51a5bce
7582a66
60bfdea
49ab83b
46e1246
f434251
c218be0
00b182c
ccde201
5059a0f
75cf5a2
0eb8545
0e3d512
8dc1c3b
be50529
2f1af68
f87f857
2910c9c
60aba0c
d5ca326
a938795
0f00895
fa0119b
cfe8cb5
0717cb6
051fdff
1043d39
da313ff
875daf8
4e18872
8991c24
f1ddccf
47e7c58
a1d98a3
8f5a70d
ae61ccd
286a889
dd95d1b
d26f2a0
cdbdb3e
45fcea6
da6a1cf
bd2b334
35f0800
2403c80
4e76dbf
22f7c41
317eef9
eca31c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] # empty |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package v1_22 //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) INDEX NOT NULL"` | ||
CommitID string | ||
License string `xorm:"VARCHAR(50) UNIQUE(s) INDEX NOT NULL"` | ||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX CREATED"` | ||
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX UPDATED"` | ||
} | ||
|
||
return x.Sync(new(RepoLicense)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// 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" | ||
"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) INDEX NOT NULL"` | ||
CommitID string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the commit_id for? Why not a branch name here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is used for checking existing licenses when updating licenses: |
||
License string `xorm:"VARCHAR(50) UNIQUE(s) INDEX NOT NULL"` | ||
6543 marked this conversation as resolved.
Show resolved
Hide resolved
yp05327 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
} |
Uh oh!
There was an error while loading. Please reload this page.