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

Add migrate from OneDev #16356

Merged
merged 26 commits into from
Aug 21, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
8a0ff92
Use context to simplify logic.
KN4CK3R Jul 5, 2021
c0661b9
Added migration from OneDev.
KN4CK3R Jul 6, 2021
e17edc0
Use "Pull Requests".
KN4CK3R Jul 6, 2021
1394141
Fixed test.
KN4CK3R Jul 6, 2021
15b903e
Changed comments.
KN4CK3R Jul 7, 2021
5962cf6
Use typed context.
KN4CK3R Aug 8, 2021
2febe35
Merge branch 'main' of https://github.com/go-gitea/gitea into feature…
KN4CK3R Aug 8, 2021
86d648f
Added suggestions.
KN4CK3R Aug 12, 2021
f8ab470
Merge branch 'main' of https://github.com/go-gitea/gitea into feature…
KN4CK3R Aug 12, 2021
eeb652b
Fixed broken pull requests.
KN4CK3R Aug 12, 2021
3afbe25
Migrate labels.
KN4CK3R Aug 12, 2021
ee381c0
Added tests.
KN4CK3R Aug 12, 2021
b78e222
Update modules/migrations/onedev_test.go
6543 Aug 12, 2021
2ce860b
Merge branch 'main' into feature-migrate-onedev
6543 Aug 12, 2021
0602dc8
Use unix constructor.
KN4CK3R Aug 13, 2021
2580cae
Merge branch 'main' into feature-migrate-onedev
6543 Aug 13, 2021
b6ba145
Updated comments.
KN4CK3R Aug 14, 2021
23212aa
Merge branch 'main' of https://github.com/go-gitea/gitea into feature…
KN4CK3R Aug 15, 2021
94b1dcc
Added missing service.
KN4CK3R Aug 17, 2021
2b5b150
Merge branch 'main' of https://github.com/go-gitea/gitea into feature…
KN4CK3R Aug 17, 2021
783c378
Merge branch 'main' of https://github.com/go-gitea/gitea into feature…
KN4CK3R Aug 18, 2021
f34557e
Merge branch 'main' into feature-migrate-onedev
6543 Aug 18, 2021
e870a16
Merge branch 'main' into feature-migrate-onedev
6543 Aug 20, 2021
76dd48e
Merge branch 'main' into feature-migrate-onedev
techknowlogick Aug 21, 2021
91d0141
Merge branch 'main' into feature-migrate-onedev
6543 Aug 21, 2021
a4ce603
Merge branch 'main' into feature-migrate-onedev
6543 Aug 21, 2021
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
8 changes: 3 additions & 5 deletions modules/migrations/base/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@ type IssueContext interface {
}

// BasicIssueContext is a 1:1 mapping between local and foreign ids.
type BasicIssueContext struct {
ID int64
}
type BasicIssueContext int64

// LocalID gets the local id.
func (c BasicIssueContext) LocalID() int64 {
return c.ID
return int64(c)
}

// ForeignID gets the foreign id.
func (c BasicIssueContext) ForeignID() int64 {
return c.ID
return int64(c)
}

// Issue is a standard issue information
Expand Down
4 changes: 2 additions & 2 deletions modules/migrations/gitea_downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func (g *GiteaDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, err
Labels: labels,
Assignees: assignees,
IsLocked: issue.IsLocked,
Context: base.BasicIssueContext{ID: issue.Index},
Context: base.BasicIssueContext(issue.Index),
})
}

Expand Down Expand Up @@ -596,7 +596,7 @@ func (g *GiteaDownloader) GetPullRequests(page, perPage int) ([]*base.PullReques
RepoName: g.repoName,
OwnerName: g.repoOwner,
},
Context: base.BasicIssueContext{ID: pr.Index},
Context: base.BasicIssueContext(pr.Index),
})
}

Expand Down
4 changes: 2 additions & 2 deletions modules/migrations/gitea_downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func TestGiteaDownloadRepo(t *testing.T) {
}, issues[1])

comments, _, err := downloader.GetComments(base.GetCommentOptions{
Context: base.BasicIssueContext{ID: 4},
Context: base.BasicIssueContext(4),
})
assert.NoError(t, err)
assert.Len(t, comments, 2)
Expand Down Expand Up @@ -299,7 +299,7 @@ func TestGiteaDownloadRepo(t *testing.T) {
PatchURL: "https://gitea.com/gitea/test_repo/pulls/12.patch",
}, prs[1])

reviews, err := downloader.GetReviews(base.BasicIssueContext{ID: 7})
reviews, err := downloader.GetReviews(base.BasicIssueContext(7))
assert.NoError(t, err)
if assert.Len(t, reviews, 3) {
assert.EqualValues(t, 689, reviews[0].ReviewerID)
Expand Down
19 changes: 10 additions & 9 deletions modules/migrations/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,9 @@ func (g *GithubDownloaderV3) GetIssues(page, perPage int) ([]*base.Issue, bool,
Labels: labels,
Reactions: reactions,
Closed: issue.ClosedAt,
IsLocked: *issue.Locked,
Context: base.BasicIssueContext{ID: int64(*issue.Number)},
IsLocked: issue.GetLocked(),
Assignees: assignees,
Context: base.BasicIssueContext(*issue.Number),
})
}

Expand Down Expand Up @@ -486,12 +487,12 @@ func (g *GithubDownloaderV3) getComments(issueContext base.IssueContext) ([]*bas

allComments = append(allComments, &base.Comment{
IssueIndex: issueContext.LocalID(),
PosterID: *comment.User.ID,
PosterName: *comment.User.Login,
PosterEmail: email,
Content: *comment.Body,
Created: *comment.CreatedAt,
Updated: *comment.UpdatedAt,
PosterID: comment.GetUser().GetID(),
PosterName: comment.GetUser().GetLogin(),
PosterEmail: comment.GetUser().GetEmail(),
Content: comment.GetBody(),
Created: comment.GetCreatedAt(),
Updated: comment.GetUpdatedAt(),
Reactions: reactions,
})
}
Expand Down Expand Up @@ -651,7 +652,7 @@ func (g *GithubDownloaderV3) GetPullRequests(page, perPage int) ([]*base.PullReq
},
PatchURL: pr.GetPatchURL(),
Reactions: reactions,
Context: base.BasicIssueContext{ID: int64(*pr.Number)},
Context: base.BasicIssueContext(*pr.Number),
})
}

Expand Down
14 changes: 7 additions & 7 deletions modules/migrations/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func TestGitHubDownloadRepo(t *testing.T) {
},
},
Closed: &closed1,
Context: base.BasicIssueContext{ID: 1},
Context: base.BasicIssueContext(1),
},
{
Number: 2,
Expand Down Expand Up @@ -237,13 +237,13 @@ func TestGitHubDownloadRepo(t *testing.T) {
},
},
Closed: &closed2,
Context: base.BasicIssueContext{ID: 2},
Context: base.BasicIssueContext(2),
},
}, issues)

// downloader.GetComments()
comments, _, err := downloader.GetComments(base.GetCommentOptions{
Context: base.BasicIssueContext{ID: 2},
Context: base.BasicIssueContext(2),
})
assert.NoError(t, err)
assert.Len(t, comments, 2)
Expand Down Expand Up @@ -319,7 +319,7 @@ func TestGitHubDownloadRepo(t *testing.T) {
Merged: true,
MergedTime: &merged1,
MergeCommitSHA: "f32b0a9dfd09a60f616f29158f772cedd89942d2",
Context: base.BasicIssueContext{ID: 3},
Context: base.BasicIssueContext(3),
},
{
Number: 4,
Expand Down Expand Up @@ -366,11 +366,11 @@ func TestGitHubDownloadRepo(t *testing.T) {
Content: "+1",
},
},
Context: base.BasicIssueContext{ID: 4},
Context: base.BasicIssueContext(4),
},
}, prs)

reviews, err := downloader.GetReviews(base.BasicIssueContext{ID: 3})
reviews, err := downloader.GetReviews(base.BasicIssueContext(3))
assert.NoError(t, err)
assert.EqualValues(t, []*base.Review{
{
Expand Down Expand Up @@ -402,7 +402,7 @@ func TestGitHubDownloadRepo(t *testing.T) {
},
}, reviews)

reviews, err = downloader.GetReviews(base.BasicIssueContext{ID: 4})
reviews, err = downloader.GetReviews(base.BasicIssueContext(4))
assert.NoError(t, err)
assert.EqualValues(t, []*base.Review{
{
Expand Down
4 changes: 2 additions & 2 deletions modules/migrations/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func TestGitlabDownloadRepo(t *testing.T) {
},
}, prs)

rvs, err := downloader.GetReviews(base.BasicIssueContext{ID: 1})
rvs, err := downloader.GetReviews(base.BasicIssueContext(1))
assert.NoError(t, err)
if assert.Len(t, rvs, 2) {
for i := range rvs {
Expand All @@ -327,7 +327,7 @@ func TestGitlabDownloadRepo(t *testing.T) {
}
}
}
rvs, err = downloader.GetReviews(base.BasicIssueContext{ID: 2})
rvs, err = downloader.GetReviews(base.BasicIssueContext(2))
assert.NoError(t, err)
if assert.Len(t, prs, 1) {
assert.EqualValues(t, 4575606, rvs[0].ReviewerID)
Expand Down
2 changes: 1 addition & 1 deletion modules/migrations/gogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func convertGogsIssue(issue *gogs.Issue) *base.Issue {
Created: issue.Created,
Labels: labels,
Closed: closed,
Context: base.BasicIssueContext{ID: issue.Index},
Context: base.BasicIssueContext(issue.Index),
}
}

Expand Down
2 changes: 1 addition & 1 deletion modules/migrations/gogs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestGogsDownloadRepo(t *testing.T) {

// downloader.GetComments()
comments, _, err := downloader.GetComments(base.GetCommentOptions{
Context: base.BasicIssueContext{ID: 1},
Context: base.BasicIssueContext(1),
})
assert.NoError(t, err)
assert.Len(t, comments, 1)
Expand Down
83 changes: 69 additions & 14 deletions modules/migrations/onedev.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import (
"strings"
"time"

"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/migrations/base"
"code.gitea.io/gitea/modules/structs"

jsoniter "github.com/json-iterator/go"
)

var (
Expand Down Expand Up @@ -126,7 +125,7 @@ func (d *OneDevDownloader) callAPI(endpoint string, parameter map[string]string,
u.RawQuery = query.Encode()
}

req, err := http.NewRequest("GET", u.String(), nil)
req, err := http.NewRequestWithContext(d.ctx, "GET", u.String(), nil)
if err != nil {
return err
}
Expand All @@ -137,7 +136,7 @@ func (d *OneDevDownloader) callAPI(endpoint string, parameter map[string]string,
}
defer resp.Body.Close()

decoder := jsoniter.NewDecoder(resp.Body)
decoder := json.NewDecoder(resp.Body)
return decoder.Decode(&result)
}

Expand Down Expand Up @@ -196,8 +195,6 @@ func (d *OneDevDownloader) GetMilestones() ([]*base.Milestone, error) {

endpoint := fmt.Sprintf("/api/projects/%d/milestones", d.repoID)

t := time.Now()

var milestones = make([]*base.Milestone, 0, 100)
offset := 0
for {
Expand All @@ -219,7 +216,7 @@ func (d *OneDevDownloader) GetMilestones() ([]*base.Milestone, error) {

for _, milestone := range rawMilestones {
d.milestoneMap[milestone.ID] = milestone.Name
closed := &t
closed := milestone.DueDate
if !milestone.Closed {
closed = nil
}
Expand All @@ -228,8 +225,6 @@ func (d *OneDevDownloader) GetMilestones() ([]*base.Milestone, error) {
Title: milestone.Name,
Description: milestone.Description,
Deadline: milestone.DueDate,
Created: t,
Updated: &t,
Closed: closed,
})
}
Expand All @@ -239,7 +234,32 @@ func (d *OneDevDownloader) GetMilestones() ([]*base.Milestone, error) {

// GetLabels returns labels
func (d *OneDevDownloader) GetLabels() ([]*base.Label, error) {
return nil, nil
return []*base.Label{
{
Name: "Bug",
Color: "f64e60",
},
{
Name: "Build Failure",
Color: "f64e60",
},
{
Name: "Discussion",
Color: "8950fc",
},
{
Name: "Improvement",
Color: "1bc5bd",
},
{
Name: "New Feature",
Color: "1bc5bd",
},
{
Name: "Support Request",
Color: "8950fc",
},
}, nil
}

type onedevIssueContext struct {
Expand Down Expand Up @@ -284,6 +304,27 @@ func (d *OneDevDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, er

issues := make([]*base.Issue, 0, len(rawIssues))
for _, issue := range rawIssues {
fields := make([]struct {
Name string `json:"name"`
Value string `json:"value"`
}, 0, 10)
err := d.callAPI(
fmt.Sprintf("/api/issues/%d/fields", issue.ID),
nil,
&fields,
)
if err != nil {
return nil, false, err
}

var label *base.Label
for _, field := range fields {
if field.Name == "Type" {
label = &base.Label{Name: field.Value}
break
}
}

state := strings.ToLower(issue.State)
if state == "released" {
state = "closed"
Expand All @@ -298,7 +339,7 @@ func (d *OneDevDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, er
Milestone: d.milestoneMap[issue.MilestoneID],
State: state,
Created: issue.SubmitDate,
//Closed: closed,
Labels: []*base.Label{label},
Context: onedevIssueContext{
foreignID: issue.ID,
localID: issue.Number,
Expand Down Expand Up @@ -441,6 +482,21 @@ func (d *OneDevDownloader) GetPullRequests(page, perPage int) ([]*base.PullReque

pullRequests := make([]*base.PullRequest, 0, len(rawPullRequests))
for _, pr := range rawPullRequests {
var mergePreview struct {
TargetHeadCommitHash string `json:"targetHeadCommitHash"`
HeadCommitHash string `json:"headCommitHash"`
MergeStrategy string `json:"mergeStrategy"`
MergeCommitHash string `json:"mergeCommitHash"`
}
err := d.callAPI(
fmt.Sprintf("/api/pull-requests/%d/merge-preview", pr.ID),
nil,
&mergePreview,
)
if err != nil {
return nil, false, err
}

state := "open"
merged := false
var closeTime *time.Time
Expand Down Expand Up @@ -469,12 +525,12 @@ func (d *OneDevDownloader) GetPullRequests(page, perPage int) ([]*base.PullReque
MergedTime: mergedTime,
Head: base.PullRequestBranch{
Ref: pr.SourceBranch,
SHA: pr.BaseCommitHash,
SHA: mergePreview.HeadCommitHash,
RepoName: d.repoName,
},
Base: base.PullRequestBranch{
Ref: pr.TargetBranch,
SHA: "",
SHA: mergePreview.TargetHeadCommitHash,
RepoName: d.repoName,
},
Context: onedevIssueContext{
Expand Down Expand Up @@ -528,7 +584,6 @@ func (d *OneDevDownloader) GetReviews(context base.IssueContext) ([]*base.Review
IssueIndex: context.LocalID(),
ReviewerID: poster.ID,
ReviewerName: poster.Name,
CreatedAt: time.Now(),
Content: content,
State: state,
})
Expand Down
Loading