2024-02-11 09:42:01 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"code.gitea.io/gitea/modules/structs"
|
2024-02-12 06:46:41 +00:00
|
|
|
gitea "code.gitea.io/sdk/gitea"
|
2024-02-11 11:25:50 +00:00
|
|
|
"fmt"
|
2024-02-11 09:42:01 +00:00
|
|
|
"github.com/google/go-github/v59/github"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func stringPtr(s string) *string {
|
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
|
|
|
func numberPtr(n int) *int {
|
|
|
|
return &n
|
|
|
|
}
|
|
|
|
|
|
|
|
func intPtr(n int) *int {
|
|
|
|
return &n
|
|
|
|
}
|
|
|
|
|
|
|
|
func int64Ptr(n int64) *int64 {
|
|
|
|
return &n
|
|
|
|
}
|
|
|
|
|
|
|
|
func boolPtr(b bool) *bool {
|
|
|
|
return &b
|
|
|
|
}
|
|
|
|
|
|
|
|
func timePtr(t time.Time) *github.Timestamp {
|
|
|
|
timestamp := github.Timestamp{Time: t}
|
|
|
|
return ×tamp
|
|
|
|
}
|
|
|
|
|
|
|
|
func timePtrIfNotNil(t *time.Time) *github.Timestamp {
|
|
|
|
if t == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return timePtr(*t)
|
|
|
|
}
|
|
|
|
|
2024-02-12 06:46:41 +00:00
|
|
|
func convertUser(user *gitea.User) *github.User {
|
2024-02-11 09:42:01 +00:00
|
|
|
if user == nil {
|
2024-02-11 09:50:01 +00:00
|
|
|
return &github.User{}
|
2024-02-11 09:42:01 +00:00
|
|
|
}
|
|
|
|
return &github.User{
|
|
|
|
Login: stringPtr(user.UserName),
|
|
|
|
ID: int64Ptr(user.ID),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-12 06:57:43 +00:00
|
|
|
func convertCoreUser(user *structs.User) *github.User {
|
|
|
|
if user == nil {
|
|
|
|
return &github.User{}
|
|
|
|
}
|
|
|
|
return &github.User{
|
|
|
|
Login: stringPtr(user.UserName),
|
|
|
|
ID: int64Ptr(user.ID),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-12 06:46:41 +00:00
|
|
|
func convertUsers(users []*gitea.User) []*github.User {
|
2024-02-11 09:42:01 +00:00
|
|
|
var ghUsers []*github.User
|
|
|
|
for _, user := range users {
|
|
|
|
ghUsers = append(ghUsers, convertUser(user))
|
|
|
|
}
|
|
|
|
return ghUsers
|
|
|
|
}
|
|
|
|
|
2024-02-12 06:57:43 +00:00
|
|
|
func convertCoreUsers(users []*structs.User) []*github.User {
|
|
|
|
var ghUsers []*github.User
|
|
|
|
for _, user := range users {
|
|
|
|
ghUsers = append(ghUsers, convertCoreUser(user))
|
|
|
|
}
|
|
|
|
return ghUsers
|
|
|
|
}
|
|
|
|
|
2024-02-12 06:46:41 +00:00
|
|
|
func convertMilestone(milestone *gitea.Milestone) *github.Milestone {
|
2024-02-11 09:42:01 +00:00
|
|
|
if milestone == nil {
|
2024-02-11 09:50:01 +00:00
|
|
|
return &github.Milestone{}
|
2024-02-11 09:42:01 +00:00
|
|
|
}
|
|
|
|
return &github.Milestone{
|
|
|
|
Title: stringPtr(milestone.Title),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-12 06:57:43 +00:00
|
|
|
func convertCoreMilestone(milestone *structs.Milestone) *github.Milestone {
|
|
|
|
if milestone == nil {
|
|
|
|
return &github.Milestone{}
|
|
|
|
}
|
|
|
|
return &github.Milestone{
|
|
|
|
Title: stringPtr(milestone.Title),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-12 06:46:41 +00:00
|
|
|
func convertLabels(labels []*gitea.Label) []*github.Label {
|
2024-02-11 09:50:01 +00:00
|
|
|
if labels == nil {
|
|
|
|
return make([]*github.Label, 0)
|
|
|
|
}
|
2024-02-11 09:42:01 +00:00
|
|
|
var ghLabels []*github.Label
|
|
|
|
for _, label := range labels {
|
2024-02-11 10:23:51 +00:00
|
|
|
if label == nil {
|
|
|
|
continue
|
|
|
|
}
|
2024-02-11 09:42:01 +00:00
|
|
|
ghLabels = append(ghLabels, &github.Label{
|
|
|
|
Name: stringPtr(label.Name),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return ghLabels
|
|
|
|
}
|
|
|
|
|
2024-02-12 06:57:43 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-02-12 06:46:41 +00:00
|
|
|
func convertPRBranch(branch *gitea.PRBranchInfo) *github.PullRequestBranch {
|
2024-02-11 09:50:01 +00:00
|
|
|
if branch == nil {
|
|
|
|
return &github.PullRequestBranch{}
|
|
|
|
}
|
2024-02-11 09:42:01 +00:00
|
|
|
return &github.PullRequestBranch{
|
|
|
|
Label: stringPtr(branch.Name),
|
|
|
|
Ref: stringPtr(branch.Ref),
|
|
|
|
SHA: stringPtr(branch.Sha),
|
|
|
|
Repo: convertRepo(branch.Repository),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-12 06:57:43 +00:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-12 06:46:41 +00:00
|
|
|
func convertRepo(repo *gitea.Repository) *github.Repository {
|
2024-02-11 09:50:01 +00:00
|
|
|
if repo == nil {
|
|
|
|
return &github.Repository{}
|
|
|
|
}
|
2024-02-11 09:42:01 +00:00
|
|
|
return &github.Repository{
|
|
|
|
ID: int64Ptr(repo.ID),
|
|
|
|
Name: stringPtr(repo.Name),
|
2024-02-11 11:32:38 +00:00
|
|
|
Owner: convertUser(repo.Owner),
|
2024-02-11 09:42:01 +00:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-12 06:57:43 +00:00
|
|
|
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 {
|
2024-02-11 09:50:01 +00:00
|
|
|
if request == nil {
|
|
|
|
return &github.PullRequest{}
|
|
|
|
}
|
2024-02-11 09:42:01 +00:00
|
|
|
pr := &github.PullRequest{
|
|
|
|
ID: int64Ptr(request.ID),
|
|
|
|
Number: intPtr(int(request.Index)),
|
|
|
|
State: stringPtr(string(request.State)),
|
|
|
|
Title: stringPtr(request.Title),
|
|
|
|
Body: stringPtr(request.Body),
|
|
|
|
CreatedAt: timePtr(*request.Created),
|
|
|
|
UpdatedAt: timePtr(*request.Updated),
|
|
|
|
ClosedAt: timePtrIfNotNil(request.Closed),
|
|
|
|
MergedAt: timePtrIfNotNil(request.Merged),
|
|
|
|
Merged: boolPtr(request.HasMerged),
|
|
|
|
Mergeable: boolPtr(request.Mergeable),
|
|
|
|
MergeCommitSHA: request.MergedCommitID,
|
|
|
|
URL: stringPtr(request.URL),
|
|
|
|
HTMLURL: stringPtr(request.HTMLURL),
|
|
|
|
DiffURL: stringPtr(request.DiffURL),
|
|
|
|
PatchURL: stringPtr(request.PatchURL),
|
|
|
|
Comments: intPtr(request.Comments),
|
2024-02-12 06:57:43 +00:00
|
|
|
Assignee: convertCoreUser(request.Assignee),
|
|
|
|
Assignees: convertCoreUsers(request.Assignees),
|
|
|
|
Milestone: convertCoreMilestone(request.Milestone),
|
|
|
|
Labels: convertCoreLabels(request.Labels),
|
2024-02-11 09:42:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert PR branch info
|
|
|
|
if request.Head != nil {
|
2024-02-12 06:57:43 +00:00
|
|
|
pr.Head = convertCorePRBranch(request.Head)
|
2024-02-11 09:42:01 +00:00
|
|
|
}
|
|
|
|
if request.Base != nil {
|
2024-02-12 06:57:43 +00:00
|
|
|
pr.Base = convertCorePRBranch(request.Base)
|
2024-02-11 09:42:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pr
|
|
|
|
}
|
|
|
|
|
2024-02-11 09:47:58 +00:00
|
|
|
func convertChanges(changes *structs.ChangesPayload) *github.EditChange {
|
|
|
|
if changes == nil {
|
2024-02-11 09:50:01 +00:00
|
|
|
return &github.EditChange{}
|
2024-02-11 09:47:58 +00:00
|
|
|
}
|
|
|
|
return &github.EditChange{
|
|
|
|
Title: &github.EditTitle{
|
|
|
|
From: stringPtr(changes.Title.From),
|
|
|
|
},
|
|
|
|
Body: &github.EditBody{
|
|
|
|
From: stringPtr(changes.Body.From),
|
|
|
|
},
|
|
|
|
Base: &github.EditBase{
|
|
|
|
Ref: &github.EditRef{
|
|
|
|
From: stringPtr(changes.Ref.From),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-12 06:46:41 +00:00
|
|
|
func convertLabel(label *gitea.Label) *github.Label {
|
2024-02-11 09:56:30 +00:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-12 06:57:43 +00:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-12 06:46:41 +00:00
|
|
|
func convertCommitFile(file *gitea.ChangedFile) *github.CommitFile {
|
2024-02-11 19:03:03 +00:00
|
|
|
if file == nil {
|
|
|
|
return &github.CommitFile{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &github.CommitFile{
|
|
|
|
Filename: stringPtr(file.Filename),
|
|
|
|
PreviousFilename: stringPtr(file.PreviousFilename),
|
|
|
|
Additions: intPtr(file.Additions),
|
|
|
|
Deletions: intPtr(file.Deletions),
|
|
|
|
Changes: intPtr(file.Changes),
|
|
|
|
Status: stringPtr(file.Status),
|
|
|
|
BlobURL: stringPtr(file.ContentsURL),
|
|
|
|
RawURL: stringPtr(file.RawURL),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-12 06:48:49 +00:00
|
|
|
func convertGitTree(tree *gitea.GitTreeResponse) *github.Tree {
|
|
|
|
if tree == nil {
|
|
|
|
return &github.Tree{}
|
|
|
|
}
|
|
|
|
return &github.Tree{
|
|
|
|
SHA: stringPtr(tree.SHA),
|
|
|
|
Truncated: boolPtr(tree.Truncated),
|
|
|
|
Entries: convertGitEntries(tree.Entries),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertGitEntries(entries []gitea.GitEntry) []*github.TreeEntry {
|
|
|
|
if entries == nil {
|
|
|
|
return make([]*github.TreeEntry, 0)
|
|
|
|
}
|
|
|
|
var ghEntries []*github.TreeEntry
|
|
|
|
for _, entry := range entries {
|
|
|
|
ghEntries = append(ghEntries, convertGitEntry(&entry))
|
|
|
|
}
|
|
|
|
return ghEntries
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertGitEntry(s *gitea.GitEntry) *github.TreeEntry {
|
|
|
|
if s == nil {
|
|
|
|
return &github.TreeEntry{}
|
|
|
|
}
|
|
|
|
return &github.TreeEntry{
|
|
|
|
Path: stringPtr(s.Path),
|
|
|
|
Mode: stringPtr(s.Mode),
|
|
|
|
Type: stringPtr(s.Type),
|
|
|
|
Size: intPtr(int(s.Size)),
|
|
|
|
SHA: stringPtr(s.SHA),
|
|
|
|
URL: stringPtr(s.URL),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-11 09:42:01 +00:00
|
|
|
func translatePrAction(action structs.HookIssueAction) string {
|
|
|
|
translatedAction := ""
|
|
|
|
|
|
|
|
switch action {
|
|
|
|
case structs.HookIssueOpened:
|
|
|
|
translatedAction = "opened"
|
|
|
|
case structs.HookIssueClosed:
|
|
|
|
translatedAction = "closed"
|
|
|
|
case structs.HookIssueReOpened:
|
|
|
|
translatedAction = "reopened"
|
|
|
|
case structs.HookIssueEdited:
|
|
|
|
translatedAction = "edited"
|
|
|
|
case structs.HookIssueAssigned:
|
|
|
|
translatedAction = "assigned"
|
|
|
|
case structs.HookIssueUnassigned:
|
|
|
|
translatedAction = "unassigned"
|
|
|
|
case structs.HookIssueLabelUpdated:
|
|
|
|
// GitHub does not have a direct "label_updated" event; use "labeled" as the closest action
|
|
|
|
translatedAction = "labeled" // Assuming you handle the update as adding a label
|
|
|
|
case structs.HookIssueLabelCleared:
|
|
|
|
// GitHub does not have a direct "label_cleared" event; use "unlabeled" as the closest action
|
|
|
|
translatedAction = "unlabeled" // Assuming you handle the clearing as removing a label
|
|
|
|
case structs.HookIssueSynchronized:
|
|
|
|
translatedAction = "synchronize"
|
|
|
|
case structs.HookIssueMilestoned:
|
|
|
|
translatedAction = "milestoned"
|
|
|
|
case structs.HookIssueDemilestoned:
|
|
|
|
translatedAction = "demilestoned"
|
|
|
|
case structs.HookIssueReviewed:
|
|
|
|
// GitHub does not have a direct "reviewed" event for PRs; this might be closest to a review submitted
|
|
|
|
translatedAction = "review_submitted" // This is not a direct GitHub event, consider how best to map this action
|
|
|
|
case structs.HookIssueReviewRequested:
|
|
|
|
translatedAction = "review_requested"
|
|
|
|
case structs.HookIssueReviewRequestRemoved:
|
|
|
|
translatedAction = "review_request_removed"
|
|
|
|
default:
|
|
|
|
// Fallback for any unhandled actions
|
|
|
|
translatedAction = "unknown_action"
|
|
|
|
}
|
|
|
|
|
2024-02-11 11:25:50 +00:00
|
|
|
translatedAction = fmt.Sprintf("pull_request.%s", translatedAction)
|
|
|
|
|
2024-02-11 09:42:01 +00:00
|
|
|
return translatedAction
|
|
|
|
}
|