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

fix(workflow-hook): log output with or without errors #3422

Merged
merged 4 commits into from
May 23, 2023

Conversation

X-Guardian
Copy link
Contributor

@X-Guardian X-Guardian commented May 20, 2023

what

  • In the pre and post workflow hook runners, the OutputHandler.SendWorkflowHook calls have been moved to before the command error handling.

why

  • So that the pre and post workflow hook output always gets logged, irrelevant of whether an error occurred.

tests

repos.yaml:

repos:
- id: /.*/
  pre_workflow_hooks:
    - run: echo "pre workflow test line 1" && exit 0
    - run: echo "pre workflow test line 2" && exit 1
  post_workflow_hooks:
    - run: echo "post workflow test line 1" && exit 0
    - run: echo "post workflow test line 2" && exit 1

Output Screenshots

Pre Workflow Hook

image

Post Workflow Hook

image

Logs

Logs
{"level":"info","ts":"2023-05-20T12:56:49.413+0100","caller":"runtime/pre_workflow_hook_runner.go:77","msg":"successfully ran \"echo \\\"pre workflow test line 1\\\" && exit 0\" in \"atlantis-test\\\\1\\\\default\"","json":{"repo":"X-Guardian/atlantis-test","pull":"1"}}
{"level":"error","ts":"2023-05-20T12:56:50.396+0100","caller":"events/command_runner.go:293","msg":"Error running pre-workflow hooks exit status 1: running \"echo \\\"pre workflow test line 2\\\" && exit 1\" in \"atlantis-test\\\\1\\\\default\": \npre workflow test line 2\n. Proceeding with plan command.","json":{"repo":"X-Guardian/atlantis-test","pull":"1"},"stacktrace":"github.com/runatlantis/atlantis/server/events.(*DefaultCommandRunner).RunCommentCommand\n\tatlantis/server/events/command_runner.go:293"}
{"level":"info","ts":"2023-05-20T12:56:51.405+0100","caller":"events/project_command_builder.go:399","msg":"found no atlantis.yaml file","json":{"repo":"X-Guardian/atlantis-test","pull":"1"}}
{"level":"info","ts":"2023-05-20T12:56:51.406+0100","caller":"events/project_command_builder.go:403","msg":"automatically determined that there were 0 projects modified in this pull request: []","json":{"repo":"X-Guardian/atlantis-test","pull":"1"}}
{"level":"info","ts":"2023-05-20T12:56:52.931+0100","caller":"runtime/post_workflow_hook_runner.go:77","msg":"successfully ran \"echo \\\"post workflow test line 1\\\" && exit 0\" in \"atlantis-test\\\\1\\\\default\"","json":{"repo":"X-Guardian/atlantis-test","pull":"1"}}
{"level":"error","ts":"2023-05-20T12:56:53.910+0100","caller":"events/command_runner.go:303","msg":"Error running post-workflow hooks exit status 1: running \"echo \\\"post workflow test line 2\\\" && exit 1\" in \"atlantis-test\\\\1\\\\default\": \npost workflow test line 2\n.","json":{"repo":"X-Guardian/atlantis-test","pull":"1"},"stacktrace":"github.com/runatlantis/atlantis/server/events.(*DefaultCommandRunner).RunCommentCommand\n\tatlantis/server/events/command_runner.go:303"}

references

@X-Guardian X-Guardian requested a review from a team as a code owner May 20, 2023 11:52
@github-actions github-actions bot added the go Pull requests that update Go code label May 20, 2023
@nitrocode
Copy link
Member

Brilliant! Thank you @X-Guardian for the deep dive and creating a fix. Is there a way we can add a unit test or e2e test or both to ensure this functionality doesn't break in the future?

@X-Guardian
Copy link
Contributor Author

Hi @nitrocode, I did look at the current unit tests for the workflow hook runners, but couldn't obviously see how to change them to check for this scenario. From what I could see, they are currently checking the out return value of the Run function, which isn't actually used.

@nitrocode
Copy link
Member

Is it not possible to modify or add to them to verify that your changes will also be tested? It must be possible.

This feature was working before and a follow up pr broke the feature and now this pr has been created to remediate the issue. Besides a thorough future review, what is preventing someone from creating a new pr and breaking the feature again? Only thing would be unit tests. Please consider adding them or we are doomed to repeat our mistakes.

@X-Guardian
Copy link
Contributor Author

I didn't say it wasn't possible. I said that I couldn't see how to change them, as they are testing the output of the function not the internal workings of the function which is where the bug I fixed is.

This is my first PR on this repo, but looking at the history of the workflow hook status feature, this was introduced in PR #2441 and has always had this bug as you can see that the OutputHandler.SendWorkflowHook function call has always been after the error checks, that if triggered, will return from the function early.

@nitrocode nitrocode added this to the v0.24.0 milestone May 22, 2023
@nitrocode nitrocode added the needs tests Change requires tests label May 22, 2023
@nitrocode nitrocode changed the title fix: Errors in Pre or Post Workflow Hook Scripts do not Produce any Output fix(workflow-hook): log output with or without errors May 22, 2023
@nitrocode nitrocode modified the milestones: v0.24.0, v0.25.0 May 22, 2023
@X-Guardian
Copy link
Contributor Author

@nitrocode, this PR is ready for review.

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! Pinging a few other reviewers too

cc @Fabianoshz @GenPage @jamengual @chenrui333

@nitrocode nitrocode removed the needs tests Change requires tests label May 23, 2023
@nitrocode nitrocode modified the milestones: v0.25.0, v0.24.1 May 23, 2023
@nitrocode nitrocode merged commit 4915d8d into runatlantis:main May 23, 2023
mtavaresmedeiros pushed a commit to mtavaresmedeiros/atlantis that referenced this pull request Jul 3, 2023
* fix: Errors in Pre or Post Workflow Hook Scripts do not Produce any Output

* Fix post workflow

* Update unit tests
ijames-gc pushed a commit to gocardless/atlantis that referenced this pull request Feb 13, 2024
* fix: Errors in Pre or Post Workflow Hook Scripts do not Produce any Output

* Fix post workflow

* Update unit tests
ijames-gc pushed a commit to gocardless/atlantis that referenced this pull request Feb 13, 2024
* fix: Errors in Pre or Post Workflow Hook Scripts do not Produce any Output

* Fix post workflow

* Update unit tests
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Errors in Pre or Post Workflow Hook Scripts do not Produce any Output
3 participants