|
| 1 | +package mirroring |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "gitlab-sync/pkg/helpers" |
| 6 | + "sync" |
| 7 | + |
| 8 | + gitlab "gitlab.com/gitlab-org/api/client-go" |
| 9 | + "go.uber.org/zap" |
| 10 | +) |
| 11 | + |
| 12 | +var ( |
| 13 | + CLOSE_STATE_EVENT = "close" |
| 14 | +) |
| 15 | + |
| 16 | +// =========================================================================== |
| 17 | +// ISSUES MIRRORING FUNCTIONS // |
| 18 | +// =========================================================================== |
| 19 | + |
| 20 | +// ================ |
| 21 | +// GET |
| 22 | +// ================ |
| 23 | + |
| 24 | +// FetchProjectIssues retrieves all issues for a project and processes them |
| 25 | +func (g *GitlabInstance) FetchProjectIssues(project *gitlab.Project) ([]*gitlab.Issue, error) { |
| 26 | + zap.L().Debug("Fetching issues for project", zap.String("project", project.PathWithNamespace)) |
| 27 | + fetchOpts := &gitlab.ListProjectIssuesOptions{ |
| 28 | + ListOptions: gitlab.ListOptions{ |
| 29 | + PerPage: 100, |
| 30 | + Page: 1, |
| 31 | + }, |
| 32 | + } |
| 33 | + |
| 34 | + var issues = make([]*gitlab.Issue, 0) |
| 35 | + |
| 36 | + for { |
| 37 | + fetchedIssues, resp, err := g.Gitlab.Issues.ListProjectIssues(project.ID, fetchOpts) |
| 38 | + if err != nil { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + issues = append(issues, fetchedIssues...) |
| 42 | + |
| 43 | + if resp.CurrentPage >= resp.TotalPages { |
| 44 | + break |
| 45 | + } |
| 46 | + fetchOpts.Page = resp.NextPage |
| 47 | + } |
| 48 | + |
| 49 | + return issues, nil |
| 50 | +} |
| 51 | + |
| 52 | +// FetchProjectIssuesTitles retrieves all issue titles for a project and returns them as a map |
| 53 | +func (g *GitlabInstance) FetchProjectIssuesTitles(project *gitlab.Project) (map[string]struct{}, error) { |
| 54 | + // Fetch existing issues from the destination project |
| 55 | + issues, err := g.FetchProjectIssues(project) |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + |
| 60 | + // Create a map of existing issue titles for quick lookup |
| 61 | + issueTitles := make(map[string]struct{}) |
| 62 | + for _, issue := range issues { |
| 63 | + if issue != nil { |
| 64 | + issueTitles[issue.Title] = struct{}{} |
| 65 | + } |
| 66 | + } |
| 67 | + return issueTitles, nil |
| 68 | +} |
| 69 | + |
| 70 | +// ================ |
| 71 | +// POST |
| 72 | +// ================ |
| 73 | + |
| 74 | +// MirrorIssue creates an issue in the destination project |
| 75 | +func (g *GitlabInstance) MirrorIssue(project *gitlab.Project, issue *gitlab.Issue) error { |
| 76 | + zap.L().Debug("Creating issue in destination project", zap.String("issue", issue.Title), zap.String(ROLE_DESTINATION, project.HTTPURLToRepo)) |
| 77 | + |
| 78 | + // Create the issue in the destination project |
| 79 | + _, _, err := g.Gitlab.Issues.CreateIssue(project.ID, &gitlab.CreateIssueOptions{ |
| 80 | + Title: &issue.Title, |
| 81 | + Description: &issue.Description, |
| 82 | + Labels: (*gitlab.LabelOptions)(&issue.Labels), |
| 83 | + CreatedAt: issue.CreatedAt, |
| 84 | + Confidential: &issue.Confidential, |
| 85 | + DueDate: issue.DueDate, |
| 86 | + Weight: &issue.Weight, |
| 87 | + IssueType: issue.IssueType, |
| 88 | + }) |
| 89 | + |
| 90 | + if err == nil && issue.State == string(gitlab.ClosedEventType) { |
| 91 | + // If the issue is closed, close it in the destination project |
| 92 | + err = g.CloseIssue(project, issue) |
| 93 | + } |
| 94 | + |
| 95 | + return err |
| 96 | +} |
| 97 | + |
| 98 | +// CloseIssue closes an issue in the destination project |
| 99 | +func (g *GitlabInstance) CloseIssue(project *gitlab.Project, issue *gitlab.Issue) error { |
| 100 | + zap.L().Debug("Closing issue in destination project", zap.String("issue", issue.Title), zap.String(ROLE_DESTINATION, project.HTTPURLToRepo)) |
| 101 | + _, _, err := g.Gitlab.Issues.UpdateIssue(project.ID, issue.IID, &gitlab.UpdateIssueOptions{ |
| 102 | + StateEvent: &CLOSE_STATE_EVENT, |
| 103 | + }) |
| 104 | + return err |
| 105 | +} |
| 106 | + |
| 107 | +// ================ |
| 108 | +// CONTROLLER |
| 109 | +// ================ |
| 110 | + |
| 111 | +// MirrorIssues mirrors issues from the source project to the destination project. |
| 112 | +// It fetches existing issues from the destination project and creates new issues for those that do not |
| 113 | +func (destinationGitlab *GitlabInstance) MirrorIssues(sourceGitlab *GitlabInstance, sourceProject *gitlab.Project, destinationProject *gitlab.Project) []error { |
| 114 | + zap.L().Info("Starting issues mirroring", zap.String(ROLE_SOURCE, sourceProject.HTTPURLToRepo), zap.String(ROLE_DESTINATION, destinationProject.HTTPURLToRepo)) |
| 115 | + |
| 116 | + // Fetch existing issues from the destination project |
| 117 | + existingIssuesTitles, err := destinationGitlab.FetchProjectIssuesTitles(destinationProject) |
| 118 | + if err != nil { |
| 119 | + return []error{fmt.Errorf("failed to fetch existing issues for destination project %s: %v", destinationProject.HTTPURLToRepo, err)} |
| 120 | + } |
| 121 | + |
| 122 | + // Fetch issues from the source project |
| 123 | + sourceIssues, err := sourceGitlab.FetchProjectIssues(sourceProject) |
| 124 | + if err != nil { |
| 125 | + return []error{fmt.Errorf("failed to fetch issues for source project %s: %v", sourceProject.HTTPURLToRepo, err)} |
| 126 | + } |
| 127 | + |
| 128 | + // Create a wait group and an error channel for handling API calls concurrently |
| 129 | + var wg sync.WaitGroup |
| 130 | + errorChan := make(chan error, len(sourceIssues)) |
| 131 | + |
| 132 | + // Iterate over each source issue |
| 133 | + for _, issue := range sourceIssues { |
| 134 | + // Check if the issue already exists in the destination project |
| 135 | + if _, exists := existingIssuesTitles[issue.Title]; exists { |
| 136 | + zap.L().Debug("Issue already exists", zap.String("issue", issue.Title), zap.String(ROLE_DESTINATION, destinationProject.HTTPURLToRepo)) |
| 137 | + continue |
| 138 | + } |
| 139 | + |
| 140 | + // Increment the wait group counter |
| 141 | + wg.Add(1) |
| 142 | + |
| 143 | + // Define the API call logic for creating an issue |
| 144 | + go func(project *gitlab.Project, issueToMirror *gitlab.Issue) { |
| 145 | + defer wg.Done() |
| 146 | + err := destinationGitlab.MirrorIssue(destinationProject, issueToMirror) |
| 147 | + if err != nil { |
| 148 | + errorChan <- fmt.Errorf("failed to create issue %s in project %s: %s", issueToMirror.Title, destinationProject.HTTPURLToRepo, err) |
| 149 | + } |
| 150 | + }(destinationProject, issue) |
| 151 | + } |
| 152 | + |
| 153 | + // Wait for all goroutines to finish |
| 154 | + wg.Wait() |
| 155 | + close(errorChan) |
| 156 | + zap.L().Info("Issues mirroring completed", zap.String(ROLE_SOURCE, sourceProject.HTTPURLToRepo), zap.String(ROLE_DESTINATION, destinationProject.HTTPURLToRepo)) |
| 157 | + return helpers.MergeErrors(errorChan) |
| 158 | +} |
0 commit comments