-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
203 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package event | ||
|
||
import ( | ||
"path/filepath" | ||
"strings" | ||
|
||
configv1alpha1 "github.com/padok-team/burrito/api/v1alpha1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
const PullRequestOpened = "opened" | ||
const PullRequestClosed = "closed" | ||
|
||
type ChangeInfo struct { | ||
ShaBefore string | ||
ShaAfter string | ||
} | ||
|
||
type Event interface { | ||
Handle(client.Client, []configv1alpha1.TerraformRepository, []configv1alpha1.TerraformLayer) error | ||
} | ||
|
||
func GetNormalizedURL(urls []string) string { | ||
return "" | ||
} | ||
|
||
func ParseRevision(ref string) string { | ||
refParts := strings.SplitN(ref, "/", 3) | ||
return refParts[len(refParts)-1] | ||
} | ||
|
||
func isLayerLinkedToAnyRepositories(repositories []configv1alpha1.TerraformRepository, layer configv1alpha1.TerraformLayer) bool { | ||
for _, r := range repositories { | ||
if r.Name == layer.Spec.Repository.Name && r.Namespace == layer.Spec.Repository.Namespace { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func layerFilesHaveChanged(layer configv1alpha1.TerraformLayer, changedFiles []string) bool { | ||
if len(changedFiles) == 0 { | ||
return true | ||
} | ||
|
||
// At last one changed file must be under refresh path | ||
for _, f := range changedFiles { | ||
f = ensureAbsPath(f) | ||
if strings.Contains(f, layer.Spec.Path) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func ensureAbsPath(input string) string { | ||
if !filepath.IsAbs(input) { | ||
return string(filepath.Separator) + input | ||
} | ||
return input | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package event | ||
|
||
import ( | ||
"fmt" | ||
|
||
configv1alpha1 "github.com/padok-team/burrito/api/v1alpha1" | ||
log "github.com/sirupsen/logrus" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
type PullRequestEvent struct { | ||
URL string | ||
ProviderLabel string | ||
Revision string | ||
Action string | ||
Base string | ||
ID string | ||
Changes []string | ||
} | ||
|
||
func (e *PullRequestEvent) getAffectedLayers(allLayers []configv1alpha1.TerraformLayer, allRepos []configv1alpha1.TerraformRepository) []configv1alpha1.TerraformLayer { | ||
affectedRepositories := []configv1alpha1.TerraformRepository{} | ||
for _, repo := range allRepos { | ||
log.Infof("evaluating terraform repository %s for url %s", repo.Name, repo.Spec.Repository.Url) | ||
if e.URL == GetNormalizedURL([]string{repo.Spec.Repository.Url}) { | ||
affectedRepositories = append(affectedRepositories, repo) | ||
continue | ||
} | ||
} | ||
layers := []configv1alpha1.TerraformLayer{} | ||
for _, layer := range allLayers { | ||
eventIsRelevant := layerFilesHaveChanged(layer, e.Changes) && isLayerLinkedToAnyRepositories(affectedRepositories, layer) && layer.Spec.Branch == e.Base | ||
if eventIsRelevant { | ||
layers = append(layers, layer) | ||
} | ||
} | ||
return layers | ||
} | ||
func (e *PullRequestEvent) Handle(c client.Client, allRepositories []configv1alpha1.TerraformRepository, allLayers []configv1alpha1.TerraformLayer) error { | ||
layers := e.getAffectedLayers(allLayers, allRepositories) | ||
for _, layer := range layers { | ||
switch e.Action { | ||
case PullRequestOpened: | ||
|
||
case PullRequestClosed: | ||
default: | ||
log.Infof("pull request event %s is not supported", e.Action) | ||
return nil | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (e *PullRequestEvent) getNewPullRequestLayer(layer configv1alpha1.TerraformLayer) *configv1alpha1.TerraformLayer { | ||
newLayer := layer.DeepCopy() | ||
newLayer.Spec.Branch = e.Revision | ||
newLayer.GenerateName = fmt.Sprintf("%s-pr-", layer.Name) | ||
newLayer.Name = "" | ||
newLayer.Labels["pull-request-event"] = "true" | ||
newLayer.Labels[e.ProviderLabel] = e.ID | ||
newLayer.Spec.RemediationStrategy = "dry" | ||
newLayer.Spec.PlanOnPullRequest = false | ||
return newLayer | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package event | ||
|
||
import ( | ||
"context" | ||
|
||
configv1alpha1 "github.com/padok-team/burrito/api/v1alpha1" | ||
"github.com/padok-team/burrito/internal/annotations" | ||
log "github.com/sirupsen/logrus" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
type PushEvent struct { | ||
URL string | ||
Revision string | ||
ChangeInfo | ||
Changes []string | ||
} | ||
|
||
func (e *PushEvent) Handle(c client.Client, allRepositories []configv1alpha1.TerraformRepository, allLayers []configv1alpha1.TerraformLayer) error { | ||
layers := e.getAffectedLayers(allLayers, allRepositories) | ||
for _, layer := range layers { | ||
ann := map[string]string{} | ||
log.Printf("evaluating terraform layer %s for revision %s", layer.Name, e.Revision) | ||
if layer.Spec.Branch != e.Revision { | ||
log.Infof("branch %s for terraform layer %s not matching revision %s", layer.Spec.Branch, layer.Name, e.Revision) | ||
continue | ||
} | ||
ann[annotations.LastBranchCommit] = e.ChangeInfo.ShaAfter | ||
if layerFilesHaveChanged(layer, e.Changes) { | ||
ann[annotations.LastConcerningCommit] = e.ChangeInfo.ShaAfter | ||
} | ||
err := annotations.Add(context.TODO(), c, layer, ann) | ||
if err != nil { | ||
log.Errorf("could not add annotation to terraform layer %s", err) | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (e *PushEvent) getAffectedLayers(allLayers []configv1alpha1.TerraformLayer, allRepos []configv1alpha1.TerraformRepository) []configv1alpha1.TerraformLayer { | ||
affectedRepositories := []configv1alpha1.TerraformRepository{} | ||
for _, repo := range allRepos { | ||
log.Infof("evaluating terraform repository %s for url %s", repo.Name, repo.Spec.Repository.Url) | ||
if e.URL == GetNormalizedURL([]string{repo.Spec.Repository.Url}) { | ||
affectedRepositories = append(affectedRepositories, repo) | ||
continue | ||
} | ||
} | ||
layers := []configv1alpha1.TerraformLayer{} | ||
for _, layer := range allLayers { | ||
if layerFilesHaveChanged(layer, e.Changes) && isLayerLinkedToAnyRepositories(affectedRepositories, layer) { | ||
layers = append(layers, layer) | ||
} | ||
} | ||
return layers | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters