Skip to content

Commit fb38179

Browse files
committed
feat(repo-controller): add LastRelevantCommit annotation to layers
1 parent 9ef1821 commit fb38179

File tree

1 file changed

+19
-9
lines changed
  • internal/controllers/terraformrepository

1 file changed

+19
-9
lines changed

internal/controllers/terraformrepository/states.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
configv1alpha1 "github.com/padok-team/burrito/api/v1alpha1"
1010
"github.com/padok-team/burrito/internal/annotations"
11+
layerCtrl "github.com/padok-team/burrito/internal/controllers/terraformlayer"
1112
repo "github.com/padok-team/burrito/internal/repository"
1213
log "github.com/sirupsen/logrus"
1314
corev1 "k8s.io/api/core/v1"
@@ -136,24 +137,33 @@ func (s *SyncNeeded) getHandler() Handler {
136137

137138
// Add annotation to trigger a sync for all layers that depend on this branch
138139
affectedLayers := retrieveLayersForRef(branch.Name, layers)
140+
date := time.Now().Format(time.UnixDate)
139141
for _, layer := range affectedLayers {
140142
ann := map[string]string{}
141-
// TODO: Set LastRelevantCommit
143+
144+
// We always set the last branch commit to the latest revision
142145
ann[annotations.LastBranchCommit] = latestRev
143-
// ann[annotations.LastBranchCommitDate] = date // TODO: add date
146+
ann[annotations.LastBranchCommitDate] = date
144147

145-
// TODO: inspect if layer files have changed when git providers will expose a function to do so
146-
// if controller.LayerFilesHaveChanged(layer, e.Changes) {
147-
// log.Infof("layer %s is affected by push event", layer.Name)
148-
// ann[annotations.LastRelevantCommit] = e.ChangeInfo.ShaAfter
149-
// ann[annotations.LastRelevantCommitDate] = date
150-
// }
148+
if currentLastRelevant, ok := layer.Annotations[annotations.LastRelevantCommit]; !ok {
149+
// If the layer does not have a last relevant commit, we set it to the last branch commit
150+
ann[annotations.LastRelevantCommit] = latestRev
151+
ann[annotations.LastRelevantCommitDate] = date
152+
} else {
153+
// We compare the current last relevant commit with the latest revision
154+
changes := gitProvider.GetChanges(currentLastRelevant, latestRev)
155+
if layerCtrl.LayerFilesHaveChanged(layer, changes) {
156+
log.Infof("layer %s/%s is affected by new revision %s", layer.Namespace, layer.Name, latestRev)
157+
ann[annotations.LastRelevantCommit] = latestRev
158+
ann[annotations.LastRelevantCommitDate] = date
159+
}
160+
}
151161

152162
err := annotations.Add(context.TODO(), r.Client, &layer, ann)
153163
if err != nil {
154164
log.Errorf("could not add annotation to TerraformLayer %s/%s: %s", layer.Namespace, layer.Name, err)
155165
} else {
156-
log.Infof("layer %s/%s annotated with new last branch commit %s", layer.Namespace, layer.Name, latestRev)
166+
log.Infof("layer %s/%s annotated with new revision %s", layer.Namespace, layer.Name, latestRev)
157167
}
158168
}
159169
}

0 commit comments

Comments
 (0)