-
Notifications
You must be signed in to change notification settings - Fork 35
/
pipeline.go
78 lines (72 loc) · 1.94 KB
/
pipeline.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
package visualizer
import (
"strings"
"time"
jenkinsv1 "github.com/jenkins-x/jx-api/v4/pkg/apis/jenkins.io/v1"
)
type Pipeline struct {
Name string
Namespace string
Provider string
Owner string
Repository string
Branch string
Build string
Context string
Author string
AuthorAvatarURL string
Commit string
Status string
Description string
Start time.Time
End time.Time
Duration time.Duration
GitUrl string
}
func (p Pipeline) PullRequestNumber() string {
if strings.HasPrefix(p.Branch, "PR-") {
return strings.TrimPrefix(p.Branch, "PR-")
}
return ""
}
func PipelineFromPipelineActivity(pa *jenkinsv1.PipelineActivity) Pipeline {
p := Pipeline{
Name: pa.Name,
Provider: pa.Labels["provider"],
Owner: pa.Spec.GitOwner,
Repository: pa.Spec.GitRepository,
Branch: pa.Spec.GitBranch,
Build: pa.Spec.Build,
Context: getContext(pa),
Author: pa.Spec.Author,
AuthorAvatarURL: pa.Spec.AuthorAvatarURL,
Commit: pa.Spec.LastCommitSHA,
Status: string(pa.Spec.Status),
Description: pa.Annotations["description"],
Namespace: pa.Namespace,
GitUrl: strings.TrimSuffix(pa.Spec.GitURL, ".git"),
}
if pa.Spec.StartedTimestamp != nil {
p.Start = pa.Spec.StartedTimestamp.Time
} else {
p.Start = pa.CreationTimestamp.Time
}
if pa.Spec.CompletedTimestamp != nil {
p.End = pa.Spec.CompletedTimestamp.Time
}
if !p.Start.IsZero() && !p.End.IsZero() {
p.Duration = p.End.Sub(p.Start)
}
return p
}
func getContext(pa *jenkinsv1.PipelineActivity) string {
if pa.Spec.Context != "" {
return pa.Spec.Context
}
for _, label := range []string{"context", "lighthouse.jenkins-x.io/context"} {
if pipelineContext, found := pa.Labels[label]; found {
return pipelineContext
}
}
return ""
}