Refactored and fixed migration tests. (#16714)

Co-authored-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
KN4CK3R 2021-08-18 02:47:18 +02:00 committed by GitHub
parent 65c6acccf6
commit 422c30d315
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 744 additions and 485 deletions

View File

@ -181,7 +181,7 @@ func (g *GiteaDownloader) GetMilestones() ([]*base.Milestone, error) {
for i := range ms { for i := range ms {
// old gitea instances dont have this information // old gitea instances dont have this information
createdAT := time.Now() createdAT := time.Time{}
var updatedAT *time.Time var updatedAT *time.Time
if ms[i].Closed != nil { if ms[i].Closed != nil {
createdAT = *ms[i].Closed createdAT = *ms[i].Closed
@ -548,11 +548,11 @@ func (g *GiteaDownloader) GetPullRequests(page, perPage int) ([]*base.PullReques
assignees = append(assignees, pr.Assignees[i].UserName) assignees = append(assignees, pr.Assignees[i].UserName)
} }
createdAt := time.Now() createdAt := time.Time{}
if pr.Created != nil { if pr.Created != nil {
createdAt = *pr.Created createdAt = *pr.Created
} }
updatedAt := time.Now() updatedAt := time.Time{}
if pr.Created != nil { if pr.Created != nil {
updatedAt = *pr.Updated updatedAt = *pr.Updated
} }

View File

@ -6,7 +6,6 @@ package migrations
import ( import (
"context" "context"
"fmt"
"net/http" "net/http"
"os" "os"
"sort" "sort"
@ -18,29 +17,6 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func assertEqualIssue(t *testing.T, issueExp, issueGet *base.Issue) {
assert.EqualValues(t, issueExp.Number, issueGet.Number)
assert.EqualValues(t, issueExp.Title, issueGet.Title)
assert.EqualValues(t, issueExp.Content, issueGet.Content)
assert.EqualValues(t, issueExp.Milestone, issueGet.Milestone)
assert.EqualValues(t, issueExp.PosterID, issueGet.PosterID)
assert.EqualValues(t, issueExp.PosterName, issueGet.PosterName)
assert.EqualValues(t, issueExp.PosterEmail, issueGet.PosterEmail)
assert.EqualValues(t, issueExp.IsLocked, issueGet.IsLocked)
assert.EqualValues(t, issueExp.Created.Unix(), issueGet.Created.Unix())
assert.EqualValues(t, issueExp.Updated.Unix(), issueGet.Updated.Unix())
if issueExp.Closed != nil {
assert.EqualValues(t, issueExp.Closed.Unix(), issueGet.Closed.Unix())
} else {
assert.True(t, issueGet.Closed == nil)
}
sort.Strings(issueExp.Assignees)
sort.Strings(issueGet.Assignees)
assert.EqualValues(t, issueExp.Assignees, issueGet.Assignees)
assert.EqualValues(t, issueExp.Labels, issueGet.Labels)
assert.EqualValues(t, issueExp.Reactions, issueGet.Reactions)
}
func TestGiteaDownloadRepo(t *testing.T) { func TestGiteaDownloadRepo(t *testing.T) {
// Skip tests if Gitea token is not found // Skip tests if Gitea token is not found
giteaToken := os.Getenv("GITEA_TOKEN") giteaToken := os.Getenv("GITEA_TOKEN")
@ -63,7 +39,7 @@ func TestGiteaDownloadRepo(t *testing.T) {
repo, err := downloader.GetRepoInfo() repo, err := downloader.GetRepoInfo()
assert.NoError(t, err) assert.NoError(t, err)
assert.EqualValues(t, &base.Repository{ assertRepositoryEqual(t, &base.Repository{
Name: "test_repo", Name: "test_repo",
Owner: "gitea", Owner: "gitea",
IsPrivate: false, IsPrivate: false,
@ -80,54 +56,57 @@ func TestGiteaDownloadRepo(t *testing.T) {
labels, err := downloader.GetLabels() labels, err := downloader.GetLabels()
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, labels, 6) assertLabelsEqual(t, []*base.Label{
for _, l := range labels { {
switch l.Name { Name: "Bug",
case "Bug": Color: "e11d21",
assertLabelEqual(t, "Bug", "e11d21", "", l) },
case "documentation": {
assertLabelEqual(t, "Enhancement", "207de5", "", l) Name: "Enhancement",
case "confirmed": Color: "207de5",
assertLabelEqual(t, "Feature", "0052cc", "a feature request", l) },
case "enhancement": {
assertLabelEqual(t, "Invalid", "d4c5f9", "", l) Name: "Feature",
case "critical": Color: "0052cc",
assertLabelEqual(t, "Question", "fbca04", "", l) Description: "a feature request",
case "discussion": },
assertLabelEqual(t, "Valid", "53e917", "", l) {
default: Name: "Invalid",
assert.Error(t, fmt.Errorf("unexpected label: %s", l.Name)) Color: "d4c5f9",
} },
} {
Name: "Question",
Color: "fbca04",
},
{
Name: "Valid",
Color: "53e917",
},
}, labels)
milestones, err := downloader.GetMilestones() milestones, err := downloader.GetMilestones()
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, milestones, 2) assertMilestonesEqual(t, []*base.Milestone{
{
for _, milestone := range milestones { Title: "V2 Finalize",
switch milestone.Title { Created: time.Unix(0, 0),
case "V1": Deadline: timePtr(time.Unix(1599263999, 0)),
assert.EqualValues(t, "Generate Content", milestone.Description) Updated: timePtr(time.Unix(0, 0)),
// assert.EqualValues(t, "ToDo", milestone.Created) State: "open",
// assert.EqualValues(t, "ToDo", milestone.Updated) },
assert.EqualValues(t, 1598985406, milestone.Closed.Unix()) {
assert.True(t, milestone.Deadline == nil) Title: "V1",
assert.EqualValues(t, "closed", milestone.State) Description: "Generate Content",
case "V2 Finalize": Created: time.Unix(0, 0),
assert.EqualValues(t, "", milestone.Description) Updated: timePtr(time.Unix(0, 0)),
// assert.EqualValues(t, "ToDo", milestone.Created) Closed: timePtr(time.Unix(1598985406, 0)),
// assert.EqualValues(t, "ToDo", milestone.Updated) State: "closed",
assert.True(t, milestone.Closed == nil) },
assert.EqualValues(t, 1599263999, milestone.Deadline.Unix()) }, milestones)
assert.EqualValues(t, "open", milestone.State)
default:
assert.Error(t, fmt.Errorf("unexpected milestone: %s", milestone.Title))
}
}
releases, err := downloader.GetReleases() releases, err := downloader.GetReleases()
assert.NoError(t, err) assert.NoError(t, err)
assert.EqualValues(t, []*base.Release{ assertReleasesEqual(t, []*base.Release{
{ {
Name: "Second Release", Name: "Second Release",
TagName: "v2-rc1", TagName: "v2-rc1",
@ -139,7 +118,7 @@ func TestGiteaDownloadRepo(t *testing.T) {
Published: time.Date(2020, 9, 1, 18, 2, 43, 0, time.UTC), Published: time.Date(2020, 9, 1, 18, 2, 43, 0, time.UTC),
PublisherID: 689, PublisherID: 689,
PublisherName: "6543", PublisherName: "6543",
PublisherEmail: "6543@noreply.gitea.io", PublisherEmail: "6543@obermui.de",
}, },
{ {
Name: "First Release", Name: "First Release",
@ -152,95 +131,85 @@ func TestGiteaDownloadRepo(t *testing.T) {
Published: time.Date(2020, 9, 1, 17, 30, 32, 0, time.UTC), Published: time.Date(2020, 9, 1, 17, 30, 32, 0, time.UTC),
PublisherID: 689, PublisherID: 689,
PublisherName: "6543", PublisherName: "6543",
PublisherEmail: "6543@noreply.gitea.io", PublisherEmail: "6543@obermui.de",
}, },
}, releases) }, releases)
issues, isEnd, err := downloader.GetIssues(1, 50) issues, isEnd, err := downloader.GetIssues(1, 50)
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, issues, 7)
assert.True(t, isEnd) assert.True(t, isEnd)
assert.Len(t, issues, 7)
assert.EqualValues(t, "open", issues[0].State) assert.EqualValues(t, "open", issues[0].State)
issues, isEnd, err = downloader.GetIssues(3, 2) issues, isEnd, err = downloader.GetIssues(3, 2)
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, issues, 2)
assert.False(t, isEnd) assert.False(t, isEnd)
var ( assertIssuesEqual(t, []*base.Issue{
closed4 = time.Date(2020, 9, 1, 15, 49, 34, 0, time.UTC) {
closed2 = time.Unix(1598969497, 0) Number: 4,
) Title: "what is this repo about?",
Content: "",
assertEqualIssue(t, &base.Issue{ Milestone: "V1",
Number: 4, PosterID: -1,
Title: "what is this repo about?", PosterName: "Ghost",
Content: "", PosterEmail: "",
Milestone: "V1", State: "closed",
PosterID: -1, IsLocked: true,
PosterName: "Ghost", Created: time.Unix(1598975321, 0),
PosterEmail: "", Updated: time.Unix(1598975400, 0),
State: "closed", Labels: []*base.Label{{
IsLocked: true, Name: "Question",
Created: time.Unix(1598975321, 0), Color: "fbca04",
Updated: time.Unix(1598975400, 0), Description: "",
Labels: []*base.Label{{ }},
Name: "Question", Reactions: []*base.Reaction{
Color: "fbca04", {
Description: "", UserID: 689,
}}, UserName: "6543",
Reactions: []*base.Reaction{ Content: "gitea",
{ },
UserID: 689, {
UserName: "6543", UserID: 689,
Content: "gitea", UserName: "6543",
}, Content: "laugh",
{ },
UserID: 689,
UserName: "6543",
Content: "laugh",
}, },
Closed: timePtr(time.Date(2020, 9, 1, 15, 49, 34, 0, time.UTC)),
}, },
Closed: &closed4, {
}, issues[0]) Number: 2,
assertEqualIssue(t, &base.Issue{ Title: "Spam",
Number: 2, Content: ":(",
Title: "Spam", Milestone: "",
Content: ":(", PosterID: 689,
Milestone: "", PosterName: "6543",
PosterID: 689, PosterEmail: "6543@obermui.de",
PosterName: "6543", State: "closed",
PosterEmail: "6543@noreply.gitea.io", IsLocked: false,
State: "closed", Created: time.Unix(1598919780, 0),
IsLocked: false, Updated: time.Unix(1598969497, 0),
Created: time.Unix(1598919780, 0), Labels: []*base.Label{{
Updated: closed2, Name: "Invalid",
Labels: []*base.Label{{ Color: "d4c5f9",
Name: "Invalid", Description: "",
Color: "d4c5f9", }},
Description: "", Closed: timePtr(time.Unix(1598969497, 0)),
}}, },
Reactions: nil, }, issues)
Closed: &closed2,
}, issues[1])
comments, _, err := downloader.GetComments(base.GetCommentOptions{ comments, _, err := downloader.GetComments(base.GetCommentOptions{
IssueNumber: 4, IssueNumber: 4,
}) })
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, comments, 2) assertCommentsEqual(t, []*base.Comment{
assert.EqualValues(t, 1598975370, comments[0].Created.Unix())
assert.EqualValues(t, 1599070865, comments[0].Updated.Unix())
assert.EqualValues(t, 1598975393, comments[1].Created.Unix())
assert.EqualValues(t, 1598975393, comments[1].Updated.Unix())
assert.EqualValues(t, []*base.Comment{
{ {
IssueIndex: 4, IssueIndex: 4,
PosterID: 689, PosterID: 689,
PosterName: "6543", PosterName: "6543",
PosterEmail: "6543@noreply.gitea.io", PosterEmail: "6543@obermui.de",
Created: comments[0].Created, Created: time.Unix(1598975370, 0),
Updated: comments[0].Updated, Updated: time.Unix(1599070865, 0),
Content: "a really good question!\n\nIt is the used as TESTSET for gitea2gitea repo migration function", Content: "a really good question!\n\nIt is the used as TESTSET for gitea2gitea repo migration function",
}, },
{ {
@ -248,8 +217,8 @@ func TestGiteaDownloadRepo(t *testing.T) {
PosterID: -1, PosterID: -1,
PosterName: "Ghost", PosterName: "Ghost",
PosterEmail: "", PosterEmail: "",
Created: comments[1].Created, Created: time.Unix(1598975393, 0),
Updated: comments[1].Updated, Updated: time.Unix(1598975393, 0),
Content: "Oh!", Content: "Oh!",
}, },
}, comments) }, comments)
@ -262,12 +231,11 @@ func TestGiteaDownloadRepo(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.False(t, isEnd) assert.False(t, isEnd)
assert.Len(t, prs, 3) assert.Len(t, prs, 3)
merged12 := time.Unix(1598982934, 0) assertPullRequestEqual(t, &base.PullRequest{
assertEqualPulls(t, &base.PullRequest{
Number: 12, Number: 12,
PosterID: 689, PosterID: 689,
PosterName: "6543", PosterName: "6543",
PosterEmail: "6543@noreply.gitea.io", PosterEmail: "6543@obermui.de",
Title: "Dont Touch", Title: "Dont Touch",
Content: "\r\nadd dont touch note", Content: "\r\nadd dont touch note",
Milestone: "V2 Finalize", Milestone: "V2 Finalize",
@ -275,10 +243,8 @@ func TestGiteaDownloadRepo(t *testing.T) {
IsLocked: false, IsLocked: false,
Created: time.Unix(1598982759, 0), Created: time.Unix(1598982759, 0),
Updated: time.Unix(1599023425, 0), Updated: time.Unix(1599023425, 0),
Closed: &merged12, Closed: timePtr(time.Unix(1598982934, 0)),
Assignees: []string{"techknowlogick"}, Assignees: []string{"techknowlogick"},
Labels: []*base.Label{},
Base: base.PullRequestBranch{ Base: base.PullRequestBranch{
CloneURL: "", CloneURL: "",
Ref: "master", Ref: "master",
@ -294,74 +260,59 @@ func TestGiteaDownloadRepo(t *testing.T) {
OwnerName: "6543-forks", OwnerName: "6543-forks",
}, },
Merged: true, Merged: true,
MergedTime: &merged12, MergedTime: timePtr(time.Unix(1598982934, 0)),
MergeCommitSHA: "827aa28a907853e5ddfa40c8f9bc52471a2685fd", MergeCommitSHA: "827aa28a907853e5ddfa40c8f9bc52471a2685fd",
PatchURL: "https://gitea.com/gitea/test_repo/pulls/12.patch", PatchURL: "https://gitea.com/gitea/test_repo/pulls/12.patch",
}, prs[1]) }, prs[1])
reviews, err := downloader.GetReviews(7) reviews, err := downloader.GetReviews(7)
assert.NoError(t, err) assert.NoError(t, err)
if assert.Len(t, reviews, 3) { assertReviewsEqual(t, []*base.Review{
assert.EqualValues(t, 689, reviews[0].ReviewerID) {
assert.EqualValues(t, "6543", reviews[0].ReviewerName) ID: 1770,
assert.EqualValues(t, "techknowlogick", reviews[1].ReviewerName) IssueIndex: 7,
assert.EqualValues(t, "techknowlogick", reviews[2].ReviewerName) ReviewerID: 689,
assert.False(t, reviews[1].Official) ReviewerName: "6543",
assert.EqualValues(t, "I think this needs some changes", reviews[1].Content) CommitID: "187ece0cb6631e2858a6872e5733433bb3ca3b03",
assert.EqualValues(t, "REQUEST_CHANGES", reviews[1].State) CreatedAt: time.Date(2020, 9, 1, 16, 12, 58, 0, time.UTC),
assert.True(t, reviews[2].Official) State: "COMMENT", // TODO
assert.EqualValues(t, "looks good", reviews[2].Content) Comments: []*base.ReviewComment{
assert.EqualValues(t, "APPROVED", reviews[2].State) {
ID: 116561,
// TODO: https://github.com/go-gitea/gitea/issues/12846 InReplyTo: 0,
// assert.EqualValues(t, 9, reviews[1].ReviewerID) Content: "is one `\\newline` to less?",
// assert.EqualValues(t, 9, reviews[2].ReviewerID) TreePath: "README.md",
DiffHunk: "@@ -2,3 +2,3 @@\n \n-Test repository for testing migration from gitea 2 gitea\n\\ No newline at end of file\n+Test repository for testing migration from gitea 2 gitea",
assert.Len(t, reviews[0].Comments, 1) Position: 0,
assert.EqualValues(t, &base.ReviewComment{ Line: 4,
ID: 116561, CommitID: "187ece0cb6631e2858a6872e5733433bb3ca3b03",
InReplyTo: 0, PosterID: 689,
Content: "is one `\\newline` to less?", Reactions: nil,
TreePath: "README.md", CreatedAt: time.Date(2020, 9, 1, 16, 12, 58, 0, time.UTC),
DiffHunk: "@@ -2,3 +2,3 @@\n \n-Test repository for testing migration from gitea 2 gitea\n\\ No newline at end of file\n+Test repository for testing migration from gitea 2 gitea", UpdatedAt: time.Date(2020, 9, 1, 16, 12, 58, 0, time.UTC),
Position: 0, },
Line: 4, },
CommitID: "187ece0cb6631e2858a6872e5733433bb3ca3b03", },
PosterID: 689, {
Reactions: nil, ID: 1771,
CreatedAt: time.Date(2020, 9, 1, 16, 12, 58, 0, time.UTC), IssueIndex: 7,
UpdatedAt: time.Date(2020, 9, 1, 16, 12, 58, 0, time.UTC), ReviewerID: 9,
}, reviews[0].Comments[0]) ReviewerName: "techknowlogick",
} CommitID: "187ece0cb6631e2858a6872e5733433bb3ca3b03",
} CreatedAt: time.Date(2020, 9, 1, 17, 6, 47, 0, time.UTC),
State: "REQUEST_CHANGES", // TODO
func assertEqualPulls(t *testing.T, pullExp, pullGet *base.PullRequest) { Content: "I think this needs some changes",
assertEqualIssue(t, pull2issue(pullExp), pull2issue(pullGet)) },
assert.EqualValues(t, 0, pullGet.OriginalNumber) {
assert.EqualValues(t, pullExp.PatchURL, pullGet.PatchURL) ID: 1772,
assert.EqualValues(t, pullExp.Merged, pullGet.Merged) IssueIndex: 7,
assert.EqualValues(t, pullExp.MergedTime.Unix(), pullGet.MergedTime.Unix()) ReviewerID: 9,
assert.EqualValues(t, pullExp.MergeCommitSHA, pullGet.MergeCommitSHA) ReviewerName: "techknowlogick",
assert.EqualValues(t, pullExp.Base, pullGet.Base) CommitID: "187ece0cb6631e2858a6872e5733433bb3ca3b03",
assert.EqualValues(t, pullExp.Head, pullGet.Head) CreatedAt: time.Date(2020, 9, 1, 17, 19, 51, 0, time.UTC),
} State: base.ReviewStateApproved,
Official: true,
func pull2issue(pull *base.PullRequest) *base.Issue { Content: "looks good",
return &base.Issue{ },
Number: pull.Number, }, reviews)
PosterID: pull.PosterID,
PosterName: pull.PosterName,
PosterEmail: pull.PosterEmail,
Title: pull.Title,
Content: pull.Content,
Milestone: pull.Milestone,
State: pull.State,
IsLocked: pull.IsLocked,
Created: pull.Created,
Updated: pull.Updated,
Closed: pull.Closed,
Labels: pull.Labels,
Reactions: pull.Reactions,
Assignees: pull.Assignees,
}
} }

View File

@ -165,11 +165,27 @@ func (g *GiteaLocalUploader) CreateMilestones(milestones ...*base.Milestone) err
if deadline == 0 { if deadline == 0 {
deadline = timeutil.TimeStamp(time.Date(9999, 1, 1, 0, 0, 0, 0, setting.DefaultUILocation).Unix()) deadline = timeutil.TimeStamp(time.Date(9999, 1, 1, 0, 0, 0, 0, setting.DefaultUILocation).Unix())
} }
if milestone.Created.IsZero() {
if milestone.Updated != nil {
milestone.Created = *milestone.Updated
} else if milestone.Deadline != nil {
milestone.Created = *milestone.Deadline
} else {
milestone.Created = time.Now()
}
}
if milestone.Updated == nil || milestone.Updated.IsZero() {
milestone.Updated = &milestone.Created
}
var ms = models.Milestone{ var ms = models.Milestone{
RepoID: g.repo.ID, RepoID: g.repo.ID,
Name: milestone.Title, Name: milestone.Title,
Content: milestone.Description, Content: milestone.Description,
IsClosed: milestone.State == "closed", IsClosed: milestone.State == "closed",
CreatedUnix: timeutil.TimeStamp(milestone.Created.Unix()),
UpdatedUnix: timeutil.TimeStamp(milestone.Updated.Unix()),
DeadlineUnix: deadline, DeadlineUnix: deadline,
} }
if ms.IsClosed && milestone.Closed != nil { if ms.IsClosed && milestone.Closed != nil {
@ -215,6 +231,14 @@ func (g *GiteaLocalUploader) CreateLabels(labels ...*base.Label) error {
func (g *GiteaLocalUploader) CreateReleases(releases ...*base.Release) error { func (g *GiteaLocalUploader) CreateReleases(releases ...*base.Release) error {
var rels = make([]*models.Release, 0, len(releases)) var rels = make([]*models.Release, 0, len(releases))
for _, release := range releases { for _, release := range releases {
if release.Created.IsZero() {
if !release.Published.IsZero() {
release.Created = release.Published
} else {
release.Created = time.Now()
}
}
var rel = models.Release{ var rel = models.Release{
RepoID: g.repo.ID, RepoID: g.repo.ID,
TagName: release.TagName, TagName: release.TagName,
@ -263,6 +287,13 @@ func (g *GiteaLocalUploader) CreateReleases(releases ...*base.Release) error {
} }
for _, asset := range release.Assets { for _, asset := range release.Assets {
if asset.Created.IsZero() {
if !asset.Updated.IsZero() {
asset.Created = asset.Updated
} else {
asset.Created = release.Created
}
}
var attach = models.Attachment{ var attach = models.Attachment{
UUID: gouuid.New().String(), UUID: gouuid.New().String(),
Name: asset.Name, Name: asset.Name,
@ -332,6 +363,21 @@ func (g *GiteaLocalUploader) CreateIssues(issues ...*base.Issue) error {
} }
} }
if issue.Created.IsZero() {
if issue.Closed != nil {
issue.Created = *issue.Closed
} else {
issue.Created = time.Now()
}
}
if issue.Updated.IsZero() {
if issue.Closed != nil {
issue.Updated = *issue.Closed
} else {
issue.Updated = time.Now()
}
}
var is = models.Issue{ var is = models.Issue{
RepoID: g.repo.ID, RepoID: g.repo.ID,
Repo: g.repo, Repo: g.repo,
@ -406,7 +452,7 @@ func (g *GiteaLocalUploader) CreateIssues(issues ...*base.Issue) error {
} }
for _, is := range iss { for _, is := range iss {
g.issues.Store(is.Index, is.ID) g.issues.Store(is.Index, is)
} }
} }
@ -417,16 +463,17 @@ func (g *GiteaLocalUploader) CreateIssues(issues ...*base.Issue) error {
func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error { func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error {
var cms = make([]*models.Comment, 0, len(comments)) var cms = make([]*models.Comment, 0, len(comments))
for _, comment := range comments { for _, comment := range comments {
var issueID int64 var issue *models.Issue
if issueIDStr, ok := g.issues.Load(comment.IssueIndex); !ok { issueInter, ok := g.issues.Load(comment.IssueIndex)
issue, err := models.GetIssueByIndex(g.repo.ID, comment.IssueIndex) if !ok {
var err error
issue, err = models.GetIssueByIndex(g.repo.ID, comment.IssueIndex)
if err != nil { if err != nil {
return err return err
} }
issueID = issue.ID g.issues.Store(comment.IssueIndex, issue)
g.issues.Store(comment.IssueIndex, issueID)
} else { } else {
issueID = issueIDStr.(int64) issue = issueInter.(*models.Issue)
} }
userid, ok := g.userMap[comment.PosterID] userid, ok := g.userMap[comment.PosterID]
@ -442,8 +489,15 @@ func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error {
} }
} }
if comment.Created.IsZero() {
comment.Created = time.Unix(int64(issue.CreatedUnix), 0)
}
if comment.Updated.IsZero() {
comment.Updated = comment.Created
}
cm := models.Comment{ cm := models.Comment{
IssueID: issueID, IssueID: issue.ID,
Type: models.CommentTypeComment, Type: models.CommentTypeComment,
Content: comment.Content, Content: comment.Content,
CreatedUnix: timeutil.TimeStamp(comment.Created.Unix()), CreatedUnix: timeutil.TimeStamp(comment.Created.Unix()),
@ -530,7 +584,7 @@ func (g *GiteaLocalUploader) CreatePullRequests(prs ...*base.PullRequest) error
return err return err
} }
for _, pr := range gprs { for _, pr := range gprs {
g.issues.Store(pr.Issue.Index, pr.Issue.ID) g.issues.Store(pr.Issue.Index, pr.Issue)
pull.AddToTaskQueue(pr) pull.AddToTaskQueue(pr)
} }
return nil return nil
@ -634,6 +688,19 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR
head = pr.Head.Ref head = pr.Head.Ref
} }
if pr.Created.IsZero() {
if pr.Closed != nil {
pr.Created = *pr.Closed
} else if pr.MergedTime != nil {
pr.Created = *pr.MergedTime
} else {
pr.Created = time.Now()
}
}
if pr.Updated.IsZero() {
pr.Updated = pr.Created
}
var issue = models.Issue{ var issue = models.Issue{
RepoID: g.repo.ID, RepoID: g.repo.ID,
Repo: g.repo, Repo: g.repo,
@ -743,16 +810,17 @@ func convertReviewState(state string) models.ReviewType {
func (g *GiteaLocalUploader) CreateReviews(reviews ...*base.Review) error { func (g *GiteaLocalUploader) CreateReviews(reviews ...*base.Review) error {
var cms = make([]*models.Review, 0, len(reviews)) var cms = make([]*models.Review, 0, len(reviews))
for _, review := range reviews { for _, review := range reviews {
var issueID int64 var issue *models.Issue
if issueIDStr, ok := g.issues.Load(review.IssueIndex); !ok { issueInter, ok := g.issues.Load(review.IssueIndex)
issue, err := models.GetIssueByIndex(g.repo.ID, review.IssueIndex) if !ok {
var err error
issue, err = models.GetIssueByIndex(g.repo.ID, review.IssueIndex)
if err != nil { if err != nil {
return err return err
} }
issueID = issue.ID g.issues.Store(review.IssueIndex, issue)
g.issues.Store(review.IssueIndex, issueID)
} else { } else {
issueID = issueIDStr.(int64) issue = issueInter.(*models.Issue)
} }
userid, ok := g.userMap[review.ReviewerID] userid, ok := g.userMap[review.ReviewerID]
@ -768,9 +836,13 @@ func (g *GiteaLocalUploader) CreateReviews(reviews ...*base.Review) error {
} }
} }
if review.CreatedAt.IsZero() {
review.CreatedAt = time.Unix(int64(issue.CreatedUnix), 0)
}
var cm = models.Review{ var cm = models.Review{
Type: convertReviewState(review.State), Type: convertReviewState(review.State),
IssueID: issueID, IssueID: issue.ID,
Content: review.Content, Content: review.Content,
Official: review.Official, Official: review.Official,
CreatedUnix: timeutil.TimeStamp(review.CreatedAt.Unix()), CreatedUnix: timeutil.TimeStamp(review.CreatedAt.Unix()),
@ -786,14 +858,14 @@ func (g *GiteaLocalUploader) CreateReviews(reviews ...*base.Review) error {
} }
// get pr // get pr
pr, ok := g.prCache[issueID] pr, ok := g.prCache[issue.ID]
if !ok { if !ok {
var err error var err error
pr, err = models.GetPullRequestByIssueIDWithNoAttributes(issueID) pr, err = models.GetPullRequestByIssueIDWithNoAttributes(issue.ID)
if err != nil { if err != nil {
return err return err
} }
g.prCache[issueID] = pr g.prCache[issue.ID] = pr
} }
for _, comment := range review.Comments { for _, comment := range review.Comments {
@ -824,10 +896,17 @@ func (g *GiteaLocalUploader) CreateReviews(reviews ...*base.Review) error {
patch, _ = git.CutDiffAroundLine(reader, int64((&models.Comment{Line: int64(line + comment.Position - 1)}).UnsignedLine()), line < 0, setting.UI.CodeCommentLines) patch, _ = git.CutDiffAroundLine(reader, int64((&models.Comment{Line: int64(line + comment.Position - 1)}).UnsignedLine()), line < 0, setting.UI.CodeCommentLines)
if comment.CreatedAt.IsZero() {
comment.CreatedAt = review.CreatedAt
}
if comment.UpdatedAt.IsZero() {
comment.UpdatedAt = comment.CreatedAt
}
var c = models.Comment{ var c = models.Comment{
Type: models.CommentTypeCode, Type: models.CommentTypeCode,
PosterID: comment.PosterID, PosterID: comment.PosterID,
IssueID: issueID, IssueID: issue.ID,
Content: comment.Content, Content: comment.Content,
Line: int64(line + comment.Position - 1), Line: int64(line + comment.Position - 1),
TreePath: comment.TreePath, TreePath: comment.TreePath,

View File

@ -70,7 +70,7 @@ func TestGiteaUploadRepo(t *testing.T) {
labels, err := models.GetLabelsByRepoID(repo.ID, "", models.ListOptions{}) labels, err := models.GetLabelsByRepoID(repo.ID, "", models.ListOptions{})
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, labels, 11) assert.Len(t, labels, 12)
releases, err := models.GetReleasesByRepoID(repo.ID, models.FindReleasesOptions{ releases, err := models.GetReleasesByRepoID(repo.ID, models.FindReleasesOptions{
ListOptions: models.ListOptions{ ListOptions: models.ListOptions{
@ -98,7 +98,7 @@ func TestGiteaUploadRepo(t *testing.T) {
SortType: "oldest", SortType: "oldest",
}) })
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, issues, 14) assert.Len(t, issues, 15)
assert.NoError(t, issues[0].LoadDiscussComments()) assert.NoError(t, issues[0].LoadDiscussComments())
assert.Empty(t, issues[0].Comments) assert.Empty(t, issues[0].Comments)
@ -106,7 +106,7 @@ func TestGiteaUploadRepo(t *testing.T) {
SortType: "oldest", SortType: "oldest",
}) })
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, pulls, 34) assert.Len(t, pulls, 30)
assert.NoError(t, pulls[0].LoadIssue()) assert.NoError(t, pulls[0].LoadIssue())
assert.NoError(t, pulls[0].Issue.LoadDiscussComments()) assert.NoError(t, pulls[0].Issue.LoadDiscussComments())
assert.Len(t, pulls[0].Issue.Comments, 2) assert.Len(t, pulls[0].Issue.Comments, 2)

View File

@ -16,53 +16,6 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func assertMilestoneEqual(t *testing.T, description, title, dueOn, created, updated, closed, state string, ms *base.Milestone) {
var tmPtr *time.Time
if dueOn != "" {
tm, err := time.Parse("2006-01-02 15:04:05 -0700 MST", dueOn)
assert.NoError(t, err)
tmPtr = &tm
}
var (
createdTM time.Time
updatedTM *time.Time
closedTM *time.Time
)
if created != "" {
var err error
createdTM, err = time.Parse("2006-01-02 15:04:05 -0700 MST", created)
assert.NoError(t, err)
}
if updated != "" {
updatedTemp, err := time.Parse("2006-01-02 15:04:05 -0700 MST", updated)
assert.NoError(t, err)
updatedTM = &updatedTemp
}
if closed != "" {
closedTemp, err := time.Parse("2006-01-02 15:04:05 -0700 MST", closed)
assert.NoError(t, err)
closedTM = &closedTemp
}
assert.EqualValues(t, &base.Milestone{
Description: description,
Title: title,
Deadline: tmPtr,
State: state,
Created: createdTM,
Updated: updatedTM,
Closed: closedTM,
}, ms)
}
func assertLabelEqual(t *testing.T, name, color, description string, label *base.Label) {
assert.EqualValues(t, &base.Label{
Name: name,
Color: color,
Description: description,
}, label)
}
func TestGitHubDownloadRepo(t *testing.T) { func TestGitHubDownloadRepo(t *testing.T) {
GithubLimitRateRemaining = 3 //Wait at 3 remaining since we could have 3 CI in // GithubLimitRateRemaining = 3 //Wait at 3 remaining since we could have 3 CI in //
downloader := NewGithubDownloaderV3(context.Background(), "https://github.com", "", "", os.Getenv("GITHUB_READ_TOKEN"), "go-gitea", "test_repo") downloader := NewGithubDownloaderV3(context.Background(), "https://github.com", "", "", os.Getenv("GITHUB_READ_TOKEN"), "go-gitea", "test_repo")
@ -71,7 +24,7 @@ func TestGitHubDownloadRepo(t *testing.T) {
repo, err := downloader.GetRepoInfo() repo, err := downloader.GetRepoInfo()
assert.NoError(t, err) assert.NoError(t, err)
assert.EqualValues(t, &base.Repository{ assertRepositoryEqual(t, &base.Repository{
Name: "test_repo", Name: "test_repo",
Owner: "go-gitea", Owner: "go-gitea",
Description: "Test repository for testing migration from github to gitea", Description: "Test repository for testing migration from github to gitea",
@ -86,52 +39,80 @@ func TestGitHubDownloadRepo(t *testing.T) {
milestones, err := downloader.GetMilestones() milestones, err := downloader.GetMilestones()
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, len(milestones) >= 2) assertMilestonesEqual(t, []*base.Milestone{
{
for _, milestone := range milestones { Title: "1.0.0",
switch milestone.Title { Description: "Milestone 1.0.0",
case "1.0.0": Deadline: timePtr(time.Date(2019, 11, 11, 8, 0, 0, 0, time.UTC)),
assertMilestoneEqual(t, "Milestone 1.0.0", "1.0.0", "2019-11-11 08:00:00 +0000 UTC", Created: time.Date(2019, 11, 12, 19, 37, 8, 0, time.UTC),
"2019-11-12 19:37:08 +0000 UTC", Updated: timePtr(time.Date(2019, 11, 12, 21, 56, 17, 0, time.UTC)),
"2019-11-12 21:56:17 +0000 UTC", Closed: timePtr(time.Date(2019, 11, 12, 19, 45, 49, 0, time.UTC)),
"2019-11-12 19:45:49 +0000 UTC", State: "closed",
"closed", milestone) },
case "1.1.0": {
assertMilestoneEqual(t, "Milestone 1.1.0", "1.1.0", "2019-11-12 08:00:00 +0000 UTC", Title: "1.1.0",
"2019-11-12 19:37:25 +0000 UTC", Description: "Milestone 1.1.0",
"2019-11-12 21:39:27 +0000 UTC", Deadline: timePtr(time.Date(2019, 11, 12, 8, 0, 0, 0, time.UTC)),
"2019-11-12 19:45:46 +0000 UTC", Created: time.Date(2019, 11, 12, 19, 37, 25, 0, time.UTC),
"closed", milestone) Updated: timePtr(time.Date(2019, 11, 12, 21, 39, 27, 0, time.UTC)),
} Closed: timePtr(time.Date(2019, 11, 12, 19, 45, 46, 0, time.UTC)),
} State: "closed",
},
}, milestones)
labels, err := downloader.GetLabels() labels, err := downloader.GetLabels()
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, len(labels) >= 8) assertLabelsEqual(t, []*base.Label{
for _, l := range labels { {
switch l.Name { Name: "bug",
case "bug": Color: "d73a4a",
assertLabelEqual(t, "bug", "d73a4a", "Something isn't working", l) Description: "Something isn't working",
case "documentation": },
assertLabelEqual(t, "documentation", "0075ca", "Improvements or additions to documentation", l) {
case "duplicate": Name: "documentation",
assertLabelEqual(t, "duplicate", "cfd3d7", "This issue or pull request already exists", l) Color: "0075ca",
case "enhancement": Description: "Improvements or additions to documentation",
assertLabelEqual(t, "enhancement", "a2eeef", "New feature or request", l) },
case "good first issue": {
assertLabelEqual(t, "good first issue", "7057ff", "Good for newcomers", l) Name: "duplicate",
case "help wanted": Color: "cfd3d7",
assertLabelEqual(t, "help wanted", "008672", "Extra attention is needed", l) Description: "This issue or pull request already exists",
case "invalid": },
assertLabelEqual(t, "invalid", "e4e669", "This doesn't seem right", l) {
case "question": Name: "enhancement",
assertLabelEqual(t, "question", "d876e3", "Further information is requested", l) Color: "a2eeef",
} Description: "New feature or request",
} },
{
Name: "good first issue",
Color: "7057ff",
Description: "Good for newcomers",
},
{
Name: "help wanted",
Color: "008672",
Description: "Extra attention is needed",
},
{
Name: "invalid",
Color: "e4e669",
Description: "This doesn't seem right",
},
{
Name: "question",
Color: "d876e3",
Description: "Further information is requested",
},
{
Name: "wontfix",
Color: "ffffff",
Description: "This will not be worked on",
},
}, labels)
releases, err := downloader.GetReleases() releases, err := downloader.GetReleases()
assert.NoError(t, err) assert.NoError(t, err)
assert.EqualValues(t, []*base.Release{ assertReleasesEqual(t, []*base.Release{
{ {
TagName: "v0.9.99", TagName: "v0.9.99",
TargetCommitish: "master", TargetCommitish: "master",
@ -142,19 +123,13 @@ func TestGitHubDownloadRepo(t *testing.T) {
PublisherID: 1669571, PublisherID: 1669571,
PublisherName: "mrsdizzie", PublisherName: "mrsdizzie",
}, },
}, releases[len(releases)-1:]) }, releases)
// downloader.GetIssues() // downloader.GetIssues()
issues, isEnd, err := downloader.GetIssues(1, 2) issues, isEnd, err := downloader.GetIssues(1, 2)
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, issues, 2)
assert.False(t, isEnd) assert.False(t, isEnd)
assertIssuesEqual(t, []*base.Issue{
var (
closed1 = time.Date(2019, 11, 12, 20, 22, 22, 0, time.UTC)
closed2 = time.Date(2019, 11, 12, 21, 1, 31, 0, time.UTC)
)
assert.EqualValues(t, []*base.Issue{
{ {
Number: 1, Number: 1,
Title: "Please add an animated gif icon to the merge button", Title: "Please add an animated gif icon to the merge button",
@ -184,7 +159,7 @@ func TestGitHubDownloadRepo(t *testing.T) {
Content: "+1", Content: "+1",
}, },
}, },
Closed: &closed1, Closed: timePtr(time.Date(2019, 11, 12, 20, 22, 22, 0, time.UTC)),
}, },
{ {
Number: 2, Number: 2,
@ -235,7 +210,7 @@ func TestGitHubDownloadRepo(t *testing.T) {
Content: "+1", Content: "+1",
}, },
}, },
Closed: &closed2, Closed: timePtr(time.Date(2019, 11, 12, 21, 1, 31, 0, time.UTC)),
}, },
}, issues) }, issues)
@ -244,8 +219,7 @@ func TestGitHubDownloadRepo(t *testing.T) {
IssueNumber: 2, IssueNumber: 2,
}) })
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, comments, 2) assertCommentsEqual(t, []*base.Comment{
assert.EqualValues(t, []*base.Comment{
{ {
IssueIndex: 2, IssueIndex: 2,
PosterID: 1669571, PosterID: 1669571,
@ -270,17 +244,12 @@ func TestGitHubDownloadRepo(t *testing.T) {
Content: "A second comment", Content: "A second comment",
Reactions: nil, Reactions: nil,
}, },
}, comments[:2]) }, comments)
// downloader.GetPullRequests() // downloader.GetPullRequests()
prs, _, err := downloader.GetPullRequests(1, 2) prs, _, err := downloader.GetPullRequests(1, 2)
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, prs, 2) assertPullRequestsEqual(t, []*base.PullRequest{
closed1 = time.Date(2019, 11, 12, 21, 39, 27, 0, time.UTC)
var merged1 = time.Date(2019, 11, 12, 21, 39, 27, 0, time.UTC)
assert.EqualValues(t, []*base.PullRequest{
{ {
Number: 3, Number: 3,
Title: "Update README.md", Title: "Update README.md",
@ -313,9 +282,9 @@ func TestGitHubDownloadRepo(t *testing.T) {
OwnerName: "go-gitea", OwnerName: "go-gitea",
RepoName: "test_repo", RepoName: "test_repo",
}, },
Closed: &closed1, Closed: timePtr(time.Date(2019, 11, 12, 21, 39, 27, 0, time.UTC)),
Merged: true, Merged: true,
MergedTime: &merged1, MergedTime: timePtr(time.Date(2019, 11, 12, 21, 39, 27, 0, time.UTC)),
MergeCommitSHA: "f32b0a9dfd09a60f616f29158f772cedd89942d2", MergeCommitSHA: "f32b0a9dfd09a60f616f29158f772cedd89942d2",
}, },
{ {
@ -368,7 +337,7 @@ func TestGitHubDownloadRepo(t *testing.T) {
reviews, err := downloader.GetReviews(3) reviews, err := downloader.GetReviews(3)
assert.NoError(t, err) assert.NoError(t, err)
assert.EqualValues(t, []*base.Review{ assertReviewsEqual(t, []*base.Review{
{ {
ID: 315859956, ID: 315859956,
IssueIndex: 3, IssueIndex: 3,
@ -400,7 +369,7 @@ func TestGitHubDownloadRepo(t *testing.T) {
reviews, err = downloader.GetReviews(4) reviews, err = downloader.GetReviews(4)
assert.NoError(t, err) assert.NoError(t, err)
assert.EqualValues(t, []*base.Review{ assertReviewsEqual(t, []*base.Review{
{ {
ID: 338338740, ID: 338338740,
IssueIndex: 4, IssueIndex: 4,

View File

@ -609,7 +609,7 @@ func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullReque
// GetReviews returns pull requests review // GetReviews returns pull requests review
func (g *GitlabDownloader) GetReviews(pullRequestNumber int64) ([]*base.Review, error) { func (g *GitlabDownloader) GetReviews(pullRequestNumber int64) ([]*base.Review, error) {
state, resp, err := g.client.MergeRequestApprovals.GetApprovalState(g.repoID, int(pullRequestNumber), gitlab.WithContext(g.ctx)) approvals, resp, err := g.client.MergeRequestApprovals.GetConfiguration(g.repoID, int(pullRequestNumber), gitlab.WithContext(g.ctx))
if err != nil { if err != nil {
if resp != nil && resp.StatusCode == 404 { if resp != nil && resp.StatusCode == 404 {
log.Error(fmt.Sprintf("GitlabDownloader: while migrating a error occurred: '%s'", err.Error())) log.Error(fmt.Sprintf("GitlabDownloader: while migrating a error occurred: '%s'", err.Error()))
@ -618,21 +618,12 @@ func (g *GitlabDownloader) GetReviews(pullRequestNumber int64) ([]*base.Review,
return nil, err return nil, err
} }
// GitLab's Approvals are equivalent to Gitea's approve reviews var reviews = make([]*base.Review, 0, len(approvals.ApprovedBy))
approvers := make(map[int]string) for _, user := range approvals.ApprovedBy {
for i := range state.Rules {
for u := range state.Rules[i].ApprovedBy {
approvers[state.Rules[i].ApprovedBy[u].ID] = state.Rules[i].ApprovedBy[u].Username
}
}
var reviews = make([]*base.Review, 0, len(approvers))
for id, name := range approvers {
reviews = append(reviews, &base.Review{ reviews = append(reviews, &base.Review{
ReviewerID: int64(id), ReviewerID: int64(user.User.ID),
ReviewerName: name, ReviewerName: user.User.Username,
// GitLab API doesn't return a creation date CreatedAt: *approvals.UpdatedAt,
CreatedAt: time.Now(),
// All we get are approvals // All we get are approvals
State: base.ReviewStateApproved, State: base.ReviewStateApproved,
}) })

View File

@ -36,7 +36,7 @@ func TestGitlabDownloadRepo(t *testing.T) {
repo, err := downloader.GetRepoInfo() repo, err := downloader.GetRepoInfo()
assert.NoError(t, err) assert.NoError(t, err)
// Repo Owner is blank in Gitlab Group repos // Repo Owner is blank in Gitlab Group repos
assert.EqualValues(t, &base.Repository{ assertRepositoryEqual(t, &base.Repository{
Name: "test_repo", Name: "test_repo",
Owner: "", Owner: "",
Description: "Test repository for testing migration from gitlab to gitea", Description: "Test repository for testing migration from gitlab to gitea",
@ -52,56 +52,66 @@ func TestGitlabDownloadRepo(t *testing.T) {
milestones, err := downloader.GetMilestones() milestones, err := downloader.GetMilestones()
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, len(milestones) >= 2) assertMilestonesEqual(t, []*base.Milestone{
{
for _, milestone := range milestones { Title: "1.1.0",
switch milestone.Title { Created: time.Date(2019, 11, 28, 8, 42, 44, 575000000, time.UTC),
case "1.0": Updated: timePtr(time.Date(2019, 11, 28, 8, 42, 44, 575000000, time.UTC)),
assertMilestoneEqual(t, "", "1.0", State: "active",
"", },
"2019-11-28 08:42:30.301 +0000 UTC", {
"2019-11-28 15:57:52.401 +0000 UTC", Title: "1.0.0",
"", Created: time.Date(2019, 11, 28, 8, 42, 30, 301000000, time.UTC),
"closed", milestone) Updated: timePtr(time.Date(2019, 11, 28, 15, 57, 52, 401000000, time.UTC)),
case "1.1.0": Closed: timePtr(time.Date(2019, 11, 28, 15, 57, 52, 401000000, time.UTC)),
assertMilestoneEqual(t, "", "1.1.0", State: "closed",
"", },
"2019-11-28 08:42:44.575 +0000 UTC", }, milestones)
"2019-11-28 08:42:44.575 +0000 UTC",
"",
"active", milestone)
}
}
labels, err := downloader.GetLabels() labels, err := downloader.GetLabels()
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, len(labels) >= 9) assertLabelsEqual(t, []*base.Label{
for _, l := range labels { {
switch l.Name { Name: "bug",
case "bug": Color: "d9534f",
assertLabelEqual(t, "bug", "d9534f", "", l) },
case "documentation": {
assertLabelEqual(t, "documentation", "f0ad4e", "", l) Name: "confirmed",
case "confirmed": Color: "d9534f",
assertLabelEqual(t, "confirmed", "d9534f", "", l) },
case "enhancement": {
assertLabelEqual(t, "enhancement", "5cb85c", "", l) Name: "critical",
case "critical": Color: "d9534f",
assertLabelEqual(t, "critical", "d9534f", "", l) },
case "discussion": {
assertLabelEqual(t, "discussion", "428bca", "", l) Name: "discussion",
case "suggestion": Color: "428bca",
assertLabelEqual(t, "suggestion", "428bca", "", l) },
case "support": {
assertLabelEqual(t, "support", "f0ad4e", "", l) Name: "documentation",
case "duplicate": Color: "f0ad4e",
assertLabelEqual(t, "duplicate", "7F8C8D", "", l) },
} {
} Name: "duplicate",
Color: "7f8c8d",
},
{
Name: "enhancement",
Color: "5cb85c",
},
{
Name: "suggestion",
Color: "428bca",
},
{
Name: "support",
Color: "f0ad4e",
},
}, labels)
releases, err := downloader.GetReleases() releases, err := downloader.GetReleases()
assert.NoError(t, err) assert.NoError(t, err)
assert.EqualValues(t, []*base.Release{ assertReleasesEqual(t, []*base.Release{
{ {
TagName: "v0.9.99", TagName: "v0.9.99",
TargetCommitish: "0720a3ec57c1f843568298117b874319e7deee75", TargetCommitish: "0720a3ec57c1f843568298117b874319e7deee75",
@ -111,18 +121,13 @@ func TestGitlabDownloadRepo(t *testing.T) {
PublisherID: 1241334, PublisherID: 1241334,
PublisherName: "lafriks", PublisherName: "lafriks",
}, },
}, releases[len(releases)-1:]) }, releases)
issues, isEnd, err := downloader.GetIssues(1, 2) issues, isEnd, err := downloader.GetIssues(1, 2)
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, issues, 2)
assert.False(t, isEnd) assert.False(t, isEnd)
var ( assertIssuesEqual(t, []*base.Issue{
closed1 = time.Date(2019, 11, 28, 8, 46, 23, 275000000, time.UTC)
closed2 = time.Date(2019, 11, 28, 8, 45, 44, 959000000, time.UTC)
)
assert.EqualValues(t, []*base.Issue{
{ {
Number: 1, Number: 1,
Title: "Please add an animated gif icon to the merge button", Title: "Please add an animated gif icon to the merge button",
@ -152,7 +157,7 @@ func TestGitlabDownloadRepo(t *testing.T) {
UserName: "lafriks", UserName: "lafriks",
Content: "open_mouth", Content: "open_mouth",
}}, }},
Closed: &closed1, Closed: timePtr(time.Date(2019, 11, 28, 8, 46, 23, 275000000, time.UTC)),
}, },
{ {
Number: 2, Number: 2,
@ -200,7 +205,7 @@ func TestGitlabDownloadRepo(t *testing.T) {
UserName: "lafriks", UserName: "lafriks",
Content: "hearts", Content: "hearts",
}}, }},
Closed: &closed2, Closed: timePtr(time.Date(2019, 11, 28, 8, 45, 44, 959000000, time.UTC)),
}, },
}, issues) }, issues)
@ -208,8 +213,7 @@ func TestGitlabDownloadRepo(t *testing.T) {
IssueNumber: 2, IssueNumber: 2,
}) })
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, comments, 4) assertCommentsEqual(t, []*base.Comment{
assert.EqualValues(t, []*base.Comment{
{ {
IssueIndex: 2, IssueIndex: 2,
PosterID: 1241334, PosterID: 1241334,
@ -242,13 +246,11 @@ func TestGitlabDownloadRepo(t *testing.T) {
Content: "A second comment", Content: "A second comment",
Reactions: nil, Reactions: nil,
}, },
}, comments[:4]) }, comments)
prs, _, err := downloader.GetPullRequests(1, 1) prs, _, err := downloader.GetPullRequests(1, 1)
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, prs, 1) assertPullRequestsEqual(t, []*base.PullRequest{
assert.EqualValues(t, []*base.PullRequest{
{ {
Number: 4, Number: 4,
OriginalNumber: 2, OriginalNumber: 2,
@ -296,27 +298,29 @@ func TestGitlabDownloadRepo(t *testing.T) {
rvs, err := downloader.GetReviews(1) rvs, err := downloader.GetReviews(1)
assert.NoError(t, err) assert.NoError(t, err)
if assert.Len(t, rvs, 2) { assertReviewsEqual(t, []*base.Review{
for i := range rvs { {
switch rvs[i].ReviewerID { ReviewerID: 4102996,
case 4102996: ReviewerName: "zeripath",
assert.EqualValues(t, "zeripath", rvs[i].ReviewerName) CreatedAt: time.Date(2019, 11, 28, 16, 02, 8, 377000000, time.UTC),
assert.EqualValues(t, "APPROVED", rvs[i].State) State: "APPROVED",
case 527793: },
assert.EqualValues(t, "axifive", rvs[i].ReviewerName) {
assert.EqualValues(t, "APPROVED", rvs[i].State) ReviewerID: 527793,
default: ReviewerName: "axifive",
t.Errorf("Unexpected Reviewer ID: %d", rvs[i].ReviewerID) CreatedAt: time.Date(2019, 11, 28, 16, 02, 8, 377000000, time.UTC),
State: "APPROVED",
},
}, rvs)
}
}
}
rvs, err = downloader.GetReviews(2) rvs, err = downloader.GetReviews(2)
assert.NoError(t, err) assert.NoError(t, err)
if assert.Len(t, prs, 1) { assertReviewsEqual(t, []*base.Review{
assert.EqualValues(t, 4575606, rvs[0].ReviewerID) {
assert.EqualValues(t, "real6543", rvs[0].ReviewerName) ReviewerID: 4575606,
assert.EqualValues(t, "APPROVED", rvs[0].State) ReviewerName: "real6543",
} CreatedAt: time.Date(2020, 04, 19, 19, 24, 21, 108000000, time.UTC),
State: "APPROVED",
},
}, rvs)
} }

View File

@ -146,16 +146,12 @@ func (g *GogsDownloader) GetMilestones() ([]*base.Milestone, error) {
return nil, err return nil, err
} }
t := time.Now()
for _, m := range ms { for _, m := range ms {
milestones = append(milestones, &base.Milestone{ milestones = append(milestones, &base.Milestone{
Title: m.Title, Title: m.Title,
Description: m.Description, Description: m.Description,
Deadline: m.Deadline, Deadline: m.Deadline,
State: string(m.State), State: string(m.State),
Created: t,
Updated: &t,
Closed: m.Closed, Closed: m.Closed,
}) })
} }
@ -294,12 +290,14 @@ func convertGogsIssue(issue *gogs.Issue) *base.Issue {
return &base.Issue{ return &base.Issue{
Title: issue.Title, Title: issue.Title,
Number: issue.Index, Number: issue.Index,
PosterID: issue.Poster.ID,
PosterName: issue.Poster.Login, PosterName: issue.Poster.Login,
PosterEmail: issue.Poster.Email, PosterEmail: issue.Poster.Email,
Content: issue.Body, Content: issue.Body,
Milestone: milestone, Milestone: milestone,
State: string(issue.State), State: string(issue.State),
Created: issue.Created, Created: issue.Created,
Updated: issue.Updated,
Labels: labels, Labels: labels,
Closed: closed, Closed: closed,
} }

View File

@ -34,65 +34,73 @@ func TestGogsDownloadRepo(t *testing.T) {
repo, err := downloader.GetRepoInfo() repo, err := downloader.GetRepoInfo()
assert.NoError(t, err) assert.NoError(t, err)
assert.EqualValues(t, &base.Repository{ assertRepositoryEqual(t, &base.Repository{
Name: "TESTREPO", Name: "TESTREPO",
Owner: "lunnytest", Owner: "lunnytest",
Description: "", Description: "",
CloneURL: "https://try.gogs.io/lunnytest/TESTREPO.git", CloneURL: "https://try.gogs.io/lunnytest/TESTREPO.git",
OriginalURL: "https://try.gogs.io/lunnytest/TESTREPO",
DefaultBranch: "master",
}, repo) }, repo)
milestones, err := downloader.GetMilestones() milestones, err := downloader.GetMilestones()
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, len(milestones) == 1) assertMilestonesEqual(t, []*base.Milestone{
{
for _, milestone := range milestones { Title: "1.0",
switch milestone.Title { State: "open",
case "1.0": },
assert.EqualValues(t, "open", milestone.State) }, milestones)
}
}
labels, err := downloader.GetLabels() labels, err := downloader.GetLabels()
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, labels, 7) assertLabelsEqual(t, []*base.Label{
for _, l := range labels { {
switch l.Name { Name: "bug",
case "bug": Color: "ee0701",
assertLabelEqual(t, "bug", "ee0701", "", l) },
case "duplicated": {
assertLabelEqual(t, "duplicated", "cccccc", "", l) Name: "duplicate",
case "enhancement": Color: "cccccc",
assertLabelEqual(t, "enhancement", "84b6eb", "", l) },
case "help wanted": {
assertLabelEqual(t, "help wanted", "128a0c", "", l) Name: "enhancement",
case "invalid": Color: "84b6eb",
assertLabelEqual(t, "invalid", "e6e6e6", "", l) },
case "question": {
assertLabelEqual(t, "question", "cc317c", "", l) Name: "help wanted",
case "wontfix": Color: "128a0c",
assertLabelEqual(t, "wontfix", "ffffff", "", l) },
} {
} Name: "invalid",
Color: "e6e6e6",
_, err = downloader.GetReleases() },
assert.Error(t, err) {
Name: "question",
Color: "cc317c",
},
{
Name: "wontfix",
Color: "ffffff",
},
}, labels)
// downloader.GetIssues() // downloader.GetIssues()
issues, isEnd, err := downloader.GetIssues(1, 8) issues, isEnd, err := downloader.GetIssues(1, 8)
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, issues, 1)
assert.False(t, isEnd) assert.False(t, isEnd)
assertIssuesEqual(t, []*base.Issue{
assert.EqualValues(t, []*base.Issue{
{ {
Number: 1, Number: 1,
PosterID: 5331,
PosterName: "lunny",
PosterEmail: "xiaolunwen@gmail.com",
Title: "test", Title: "test",
Content: "test", Content: "test",
Milestone: "", Milestone: "",
PosterName: "lunny",
PosterEmail: "xiaolunwen@gmail.com",
State: "open", State: "open",
Created: time.Date(2019, 06, 11, 8, 16, 44, 0, time.UTC), Created: time.Date(2019, 06, 11, 8, 16, 44, 0, time.UTC),
Updated: time.Date(2019, 10, 26, 11, 07, 2, 0, time.UTC),
Labels: []*base.Label{ Labels: []*base.Label{
{ {
Name: "bug", Name: "bug",
@ -107,14 +115,24 @@ func TestGogsDownloadRepo(t *testing.T) {
IssueNumber: 1, IssueNumber: 1,
}) })
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, comments, 1) assertCommentsEqual(t, []*base.Comment{
assert.EqualValues(t, []*base.Comment{
{ {
IssueIndex: 1,
PosterID: 5331,
PosterName: "lunny", PosterName: "lunny",
PosterEmail: "xiaolunwen@gmail.com", PosterEmail: "xiaolunwen@gmail.com",
Created: time.Date(2019, 06, 11, 8, 19, 50, 0, time.UTC), Created: time.Date(2019, 06, 11, 8, 19, 50, 0, time.UTC),
Updated: time.Date(2019, 06, 11, 8, 19, 50, 0, time.UTC), Updated: time.Date(2019, 06, 11, 8, 19, 50, 0, time.UTC),
Content: `1111`, Content: "1111",
},
{
IssueIndex: 1,
PosterID: 15822,
PosterName: "clacplouf",
PosterEmail: "test1234@dbn.re",
Created: time.Date(2019, 10, 26, 11, 7, 2, 0, time.UTC),
Updated: time.Date(2019, 10, 26, 11, 7, 2, 0, time.UTC),
Content: "88888888",
}, },
}, comments) }, comments)

View File

@ -8,10 +8,259 @@ package migrations
import ( import (
"path/filepath" "path/filepath"
"testing" "testing"
"time"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/migrations/base"
"github.com/stretchr/testify/assert"
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
models.MainTest(m, filepath.Join("..", "..")) models.MainTest(m, filepath.Join("..", ".."))
} }
func timePtr(t time.Time) *time.Time {
return &t
}
func assertTimeEqual(t *testing.T, expected, actual time.Time) {
assert.Equal(t, expected.UTC(), actual.UTC())
}
func assertTimePtrEqual(t *testing.T, expected, actual *time.Time) {
if expected == nil {
assert.Nil(t, actual)
} else {
assertTimeEqual(t, *expected, *actual)
}
}
func assertCommentEqual(t *testing.T, expected, actual *base.Comment) {
assert.Equal(t, expected.IssueIndex, actual.IssueIndex)
assert.Equal(t, expected.PosterID, actual.PosterID)
assert.Equal(t, expected.PosterName, actual.PosterName)
assert.Equal(t, expected.PosterEmail, actual.PosterEmail)
assertTimeEqual(t, expected.Created, actual.Created)
assertTimeEqual(t, expected.Updated, actual.Updated)
assert.Equal(t, expected.Content, actual.Content)
assertReactionsEqual(t, expected.Reactions, actual.Reactions)
}
func assertCommentsEqual(t *testing.T, expected, actual []*base.Comment) {
if assert.Len(t, actual, len(expected)) {
for i := range expected {
assertCommentEqual(t, expected[i], actual[i])
}
}
}
func assertLabelEqual(t *testing.T, expected, actual *base.Label) {
assert.Equal(t, expected.Name, actual.Name)
assert.Equal(t, expected.Color, actual.Color)
assert.Equal(t, expected.Description, actual.Description)
}
func assertLabelsEqual(t *testing.T, expected, actual []*base.Label) {
if assert.Len(t, actual, len(expected)) {
for i := range expected {
assertLabelEqual(t, expected[i], actual[i])
}
}
}
func assertMilestoneEqual(t *testing.T, expected, actual *base.Milestone) {
assert.Equal(t, expected.Title, actual.Title)
assert.Equal(t, expected.Description, actual.Description)
assertTimePtrEqual(t, expected.Deadline, actual.Deadline)
assertTimeEqual(t, expected.Created, actual.Created)
assertTimePtrEqual(t, expected.Updated, actual.Updated)
assertTimePtrEqual(t, expected.Closed, actual.Closed)
assert.Equal(t, expected.State, actual.State)
}
func assertMilestonesEqual(t *testing.T, expected, actual []*base.Milestone) {
if assert.Len(t, actual, len(expected)) {
for i := range expected {
assertMilestoneEqual(t, expected[i], actual[i])
}
}
}
func assertIssueEqual(t *testing.T, expected, actual *base.Issue) {
assert.Equal(t, expected.Number, actual.Number)
assert.Equal(t, expected.PosterID, actual.PosterID)
assert.Equal(t, expected.PosterName, actual.PosterName)
assert.Equal(t, expected.PosterEmail, actual.PosterEmail)
assert.Equal(t, expected.Title, actual.Title)
assert.Equal(t, expected.Content, actual.Content)
assert.Equal(t, expected.Ref, actual.Ref)
assert.Equal(t, expected.Milestone, actual.Milestone)
assert.Equal(t, expected.State, actual.State)
assert.Equal(t, expected.IsLocked, actual.IsLocked)
assertTimeEqual(t, expected.Created, actual.Created)
assertTimeEqual(t, expected.Updated, actual.Updated)
assertTimePtrEqual(t, expected.Closed, actual.Closed)
assertLabelsEqual(t, expected.Labels, actual.Labels)
assertReactionsEqual(t, expected.Reactions, actual.Reactions)
assert.ElementsMatch(t, expected.Assignees, actual.Assignees)
}
func assertIssuesEqual(t *testing.T, expected, actual []*base.Issue) {
if assert.Len(t, actual, len(expected)) {
for i := range expected {
assertIssueEqual(t, expected[i], actual[i])
}
}
}
func assertPullRequestEqual(t *testing.T, expected, actual *base.PullRequest) {
assert.Equal(t, expected.Number, actual.Number)
assert.Equal(t, expected.Title, actual.Title)
assert.Equal(t, expected.PosterID, actual.PosterID)
assert.Equal(t, expected.PosterName, actual.PosterName)
assert.Equal(t, expected.PosterEmail, actual.PosterEmail)
assert.Equal(t, expected.Content, actual.Content)
assert.Equal(t, expected.Milestone, actual.Milestone)
assert.Equal(t, expected.State, actual.State)
assertTimeEqual(t, expected.Created, actual.Created)
assertTimeEqual(t, expected.Updated, actual.Updated)
assertTimePtrEqual(t, expected.Closed, actual.Closed)
assertLabelsEqual(t, expected.Labels, actual.Labels)
assert.Equal(t, expected.PatchURL, actual.PatchURL)
assert.Equal(t, expected.Merged, actual.Merged)
assertTimePtrEqual(t, expected.MergedTime, actual.MergedTime)
assert.Equal(t, expected.MergeCommitSHA, actual.MergeCommitSHA)
assertPullRequestBranchEqual(t, expected.Head, actual.Head)
assertPullRequestBranchEqual(t, expected.Base, actual.Base)
assert.ElementsMatch(t, expected.Assignees, actual.Assignees)
assert.Equal(t, expected.IsLocked, actual.IsLocked)
assertReactionsEqual(t, expected.Reactions, actual.Reactions)
}
func assertPullRequestsEqual(t *testing.T, expected, actual []*base.PullRequest) {
if assert.Len(t, actual, len(expected)) {
for i := range expected {
assertPullRequestEqual(t, expected[i], actual[i])
}
}
}
func assertPullRequestBranchEqual(t *testing.T, expected, actual base.PullRequestBranch) {
assert.Equal(t, expected.CloneURL, actual.CloneURL)
assert.Equal(t, expected.Ref, actual.Ref)
assert.Equal(t, expected.SHA, actual.SHA)
assert.Equal(t, expected.RepoName, actual.RepoName)
assert.Equal(t, expected.OwnerName, actual.OwnerName)
}
func assertReactionEqual(t *testing.T, expected, actual *base.Reaction) {
assert.Equal(t, expected.UserID, actual.UserID)
assert.Equal(t, expected.UserName, actual.UserName)
assert.Equal(t, expected.Content, actual.Content)
}
func assertReactionsEqual(t *testing.T, expected, actual []*base.Reaction) {
if assert.Len(t, actual, len(expected)) {
for i := range expected {
assertReactionEqual(t, expected[i], actual[i])
}
}
}
func assertReleaseAssetEqual(t *testing.T, expected, actual *base.ReleaseAsset) {
assert.Equal(t, expected.ID, actual.ID)
assert.Equal(t, expected.Name, actual.Name)
assert.Equal(t, expected.ContentType, actual.ContentType)
assert.Equal(t, expected.Size, actual.Size)
assert.Equal(t, expected.DownloadCount, actual.DownloadCount)
assertTimeEqual(t, expected.Created, actual.Created)
assertTimeEqual(t, expected.Updated, actual.Updated)
assert.Equal(t, expected.DownloadURL, actual.DownloadURL)
}
func assertReleaseAssetsEqual(t *testing.T, expected, actual []*base.ReleaseAsset) {
if assert.Len(t, actual, len(expected)) {
for i := range expected {
assertReleaseAssetEqual(t, expected[i], actual[i])
}
}
}
func assertReleaseEqual(t *testing.T, expected, actual *base.Release) {
assert.Equal(t, expected.TagName, actual.TagName)
assert.Equal(t, expected.TargetCommitish, actual.TargetCommitish)
assert.Equal(t, expected.Name, actual.Name)
assert.Equal(t, expected.Body, actual.Body)
assert.Equal(t, expected.Draft, actual.Draft)
assert.Equal(t, expected.Prerelease, actual.Prerelease)
assert.Equal(t, expected.PublisherID, actual.PublisherID)
assert.Equal(t, expected.PublisherName, actual.PublisherName)
assert.Equal(t, expected.PublisherEmail, actual.PublisherEmail)
assertReleaseAssetsEqual(t, expected.Assets, actual.Assets)
assertTimeEqual(t, expected.Created, actual.Created)
assertTimeEqual(t, expected.Published, actual.Published)
}
func assertReleasesEqual(t *testing.T, expected, actual []*base.Release) {
if assert.Len(t, actual, len(expected)) {
for i := range expected {
assertReleaseEqual(t, expected[i], actual[i])
}
}
}
func assertRepositoryEqual(t *testing.T, expected, actual *base.Repository) {
assert.Equal(t, expected.Name, actual.Name)
assert.Equal(t, expected.Owner, actual.Owner)
assert.Equal(t, expected.IsPrivate, actual.IsPrivate)
assert.Equal(t, expected.IsMirror, actual.IsMirror)
assert.Equal(t, expected.Description, actual.Description)
assert.Equal(t, expected.CloneURL, actual.CloneURL)
assert.Equal(t, expected.OriginalURL, actual.OriginalURL)
assert.Equal(t, expected.DefaultBranch, actual.DefaultBranch)
}
func assertReviewEqual(t *testing.T, expected, actual *base.Review) {
assert.Equal(t, expected.ID, actual.ID)
assert.Equal(t, expected.IssueIndex, actual.IssueIndex)
assert.Equal(t, expected.ReviewerID, actual.ReviewerID)
assert.Equal(t, expected.ReviewerName, actual.ReviewerName)
assert.Equal(t, expected.Official, actual.Official)
assert.Equal(t, expected.CommitID, actual.CommitID)
assert.Equal(t, expected.Content, actual.Content)
assertTimeEqual(t, expected.CreatedAt, actual.CreatedAt)
assert.Equal(t, expected.State, actual.State)
assertReviewCommentsEqual(t, expected.Comments, actual.Comments)
}
func assertReviewsEqual(t *testing.T, expected, actual []*base.Review) {
if assert.Len(t, actual, len(expected)) {
for i := range expected {
assertReviewEqual(t, expected[i], actual[i])
}
}
}
func assertReviewCommentEqual(t *testing.T, expected, actual *base.ReviewComment) {
assert.Equal(t, expected.ID, actual.ID)
assert.Equal(t, expected.InReplyTo, actual.InReplyTo)
assert.Equal(t, expected.Content, actual.Content)
assert.Equal(t, expected.TreePath, actual.TreePath)
assert.Equal(t, expected.DiffHunk, actual.DiffHunk)
assert.Equal(t, expected.Position, actual.Position)
assert.Equal(t, expected.Line, actual.Line)
assert.Equal(t, expected.CommitID, actual.CommitID)
assert.Equal(t, expected.PosterID, actual.PosterID)
assertReactionsEqual(t, expected.Reactions, actual.Reactions)
assertTimeEqual(t, expected.CreatedAt, actual.CreatedAt)
assertTimeEqual(t, expected.UpdatedAt, actual.UpdatedAt)
}
func assertReviewCommentsEqual(t *testing.T, expected, actual []*base.ReviewComment) {
if assert.Len(t, actual, len(expected)) {
for i := range expected {
assertReviewCommentEqual(t, expected[i], actual[i])
}
}
}