Github Actions with ChatOps to write Beautiful Python Code
In this tutorial, I will show how to:
-
Trigger Github Actions Workflow using PR comments like ‘/format’ (ChatOps). I will use slash-command-dispatch for this.
-
Format the python code with PEP8 as part of PR and re-commit it again.
Github Actions published guides:
-
GitOps way with Github Actions and self-hosted runner on Kubernetes
-
Automatic scaling with Github Actions and self-hosted runners
-
Github Actions with k8s and Karpenter to dynamically provision your runners on spot instances
-
Use OpenID Connect with Github Actions to authenticate with Amazon Web Services
Let’s start
I will start from the first part.
Trigger Github Actions Workflow using PR comments like ‘/format’ (ChatOps). I will use slash-command-dispatch for this.
Why create dispatch events?
ChatOps with slash commands can work in a basic way by parsing the commands during issue_comment events and immediately processing the command. In repositories with a lot of activity, the workflow queue will get backed up very quickly trying to handle new issue_comment events and process the commands themselves. Dispatching commands to be processed elsewhere keeps the workflow queue moving quickly. It essentially enables parallel processing of workflows. An additional benefit of dispatching is that it allows non-sensitive workloads to be run in public repositories to save using private repository GitHub Action minutes.
Read more: https://github.com/peter-evans/slash-command-dispatch
Adding ‘Slash Command Dispatch’ workflow.
.github/workflows/sh-dispatcher.yaml
For ‘Slash Command Dispatch’ I used ‘secrets.GH_PAT’, I explained in previous posts: https://github.com/warolv/github-actions-series/blob/master/scale-runners.md how to generate PAT, please read it.
After PAT generated, need to add it as a secret (GH_PAT) of your GitHub repo.
Go to repo’s settings -> ‘Secrets and variables’ -> ‘actions’ -> ‘New repository secret’.
And the command itself:
.github/workflows/format-command.yml
Verifying ‘format’ command is executed on PR’s comment
I will verify first that ‘/format’ command triggered on PR’s comments and ‘format command executed!’ being seen in the console.
For verification need to create the PR, in my example:
Do some change, I changed output for echo in my case,
from ‘echo ‘format command executed!’’ to ‘echo ‘Format command executed!’’
Open PR:
Now add comment ‘/format’.
You can see ‘format command executed!’ , so it works :-) !
Next step will be to use ‘black’ tool for the command.
You can find example of usage here
Using ‘black’ to format python code
https://pypi.org/project/black/
Black is the uncompromising Python code formatter. By using it, you agree to cede control over minutiae of hand-formatting. In return, Black gives you speed, determinism, and freedom from pycodestyle nagging about formatting. You will save time and mental energy for more important matters.
To use ‘black’ I will rewrite command workflow:
.github/workflows/format-command.yaml
Now I will create another PR to test ‘/format’ workflow.
As part of PR I added python code to test ‘black’ with this code:
You can see above new commit was pushed to this PR.
It working great as you can see:-)
You can find all the code | workflows and python example code in my repo: |
Thank you for reading, I hope you enjoyed it.
If you want to be notified when the next post of this tutorial is published, please follow me on Twitter @warolv.
I also will create Youtube video for this tutorial on my YT channel, please subscribe!