-
Install Azure Functions Core Tools
See Installing Azure Functions Core Tools.
brew tap azure/functions brew install azure-functions-core-tools@4
-
Install Azure Functions VS Code Extension
This extension is required locally even when developing in the dev container.
-
Install Azure Functions PowerShell Module
Install-Module -Name Az
-
Install Pester
Install-Module -Name Pester
-
Install Docker Desktop
See Docker Desktop.
Follow these steps to set up and develop the project locally without using a development container.
-
Clone the Repository
git clone https://github.com/yourusername/DscCommunity.MaintenanceAutomation.git cd DscCommunity.MaintenanceAutomation
-
Configure Local Settings
Copy the template settings file and update the necessary values.
cp local.settings.template.json local.settings.json
Update
local.settings.json
with your configurations, such asGITHUB_TOKEN
andRELABELER_CONFIG_PATH
. -
Install Dependencies
Install the required PowerShell modules.
Install-Module -Name Az.Accounts -RequiredVersion 3.0.4 Install-Module -Name Az.ApplicationInsights -RequiredVersion 2.2.5 Install-Module -Name powershell-yaml -RequiredVersion 0.4.4
-
Run the Azure Function Locally
Start the Azure Functions host.
func host start
-
Test the Function
Use
curl
or Postman to send requests tohttp://localhost:7071/api/Relabeler
.
For an isolated and consistent development environment, use the provided development container.
-
Prerequisites
- Visual Studio Code with the Remote - Containers extension installed.
- Read Sharing Git credentials with your container and see if you need to take actions.
-
Open the Project in the Dev Container
- Open Visual Studio Code.
- Click on the green >< icon in the bottom-left corner.
- Select "Reopen in Container".
-
Configure Environment Variables
Ensure that
.devcontainer/devcontainer.env
is properly set up with necessary environment variables. -
Build the Dev Container
The container will automatically build based on the provided
Dockerfile
anddevcontainer.json
. If you need to rebuild manually:- Open the Command Palette (
Ctrl+Shift+P
orCmd+Shift+P
). - Select "Dev Containers: Rebuild Container" or "Dev Containers: Rebuild Container Without Cache".
- Open the Command Palette (
-
Run Post-Creation Scripts
The
postCreateCommand
indevcontainer.json
will executecontainerPostCreate.ps1
to install dependencies. -
Start the Azure Function (without debugging)
Use the integrated terminal to start the Azure Functions host.
func host start
-
Start the Azure Function with Debugging
- Press
F5
to start debugging.
If you get the error
Could not find the task 'func: host start'
see the issue Fails debugging Azure Function inside a .devcontainer using VS Code. - Press
Set webHookType
to github
in function.json
. Avoid using the authLevel
property with GitHub webhooks. When setting the webHookType
property, do not set the methods
property on the binding.
See WebHook type and GitHub Webhooks.
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "Request",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "Response"
}
]
}
{
"bindings": [
{
"webHookType": "github",
"type": "httpTrigger",
"direction": "in",
"name": "Request"
},
{
"type": "http",
"direction": "out",
"name": "Response"
}
]
}
Use octokit/webhooks payload examples to test your Azure Function.
Ensure the payload is saved as a file, then run:
curl -X POST http://localhost:7071/api/Relabeler `
-H "Content-Type: application/json" `
-H "X-GitHub-Event: pull_request" `
-H "X-GitHub-Delivery: $(uuidgen)" `
--% -d @./tests/payloads/issue_comment.created.json
Ensure the payload is saved as a file, then run:
curl --request POST http://localhost:7071/api/Relabeler \
--header "Content-Type: application/json" \
--header "X-GitHub-Event: pull_request" \
--header "X-GitHub-Delivery: 1234567890" \
--data @./tests/payloads/issue_comment.created.json
Must be run in bash
or zsh
as it downloads the payload content and then uses it.
curl -X POST -H "Content-Type: application/json" \
--data-binary @<(curl -s 'https://raw.githubusercontent.com/octokit/webhooks/main/payload-examples/api.github.com/pull_request/opened.payload.json') \
http://localhost:7071/api/Relabeler
The StackExchange.Redis package can be downloaded using:
# StackExchange.Redis 2.8.16
Invoke-WebRequest -Uri https://www.nuget.org/api/v2/package/StackExchange.Redis -OutFile stackexchange.redis.nupkg
# Pipelines.Sockets.Unofficial 2.2.8
Invoke-WebRequest -Uri https://www.nuget.org/api/v2/package/Pipelines.Sockets.Unofficial -OutFile pipelines.sockets.unofficial.nupkg
# System.IO.Pipelines 8.0.0
Invoke-WebRequest -Uri https://www.nuget.org/api/v2/package/System.IO.Pipelines -OutFile system.io.pipelines.nupkg
Extract the packages and copy the dlls (not the XMLs) from lib/netstandard2.0
to the path Relabeler/bin/netstandard2.0
.
Randomly generate a secret token and set secretToken
in function.json
to the value of GITHUB_WEBHOOK_SECRET
in local.settings.json
.
openssl rand -base64 24
If your Azure Function is configured to validate GitHub webhook signatures using a secret, include the X-Hub-Signature-256
header in your requests.
Define a secret string that both GitHub and your Azure Function know.
SECRET=your_secret_here
Calculate the HMAC SHA-256 signature of the payload using the secret.
SIGNATURE=$(echo -n '{"action":"opened",...}' | openssl dgst -sha256 -hmac "$SECRET" | sed 's/^.* //')
Note: Replace
'{"action":"opened",...}'
with your actual JSON payload.
Add the X-Hub-Signature-256
header to your request.
curl -X POST http://localhost:7071/api/Relabeler \
-H "Content-Type: application/json" \
-H "X-GitHub-Event: pull_request" \
-H "X-GitHub-Delivery: 1234567890" \
-H "X-Hub-Signature-256: sha256=$SIGNATURE" \
-d @pull_request_opened.json
- Set
githubToken
inlocal.settings.json
to a GitHub personal access token with therepo
scope. - See GitHub Tokens.
To test webhooks from GitHub directly to your local machine without deploying, use Ngrok to create a secure tunnel.
ngrok http 7071
- Send Test Payloads: Use GitHub's "Test webhook" feature to send sample payloads and ensure your function processes them correctly.
- Monitor Logs: Utilize Azure's monitoring tools, including Application Insights, to check the logs for any discrepancies or errors during webhook handling.