-
Notifications
You must be signed in to change notification settings - Fork 783
/
sourcerepository_impl.go
105 lines (92 loc) · 3.69 KB
/
sourcerepository_impl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package kube
import (
"fmt"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/client/clientset/versioned"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"reflect"
)
//SourceRepositoryService is the implementation of SourceRepoer
type SourceRepositoryService struct {
client versioned.Interface
namespace string
}
// NewSourceRepositoryService creates a new Service for interacting with the Source Repository Custom Resource
func NewSourceRepositoryService(client versioned.Interface, namespace string) SourceRepoer {
return &SourceRepositoryService{
client: client,
namespace: namespace,
}
}
// CreateOrUpdateSourceRepository creates a repo if it doesn't exist or updates it if the URL has changed
func (service *SourceRepositoryService) CreateOrUpdateSourceRepository(name, organisation, providerURL string) error {
//FIXME: repo is not always == name, need to find a better value for ObjectMeta.Name!
// for now lets convert to a safe name using the organisation + repo name
resourceName := ToValidName(organisation + "-" + name)
repositories := service.client.JenkinsV1().SourceRepositories(service.namespace)
description := fmt.Sprintf("Imported application for %s/%s", organisation, name)
_, err := repositories.Create(&v1.SourceRepository{
ObjectMeta: metav1.ObjectMeta{
Name: resourceName,
},
Spec: v1.SourceRepositorySpec{
Description: description,
Org: organisation,
Provider: providerURL,
Repo: name,
},
})
if err != nil {
// lets see if it already exists
sr, err := repositories.Get(resourceName, metav1.GetOptions{})
if err != nil {
return errors.Wrapf(err, "failed to get SourceRepository %s after failing to create a new one", resourceName)
}
copy := *sr
copy.Spec.Description = description
copy.Spec.Org = organisation
copy.Spec.Provider = providerURL
copy.Spec.Repo = name
if !reflect.DeepEqual(©.Spec, sr.Spec) {
_, err = repositories.Update(©)
if err != nil {
return errors.Wrapf(err, "failed to update SourceRepository %s", resourceName)
}
}
}
return nil
}
// CreateSourceRepository creates a repo. If a repo already exists, it will return an error
func (service *SourceRepositoryService) CreateSourceRepository(name, organisation, providerURL string) error {
//FIXME: repo is not always == name, need to find a better value for ObjectMeta.Name!
// for now lets convert to a safe name using the organisation + repo name
resourceName := ToValidName(organisation + "-" + name)
_, err := service.client.JenkinsV1().SourceRepositories(service.namespace).Create(&v1.SourceRepository{
ObjectMeta: metav1.ObjectMeta{
Name: resourceName,
},
Spec: v1.SourceRepositorySpec{
Description: fmt.Sprintf("Imported application for %s/%s", organisation, name),
Org: organisation,
Provider: providerURL,
Repo: name,
},
})
if err != nil {
return err
}
return nil
}
// GetSourceRepository gets repo, if it exists and returns an error otherwise
func (service *SourceRepositoryService) GetSourceRepository(name string) (*v1.SourceRepository, error) {
return service.client.JenkinsV1().SourceRepositories(service.namespace).Get(name, metav1.GetOptions{})
}
// DeleteSourceRepository deletes a repo
func (service *SourceRepositoryService) DeleteSourceRepository(name string) error {
return service.client.JenkinsV1().SourceRepositories(service.namespace).Delete(name, &metav1.DeleteOptions{})
}
// ListSourceRepositories gets a list of all the repos
func (service *SourceRepositoryService) ListSourceRepositories() (*v1.SourceRepositoryList, error) {
return service.client.JenkinsV1().SourceRepositories(service.namespace).List(metav1.ListOptions{})
}