-
Notifications
You must be signed in to change notification settings - Fork 0
/
pr_state.go
150 lines (118 loc) · 3.36 KB
/
pr_state.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
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
package simver
import (
"context"
"encoding/json"
"errors"
"github.com/rs/zerolog"
)
var _ Execution = &ActivePRProjectState{}
type ActivePRProjectState struct {
CurrentPR *PRDetails
CurrentRootBranchTags Tags
CurrentRootCommitTags Tags
CurrentHeadCommitTags Tags
CurrentBaseCommitTags Tags
CurrentBaseBranchTags Tags
CurrentHeadBranchTags Tags
}
func (e *ActivePRProjectState) ProvideRefs() RefProvider {
return e.CurrentPR
}
func (e *ActivePRProjectState) BaseBranchTags() Tags {
return e.CurrentBaseBranchTags
}
func (e *ActivePRProjectState) HeadBranchTags() Tags {
return e.CurrentHeadBranchTags
}
func (e *ActivePRProjectState) PR() int {
return e.CurrentPR.Number
}
func (e *ActivePRProjectState) IsMerge() bool {
return !e.CurrentPR.IsSimulatedPush() && e.CurrentPR.Merged
}
func (e *ActivePRProjectState) RootBranch() string {
return e.CurrentPR.RootBranch
}
func (e *ActivePRProjectState) RootBranchTags() Tags {
return e.CurrentRootBranchTags
}
func (e *ActivePRProjectState) IsTargetingRoot() bool {
return e.CurrentPR.BaseBranch == e.CurrentPR.RootBranch
}
func (e *ActivePRProjectState) HeadCommitTags() Tags {
return e.CurrentHeadCommitTags
}
func (e *ActivePRProjectState) IsDirty() bool {
return false
}
func (e *ActivePRProjectState) IsLocal() bool {
return false
}
func LoadExecutionFromPR(ctx context.Context, tprov TagReader, prr PRResolver) (Execution, *PRDetails, error) {
pr, err := prr.CurrentPR(ctx)
if err != nil {
return nil, nil, err
}
baseCommitTags, err := tprov.TagsFromCommit(ctx, pr.BaseCommit)
if err != nil {
return nil, nil, err
}
baseBranchTags, err := tprov.TagsFromBranch(ctx, pr.BaseBranch)
if err != nil {
return nil, nil, err
}
rootCommitTags, err := tprov.TagsFromCommit(ctx, pr.RootCommit)
if err != nil {
return nil, nil, err
}
rootBranchTags, err := tprov.TagsFromBranch(ctx, pr.RootBranch)
if err != nil {
return nil, nil, err
}
var headBranchTags Tags
var headCommit string
if pr.Merged {
headCommit = pr.MergeCommit
headBranchTags = baseBranchTags
} else {
headCommit = pr.HeadCommit
branchTags, err := tprov.TagsFromBranch(ctx, pr.HeadBranch)
if err != nil {
return nil, nil, err
}
headBranchTags = branchTags
}
headTags, err := tprov.TagsFromCommit(ctx, headCommit)
if err != nil {
return nil, nil, err
}
ex := &ActivePRProjectState{
CurrentPR: pr,
CurrentHeadCommitTags: headTags,
CurrentBaseCommitTags: baseCommitTags,
CurrentBaseBranchTags: baseBranchTags,
CurrentHeadBranchTags: headBranchTags,
CurrentRootBranchTags: rootBranchTags,
CurrentRootCommitTags: rootCommitTags,
}
zerolog.Ctx(ctx).Debug().
Array("CurrentRootBranchTags", ex.CurrentRootBranchTags).
Array("CurrentRootCommitTags", ex.CurrentRootCommitTags).
Array("CurrentHeadCommitTags", ex.CurrentHeadCommitTags).
Array("CurrentBaseCommitTags", ex.CurrentBaseCommitTags).
Array("CurrentBaseBranchTags", ex.CurrentBaseBranchTags).
Array("CurrentHeadBranchTags", ex.CurrentHeadBranchTags).
Bool("IsTargetingRoot", ex.IsTargetingRoot()).
Msg("loaded tags")
return ex, pr, nil
}
func ExecutionAsString(ctx context.Context, e Execution) (string, error) {
if ex, ok := e.(*ActivePRProjectState); ok {
jsn, err := json.Marshal(ex)
if err != nil {
return "", err
}
return string(jsn), nil
}
return "", errors.New("execution is not an ActiveProjectState")
}