Skip to content

Commit e183e57

Browse files
committed
feat: add license check for destination gitlab
The execution now fails gracefully if the license tier of destination gitlab is not at least premium (required for pull mirroring)
1 parent 544df8d commit e183e57

File tree

4 files changed

+84
-19
lines changed

4 files changed

+84
-19
lines changed

internal/mirroring/get.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313

1414
const (
1515
INSTANCE_SEMVER_THRESHOLD = "17.6"
16+
ULTIMATE_PLAN = "ultimate"
17+
PREMIUM_PLAN = "premium"
1618
)
1719

1820
// fetchAll retrieves all projects and groups from the GitLab instance
@@ -104,3 +106,18 @@ func (g *GitlabInstance) CheckVersion() error {
104106
}
105107
return nil
106108
}
109+
110+
func (g *GitlabInstance) CheckLicense() error {
111+
license, _, err := g.Gitlab.License.GetLicense()
112+
if err != nil {
113+
return fmt.Errorf("failed to get GitLab license: %w", err)
114+
}
115+
if license.Plan != ULTIMATE_PLAN && license.Plan != PREMIUM_PLAN {
116+
return fmt.Errorf("GitLab license plan %s is not supported, only %s and %s are supported", license.Plan, ULTIMATE_PLAN, PREMIUM_PLAN)
117+
} else if license.Expired {
118+
return fmt.Errorf("GitLab license is expired")
119+
}
120+
121+
zap.L().Debug("GitLab Instance license", zap.String(ROLE, g.Role), zap.String("plan", license.Plan))
122+
return nil
123+
}

internal/mirroring/get_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,58 @@ func TestCheckVersion(t *testing.T) {
250250
})
251251
}
252252
}
253+
254+
func TestCheckLicense(t *testing.T) {
255+
tests := []struct {
256+
name string
257+
license string
258+
expectedError bool
259+
}{
260+
{
261+
name: "Ultimate tier license",
262+
license: "ultimate",
263+
expectedError: false,
264+
},
265+
{
266+
name: "Premium tier license",
267+
license: "premium",
268+
expectedError: false,
269+
},
270+
{
271+
name: "Free tier license",
272+
license: "free",
273+
expectedError: true,
274+
},
275+
{
276+
name: "Invalid license",
277+
license: "invalid",
278+
expectedError: true,
279+
},
280+
{
281+
name: "Empty license",
282+
license: "",
283+
expectedError: true,
284+
},
285+
}
286+
// Iterate over the test cases
287+
for _, test := range tests {
288+
t.Run(test.name, func(t *testing.T) {
289+
t.Parallel()
290+
291+
mux, gitlabInstance := setupEmptyTestServer(t, ROLE_DESTINATION, INSTANCE_SIZE_SMALL)
292+
mux.HandleFunc("/api/v4/license", func(w http.ResponseWriter, r *http.Request) {
293+
w.Header().Set("Content-Type", "application/json")
294+
w.WriteHeader(http.StatusOK)
295+
_, err := w.Write([]byte(`{"plan": "` + test.license + `"}`))
296+
if err != nil {
297+
t.Errorf("failed to write response: %v", err)
298+
}
299+
})
300+
301+
err := gitlabInstance.CheckLicense()
302+
if (err != nil) != test.expectedError {
303+
t.Errorf("expected error: %v, got: %v", test.expectedError, err)
304+
}
305+
})
306+
}
307+
}

internal/mirroring/helper_test.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -554,24 +554,6 @@ func setupTestProject(mux *http.ServeMux, project *gitlab.Project, stringRespons
554554
})
555555
}
556556

557-
// setupMetadata sets up the test HTTP server with handlers for metadata-related actions.
558-
// This includes the version endpoint.
559-
func setupMetadata(mux *http.ServeMux) {
560-
// Setup the version endpoint to return a mock response.
561-
mux.HandleFunc("/api/v4/metadata", func(w http.ResponseWriter, r *http.Request) {
562-
switch r.Method {
563-
case http.MethodGet:
564-
w.Header().Set(HEADER_CONTENT_TYPE, HEADER_ACCEPT)
565-
// Set response status to 200 OK
566-
w.WriteHeader(http.StatusOK)
567-
fmt.Fprint(w, `{"version": "18.0.0"}`)
568-
default:
569-
// Set response status to 405 Method Not Allowed
570-
w.WriteHeader(http.StatusMethodNotAllowed)
571-
}
572-
})
573-
}
574-
575557
func TestReverseGroupMirrorMap(t *testing.T) {
576558
tests := []struct {
577559
name string

internal/mirroring/main.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func MirrorGitlabs(gitlabMirrorArgs *utils.ParserArgs) []error {
4646
if err != nil {
4747
return []error{err}
4848
}
49-
err = destinationGitlabInstance.CheckVersion()
49+
err = destinationGitlabInstance.CheckDestinationInstance()
5050
if err != nil {
5151
return []error{err}
5252
}
@@ -143,3 +143,14 @@ func DryRun(sourceGitlabInstance *GitlabInstance, gitlabMirrorArgs *utils.Parser
143143
}
144144
}
145145
}
146+
147+
func (destinationGitlab *GitlabInstance) CheckDestinationInstance() error {
148+
zap.L().Info("Checking destination GitLab instance")
149+
if err := destinationGitlab.CheckVersion(); err != nil {
150+
return fmt.Errorf("destination GitLab instance version check failed: %w", err)
151+
}
152+
if err := destinationGitlab.CheckVersion(); err != nil {
153+
return fmt.Errorf("destination GitLab instance version check failed: %w", err)
154+
}
155+
return nil
156+
}

0 commit comments

Comments
 (0)