From 15333b3bd76a3130cd30166d45c5199f24962cb4 Mon Sep 17 00:00:00 2001 From: Luca CORRIERI Date: Mon, 10 Jul 2023 17:03:21 +0200 Subject: [PATCH] fix(webhook): support relative parent paths --- internal/webhook/event/common.go | 2 ++ internal/webhook/event/event_test.go | 22 ++++++++++----------- internal/webhook/event/pullrequest.go | 2 +- internal/webhook/event/testdata/layers.yaml | 2 +- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/internal/webhook/event/common.go b/internal/webhook/event/common.go index 975f9ea3..55030f47 100644 --- a/internal/webhook/event/common.go +++ b/internal/webhook/event/common.go @@ -67,6 +67,8 @@ func layerFilesHaveChanged(layer configv1alpha1.TerraformLayer, changedFiles []s if val, ok := layer.Annotations[annotations.AdditionnalTriggerPaths]; ok { for _, p := range strings.Split(val, ",") { p = ensureAbsPath(p) + // Handle relative parent paths (like "../") + p = filepath.Clean(filepath.Join(layer.Spec.Path, p)) if strings.Contains(f, p) { return true } diff --git a/internal/webhook/event/event_test.go b/internal/webhook/event/event_test.go index 7944a3ad..a96fa4ac 100644 --- a/internal/webhook/event/event_test.go +++ b/internal/webhook/event/event_test.go @@ -200,17 +200,17 @@ var _ = Describe("Webhook", func() { Expect(layer.Annotations[annotations.LastRelevantCommit]).To(Equal(PushEventAdditionalPathChanges.ChangeInfo.ShaAfter)) }) // TODO: make this test pass - // It("should have updated commit annotations for a relative change path", func() { - // layer := &configv1alpha1.TerraformLayer{} - // err := k8sClient.Get(context.TODO(), types.NamespacedName{ - // Namespace: "default", - // Name: "layer-additional-paths-2", - // }, layer) - // Expect(err).NotTo(HaveOccurred()) - // Expect(handleErr).NotTo(HaveOccurred()) - // Expect(layer.Annotations[annotations.LastBranchCommit]).To(Equal(PushEventAdditionalPathChanges.ChangeInfo.ShaAfter)) - // Expect(layer.Annotations[annotations.LastRelevantCommit]).To(Equal(PushEventAdditionalPathChanges.ChangeInfo.ShaAfter)) - // }) + It("should have updated commit annotations for a relative change path", func() { + layer := &configv1alpha1.TerraformLayer{} + err := k8sClient.Get(context.TODO(), types.NamespacedName{ + Namespace: "default", + Name: "layer-additional-paths-2", + }, layer) + Expect(err).NotTo(HaveOccurred()) + Expect(handleErr).NotTo(HaveOccurred()) + Expect(layer.Annotations[annotations.LastBranchCommit]).To(Equal(PushEventAdditionalPathChanges.ChangeInfo.ShaAfter)) + Expect(layer.Annotations[annotations.LastRelevantCommit]).To(Equal(PushEventAdditionalPathChanges.ChangeInfo.ShaAfter)) + }) }) Describe("Multiple paths have been modified", Ordered, func() { BeforeAll(func() { diff --git a/internal/webhook/event/pullrequest.go b/internal/webhook/event/pullrequest.go index 80a4c924..d697c060 100644 --- a/internal/webhook/event/pullrequest.go +++ b/internal/webhook/event/pullrequest.go @@ -102,7 +102,7 @@ func (e *PullRequestEvent) getAffectedRepositories(repositories []configv1alpha1 affectedRepositories := []configv1alpha1.TerraformRepository{} for _, repo := range repositories { log.Infof("evaluating terraform repository %s for url %s", repo.Name, repo.Spec.Repository.Url) - log.Infof("comparing noramlized url %s with received URL from paylaod %s", NormalizeUrl(repo.Spec.Repository.Url), e.URL) + log.Infof("comparing normalized url %s with received URL from payload %s", NormalizeUrl(repo.Spec.Repository.Url), e.URL) if e.URL == NormalizeUrl(repo.Spec.Repository.Url) { affectedRepositories = append(affectedRepositories, repo) } diff --git a/internal/webhook/event/testdata/layers.yaml b/internal/webhook/event/testdata/layers.yaml index 0d6e551f..2a1f2e4b 100644 --- a/internal/webhook/event/testdata/layers.yaml +++ b/internal/webhook/event/testdata/layers.yaml @@ -117,7 +117,7 @@ metadata: name: layer-additional-paths-2 namespace: default annotations: - config.terraform.padok.cloud/additionnal-trigger-paths: "../module.hcl" + config.terraform.padok.cloud/additionnal-trigger-paths: "../../terragrunt.hcl,../module.hcl" spec: branch: main path: terragrunt/layer-path-changed/prod/