@@ -265,6 +265,52 @@ func TestGetTemplater_Github_PullRequestComment(t *testing.T) {
265265 assert .Equal (t , "This is a comment" , notification .GitHub .PullRequestComment .Content )
266266}
267267
268+ func TestGetTemplater_Github_RepositoryDispatch (t * testing.T ) {
269+ n := Notification {
270+ GitHub : & GitHubNotification {
271+ RepoURLPath : "{{.sync.spec.git.repo}}" ,
272+ RevisionPath : "{{.sync.status.lastSyncedCommit}}" ,
273+ RepositoryDispatch : & GitHubRepositoryDispatch {
274+ EventType : "sync" ,
275+ ClientPayload : `{ "sha": "{{.sync.status.lastSyncedCommit}}" }` ,
276+ },
277+ },
278+ }
279+ templater , err := n .GetTemplater ("" , template.FuncMap {})
280+
281+ if ! assert .NoError (t , err ) {
282+ return
283+ }
284+
285+ var notification Notification
286+ err = templater (& notification , map [string ]interface {}{
287+ "sync" : map [string ]interface {}{
288+ "metadata" : map [string ]interface {}{
289+ "name" : "root-sync-test" ,
290+ },
291+ "spec" : map [string ]interface {}{
292+ "git" : map [string ]interface {}{
293+ "repo" : "https://github.com/argoproj-labs/argocd-notifications.git" ,
294+ },
295+ },
296+ "status" : map [string ]interface {}{
297+ "lastSyncedCommit" : "0123456789" ,
298+ },
299+ },
300+ })
301+
302+ if ! assert .NoError (t , err ) {
303+ return
304+ }
305+
306+ assert .Equal (t , "{{.sync.spec.git.repo}}" , notification .GitHub .RepoURLPath )
307+ assert .Equal (t , "{{.sync.status.lastSyncedCommit}}" , notification .GitHub .RevisionPath )
308+ assert .Equal (t , "https://github.com/argoproj-labs/argocd-notifications.git" , notification .GitHub .repoURL )
309+ assert .Equal (t , "0123456789" , notification .GitHub .revision )
310+ assert .Equal (t , "sync" , notification .GitHub .RepositoryDispatch .EventType )
311+ assert .Equal (t , `{ "sha": "0123456789" }` , notification .GitHub .RepositoryDispatch .ClientPayload )
312+ }
313+
268314func TestGetTemplater_Github_PullRequestCommentWithTag (t * testing.T ) {
269315 n := Notification {
270316 GitHub : & GitHubNotification {
@@ -357,7 +403,9 @@ type mockPullRequestsService struct {
357403 prs []* github.PullRequest
358404}
359405
360- type mockRepositoriesService struct {}
406+ type mockRepositoriesService struct {
407+ dispatches []github.DispatchRequestOptions
408+ }
361409
362410func (m * mockRepositoriesService ) CreateStatus (ctx context.Context , owner , repo , ref string , status * github.RepoStatus ) (* github.RepoStatus , * github.Response , error ) {
363411 return status , nil , nil
@@ -375,6 +423,11 @@ func (m *mockRepositoriesService) CreateDeploymentStatus(ctx context.Context, ow
375423 return & github.DeploymentStatus {}, nil , nil
376424}
377425
426+ func (m * mockRepositoriesService ) Dispatch (ctx context.Context , owner , repo string , request github.DispatchRequestOptions ) (* github.Repository , * github.Response , error ) {
427+ m .dispatches = append (m .dispatches , request )
428+ return & github.Repository {}, nil , nil
429+ }
430+
378431type mockChecksService struct {}
379432
380433func (m * mockChecksService ) CreateCheckRun (ctx context.Context , owner , repo string , opts github.CreateCheckRunOptions ) (* github.CheckRun , * github.Response , error ) {
@@ -394,20 +447,46 @@ func (m *mockGitHubClientImpl) GetPullRequests() pullRequestsService { return m.
394447func (m * mockGitHubClientImpl ) GetRepositories () repositoriesService { return m .repos }
395448func (m * mockGitHubClientImpl ) GetChecks () checksService { return m .checks }
396449
397- func setupMockServices () (* mockIssuesService , * mockPullRequestsService , githubClient ) {
450+ func setupMockServices () (* mockIssuesService , * mockPullRequestsService , * mockRepositoriesService , githubClient ) {
398451 issues := & mockIssuesService {comments : []* github.IssueComment {}}
399452 pulls := & mockPullRequestsService {prs : []* github.PullRequest {{Number : github .Ptr (1 )}}}
453+ repos := & mockRepositoriesService {}
400454 client := & mockGitHubClientImpl {
401455 issues : issues ,
402456 prs : pulls ,
403- repos : & mockRepositoriesService {} ,
457+ repos : repos ,
404458 checks : & mockChecksService {},
405459 }
406- return issues , pulls , client
460+ return issues , pulls , repos , client
461+ }
462+
463+ func TestGitHubService_Send_RepositoryDispatch (t * testing.T ) {
464+ _ , _ , repos , client := setupMockServices ()
465+
466+ service := & gitHubService {client : client }
467+
468+ err := service .Send (Notification {
469+ GitHub : & GitHubNotification {
470+ repoURL : "https://github.com/owner/repo" ,
471+ revision : "abc123" ,
472+ RepositoryDispatch : & GitHubRepositoryDispatch {
473+ EventType : "sync" ,
474+ ClientPayload : `{ "sha": "12345678" }` ,
475+ },
476+ },
477+ }, Destination {})
478+
479+ assert .NoError (t , err )
480+ assert .Len (t , repos .dispatches , 1 )
481+ assert .Equal (t , repos .dispatches [0 ].EventType , "sync" )
482+
483+ payload , err := repos .dispatches [0 ].ClientPayload .MarshalJSON ()
484+ assert .NoError (t , err )
485+ assert .Equal (t , string (payload ), `{ "sha": "12345678" }` )
407486}
408487
409488func TestGitHubService_Send_PullRequestCommentWithTag (t * testing.T ) {
410- issues , _ , client := setupMockServices ()
489+ issues , _ , _ , client := setupMockServices ()
411490
412491 service := & gitHubService {client : client }
413492
0 commit comments