refactor: need to duplicate and make "core" copies of functions when we have to convert from a core version of a struct

This commit is contained in:
Derrick Hammer 2024-02-12 01:57:43 -05:00
parent d132946e40
commit 6eab62d128
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 111 additions and 12 deletions

View File

@ -50,6 +50,16 @@ func convertUser(user *gitea.User) *github.User {
} }
} }
func convertCoreUser(user *structs.User) *github.User {
if user == nil {
return &github.User{}
}
return &github.User{
Login: stringPtr(user.UserName),
ID: int64Ptr(user.ID),
}
}
func convertUsers(users []*gitea.User) []*github.User { func convertUsers(users []*gitea.User) []*github.User {
var ghUsers []*github.User var ghUsers []*github.User
for _, user := range users { for _, user := range users {
@ -58,6 +68,14 @@ func convertUsers(users []*gitea.User) []*github.User {
return ghUsers return ghUsers
} }
func convertCoreUsers(users []*structs.User) []*github.User {
var ghUsers []*github.User
for _, user := range users {
ghUsers = append(ghUsers, convertCoreUser(user))
}
return ghUsers
}
func convertMilestone(milestone *gitea.Milestone) *github.Milestone { func convertMilestone(milestone *gitea.Milestone) *github.Milestone {
if milestone == nil { if milestone == nil {
return &github.Milestone{} return &github.Milestone{}
@ -67,6 +85,15 @@ func convertMilestone(milestone *gitea.Milestone) *github.Milestone {
} }
} }
func convertCoreMilestone(milestone *structs.Milestone) *github.Milestone {
if milestone == nil {
return &github.Milestone{}
}
return &github.Milestone{
Title: stringPtr(milestone.Title),
}
}
func convertLabels(labels []*gitea.Label) []*github.Label { func convertLabels(labels []*gitea.Label) []*github.Label {
if labels == nil { if labels == nil {
return make([]*github.Label, 0) return make([]*github.Label, 0)
@ -83,6 +110,22 @@ func convertLabels(labels []*gitea.Label) []*github.Label {
return ghLabels return ghLabels
} }
func convertCoreLabels(labels []*structs.Label) []*github.Label {
if labels == nil {
return make([]*github.Label, 0)
}
var ghLabels []*github.Label
for _, label := range labels {
if label == nil {
continue
}
ghLabels = append(ghLabels, &github.Label{
Name: stringPtr(label.Name),
})
}
return ghLabels
}
func convertPRBranch(branch *gitea.PRBranchInfo) *github.PullRequestBranch { func convertPRBranch(branch *gitea.PRBranchInfo) *github.PullRequestBranch {
if branch == nil { if branch == nil {
return &github.PullRequestBranch{} return &github.PullRequestBranch{}
@ -95,6 +138,18 @@ func convertPRBranch(branch *gitea.PRBranchInfo) *github.PullRequestBranch {
} }
} }
func convertCorePRBranch(branch *structs.PRBranchInfo) *github.PullRequestBranch {
if branch == nil {
return &github.PullRequestBranch{}
}
return &github.PullRequestBranch{
Label: stringPtr(branch.Name),
Ref: stringPtr(branch.Ref),
SHA: stringPtr(branch.Sha),
Repo: convertCoreRepo(branch.Repository),
}
}
func convertRepo(repo *gitea.Repository) *github.Repository { func convertRepo(repo *gitea.Repository) *github.Repository {
if repo == nil { if repo == nil {
return &github.Repository{} return &github.Repository{}
@ -126,7 +181,38 @@ func convertRepo(repo *gitea.Repository) *github.Repository {
} }
} }
func convertPullRequest(request *gitea.PullRequest) *github.PullRequest { func convertCoreRepo(repo *structs.Repository) *github.Repository {
if repo == nil {
return &github.Repository{}
}
return &github.Repository{
ID: int64Ptr(repo.ID),
Name: stringPtr(repo.Name),
Owner: convertCoreUser(repo.Owner),
FullName: stringPtr(repo.FullName),
Description: stringPtr(repo.Description),
Homepage: stringPtr(repo.Website),
HTMLURL: stringPtr(repo.HTMLURL),
CloneURL: stringPtr(repo.CloneURL),
GitURL: stringPtr(repo.CloneURL),
SSHURL: stringPtr(repo.SSHURL),
DefaultBranch: stringPtr(repo.DefaultBranch),
CreatedAt: timePtr(repo.Created),
UpdatedAt: timePtr(repo.Updated),
Private: boolPtr(repo.Private),
Fork: boolPtr(repo.Fork),
Size: intPtr(repo.Size),
StargazersCount: intPtr(repo.Stars),
SubscribersCount: intPtr(repo.Watchers),
ForksCount: intPtr(repo.Forks),
Watchers: intPtr(repo.Watchers),
WatchersCount: intPtr(repo.Stars),
OpenIssuesCount: intPtr(repo.OpenIssues),
Archived: boolPtr(repo.Archived),
}
}
func convertPullRequest(request *structs.PullRequest) *github.PullRequest {
if request == nil { if request == nil {
return &github.PullRequest{} return &github.PullRequest{}
} }
@ -148,18 +234,18 @@ func convertPullRequest(request *gitea.PullRequest) *github.PullRequest {
DiffURL: stringPtr(request.DiffURL), DiffURL: stringPtr(request.DiffURL),
PatchURL: stringPtr(request.PatchURL), PatchURL: stringPtr(request.PatchURL),
Comments: intPtr(request.Comments), Comments: intPtr(request.Comments),
Assignee: convertUser(request.Assignee), Assignee: convertCoreUser(request.Assignee),
Assignees: convertUsers(request.Assignees), Assignees: convertCoreUsers(request.Assignees),
Milestone: convertMilestone(request.Milestone), Milestone: convertCoreMilestone(request.Milestone),
Labels: convertLabels(request.Labels), Labels: convertCoreLabels(request.Labels),
} }
// Convert PR branch info // Convert PR branch info
if request.Head != nil { if request.Head != nil {
pr.Head = convertPRBranch(request.Head) pr.Head = convertCorePRBranch(request.Head)
} }
if request.Base != nil { if request.Base != nil {
pr.Base = convertPRBranch(request.Base) pr.Base = convertCorePRBranch(request.Base)
} }
return pr return pr
@ -197,6 +283,19 @@ func convertLabel(label *gitea.Label) *github.Label {
} }
} }
func convertCoreLabel(label *structs.Label) *github.Label {
if label == nil {
return &github.Label{}
}
return &github.Label{
ID: int64Ptr(label.ID),
Name: stringPtr(label.Name),
Color: stringPtr(label.Color),
Description: stringPtr(label.Description),
URL: stringPtr(label.URL),
}
}
func convertCommitFile(file *gitea.ChangedFile) *github.CommitFile { func convertCommitFile(file *gitea.ChangedFile) *github.CommitFile {
if file == nil { if file == nil {
return &github.CommitFile{} return &github.CommitFile{}

View File

@ -17,12 +17,12 @@ func convertPullRequestEvent(event *structs.PullRequestPayload) *github.PullRequ
return &github.PullRequestEvent{ return &github.PullRequestEvent{
Action: stringPtr(translatePrAction(event.Action)), Action: stringPtr(translatePrAction(event.Action)),
PullRequest: convertPullRequest(event.PullRequest), PullRequest: convertPullRequest(event.PullRequest),
Repo: convertRepo(event.Repository), Repo: convertCoreRepo(event.Repository),
Assignee: convertUser(event.PullRequest.Assignee), Assignee: convertCoreUser(event.PullRequest.Assignee),
Number: intPtr(int(event.Index)), Number: intPtr(int(event.Index)),
Changes: convertChanges(event.Changes), Changes: convertChanges(event.Changes),
RequestedReviewer: convertUser(event.PullRequest.RequestedReviewers[0]), RequestedReviewer: convertCoreUser(event.PullRequest.RequestedReviewers[0]),
Sender: convertUser(event.Sender), Sender: convertCoreUser(event.Sender),
Label: convertLabel(event.PullRequest.Labels[0]), Label: convertCoreLabel(event.PullRequest.Labels[0]),
} }
} }