-
Notifications
You must be signed in to change notification settings - Fork 7
/
job.go
97 lines (87 loc) · 2.38 KB
/
job.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
package webui
import (
"regexp"
"strings"
"time"
lhv1alpha1 "github.com/jenkins-x/lighthouse/pkg/apis/lighthouse/v1alpha1"
)
type Jobs struct {
Jobs []Job
Counts struct {
States map[string]int
Types map[string]int
Repositories map[string]int
Authors map[string]int
}
}
type Job struct {
Name string
Type string
EventGUID string
Owner string
Repository string
Branch string
Build string
Context string
Author string
State string
Description string
ReportURL string
TraceID string
Start time.Time
End time.Time
Duration time.Duration
}
func (j Job) PullRequestNumber() string {
if strings.HasPrefix(j.Branch, "PR-") {
return strings.TrimPrefix(j.Branch, "PR-")
}
return ""
}
func JobFromLighthouseJob(lhjob *lhv1alpha1.LighthouseJob) Job {
j := Job{
Name: lhjob.Name,
Type: string(lhjob.Spec.Type),
EventGUID: lhjob.Labels["event-GUID"],
Owner: lhjob.Labels["lighthouse.jenkins-x.io/refs.org"],
Repository: lhjob.Labels["lighthouse.jenkins-x.io/refs.repo"],
Branch: lhjob.Labels["lighthouse.jenkins-x.io/branch"],
Build: lhjob.Labels["lighthouse.jenkins-x.io/buildNum"],
Context: lhjob.Labels["lighthouse.jenkins-x.io/context"],
State: string(lhjob.Status.State),
Description: lhjob.Status.Description,
ReportURL: lhjob.Status.ReportURL,
TraceID: extractTraceIDFromLighthouseJob(lhjob),
Start: lhjob.Status.StartTime.Time,
}
if lhjob.Status.CompletionTime != nil {
j.End = lhjob.Status.CompletionTime.Time
j.Duration = j.End.Sub(j.Start)
}
if lhjob.Spec.Refs != nil {
for _, pr := range lhjob.Spec.Refs.Pulls {
if pr.Author != "" && j.Author == "" {
j.Author = pr.Author
}
}
}
return j
}
var traceCtxRegExp = regexp.MustCompile("^(?P<version>[0-9a-f]{2})-(?P<traceID>[a-f0-9]{32})-(?P<spanID>[a-f0-9]{16})-(?P<traceFlags>[a-f0-9]{2})(?:-.*)?$")
func extractTraceIDFromLighthouseJob(lhjob *lhv1alpha1.LighthouseJob) string {
if traceID := lhjob.Annotations["lighthouse.jenkins-x.io/traceID"]; traceID != "" {
return traceID
}
traceCtx := lhjob.Annotations["lighthouse.jenkins-x.io/traceparent"]
if traceCtx == "" {
return ""
}
matches := traceCtxRegExp.FindStringSubmatch(traceCtx)
if len(matches) < 5 {
return ""
}
if len(matches[2]) != 32 {
return ""
}
return matches[2]
}