Skip to content

Commit eb56d82

Browse files
committed
fix: correct nilpointer exception in updateProjectFromSource
Fixes #18
1 parent 1bc10be commit eb56d82

File tree

3 files changed

+44
-26
lines changed

3 files changed

+44
-26
lines changed

internal/mirroring/post.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func (g *GitlabInstance) createGroupFromSource(sourceGroup *gitlab.Group, copyOp
111111
}
112112

113113
// ============================================================ //
114-
// PROJECT CREATION FUNCTIONS //
114+
// PROJECT CREATION FUNCTIONS //
115115
// ============================================================ //
116116

117117
// createProjects creates GitLab projects in the destination GitLab instance based on the mirror mapping.
@@ -139,7 +139,7 @@ func (destinationGitlab *GitlabInstance) createProjects(sourceGitlab *GitlabInst
139139
defer wg.Done()
140140
_, err := destinationGitlab.createProject(sourcePath, destinationCopyOptions, sourceGitlab)
141141
if err != nil {
142-
errorChan <- fmt.Errorf("failed to create project %s in destination GitLab instance: %s", destinationCopyOptions.DestinationPath, err)
142+
errorChan <- fmt.Errorf("failed to create project %s in destination GitLab instance: %v", destinationCopyOptions.DestinationPath, err)
143143
}
144144
}(sourceProjectPath, destinationProjectOptions)
145145
}
@@ -168,7 +168,7 @@ func (destinationGitlab *GitlabInstance) createProject(sourceProjectPath string,
168168
// If it does not exist, create it
169169
if destinationProject == nil {
170170
destinationProject, err = destinationGitlab.createProjectFromSource(sourceProject, projectCreationOptions)
171-
if err != nil {
171+
if err != nil || destinationProject == nil {
172172
return nil, []error{fmt.Errorf("failed to create project %s in destination GitLab instance: %s", destinationProjectPath, err)}
173173
}
174174
}
@@ -215,7 +215,7 @@ func (g *GitlabInstance) createProjectFromSource(sourceProject *gitlab.Project,
215215
g.addProject(destinationProject)
216216
}
217217

218-
return destinationProject, nil
218+
return destinationProject, err
219219
}
220220

221221
// ============================================================ //

internal/mirroring/post_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func TestCreateProjects(t *testing.T) {
211211
},
212212
}
213213
err := destinationGitlabInstance.createProjects(sourceGitlabInstance, mirrorMapping)
214-
if err != nil {
214+
if err != nil && len(err) > 0 {
215215
t.Errorf("Unexpected error when creating projects: %v", err)
216216
}
217217
if len(destinationGitlabInstance.Projects) == 0 {

internal/mirroring/put.go

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ import (
2121
// It also mirrors releases if the option is set.
2222
// The function uses goroutines to perform these tasks concurrently and waits for all of them to finish.
2323
func (destinationGitlabInstance *GitlabInstance) updateProjectFromSource(sourceGitlabInstance *GitlabInstance, sourceProject *gitlab.Project, destinationProject *gitlab.Project, copyOptions *utils.MirroringOptions) []error {
24+
// Immediately capture pointers in local variables to avoid any late overrides
25+
srcProj := sourceProject
26+
dstProj := destinationProject
27+
if srcProj == nil || dstProj == nil {
28+
return []error{fmt.Errorf("source or destination project is nil")}
29+
}
30+
2431
wg := sync.WaitGroup{}
2532
maxErrors := 3
2633
if copyOptions.CI_CD_Catalog {
@@ -29,41 +36,43 @@ func (destinationGitlabInstance *GitlabInstance) updateProjectFromSource(sourceG
2936
if copyOptions.MirrorReleases {
3037
maxErrors++
3138
}
39+
3240
wg.Add(maxErrors)
3341
errorChan := make(chan error, maxErrors)
3442

35-
go func() {
43+
go func(sp *gitlab.Project, dp *gitlab.Project) {
3644
defer wg.Done()
37-
errorChan <- destinationGitlabInstance.syncProjectAttributes(sourceProject, destinationProject, copyOptions)
38-
}()
45+
errorChan <- destinationGitlabInstance.syncProjectAttributes(sp, dp, copyOptions)
46+
}(srcProj, dstProj)
3947

40-
go func() {
48+
go func(sp *gitlab.Project, dp *gitlab.Project) {
4149
defer wg.Done()
42-
errorChan <- destinationGitlabInstance.enableProjectMirrorPull(sourceProject, destinationProject, copyOptions)
43-
}()
50+
errorChan <- destinationGitlabInstance.enableProjectMirrorPull(sp, dp, copyOptions)
51+
}(srcProj, dstProj)
4452

45-
go func() {
53+
go func(sp *gitlab.Project, dp *gitlab.Project) {
4654
defer wg.Done()
47-
errorChan <- sourceGitlabInstance.copyProjectAvatar(destinationGitlabInstance, destinationProject, sourceProject)
48-
}()
55+
errorChan <- sourceGitlabInstance.copyProjectAvatar(destinationGitlabInstance, dp, sp)
56+
}(srcProj, dstProj)
4957

5058
if copyOptions.CI_CD_Catalog {
51-
go func() {
59+
go func(dp *gitlab.Project) {
5260
defer wg.Done()
53-
errorChan <- destinationGitlabInstance.addProjectToCICDCatalog(destinationProject)
54-
}()
61+
errorChan <- destinationGitlabInstance.addProjectToCICDCatalog(dp)
62+
}(dstProj)
5563
}
5664

5765
allErrors := []error{}
5866
if copyOptions.MirrorReleases {
59-
go func() {
67+
go func(sp *gitlab.Project, dp *gitlab.Project) {
6068
defer wg.Done()
61-
allErrors = destinationGitlabInstance.mirrorReleases(sourceGitlabInstance, sourceProject, destinationProject)
62-
}()
69+
allErrors = destinationGitlabInstance.mirrorReleases(sourceGitlabInstance, sp, dp)
70+
}(srcProj, dstProj)
6371
}
6472

6573
wg.Wait()
6674
close(errorChan)
75+
6776
for err := range errorChan {
6877
if err != nil {
6978
allErrors = append(allErrors, err)
@@ -176,20 +185,29 @@ func (sourceGitlabInstance *GitlabInstance) copyProjectAvatar(destinationGitlabI
176185
// updateGroupFromSource updates the destination group with settings from the source group.
177186
// It copies the group avatar and updates the group attributes.
178187
func (destinationGitlabInstance *GitlabInstance) updateGroupFromSource(sourceGitlabInstance *GitlabInstance, sourceGroup *gitlab.Group, destinationGroup *gitlab.Group, copyOptions *utils.MirroringOptions) []error {
188+
// Immediately capture pointers in local variables to avoid any late overrides
189+
srcGroup := sourceGroup
190+
dstGroup := destinationGroup
191+
cpOpts := copyOptions
192+
193+
if srcGroup == nil || dstGroup == nil {
194+
return []error{fmt.Errorf("source or destination group is nil")}
195+
}
196+
179197
wg := sync.WaitGroup{}
180198
maxErrors := 2
181199
wg.Add(maxErrors)
182200
errorChan := make(chan error, maxErrors)
183201

184-
go func() {
202+
go func(sg *gitlab.Group, dg *gitlab.Group, cp *utils.MirroringOptions) {
185203
defer wg.Done()
186-
errorChan <- destinationGitlabInstance.syncGroupAttributes(sourceGroup, destinationGroup, copyOptions)
187-
}()
204+
errorChan <- destinationGitlabInstance.syncGroupAttributes(sg, dg, cp)
205+
}(srcGroup, dstGroup, cpOpts)
188206

189-
go func() {
207+
go func(sg *gitlab.Group, dg *gitlab.Group) {
190208
defer wg.Done()
191-
errorChan <- sourceGitlabInstance.copyGroupAvatar(destinationGitlabInstance, destinationGroup, sourceGroup)
192-
}()
209+
errorChan <- sourceGitlabInstance.copyGroupAvatar(destinationGitlabInstance, dg, sg)
210+
}(srcGroup, dstGroup)
193211

194212
wg.Wait()
195213
close(errorChan)

0 commit comments

Comments
 (0)