Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stats): pass plan stats to markdown templates #3478

Merged
merged 8 commits into from Jun 3, 2023

Conversation

inkel
Copy link
Contributor

@inkel inkel commented Jun 2, 2023

what

  • This PR adds an additional struct to the data passed down to the templates in the Markdown renderer with stats about a successful plan: number of resources to add, update, destroy, if it include changes (something that's already covered but I started working on this branch before that change went in) and if there are any resources changed outside of Terraform.

why

  • The benefit of passing down this stats won't probably be notable with the default templates as they are, but it could, however, allow users to render a successful plan like the following:

The following environments have Terraform changes

environment add change destroy
staging 3 1 0
dev 0 0 5

The following environments have changes outside of Terraform

  • prod-01
  • prod-02

The template code to do something like that could be:

{{ $withChangesOutside := false }}
### The following environments have Terraform changes
| environment | add | change | destroy |
|-|-|-|-|
{{ range $i, $result := range .Results }}
{{- if $result.Stats.ChangesOutside }}{{ $withChangesOutside = true }}{{ end -}}
{{- if $result.Stats.Changes }}
| `{{ $result.ProjectName }}` | {{ $result.Stats.Add }} | {{ $result.Stats.Change }} | {{ $result.Stats.Destroy }} |
{{ end -}}
{{ end }}

{{ if $withChangesOutside }}
### The following environments have changes outside of Terraform
{{ range $i, $result := range .Results }}
- `{{ $result.ProjectName }}`
{{ end }}
{{ end }}

tests

  • I've been testing these changes locally and if everything goes according to plan it is likely we will test it thoroughly at Grafana Labs, where we manage 300+ environments with Atlantis.

  • Adding tests to the test suite implies changing the default templates, which is not probably something we would like to introduce unless this is first approved/merged.

references

inkel added 4 commits June 2, 2023 15:51
Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>
This will be useful to pass down to the templates to include
additional information when formatting the messages emitted by Atlantis.

Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>
Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>
Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>
@nitrocode nitrocode changed the title Pass plan stats to templates feat(stats): pass plan stats to markdown templates Jun 3, 2023
inkel added 2 commits June 2, 2023 23:54
DRY. Also highlights a bug where the stats should be a field in
planSuccessData in order to be accessible from the templates.

Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>
Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>
@inkel inkel marked this pull request as ready for review June 3, 2023 03:12
@inkel inkel requested a review from a team as a code owner June 3, 2023 03:12
@github-actions github-actions bot added the go Pull requests that update Go code label Jun 3, 2023
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've identified the changes in lines 199-211 as something that could be done for the rest of the templates. In fact, I believe this particular function could be refactored further to make it more readable, however, such a change is out of the scope of this PR.

@nitrocode nitrocode changed the title feat(stats): pass plan stats to markdown templates pass plan stats to markdown templates Jun 3, 2023
@nitrocode nitrocode changed the title pass plan stats to markdown templates feat(stats): pass plan stats to markdown templates Jun 3, 2023
Copy link
Member

@nitrocode nitrocode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@nitrocode nitrocode added this to the v0.24.3 milestone Jun 3, 2023
@nitrocode nitrocode merged commit 339c0fe into runatlantis:main Jun 3, 2023
14 of 15 checks passed
@inkel
Copy link
Contributor Author

inkel commented Jun 3, 2023

Thanks for merging!

mtavaresmedeiros pushed a commit to mtavaresmedeiros/atlantis that referenced this pull request Jul 3, 2023
* Capture stats in plan changes regexp

Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>

* Add PlanSuccessStats model to hold plan stats

This will be useful to pass down to the templates to include
additional information when formatting the messages emitted by Atlantis.

Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>

* Return stats for a PlanSuccess

Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>

* Add plan success stats to template data

Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>

* Refactor plan success template data creation

DRY. Also highlights a bug where the stats should be a field in
planSuccessData in order to be accessible from the templates.

Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>

* Move plan stats to planSuccessData struct

Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>

---------

Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>
Co-authored-by: PePe Amengual <jose.amengual@gmail.com>
Co-authored-by: nitrocode <7775707+nitrocode@users.noreply.github.com>
ijames-gc pushed a commit to gocardless/atlantis that referenced this pull request Feb 13, 2024
* Capture stats in plan changes regexp

Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>

* Add PlanSuccessStats model to hold plan stats

This will be useful to pass down to the templates to include
additional information when formatting the messages emitted by Atlantis.

Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>

* Return stats for a PlanSuccess

Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>

* Add plan success stats to template data

Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>

* Refactor plan success template data creation

DRY. Also highlights a bug where the stats should be a field in
planSuccessData in order to be accessible from the templates.

Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>

* Move plan stats to planSuccessData struct

Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>

---------

Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>
Co-authored-by: PePe Amengual <jose.amengual@gmail.com>
Co-authored-by: nitrocode <7775707+nitrocode@users.noreply.github.com>
ijames-gc pushed a commit to gocardless/atlantis that referenced this pull request Feb 13, 2024
* Capture stats in plan changes regexp

Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>

* Add PlanSuccessStats model to hold plan stats

This will be useful to pass down to the templates to include
additional information when formatting the messages emitted by Atlantis.

Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>

* Return stats for a PlanSuccess

Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>

* Add plan success stats to template data

Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>

* Refactor plan success template data creation

DRY. Also highlights a bug where the stats should be a field in
planSuccessData in order to be accessible from the templates.

Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>

* Move plan stats to planSuccessData struct

Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>

---------

Signed-off-by: Leandro López (inkel) <leandro.lopez@grafana.com>
Co-authored-by: PePe Amengual <jose.amengual@gmail.com>
Co-authored-by: nitrocode <7775707+nitrocode@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
go Pull requests that update Go code stats
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants