@@ -2,8 +2,12 @@ package plugin
22
33import (
44 "context"
5+ "crypto/hmac"
6+ "crypto/sha1" // #nosec G505
7+ "encoding/hex"
58 "fmt"
69 "testing"
10+ "time"
711
812 "github.com/golang/mock/gomock"
913 "github.com/google/go-github/v54/github"
@@ -15,24 +19,30 @@ import (
1519)
1620
1721const (
18- MockUserID = "mockUserID"
19- MockUsername = "mockUsername"
20- MockAccessToken = "mockAccessToken"
21- MockChannelID = "mockChannelID"
22- MockCreatorID = "mockCreatorID"
23- MockBotID = "mockBotID"
24- MockOrg = "mockOrg"
25- MockSender = "mockSender"
26- MockPostMessage = "mockPostMessage"
27- MockOrgRepo = "mockOrg/mockRepo"
28- MockHead = "mockHead"
29- MockRepoName = "mockRepoName"
30- MockEventReference = "refs/heads/main"
31- MockUserLogin = "mockUser"
32- MockBranch = "mockBranch"
33- MockRepo = "mockRepo"
34- MockIssueAuthor = "issueAuthor"
35- GithubBaseURL = "https://github.com/"
22+ MockUserID = "mockUserID"
23+ MockUsername = "mockUsername"
24+ MockAccessToken = "mockAccessToken"
25+ MockChannelID = "mockChannelID"
26+ MockCreatorID = "mockCreatorID"
27+ MockWebhookSecret = "mockWebhookSecret" // #nosec G101
28+ MockBotID = "mockBotID"
29+ MockOrg = "mockOrg"
30+ MockSender = "mockSender"
31+ MockPostMessage = "mockPostMessage"
32+ MockOrgRepo = "mockOrg/mockRepo"
33+ MockHead = "mockHead"
34+ MockPRTitle = "mockPRTitle"
35+ MockProfileUsername = "@username"
36+ MockPostID = "mockPostID"
37+ MockRepoName = "mockRepoName"
38+ MockEventReference = "refs/heads/main"
39+ MockUserLogin = "mockUser"
40+ MockBranch = "mockBranch"
41+ MockRepo = "mockRepo"
42+ MockLabel = "mockLabel"
43+ MockValidLabel = "validLabel"
44+ MockIssueAuthor = "issueAuthor"
45+ GithubBaseURL = "https://github.com/"
3646)
3747
3848type GitHubUserResponse struct {
@@ -93,6 +103,104 @@ func GetMockUserContext(p *Plugin, mockLogger *mocks.MockLogger) (*UserContext,
93103 return mockUserContext , nil
94104}
95105
106+ func generateSignature (secret , body []byte ) string {
107+ h := hmac .New (sha1 .New , secret )
108+ h .Write (body )
109+ return "sha1=" + hex .EncodeToString (h .Sum (nil ))
110+ }
111+
112+ func GetMockPingEvent () * github.PingEvent {
113+ return & github.PingEvent {
114+ Zen : github .String ("Keep it logically awesome." ),
115+ HookID : github .Int64 (123456 ),
116+ Hook : & github.Hook {
117+ Type : github .String ("Repository" ),
118+ ID : github .Int64 (654321 ),
119+ Config : map [string ]interface {}{
120+ "url" : "https://example.com/webhook" ,
121+ "content_type" : "json" ,
122+ "secret" : "mocksecret" ,
123+ "insecure_ssl" : "0" ,
124+ },
125+ Active : github .Bool (true ),
126+ },
127+ Repo : & github.Repository {
128+ Name : github .String (MockRepoName ),
129+ FullName : github .String (MockOrgRepo ),
130+ Private : github .Bool (false ),
131+ HTMLURL : github .String (fmt .Sprintf ("%s/%s" , GithubBaseURL , MockOrgRepo )),
132+ },
133+ Org : & github.Organization {
134+ Login : github .String ("mockorg" ),
135+ ID : github .Int64 (12345 ),
136+ URL : github .String (fmt .Sprintf ("%s/mockorg" , GithubBaseURL )),
137+ },
138+ Sender : & github.User {
139+ Login : github .String (MockUserLogin ),
140+ ID : github .Int64 (98765 ),
141+ URL : github .String (fmt .Sprintf ("%s/users/%s" , GithubBaseURL , MockUserLogin )),
142+ },
143+ Installation : & github.Installation {
144+ ID : github .Int64 (246810 ),
145+ NodeID : github .String ("MDQ6VXNlcjE=" ),
146+ },
147+ }
148+ }
149+
150+ func GetMockPRDescriptionEvent (repo , org , sender , prUser , action , label string ) * github.PullRequestEvent {
151+ return & github.PullRequestEvent {
152+ Action : github .String (action ),
153+ PullRequest : & github.PullRequest {
154+ Title : github .String (MockPRTitle ),
155+ Body : github .String ("Mock PR description with label: " + label ),
156+ State : github .String ("open" ),
157+ User : & github.User {Login : github .String (prUser )},
158+ Head : & github.PullRequestBranch {Ref : github .String (MockBranch )},
159+ Base : & github.PullRequestBranch {Ref : github .String ("main" )},
160+ HTMLURL : github .String (GithubBaseURL + org + "/" + repo + "/pull/1" ),
161+ Number : github .Int (1 ),
162+ },
163+ Repo : & github.Repository {
164+ Name : github .String (repo ),
165+ Owner : & github.User {Login : github .String (org )},
166+ FullName : github .String (org + "/" + repo ),
167+ },
168+ Sender : & github.User {
169+ Login : github .String (sender ),
170+ },
171+ }
172+ }
173+
174+ func GetMockIssueEvent (repo , org , sender , action , label string ) * github.IssuesEvent {
175+ event := & github.IssuesEvent {
176+ Repo : & github.Repository {
177+ Name : github .String (repo ),
178+ Owner : & github.User {Login : github .String (org )},
179+ FullName : github .String (fmt .Sprintf ("%s/%s" , repo , org )),
180+ },
181+ Sender : & github.User {Login : github .String (sender )},
182+ Issue : & github.Issue {
183+ Number : github .Int (123 ),
184+ Labels : []* github.Label {
185+ {Name : github .String (label )},
186+ },
187+ },
188+ Action : github .String (action ),
189+ }
190+
191+ if action == actionLabeled || action == "unlabeled" {
192+ event .Label = & github.Label {Name : github .String (label )}
193+ }
194+
195+ return event
196+ }
197+
198+ func GetMockIssueEventWithTimeDiff (repo , org , sender , action , label string , timeDiff time.Duration ) * github.IssuesEvent {
199+ event := GetMockIssueEvent (repo , org , sender , action , label )
200+ event .Issue .CreatedAt = & github.Timestamp {Time : time .Now ().Add (timeDiff )}
201+ return event
202+ }
203+
96204func GetMockPushEvent () * github.PushEvent {
97205 return & github.PushEvent {
98206 PushID : github .Int64 (1 ),
@@ -351,9 +459,10 @@ func GetMockIssueCommentEventWithAssignees(eventType, action, body, sender strin
351459 }
352460}
353461
354- func GetMockPullRequestEvent (action , repoName string , isPrivate bool , sender , user , assignee string ) * github.PullRequestEvent {
462+ func GetMockPullRequestEvent (action , repoName , eventLabel string , isPrivate bool , sender , user , assignee string ) * github.PullRequestEvent {
355463 return & github.PullRequestEvent {
356464 Action : github .String (action ),
465+ Label : & github.Label {Name : github .String (eventLabel )},
357466 Repo : & github.Repository {
358467 Name : github .String (repoName ),
359468 FullName : github .String (fmt .Sprintf ("mockOrg/%s" , repoName )),
@@ -364,6 +473,8 @@ func GetMockPullRequestEvent(action, repoName string, isPrivate bool, sender, us
364473 HTMLURL : github .String (fmt .Sprintf ("%s%s/%s/pull/123" , GithubBaseURL , MockOrgRepo , repoName )),
365474 Assignee : & github.User {Login : github .String (assignee )},
366475 RequestedReviewers : []* github.User {{Login : github .String (user )}},
476+ Labels : []* github.Label {{Name : github .String ("validLabel" )}},
477+ Draft : github .Bool (true ),
367478 },
368479 Sender : & github.User {
369480 Login : github .String (sender ),
0 commit comments