From 6eab62d1283e1808a63a81936638dd06d613ffea Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 12 Feb 2024 01:57:43 -0500 Subject: [PATCH] refactor: need to duplicate and make "core" copies of functions when we have to convert from a core version of a struct --- api/convert.go | 113 ++++++++++++++++++++++++++++++++++++++++--- api/convert_event.go | 10 ++-- 2 files changed, 111 insertions(+), 12 deletions(-) diff --git a/api/convert.go b/api/convert.go index 85b7e55..18fce69 100644 --- a/api/convert.go +++ b/api/convert.go @@ -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 { var ghUsers []*github.User for _, user := range users { @@ -58,6 +68,14 @@ func convertUsers(users []*gitea.User) []*github.User { 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 { if milestone == nil { 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 { if labels == nil { return make([]*github.Label, 0) @@ -83,6 +110,22 @@ func convertLabels(labels []*gitea.Label) []*github.Label { 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 { if branch == nil { 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 { if repo == nil { 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 { return &github.PullRequest{} } @@ -148,18 +234,18 @@ func convertPullRequest(request *gitea.PullRequest) *github.PullRequest { DiffURL: stringPtr(request.DiffURL), PatchURL: stringPtr(request.PatchURL), Comments: intPtr(request.Comments), - Assignee: convertUser(request.Assignee), - Assignees: convertUsers(request.Assignees), - Milestone: convertMilestone(request.Milestone), - Labels: convertLabels(request.Labels), + Assignee: convertCoreUser(request.Assignee), + Assignees: convertCoreUsers(request.Assignees), + Milestone: convertCoreMilestone(request.Milestone), + Labels: convertCoreLabels(request.Labels), } // Convert PR branch info if request.Head != nil { - pr.Head = convertPRBranch(request.Head) + pr.Head = convertCorePRBranch(request.Head) } if request.Base != nil { - pr.Base = convertPRBranch(request.Base) + pr.Base = convertCorePRBranch(request.Base) } 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 { if file == nil { return &github.CommitFile{} diff --git a/api/convert_event.go b/api/convert_event.go index 5917fc2..cf0d402 100644 --- a/api/convert_event.go +++ b/api/convert_event.go @@ -17,12 +17,12 @@ func convertPullRequestEvent(event *structs.PullRequestPayload) *github.PullRequ return &github.PullRequestEvent{ Action: stringPtr(translatePrAction(event.Action)), PullRequest: convertPullRequest(event.PullRequest), - Repo: convertRepo(event.Repository), - Assignee: convertUser(event.PullRequest.Assignee), + Repo: convertCoreRepo(event.Repository), + Assignee: convertCoreUser(event.PullRequest.Assignee), Number: intPtr(int(event.Index)), Changes: convertChanges(event.Changes), - RequestedReviewer: convertUser(event.PullRequest.RequestedReviewers[0]), - Sender: convertUser(event.Sender), - Label: convertLabel(event.PullRequest.Labels[0]), + RequestedReviewer: convertCoreUser(event.PullRequest.RequestedReviewers[0]), + Sender: convertCoreUser(event.Sender), + Label: convertCoreLabel(event.PullRequest.Labels[0]), } }