-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathattestJunit.go
More file actions
188 lines (159 loc) · 5.25 KB
/
attestJunit.go
File metadata and controls
188 lines (159 loc) · 5.25 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"fmt"
"io"
"net/http"
"os"
"github.com/kosli-dev/cli/internal/requests"
"github.com/spf13/cobra"
)
type JunitAttestationPayload struct {
*CommonAttestationPayload
JUnitResults []*JUnitResults `json:"junit_results"`
}
type attestJunitOptions struct {
*CommonAttestationOptions
testResultsDir string
uploadResultsDir bool
payload JunitAttestationPayload
}
const attestJunitShortDesc = `Report a junit attestation to an artifact or a trail in a Kosli flow. `
const attestJunitLongDesc = attestJunitShortDesc + attestationBindingDesc + `
` + commitDescription
const attestJunitExample = `
# report a junit attestation about a pre-built docker artifact (kosli calculates the fingerprint):
kosli attest junit yourDockerImageName \
--artifact-type docker \
--name yourAttestationName \
--flow yourFlowName \
--trail yourTrailName \
--results-dir yourFolderWithJUnitResults \
--api-token yourAPIToken \
--org yourOrgName
# report a junit attestation about a pre-built docker artifact (you provide the fingerprint):
kosli attest junit \
--fingerprint yourDockerImageFingerprint \
--name yourAttestationName \
--flow yourFlowName \
--trail yourTrailName \
--results-dir yourFolderWithJUnitResults \
--api-token yourAPIToken \
--org yourOrgName
# report a junit attestation about a trail:
kosli attest junit \
--name yourAttestationName \
--flow yourFlowName \
--trail yourTrailName \
--results-dir yourFolderWithJUnitResults \
--api-token yourAPIToken \
--org yourOrgName
# report a junit attestation about an artifact which has not been reported yet in a trail:
kosli attest junit \
--name yourTemplateArtifactName.yourAttestationName \
--flow yourFlowName \
--trail yourTrailName \
--commit yourArtifactGitCommit \
--results-dir yourFolderWithJUnitResults \
--api-token yourAPIToken \
--org yourOrgName
# report a junit attestation about a trail with an attachment:
kosli attest junit \
--name yourAttestationName \
--flow yourFlowName \
--trail yourTrailName \
--results-dir yourFolderWithJUnitResults \
--attachments yourAttachmentPathName \
--api-token yourAPIToken \
--org yourOrgName
`
func newAttestJunitCmd(out io.Writer) *cobra.Command {
o := &attestJunitOptions{
CommonAttestationOptions: &CommonAttestationOptions{
fingerprintOptions: &fingerprintOptions{},
},
payload: JunitAttestationPayload{
CommonAttestationPayload: &CommonAttestationPayload{},
},
}
cmd := &cobra.Command{
// Args: cobra.MaximumNArgs(1), // See CustomMaximumNArgs() below
Use: "junit [IMAGE-NAME | FILE-PATH | DIR-PATH]",
Short: attestJunitShortDesc,
Long: attestJunitLongDesc,
Example: attestJunitExample,
PreRunE: func(cmd *cobra.Command, args []string) error {
err := CustomMaximumNArgs(1, args)
if err != nil {
return err
}
err = RequireGlobalFlags(global, []string{"Org", "ApiToken"})
if err != nil {
return ErrorBeforePrintingUsage(cmd, err.Error())
}
err = MuXRequiredFlags(cmd, []string{"fingerprint", "artifact-type"}, false)
if err != nil {
return err
}
err = ValidateSliceValues(o.redactedCommitInfo, allowedCommitRedactionValues)
if err != nil {
return fmt.Errorf("%s for --redact-commit-info", err.Error())
}
err = ValidateAttestationArtifactArg(args, o.fingerprintOptions.artifactType, o.payload.ArtifactFingerprint)
if err != nil {
return ErrorBeforePrintingUsage(cmd, err.Error())
}
return ValidateRegistryFlags(cmd, o.fingerprintOptions)
},
RunE: func(cmd *cobra.Command, args []string) error {
return o.run(args)
},
}
ci := WhichCI()
addAttestationFlags(cmd, o.CommonAttestationOptions, o.payload.CommonAttestationPayload, ci)
cmd.Flags().StringVarP(&o.testResultsDir, "results-dir", "R", ".", resultsDirFlag)
cmd.Flags().BoolVar(&o.uploadResultsDir, "upload-results", true, uploadJunitResultsFlag)
err := RequireFlags(cmd, []string{"flow", "trail", "name"})
if err != nil {
logger.Error("failed to configure required flags: %v", err)
}
return cmd
}
func (o *attestJunitOptions) run(args []string) error {
url := fmt.Sprintf("%s/api/v2/attestations/%s/%s/trail/%s/junit", global.Host, global.Org, o.flowName, o.trailName)
err := o.CommonAttestationOptions.run(args, o.payload.CommonAttestationPayload)
if err != nil {
return err
}
o.payload.JUnitResults, err = ingestJunitDir(o.testResultsDir)
if err != nil {
return err
}
if o.uploadResultsDir {
// prepare the files to upload as attachments. We are only interested in the actual Junit XMl files
junitFilenames, err := getJunitFilenames(o.testResultsDir)
if err != nil {
return err
}
o.attachments = append(o.attachments, junitFilenames...)
}
form, cleanupNeeded, evidencePath, err := prepareAttestationForm(o.payload, o.attachments)
if err != nil {
return err
}
// if we created a tar package, remove it after uploading it
if cleanupNeeded {
defer os.Remove(evidencePath)
}
reqParams := &requests.RequestParams{
Method: http.MethodPost,
URL: url,
Form: form,
DryRun: global.DryRun,
Password: global.ApiToken,
}
_, err = kosliClient.Do(reqParams)
if err == nil && global.DryRun == "false" {
logger.Info("junit attestation '%s' is reported to trail: %s", o.payload.AttestationName, o.trailName)
}
return wrapAttestationError(err)
}