-
Notifications
You must be signed in to change notification settings - Fork 787
/
step_pre_check_compliance.go
123 lines (108 loc) · 3.02 KB
/
step_pre_check_compliance.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package cmd
import (
"fmt"
"io"
"os"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
jenkinsv1 "github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
jenkinsv1client "github.com/jenkins-x/jx/pkg/client/clientset/versioned"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
// StepPreBuildOptions contains the command line flags
type StepPreCheckComplianceOptions struct {
StepOptions
}
var (
StepPreCheckComplianceLong = templates.LongDesc(`
This pipeline step adds a pending state on compliance checks
`)
StepPreCheckComplianceExample = templates.Examples(`
jx step pre check compliance
`)
)
func NewCmdStepPreCheckCompliance(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := StepPreCheckComplianceOptions{
StepOptions: StepOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "compliance",
Short: "Adds a pending state",
Long: StepPreCheckComplianceLong,
Example: StepPreCheckComplianceExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
return cmd
}
func (o *StepPreCheckComplianceOptions) Run() error {
jxClient, ns, err := o.JXClientAndDevNamespace()
if err != nil {
return err
}
apisClient, err := o.CreateApiExtensionsClient()
if err != nil {
return err
}
err = kube.RegisterComplianceCheckCRD(apisClient)
if err != nil {
return err
}
err = kube.RegisterPipelineActivityCRD(apisClient)
if err != nil {
return err
}
err = o.Init(jxClient, ns)
return nil
}
func (o *StepPreCheckComplianceOptions) Init(jxClient jenkinsv1client.Interface, ns string) error {
org := os.Getenv("REPO_OWNER")
repo := os.Getenv("REPO_NAME")
pullRequest := os.Getenv("PULL_REQUEST")
sha := os.Getenv("PULL_PULL_SHA")
// TODO Fix this once PROW supports other providers
url := fmt.Sprintf("https://github.com/%s/%s.git", org, repo)
pipeline := fmt.Sprintf("%s-%s", org, repo)
if pipeline != "" && sha != "" {
name := kube.ToValidName(pipeline + "-" + sha)
_, err := jxClient.JenkinsV1().ComplianceChecks(ns).Get(name, metav1.GetOptions{})
if err != nil {
log.Infof("Creating compliance check for %s\n", name)
_, err := jxClient.JenkinsV1().ComplianceChecks(ns).Create(&jenkinsv1.ComplianceCheck{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: jenkinsv1.ComplianceCheckSpec{
Checked: false,
Commit: jenkinsv1.ComplianceCheckCommitReference{
GitURL: url,
PullRequest: pullRequest,
SHA: sha,
},
},
})
if err != nil {
return err
}
} else {
log.Infof("Compliance Check already exists for %s\n", name)
}
} else {
log.Errorf("Cannot determine pipeline (found %s) and sha (found %s) for compliance check\n", pipeline, sha)
}
return nil
}