-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpullrequest.go
148 lines (141 loc) · 4.54 KB
/
pullrequest.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
package vsts
import (
"encoding/base64"
"encoding/json"
"fmt"
"os"
"strings"
"time"
)
// PullRequest is a pull request from VSTS
type PullRequest struct {
ID string `json:"id"`
EventType string `json:"eventType"`
PublisherID string `json:"publisherId"`
Resource struct {
Repository struct {
ID string `json:"id"`
Name string `json:"name"`
URL string `json:"url"`
Project struct {
ID string `json:"id"`
Name string `json:"name"`
URL string `json:"url"`
State string `json:"state"`
Revision int `json:"revision"`
Visibility string `json:"visibility"`
} `json:"project"`
RemoteURL string `json:"remoteUrl"`
SSHURL string `json:"sshUrl"`
} `json:"repository"`
PullRequestID int `json:"pullRequestId"`
CodeReviewID int `json:"codeReviewId"`
Status string `json:"status"`
CreatedBy struct {
DisplayName string `json:"displayName"`
URL string `json:"url"`
ID string `json:"id"`
UniqueName string `json:"uniqueName"`
ImageURL string `json:"imageUrl"`
Descriptor string `json:"descriptor"`
} `json:"createdBy"`
CreationDate time.Time `json:"creationDate"`
Title string `json:"title"`
Description string `json:"description"`
SourceRefName string `json:"sourceRefName"`
TargetRefName string `json:"targetRefName"`
MergeStatus string `json:"mergeStatus"`
MergeID string `json:"mergeId"`
LastMergeSourceCommit struct {
CommitID string `json:"commitId"`
URL string `json:"url"`
} `json:"lastMergeSourceCommit"`
LastMergeTargetCommit struct {
CommitID string `json:"commitId"`
URL string `json:"url"`
} `json:"lastMergeTargetCommit"`
LastMergeCommit struct {
CommitID string `json:"commitId"`
Author struct {
Name string `json:"name"`
Email string `json:"email"`
Date time.Time `json:"date"`
} `json:"author"`
Committer struct {
Name string `json:"name"`
Email string `json:"email"`
Date time.Time `json:"date"`
} `json:"committer"`
Comment string `json:"comment"`
URL string `json:"url"`
} `json:"lastMergeCommit"`
Reviewers []struct {
ReviewerURL string `json:"reviewerUrl"`
Vote int `json:"vote"`
DisplayName string `json:"displayName"`
URL string `json:"url"`
ID string `json:"id"`
UniqueName string `json:"uniqueName"`
ImageURL string `json:"imageUrl"`
IsContainer bool `json:"isContainer,omitempty"`
VotedFor []struct {
ReviewerURL string `json:"reviewerUrl"`
Vote int `json:"vote"`
DisplayName string `json:"displayName"`
URL string `json:"url"`
ID string `json:"id"`
UniqueName string `json:"uniqueName"`
ImageURL string `json:"imageUrl"`
IsContainer bool `json:"isContainer"`
} `json:"votedFor,omitempty"`
} `json:"reviewers"`
URL string `json:"url"`
Links struct {
Web struct {
Href string `json:"href"`
} `json:"web"`
Statuses struct {
Href string `json:"href"`
} `json:"statuses"`
} `json:"_links"`
SupportsIterations bool `json:"supportsIterations"`
ArtifactID string `json:"artifactId"`
} `json:"resource"`
ResourceVersion string `json:"resourceVersion"`
ResourceContainers struct {
Collection struct {
ID string `json:"id"`
BaseURL string `json:"baseUrl"`
} `json:"collection"`
Account struct {
ID string `json:"id"`
BaseURL string `json:"baseUrl"`
} `json:"account"`
Project struct {
ID string `json:"id"`
BaseURL string `json:"baseUrl"`
} `json:"project"`
} `json:"resourceContainers"`
CreatedDate time.Time `json:"createdDate"`
}
// ParsePullRequest parse pull request from encoded string
func ParsePullRequest() (*PullRequest, error) {
encodedPRContentString := os.Getenv("PR_CONTENT")
if len(encodedPRContentString) == 0 {
return nil, fmt.Errorf("env PR_CONTENT not found")
}
prContentBytes, err := base64.StdEncoding.DecodeString(encodedPRContentString)
if err != nil {
return nil, err
}
prContentString := string(prContentBytes)
prContentRaw := prContentString[strings.Index(prContentString, "{"):(strings.LastIndex(prContentString, "}") + 1)]
prContent := PullRequest{}
if err := json.Unmarshal([]byte(prContentRaw), &prContent); err != nil {
return nil, err
}
if strings.EqualFold(prContent.ID, "") {
return nil, fmt.Errorf("PR ID is empty: %v", prContentString)
}
return &prContent, nil
}