@@ -310,6 +310,72 @@ func TestGitHubCommitEvent_AppsInOtherNamespaces(t *testing.T) {
310310 hook .Reset ()
311311}
312312
313+ // TestGitHubCommitEvent_Hydrate makes sure that a webhook will hydrate an app when dry source changed.
314+ func TestGitHubCommitEvent_Hydrate (t * testing.T ) {
315+ hook := test .NewGlobal ()
316+ var patched bool
317+ reaction := func (action kubetesting.Action ) (handled bool , ret runtime.Object , err error ) {
318+ patchAction := action .(kubetesting.PatchAction )
319+ assert .Equal (t , "app-to-hydrate" , patchAction .GetName ())
320+ patched = true
321+ return true , nil , nil
322+ }
323+ h := NewMockHandler (& reactorDef {"patch" , "applications" , reaction }, []string {}, & v1alpha1.Application {
324+ ObjectMeta : metav1.ObjectMeta {
325+ Name : "app-to-hydrate" ,
326+ Namespace : "argocd" ,
327+ },
328+ Spec : v1alpha1.ApplicationSpec {
329+ SourceHydrator : & v1alpha1.SourceHydrator {
330+ DrySource : v1alpha1.DrySource {
331+ RepoURL : "https://github.com/jessesuen/test-repo" ,
332+ TargetRevision : "HEAD" ,
333+ Path : "." ,
334+ },
335+ SyncSource : v1alpha1.SyncSource {
336+ TargetBranch : "environments/dev" ,
337+ Path : "." ,
338+ },
339+ HydrateTo : nil ,
340+ },
341+ },
342+ }, & v1alpha1.Application {
343+ ObjectMeta : metav1.ObjectMeta {
344+ Name : "app-to-ignore" ,
345+ },
346+ Spec : v1alpha1.ApplicationSpec {
347+ Sources : v1alpha1.ApplicationSources {
348+ {
349+ RepoURL : "https://github.com/some/unrelated-repo" ,
350+ Path : "." ,
351+ },
352+ },
353+ },
354+ },
355+ )
356+ req := httptest .NewRequest (http .MethodPost , "/api/webhook" , nil )
357+ req .Header .Set ("X-GitHub-Event" , "push" )
358+ eventJSON , err := os .ReadFile ("testdata/github-commit-event.json" )
359+ require .NoError (t , err )
360+ req .Body = io .NopCloser (bytes .NewReader (eventJSON ))
361+ w := httptest .NewRecorder ()
362+ h .Handler (w , req )
363+ close (h .queue )
364+ h .Wait ()
365+ assert .Equal (t , http .StatusOK , w .Code )
366+ assert .True (t , patched )
367+
368+ logMessages := make ([]string , 0 , len (hook .Entries ))
369+ for _ , entry := range hook .Entries {
370+ logMessages = append (logMessages , entry .Message )
371+ }
372+
373+ assert .Contains (t , logMessages , "webhook trigger refresh app to hydrate 'app-to-hydrate'" )
374+ assert .NotContains (t , logMessages , "webhook trigger refresh app to hydrate 'app-to-ignore'" )
375+
376+ hook .Reset ()
377+ }
378+
313379func TestGitHubTagEvent (t * testing.T ) {
314380 hook := test .NewGlobal ()
315381 h := NewMockHandler (nil , []string {})
0 commit comments