Skip to content

Commit d2c5a8f

Browse files
committed
Convert v1alpha1 Github projects to v1alpha2 Git projects
When converting a devfile in v1alpha1 that contains projects with type Github, convert those projects to Git-type projects in v1alpha2 and apply attribute 'conversion.api.devfile.io/converted-from=GitHub' to them. When converting from v1alpha2, convert Git-type projects with the above attribute back to Github-type projects in v1alpha1. Reenable testing conversion for Github-type projects in v1alpha1 now that it is supported. Signed-off-by: Angel Misevski <[email protected]>
1 parent a5a1cda commit d2c5a8f

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

Diff for: pkg/apis/workspaces/v1alpha1/conversion.go

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import (
44
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
55
)
66

7+
const (
8+
// ConvertedFromAttribute marks a devfile element as being converted from a different underlying field. For example,
9+
// since v1alpha2 does not support
10+
ConvertedFromAttribute = "conversion.api.devfile.io/converted-from"
11+
)
12+
713
func convertDevWorkspaceTo_v1alpha2(src *DevWorkspace, dest *v1alpha2.DevWorkspace) error {
814
dest.ObjectMeta = src.ObjectMeta
915
dest.Status.DevWorkspaceId = src.Status.WorkspaceId

Diff for: pkg/apis/workspaces/v1alpha1/conversion_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -148,21 +148,25 @@ var parentCommandFuzzFunc = func(command *Command, c fuzz.Continue) {
148148
var parentProjectFuzzFunc = func(project *Project, c fuzz.Continue) {
149149
// Custom projects are not supported in v1alpha2 parent
150150
project.Name = c.RandString()
151-
switch c.Intn(2) {
151+
switch c.Intn(3) {
152152
case 0:
153153
c.Fuzz(&project.Git)
154154
case 1:
155+
c.Fuzz(&project.Github)
156+
case 2:
155157
c.Fuzz(&project.Zip)
156158
}
157159
}
158160

159161
var projectFuzzFunc = func(project *Project, c fuzz.Continue) {
160-
switch c.Intn(3) {
162+
switch c.Intn(4) {
161163
case 0:
162164
c.Fuzz(&project.Git)
163165
case 1:
164-
c.Fuzz(&project.Zip)
166+
c.Fuzz(&project.Github)
165167
case 2:
168+
c.Fuzz(&project.Zip)
169+
case 3:
166170
c.Fuzz(&project.Custom)
167171
}
168172
}

Diff for: pkg/apis/workspaces/v1alpha1/projects_conversion.go

+62
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,25 @@ import (
44
"encoding/json"
55

66
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
7+
"github.com/devfile/api/v2/pkg/attributes"
8+
)
9+
10+
const (
11+
GitHubConversionFromAttributeValue = "GitHub"
712
)
813

914
func convertProjectTo_v1alpha2(src *Project, dest *v1alpha2.Project) error {
15+
// Convert Github type projects in v1alpha1 to Git-type projects in v1alpha2, since Github was dropped
16+
if src.Github != nil {
17+
src.Git = &GitProjectSource{
18+
GitLikeProjectSource: src.Github.GitLikeProjectSource,
19+
}
20+
if dest.Attributes == nil {
21+
dest.Attributes = attributes.Attributes{}
22+
}
23+
dest.Attributes.PutString(ConvertedFromAttribute, GitHubConversionFromAttributeValue)
24+
}
25+
1026
jsonProject, err := json.Marshal(src)
1127
if err != nil {
1228
return err
@@ -15,6 +31,7 @@ func convertProjectTo_v1alpha2(src *Project, dest *v1alpha2.Project) error {
1531
if err != nil {
1632
return err
1733
}
34+
1835
var sparseCheckoutDir string
1936
switch {
2037
case src.Git != nil:
@@ -25,6 +42,12 @@ func convertProjectTo_v1alpha2(src *Project, dest *v1alpha2.Project) error {
2542
if sparseCheckoutDir != "" {
2643
dest.SparseCheckoutDirs = []string{sparseCheckoutDir}
2744
}
45+
46+
// Make sure we didn't modify underlying src struct
47+
if src.Github != nil {
48+
src.Git = nil
49+
}
50+
2851
return nil
2952
}
3053

@@ -51,10 +74,33 @@ func convertProjectFrom_v1alpha2(src *v1alpha2.Project, dest *Project) error {
5174
dest.Zip.SparseCheckoutDir = sparseCheckoutDir
5275
}
5376
}
77+
78+
// Check if a Git-type project was originally a Github-type project in v1alpha1
79+
if src.Git != nil && src.Attributes != nil {
80+
convertedFrom := src.Attributes.GetString(ConvertedFromAttribute, nil)
81+
if convertedFrom == GitHubConversionFromAttributeValue {
82+
dest.Github = &GithubProjectSource{
83+
GitLikeProjectSource: dest.Git.GitLikeProjectSource,
84+
}
85+
dest.Git = nil
86+
}
87+
}
88+
5489
return nil
5590
}
5691

5792
func convertStarterProjectTo_v1alpha2(src *StarterProject, dest *v1alpha2.StarterProject) error {
93+
// Convert Github type projects in v1alpha1 to Git-type projects in v1alpha2, since Github was dropped
94+
if src.Github != nil {
95+
src.Git = &GitProjectSource{
96+
GitLikeProjectSource: src.Github.GitLikeProjectSource,
97+
}
98+
if dest.Attributes == nil {
99+
dest.Attributes = attributes.Attributes{}
100+
}
101+
dest.Attributes.PutString(ConvertedFromAttribute, GitHubConversionFromAttributeValue)
102+
}
103+
58104
jsonProject, err := json.Marshal(src)
59105
if err != nil {
60106
return err
@@ -74,6 +120,11 @@ func convertStarterProjectTo_v1alpha2(src *StarterProject, dest *v1alpha2.Starte
74120
dest.SubDir = src.Zip.SparseCheckoutDir
75121
}
76122

123+
// Make sure we didn't modify underlying src struct
124+
if src.Github != nil {
125+
src.Git = nil
126+
}
127+
77128
return nil
78129
}
79130

@@ -96,5 +147,16 @@ func convertStarterProjectFrom_v1alpha2(src *v1alpha2.StarterProject, dest *Star
96147
}
97148
}
98149

150+
// Check if a Git-type project was originally a Github-type project in v1alpha1
151+
if src.Git != nil && src.Attributes != nil {
152+
convertedFrom := src.Attributes.GetString(ConvertedFromAttribute, nil)
153+
if convertedFrom == GitHubConversionFromAttributeValue {
154+
dest.Github = &GithubProjectSource{
155+
GitLikeProjectSource: dest.Git.GitLikeProjectSource,
156+
}
157+
dest.Git = nil
158+
}
159+
}
160+
99161
return nil
100162
}

0 commit comments

Comments
 (0)