Skip to content

Revisions (second try) #13313

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
70 changes: 70 additions & 0 deletions integrations/pull_revision_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package integrations

import (
"fmt"
"net/http"
"net/url"
"testing"
"time"

"code.gitea.io/gitea/models"
api "code.gitea.io/gitea/modules/structs"
"github.com/stretchr/testify/assert"
)

func TestPullRevisions(t *testing.T) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
session := loginUser(t, "user1")
testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
testEditFileToNewBranch(t, session, "user1", "repo1", "master", "base", "README.md", "Hello, World (Edited Twice)\n")

user1 := models.AssertExistsAndLoadBean(t, &models.User{
Name: "user1",
}).(*models.User)
repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{
OwnerID: user1.ID,
Name: "repo1",
}).(*models.Repository)

testEditFileToNewBranch(t, session, "user1", "repo1", "base", "head", "README.md", "Hello, World (Edited Once)\n")

// Use API to create a conflicting pr
token := getTokenForLoggedInUser(t, session)
req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls?token=%s", "user1", "repo1", token), &api.CreatePullRequestOption{
Head: "head",
Base: "base",
Title: "revisions",
})
session.MakeRequest(t, req, 201)

pr := models.AssertExistsAndLoadBean(t, &models.PullRequest{
HeadRepoID: repo1.ID,
BaseRepoID: repo1.ID,
HeadBranch: "head",
BaseBranch: "base",
}).(*models.PullRequest)

req = NewRequest(t, "GET", fmt.Sprintf("/user1/repo1/pulls/%d/revisions", pr.Index))
resp := session.MakeRequest(t, req, http.StatusOK)

htmlDoc := NewHTMLParser(t, resp.Body)
revisions := htmlDoc.doc.Find("td.revision")
assert.Equal(t, 1, len(revisions.Nodes), "The template has changed")

testEditFile(t, session, "user1", "repo1", "head", "README.md", "Revision2")

//Wait for AddTestPullRequestTask
time.Sleep(2 * time.Second)

req = NewRequest(t, "GET", fmt.Sprintf("/user1/repo1/pulls/%d/revisions", pr.Index))
resp = session.MakeRequest(t, req, http.StatusOK)

htmlDoc = NewHTMLParser(t, resp.Body)
revisions = htmlDoc.doc.Find("td.revision")
assert.Equal(t, 2, len(revisions.Nodes), "The template has changed")
})
}
35 changes: 25 additions & 10 deletions models/commit_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"time"

"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
Expand Down Expand Up @@ -249,6 +250,29 @@ type SignCommitWithStatuses struct {
*SignCommit
}

// ParseGitCommitWithStatus parse a simple git.Commit directly into SignCommitWithStatuses
func ParseGitCommitWithStatus(commit *git.Commit, repo *Repository, emailCache *map[string]*User, keyMapCache *map[string]bool) SignCommitWithStatuses {
withEmail := ValidateCommitWithEmails(commit, emailCache)
withSignature := ParseUserCommitWithSignature(withEmail, repo, keyMapCache)

return ParseCommitWithStatus(withSignature, repo)
}

// ParseCommitWithStatus checks the commit's latest status and calculates its worst status state
func ParseCommitWithStatus(commit SignCommit, repo *Repository) SignCommitWithStatuses {
signCommitWithStatus := SignCommitWithStatuses{
SignCommit: &commit,
}
statuses, err := GetLatestCommitStatus(repo, signCommitWithStatus.ID.String(), 0)
if err != nil {
log.Error("GetLatestCommitStatus: %v", err)
} else {
signCommitWithStatus.Status = CalcCommitStatus(statuses)
}

return signCommitWithStatus
}

// ParseCommitsWithStatus checks commits latest statuses and calculates its worst status state
func ParseCommitsWithStatus(oldCommits *list.List, repo *Repository) *list.List {
var (
Expand All @@ -258,17 +282,8 @@ func ParseCommitsWithStatus(oldCommits *list.List, repo *Repository) *list.List

for e != nil {
c := e.Value.(SignCommit)
commit := SignCommitWithStatuses{
SignCommit: &c,
}
statuses, err := GetLatestCommitStatus(repo, commit.ID.String(), 0)
if err != nil {
log.Error("GetLatestCommitStatus: %v", err)
} else {
commit.Status = CalcCommitStatus(statuses)
}

newCommits.PushBack(commit)
newCommits.PushBack(ParseCommitWithStatus(c, repo))
e = e.Next()
}
return newCommits
Expand Down
19 changes: 19 additions & 0 deletions models/fixtures/revision.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@


-
id: 1
index: 1
commit: 4a357436d925b5c974181ff12a994538ddc5a269
pr_id: 2
user_id: 1
created: 946684800
number_of_commits: 1

-
id: 2
index: 2
commit: 4a357436d925b5c974181ff12a994538ddc5a269
pr_id: 2
user_id: 98989897
created: 946684800
number_of_commits: 1
26 changes: 18 additions & 8 deletions models/gpg_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,23 @@ func verifyWithGPGSettings(gpgSettings *git.GPGSettings, sig *packet.Signature,
return nil
}

// ParseCommitsWithSignature checks if signaute of commits are corresponding to users gpg keys.
// ParseUserCommitWithSignature checks if signature of a commit is corresponding to users gpg keys.
func ParseUserCommitWithSignature(commit UserCommit, repository *Repository, keyMapCache *map[string]bool) SignCommit {
if keyMapCache == nil {
keyMapCache = &map[string]bool{}
}

signCommit := SignCommit{
UserCommit: &commit,
Verification: ParseCommitWithSignature(commit.Commit),
}

_ = CalculateTrustStatus(signCommit.Verification, repository, keyMapCache)

return signCommit
}

// ParseCommitsWithSignature checks if signature of commits are corresponding to users gpg keys.
func ParseCommitsWithSignature(oldCommits *list.List, repository *Repository) *list.List {
var (
newCommits = list.New()
Expand All @@ -835,14 +851,8 @@ func ParseCommitsWithSignature(oldCommits *list.List, repository *Repository) *l

for e != nil {
c := e.Value.(UserCommit)
signCommit := SignCommit{
UserCommit: &c,
Verification: ParseCommitWithSignature(c.Commit),
}

_ = CalculateTrustStatus(signCommit.Verification, repository, &keyMap)

newCommits.PushBack(signCommit)
newCommits.PushBack(ParseUserCommitWithSignature(c, repository, &keyMap))
e = e.Next()
}
return newCommits
Expand Down
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ var migrations = []Migration{
NewMigration("fix publisher ID for tag releases", fixPublisherIDforTagReleases),
// v157 -> v158
NewMigration("ensure repo topics are up-to-date", fixRepoTopics),
// v158 -> v159
NewMigration("create revision table", addRevisionTable),
}

// GetCurrentDBVersion returns the current db version
Expand Down
24 changes: 24 additions & 0 deletions models/migrations/v158.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package migrations

import (
"code.gitea.io/gitea/modules/timeutil"

"xorm.io/xorm"
)

func addRevisionTable(x *xorm.Engine) error {
type Revision struct {
ID int64 `xorm:"pk autoincr"`
Commit string `xorm:"NOT NULL"`
NumberOfCommits int64 `xorm:"NOT NULL"`
UserID int64 `xorm:"NOT NULL"`
PRID int64 `xorm:"pr_id NOT NULL"`
Index int64 `xorm:"NOT NULL"`
Created timeutil.TimeStamp `xorm:"NOT NULL"`
}
return x.Sync2(new(Revision))
}
1 change: 1 addition & 0 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func init() {
new(Action),
new(Issue),
new(PullRequest),
new(Revision),
new(Comment),
new(Attachment),
new(Label),
Expand Down
Loading