Skip to content

Commit 19b1d19

Browse files
committed
Merge remote-tracking branch 'giteaofficial/main'
* giteaofficial/main: Fix protected branch files detection on pre_receive hook (go-gitea#31778) Add signature support for the RPM module (go-gitea#27069) Fix null requested_reviewer from API (go-gitea#31773)
2 parents bba3608 + df7f1c2 commit 19b1d19

File tree

16 files changed

+253
-35
lines changed

16 files changed

+253
-35
lines changed

custom/conf/app.example.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2555,7 +2555,8 @@ LEVEL = Info
25552555
;LIMIT_SIZE_SWIFT = -1
25562556
;; Maximum size of a Vagrant upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
25572557
;LIMIT_SIZE_VAGRANT = -1
2558-
2558+
;; Enable RPM re-signing by default. (It will overwrite the old signature ,using v4 format, not compatible with CentOS 6 or older)
2559+
;DEFAULT_RPM_SIGN_ENABLED = false
25592560
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25602561
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25612562
;; default storage for attachments, lfs and avatars

models/issues/pull.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ type PullRequest struct {
165165
Issue *Issue `xorm:"-"`
166166
Index int64
167167
RequestedReviewers []*user_model.User `xorm:"-"`
168+
RequestedReviewersTeams []*org_model.Team `xorm:"-"`
168169
isRequestedReviewersLoaded bool `xorm:"-"`
169170

170171
HeadRepoID int64 `xorm:"INDEX"`
@@ -305,7 +306,28 @@ func (pr *PullRequest) LoadRequestedReviewers(ctx context.Context) error {
305306
}
306307
pr.isRequestedReviewersLoaded = true
307308
for _, review := range reviews {
308-
pr.RequestedReviewers = append(pr.RequestedReviewers, review.Reviewer)
309+
if review.ReviewerID != 0 {
310+
pr.RequestedReviewers = append(pr.RequestedReviewers, review.Reviewer)
311+
}
312+
}
313+
314+
return nil
315+
}
316+
317+
// LoadRequestedReviewersTeams loads the requested reviewers teams.
318+
func (pr *PullRequest) LoadRequestedReviewersTeams(ctx context.Context) error {
319+
reviews, err := GetReviewsByIssueID(ctx, pr.Issue.ID)
320+
if err != nil {
321+
return err
322+
}
323+
if err = reviews.LoadReviewersTeams(ctx); err != nil {
324+
return err
325+
}
326+
327+
for _, review := range reviews {
328+
if review.ReviewerTeamID != 0 {
329+
pr.RequestedReviewersTeams = append(pr.RequestedReviewersTeams, review.ReviewerTeam)
330+
}
309331
}
310332

311333
return nil

models/issues/review_list.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88

99
"code.gitea.io/gitea/models/db"
10+
organization_model "code.gitea.io/gitea/models/organization"
1011
user_model "code.gitea.io/gitea/models/user"
1112
"code.gitea.io/gitea/modules/container"
1213
"code.gitea.io/gitea/modules/optional"
@@ -37,6 +38,34 @@ func (reviews ReviewList) LoadReviewers(ctx context.Context) error {
3738
return nil
3839
}
3940

41+
// LoadReviewersTeams loads reviewers teams
42+
func (reviews ReviewList) LoadReviewersTeams(ctx context.Context) error {
43+
reviewersTeamsIDs := make([]int64, 0)
44+
for _, review := range reviews {
45+
if review.ReviewerTeamID != 0 {
46+
reviewersTeamsIDs = append(reviewersTeamsIDs, review.ReviewerTeamID)
47+
}
48+
}
49+
50+
teamsMap := make(map[int64]*organization_model.Team, 0)
51+
for _, teamID := range reviewersTeamsIDs {
52+
team, err := organization_model.GetTeamByID(ctx, teamID)
53+
if err != nil {
54+
return err
55+
}
56+
57+
teamsMap[teamID] = team
58+
}
59+
60+
for _, review := range reviews {
61+
if review.ReviewerTeamID != 0 {
62+
review.ReviewerTeam = teamsMap[review.ReviewerTeamID]
63+
}
64+
}
65+
66+
return nil
67+
}
68+
4069
func (reviews ReviewList) LoadIssues(ctx context.Context) error {
4170
issueIDs := container.FilterSlice(reviews, func(review *Review) (int64, bool) {
4271
return review.IssueID, true

modules/git/commit_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package git
55

66
import (
7+
"context"
8+
"os"
79
"path/filepath"
810
"strings"
911
"testing"
@@ -345,3 +347,18 @@ func TestGetCommitFileStatusMerges(t *testing.T) {
345347
assert.Equal(t, commitFileStatus.Removed, expected.Removed)
346348
assert.Equal(t, commitFileStatus.Modified, expected.Modified)
347349
}
350+
351+
func Test_GetCommitBranchStart(t *testing.T) {
352+
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
353+
repo, err := OpenRepository(context.Background(), bareRepo1Path)
354+
assert.NoError(t, err)
355+
defer repo.Close()
356+
commit, err := repo.GetBranchCommit("branch1")
357+
assert.NoError(t, err)
358+
assert.EqualValues(t, "2839944139e0de9737a044f78b0e4b40d989a9e3", commit.ID.String())
359+
360+
startCommitID, err := repo.GetCommitBranchStart(os.Environ(), "branch1", commit.ID.String())
361+
assert.NoError(t, err)
362+
assert.NotEmpty(t, startCommitID)
363+
assert.EqualValues(t, "9c9aef8dd84e02bc7ec12641deb4c930a7c30185", startCommitID)
364+
}

modules/git/diff.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,17 @@ func CutDiffAroundLine(originalDiff io.Reader, line int64, old bool, numbersOfLi
271271
}
272272

273273
// GetAffectedFiles returns the affected files between two commits
274-
func GetAffectedFiles(repo *Repository, oldCommitID, newCommitID string, env []string) ([]string, error) {
274+
func GetAffectedFiles(repo *Repository, branchName, oldCommitID, newCommitID string, env []string) ([]string, error) {
275+
if oldCommitID == emptySha1ObjectID.String() || oldCommitID == emptySha256ObjectID.String() {
276+
startCommitID, err := repo.GetCommitBranchStart(env, branchName, newCommitID)
277+
if err != nil {
278+
return nil, err
279+
}
280+
if startCommitID == "" {
281+
return nil, fmt.Errorf("cannot find the start commit of %s", newCommitID)
282+
}
283+
oldCommitID = startCommitID
284+
}
275285
stdoutReader, stdoutWriter, err := os.Pipe()
276286
if err != nil {
277287
log.Error("Unable to create os.Pipe for %s", repo.Path)

modules/git/repo_commit.go

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package git
77
import (
88
"bytes"
99
"io"
10+
"os"
1011
"strconv"
1112
"strings"
1213

@@ -414,7 +415,7 @@ func (repo *Repository) commitsBefore(id ObjectID, limit int) ([]*Commit, error)
414415

415416
commits := make([]*Commit, 0, len(formattedLog))
416417
for _, commit := range formattedLog {
417-
branches, err := repo.getBranches(commit, 2)
418+
branches, err := repo.getBranches(os.Environ(), commit.ID.String(), 2)
418419
if err != nil {
419420
return nil, err
420421
}
@@ -437,12 +438,15 @@ func (repo *Repository) getCommitsBeforeLimit(id ObjectID, num int) ([]*Commit,
437438
return repo.commitsBefore(id, num)
438439
}
439440

440-
func (repo *Repository) getBranches(commit *Commit, limit int) ([]string, error) {
441+
func (repo *Repository) getBranches(env []string, commitID string, limit int) ([]string, error) {
441442
if DefaultFeatures().CheckVersionAtLeast("2.7.0") {
442443
stdout, _, err := NewCommand(repo.Ctx, "for-each-ref", "--format=%(refname:strip=2)").
443444
AddOptionFormat("--count=%d", limit).
444-
AddOptionValues("--contains", commit.ID.String(), BranchPrefix).
445-
RunStdString(&RunOpts{Dir: repo.Path})
445+
AddOptionValues("--contains", commitID, BranchPrefix).
446+
RunStdString(&RunOpts{
447+
Dir: repo.Path,
448+
Env: env,
449+
})
446450
if err != nil {
447451
return nil, err
448452
}
@@ -451,7 +455,10 @@ func (repo *Repository) getBranches(commit *Commit, limit int) ([]string, error)
451455
return branches, nil
452456
}
453457

454-
stdout, _, err := NewCommand(repo.Ctx, "branch").AddOptionValues("--contains", commit.ID.String()).RunStdString(&RunOpts{Dir: repo.Path})
458+
stdout, _, err := NewCommand(repo.Ctx, "branch").AddOptionValues("--contains", commitID).RunStdString(&RunOpts{
459+
Dir: repo.Path,
460+
Env: env,
461+
})
455462
if err != nil {
456463
return nil, err
457464
}
@@ -513,3 +520,35 @@ func (repo *Repository) AddLastCommitCache(cacheKey, fullName, sha string) error
513520
}
514521
return nil
515522
}
523+
524+
func (repo *Repository) GetCommitBranchStart(env []string, branch, endCommitID string) (string, error) {
525+
cmd := NewCommand(repo.Ctx, "log", prettyLogFormat)
526+
cmd.AddDynamicArguments(endCommitID)
527+
528+
stdout, _, runErr := cmd.RunStdBytes(&RunOpts{
529+
Dir: repo.Path,
530+
Env: env,
531+
})
532+
if runErr != nil {
533+
return "", runErr
534+
}
535+
536+
parts := bytes.Split(bytes.TrimSpace(stdout), []byte{'\n'})
537+
538+
var startCommitID string
539+
for _, commitID := range parts {
540+
branches, err := repo.getBranches(env, string(commitID), 2)
541+
if err != nil {
542+
return "", err
543+
}
544+
for _, b := range branches {
545+
if b != branch {
546+
return startCommitID, nil
547+
}
548+
}
549+
550+
startCommitID = string(commitID)
551+
}
552+
553+
return "", nil
554+
}

modules/git/repo_commit_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package git
55

66
import (
7+
"os"
78
"path/filepath"
89
"testing"
910

@@ -31,7 +32,7 @@ func TestRepository_GetCommitBranches(t *testing.T) {
3132
for _, testCase := range testCases {
3233
commit, err := bareRepo1.GetCommit(testCase.CommitID)
3334
assert.NoError(t, err)
34-
branches, err := bareRepo1.getBranches(commit, 2)
35+
branches, err := bareRepo1.getBranches(os.Environ(), commit.ID.String(), 2)
3536
assert.NoError(t, err)
3637
assert.Equal(t, testCase.ExpectedBranches, branches)
3738
}

modules/setting/packages.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ var (
4242
LimitSizeRubyGems int64
4343
LimitSizeSwift int64
4444
LimitSizeVagrant int64
45+
46+
DefaultRPMSignEnabled bool
4547
}{
4648
Enabled: true,
4749
LimitTotalOwnerCount: -1,
@@ -97,6 +99,7 @@ func loadPackagesFrom(rootCfg ConfigProvider) (err error) {
9799
Packages.LimitSizeRubyGems = mustBytes(sec, "LIMIT_SIZE_RUBYGEMS")
98100
Packages.LimitSizeSwift = mustBytes(sec, "LIMIT_SIZE_SWIFT")
99101
Packages.LimitSizeVagrant = mustBytes(sec, "LIMIT_SIZE_VAGRANT")
102+
Packages.DefaultRPMSignEnabled = sec.Key("DEFAULT_RPM_SIGN_ENABLED").MustBool(false)
100103
return nil
101104
}
102105

modules/structs/pull.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@ import (
99

1010
// PullRequest represents a pull request
1111
type PullRequest struct {
12-
ID int64 `json:"id"`
13-
URL string `json:"url"`
14-
Index int64 `json:"number"`
15-
Poster *User `json:"user"`
16-
Title string `json:"title"`
17-
Body string `json:"body"`
18-
Labels []*Label `json:"labels"`
19-
Milestone *Milestone `json:"milestone"`
20-
Assignee *User `json:"assignee"`
21-
Assignees []*User `json:"assignees"`
22-
RequestedReviewers []*User `json:"requested_reviewers"`
23-
State StateType `json:"state"`
24-
Draft bool `json:"draft"`
25-
IsLocked bool `json:"is_locked"`
26-
Comments int `json:"comments"`
12+
ID int64 `json:"id"`
13+
URL string `json:"url"`
14+
Index int64 `json:"number"`
15+
Poster *User `json:"user"`
16+
Title string `json:"title"`
17+
Body string `json:"body"`
18+
Labels []*Label `json:"labels"`
19+
Milestone *Milestone `json:"milestone"`
20+
Assignee *User `json:"assignee"`
21+
Assignees []*User `json:"assignees"`
22+
RequestedReviewers []*User `json:"requested_reviewers"`
23+
RequestedReviewersTeams []*Team `json:"requested_reviewers_teams"`
24+
State StateType `json:"state"`
25+
Draft bool `json:"draft"`
26+
IsLocked bool `json:"is_locked"`
27+
Comments int `json:"comments"`
2728
// number of review comments made on the diff of a PR review (not including comments on commits or issues in a PR)
2829
ReviewComments int `json:"review_comments"`
2930
Additions int `json:"additions"`

routers/api/packages/rpm/rpm.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,21 @@ func UploadPackageFile(ctx *context.Context) {
133133
}
134134
defer buf.Close()
135135

136+
// if rpm sign enabled
137+
if setting.Packages.DefaultRPMSignEnabled || ctx.FormBool("sign") {
138+
pri, _, err := rpm_service.GetOrCreateKeyPair(ctx, ctx.Package.Owner.ID)
139+
if err != nil {
140+
apiError(ctx, http.StatusInternalServerError, err)
141+
return
142+
}
143+
buf, err = rpm_service.SignPackage(buf, pri)
144+
if err != nil {
145+
// Not in rpm format, parsing failed.
146+
apiError(ctx, http.StatusBadRequest, err)
147+
return
148+
}
149+
}
150+
136151
pck, err := rpm_module.ParsePackage(buf)
137152
if err != nil {
138153
if errors.Is(err, util.ErrInvalidArgument) {
@@ -142,7 +157,6 @@ func UploadPackageFile(ctx *context.Context) {
142157
}
143158
return
144159
}
145-
146160
if _, err := buf.Seek(0, io.SeekStart); err != nil {
147161
apiError(ctx, http.StatusInternalServerError, err)
148162
return

routers/private/hook_pre_receive.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r
235235

236236
globs := protectBranch.GetProtectedFilePatterns()
237237
if len(globs) > 0 {
238-
_, err := pull_service.CheckFileProtection(gitRepo, oldCommitID, newCommitID, globs, 1, ctx.env)
238+
_, err := pull_service.CheckFileProtection(gitRepo, branchName, oldCommitID, newCommitID, globs, 1, ctx.env)
239239
if err != nil {
240240
if !models.IsErrFilePathProtected(err) {
241241
log.Error("Unable to check file protection for commits from %s to %s in %-v: %v", oldCommitID, newCommitID, repo, err)
@@ -293,7 +293,7 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r
293293
// Allow commits that only touch unprotected files
294294
globs := protectBranch.GetUnprotectedFilePatterns()
295295
if len(globs) > 0 {
296-
unprotectedFilesOnly, err := pull_service.CheckUnprotectedFiles(gitRepo, oldCommitID, newCommitID, globs, ctx.env)
296+
unprotectedFilesOnly, err := pull_service.CheckUnprotectedFiles(gitRepo, branchName, oldCommitID, newCommitID, globs, ctx.env)
297297
if err != nil {
298298
log.Error("Unable to check file protection for commits from %s to %s in %-v: %v", oldCommitID, newCommitID, repo, err)
299299
ctx.JSON(http.StatusInternalServerError, private.Response{

services/convert/pull.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,25 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
106106
log.Error("LoadRequestedReviewers[%d]: %v", pr.ID, err)
107107
return nil
108108
}
109+
if err = pr.LoadRequestedReviewersTeams(ctx); err != nil {
110+
log.Error("LoadRequestedReviewersTeams[%d]: %v", pr.ID, err)
111+
return nil
112+
}
113+
109114
for _, reviewer := range pr.RequestedReviewers {
110115
apiPullRequest.RequestedReviewers = append(apiPullRequest.RequestedReviewers, ToUser(ctx, reviewer, nil))
111116
}
112117

118+
for _, reviewerTeam := range pr.RequestedReviewersTeams {
119+
convertedTeam, err := ToTeam(ctx, reviewerTeam, true)
120+
if err != nil {
121+
log.Error("LoadRequestedReviewersTeams[%d]: %v", pr.ID, err)
122+
return nil
123+
}
124+
125+
apiPullRequest.RequestedReviewersTeams = append(apiPullRequest.RequestedReviewersTeams, convertedTeam)
126+
}
127+
113128
if pr.Issue.ClosedUnix != 0 {
114129
apiPullRequest.Closed = pr.Issue.ClosedUnix.AsTimePtr()
115130
}

0 commit comments

Comments
 (0)