In 2016 github added a feature, that allows users to create templates for new issues and pull requests. This is nice, because if the maintainers can help users to provide all necessary data without asking for them several times. Unfortunately, some people just delete all of the template and start typing. In this blog post I’ll show my solution for this problem.
When a user opens a new issue, github sends the issue information to a Azure Function web hook. The function analyses the issue data and compares the issue text with the issue template. After computing the matching quote, it adds an comment using the github API.
Creating a new Azure Function is very easy.
- open https://portal.azure.com/
- add a Function app and open it
- select scenario
Webhook + API
- select language
Create this function
If you need a more detailed tutorial, click here.
The github API for .NET is implemented in
Octokit and it’s available on NuGet.
To add the NuGet package to your Function:
- name it
- paste the following code and save it
Now, you have to configure github to call your webhook when a new issue was opened.
- Open the settings of the repository that you want to monitor.
- enter the Azure Function url into
- enter the GitHub Secret into
Let me select individual events
To verify your setup, you can open an issue. If you look at the log output of your function, you’ll see something like:
Pro tip: Before opening the issue, add this line to your function:
The request body will be written to the log. You can then copy it and paste it into the
Test section and every time you press
Run the “real” request will be processed. So you don’t need to open a new issue on github to test you function.
OAuth API token
The github API requires an access token. Create a new one following these steps:
- open https://github.com/
- log in and open your settings
Personal access tokens
Generate new token
- enter a name and select
- save the generated token
If your repository doesn’t contain an issue template
ISSUE_TEMPLATE.md, add one. Click here for more information on issue templates.
Additionally add the file
ISSUE_TEMPLATE_CHECK.md. This file contains the lines that should be included in the issue in the given order. I’ve chosen the headings and some bold bullet points.
Now, everything is setup correctly and you can code (or copy paste) your Azure Function. The code is available on github.
ProcessIssueAsync from your
ProcessIssueAsync implements the workflow of the issue. You need to check if the
"opened", because github sends all issue related events to your webhook.
The function then:
- reads some properties from the request
- splits the issue text into lines
- gets all lines of
- checks the
templateLinesand calculates a matching quote in percent
- generates a message based on the matching quote
- creates a new comment on the issue
The format of the issue request is documented here.
CheckIssueWithTemplate counts the lines of
templateLines that occur in
issueLines and calculates a quote that expresses how well the check condition was satisfied. You can implement any logic you want. It just has to project two string arrays to a number between
0 (=issue template wasn’t used at all) and
1 (=100% sure that the issue template was used).
GetMessage generates a praisingly message if the user used the template and a sad one if he probably didn’t. You can add your on text and thresholds if you want.
CreateCommentAsync Last you have just to send the comment to github. The API makes this very easy. The developers of github (and ofc. the contributers) have done a great job! You have to replace the token with your own one.
will result in:
A nice looking issue will be commented like:
I think you are now aware of the possibilities that the connection of github webhooks, Azure Functions and the github API offers you. The Function can be easily enhanced with features like
- close issues automatically if match qoute is smaller than 10%
- save issue opener to database and block them after 3 bad issues
- make the matching algorithm
- make the messages configurable like
Tweet me your best idea and win a like :)
Found a typo? Send me a pull request!