Skip to content

Commit

Permalink
updated example summary comment with additional badges and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielle authored and Daniellem97 committed Aug 30, 2024
1 parent a696d3d commit fb55209
Show file tree
Hide file tree
Showing 3 changed files with 326 additions and 0 deletions.
86 changes: 86 additions & 0 deletions notification/pull-request-comment-summary.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# regal ignore:use-rego-v1
package spacelift

import future.keywords

# regal ignore:line-length
header := sprintf("### Resource changes ([link](%s))\n\n![add](https://img.shields.io/badge/add-%d-brightgreen) ![change](https://img.shields.io/badge/change-%d-yellow) ![destroy](https://img.shields.io/badge/destroy-%d-red) ![import](https://img.shields.io/badge/import-%d-blue) ![move](https://img.shields.io/badge/move-%d-purple) ![forget](https://img.shields.io/badge/forget-%d-b07878) \n\n| Action | Resource | Changes |\n| --- | --- | --- |", [input.run_updated.urls.run, count(added), count(changed), count(deleted), count(imported), count(moved), count(forgotten)])

addedresources := concat("\n", added)

changedresources := concat("\n", changed)

deletedresources := concat("\n", deleted)

importedresources := concat("\n", imported)

movedresources := concat("\n", moved)

forgottenresources := concat("\n", forgotten)

added contains row if {
some x in input.run_updated.run.changes

# regal ignore:line-length
row := sprintf("| Added | `%s` | <details><summary>Value</summary>`%s`</details> |", [x.entity.address, x.entity.data.values])

# regal ignore:deprecated-builtin
any([x.action == "added", x.action == "destroy-Before-create-replaced", x.action == "create-Before-destroy-replaced"])
x.entity.entity_type == "resource"
not x.moved
}

changed contains row if {
some x in input.run_updated.run.changes

# regal ignore:line-length
row := sprintf("| Changed | `%s` | <details><summary>New value</summary>`%s`</details> |", [x.entity.address, x.entity.data.values])
x.entity.entity_type == "resource"
x.action == "changed"
not x.moved
}

deleted contains row if {
some x in input.run_updated.run.changes
row := sprintf("| Deleted | `%s` | :x: |", [x.entity.address])

# regal ignore:line-length, deprecated-builtin
any([x.action == "deleted", x.action == "destroy-Before-create-replaced", x.action == "create-Before-destroy-replaced"])
x.entity.entity_type == "resource"
not x.moved
}

imported contains row if {
some x in input.run_updated.run.changes

# regal ignore:line-length
row := sprintf("| Imported | `%s` | <details><summary>New value</summary>`%s`</details> |", [x.entity.address, x.entity.data.values])
x.action == "import"
x.entity.entity_type == "resource"
not x.moved
}

moved contains row if {
some x in input.run_updated.run.changes

# regal ignore:line-length
row := sprintf("| Moved | `%s` | <details><summary>New value</summary>`%s`</details> |", [x.entity.address, x.entity.data.values])
x.entity.entity_type == "resource"
x.moved
}

forgotten contains row if {
some x in input.run_updated.run.changes
row := sprintf("| Forgotten | `%s` | :x: |", [x.entity.address])
x.action == "forget"
x.entity.entity_type == "resource"
not x.moved
}

# regal ignore:line-length
pull_request contains {"commit": input.run_updated.run.commit.hash, "body": replace(replace(concat("\n", [header, addedresources, changedresources, deletedresources, importedresources, movedresources, forgottenresources]), "\n\n\n", "\n"), "\n\n", "\n")} if {
input.run_updated.run.state == "FINISHED"
input.run_updated.run.type == "PROPOSED"
}

sample := true
13 changes: 13 additions & 0 deletions notification/pull-request-comment-summary.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Notification as a comment on your pull request which summarises your logs
source: pull-request-comment-summary.rego
type: notification
description: |
This policy will add a comment to a pull request where it will list all the resources that were added, changed, deleted, moved, imported or forgotten.
labels:
- notification
- slack
- github
- pull-request
- PR
- comment
- run
227 changes: 227 additions & 0 deletions notification/pull-request-comment-summary_test.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
# regal ignore:use-rego-v1
package spacelift_test

import data.spacelift
import future.keywords

# Test with 1 resource having "create-before-destroy" action with moved false
test_create_before_destroy if {
input_data := {"run_updated": {
"run": {
"changes": [{
"action": "create-Before-destroy-replaced",
"entity": {
"address": "aws_instance.example",
"data": {"values": {"ami": "ami-12345678"}},
"entity_type": "resource",
},
"moved": false,
}],
"commit": {"hash": "COMMIT_HASH_CREATE_BEFORE_DESTROY"},
"state": "FINISHED",
"type": "PROPOSED",
},
"urls": {"run": "https://example.com/run/02"},
}}

result := spacelift.pull_request with input as input_data

count(result) == 1

# Get the added resources with the input data applied
added_resources := spacelift.added with input as input_data

# Get the deleted resources with the input data applied
deleted_resources := spacelift.deleted with input as input_data

# Correct count for added resources
count(added_resources) == 1

# Correct count for deleted resources
count(deleted_resources) == 1
}

# Test with 3 resources with action "added", 2 with moved:false and 1 with moved: true
# regal ignore:rule-length
test_added_and_moved_resources if {
input_data := {"run_updated": {
"run": {
"changes": [
{
"action": "added",
"entity": {
"address": "aws_instance.example1",
"data": {"values": {"ami": "ami-12345678"}},
"entity_type": "resource",
},
"moved": false,
},
{
"action": "added",
"entity": {
"address": "aws_instance.example2",
"data": {"values": {"ami": "ami-87654321"}},
"entity_type": "resource",
},
"moved": false,
},
{
"action": "added",
"entity": {
"address": "aws_instance.example3",
"data": {"values": {"ami": "ami-11223344"}},
"entity_type": "resource",
},
"moved": true,
},
],
"commit": {"hash": "COMMIT_HASH_ADDED_AND_MOVED"},
"state": "FINISHED",
"type": "PROPOSED",
},
"urls": {"run": "https://example.com/run/03"},
}}

result := spacelift.pull_request with input as input_data

count(result) == 1

# Get the added resources with the input data applied
added_resources := spacelift.added with input as input_data

# Get the moved resources with the input data applied
moved_resources := spacelift.moved with input as input_data

# Correct count for added resources
count(added_resources) == 2

# Correct count for moved resources
count(moved_resources) == 1
}

# Additional test case 1: Check if changed resources are categorized correctly
test_changed_resources if {
input_data := {"run_updated": {
"run": {
"changes": [{
"action": "changed",
"entity": {
"address": "aws_s3_bucket.example",
"data": {"values": {
"bucket": "example-bucket",
"region": "us-west-1",
}},
"entity_type": "resource",
},
"moved": false,
}],
"commit": {"hash": "COMMIT_HASH_CHANGED"},
"state": "FINISHED",
"type": "PROPOSED",
},
"urls": {"run": "https://example.com/run/04"},
}}

result := spacelift.pull_request with input as input_data

count(result) == 1

# Get the changed resources with the input data applied
changed_resources := spacelift.changed with input as input_data

# Correct count for changed resources
count(changed_resources) == 1
}

# Additional test case 2: Check if imported resources are categorized correctly
test_imported_resources if {
input_data := {"run_updated": {
"run": {
"changes": [{
"action": "import",
"entity": {
"address": "aws_s3_bucket.example_imported",
"data": {"values": {}},
"entity_type": "resource",
},
"moved": false,
}],
"commit": {"hash": "COMMIT_HASH_IMPORTED"},
"state": "FINISHED",
"type": "PROPOSED",
},
"urls": {"run": "https://example.com/run/05"},
}}

result := spacelift.pull_request with input as input_data

count(result) == 1

# Get the imported resources with the input data applied
imported_resources := spacelift.imported with input as input_data

# Correct count for imported resources
count(imported_resources) == 1
}

# Additional test case 3: Check if moved resources are categorized correctly
test_moved_resources if {
input_data := {"run_updated": {
"run": {
"changes": [{
"action": "moved",
"entity": {
"address": "aws_instance.example_moved",
"data": {"values": {"ami": "ami-99887766"}},
"entity_type": "resource",
},
"moved": true,
}],
"commit": {"hash": "COMMIT_HASH_MOVED"},
"state": "FINISHED",
"type": "PROPOSED",
},
"urls": {"run": "https://example.com/run/06"},
}}

result := spacelift.pull_request with input as input_data

count(result) == 1

# Get the moved resources with the input data applied
moved_resources := spacelift.moved with input as input_data

# Correct count for moved resources
count(moved_resources) == 1
}

# Additional test case 4: Check if forgotten resources are categorized correctly
test_forgotten_resources if {
input_data := {"run_updated": {
"run": {
"changes": [{
"action": "forget",
"entity": {
"address": "random_pet.example_forget",
"data": {"values": {}},
"entity_type": "resource",
},
"moved": false,
}],
"commit": {"hash": "COMMIT_HASH_FORGOTTEN"},
"state": "FINISHED",
"type": "PROPOSED",
},
"urls": {"run": "https://example.com/run/07"},
}}

result := spacelift.pull_request with input as input_data

count(result) == 1

# Get the forgotten resources with the input data applied
forgotten_resources := spacelift.forgotten with input as input_data

# Correct count for forgotten resources
count(forgotten_resources) == 1
}

0 comments on commit fb55209

Please sign in to comment.