Cloud function used as a Github Webhook. Will check if a PR name is properly linked to a jira ticket.
The current document is for the cloud function hosted in Google Cloud that will be called from Github in order to validate the branch names in project.
A proper branch name must follow the jira convention, and contain an issue name.
Locally:
$ functions_framework --target=jira_github_pr_check
Runs on port localhost:8080
In Cloud Run, using gcloud for deployment (test only):
$ gcloud deploy github-jira-webhook --source . --runtime python310 --entry-point jira_github_pr_check
...
The cloud function grabs the configuration secrets from environment variables. These environment variables contain secrets, and should be provided to the cloud function during deployment (see the dedicated terraform code).
For local development, a dev/.env
file can be created (ignored by git) in order to add these configurations and secrets, see below.
Required environment variables:
Environment variable | Description | Sensitive | Example |
---|---|---|---|
JIRA_DOMAIN | Domain of the Jira server to use | No | altirnao.atlassian.net |
LOG_LEVEL | Log level of the cloud function, see the python logger documentation for the available values | No | 10 |
JIRA_EMAIL | Account of the jira user associated with the jira_token | No | [email protected] |
JIRA_TOKEN | Jira token associated with the JIRA_EMAIL |
Yes | N/A |
GITHUB_TOKEN | Github PAT token. See here for generating a API token | Yes | github_pat_xxxxxx |
GITHUB_WEBHOOK_SECRET | Webhook secret, provided at webhook creation | Yes | 123456 |
CALLBACK_URL | Url of the "detail" link in github. For now, the relevant page of the documentation site | No | https://docs.tech.aodocs.app/ |
VSCode: for convenience, development / debugging of this cloud function should be done in a dedicated vscode workspace containing the current source folder only. The root folder of the cloud function sources contains a vscode configuration file (.vscode/launch.json
) which, when in a dedicated workspace, provides the configuration for using the debugger of the python extension.
This project uses poetry for managing the the project dependencies.
Generate the requirements.txt
for the google cloud builder with the following command once a new dependency has been added / removed:
$ poetry export --without-hashes --format=requirements.txt > requirements.txt
# dev/.env file
JIRA_DOMAIN=altirnao.atlassian.net
# See https://docs.python.org/3/library/logging.html#logging-levels
LOG_LEVEL=10
# Account of the jira user associated with the jira_token:
JIRA_EMAIL[email protected]
# See gcloud secret:
# List the versions:
# gcloud secrets versions list jira-github-pr-check-user-token
# Get the value for the latest version X:
# gcloud secrets versions access X --secret jira-github-pr-check-user-token
JIRA_TOKEN=< see google cloud secrets>
# Github PAT token
# See here for generating a API token :
# https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token
GITHUB_TOKEN=github_pat_xxxxxx
# Webhook secret, provided at webhook creation:
GITHUB_WEBHOOK_SECRET=123456
$ poetry shell
Spawning shell within /Users/fredericbarrau/Library/Caches/pypoetry/virtualenvs/jira-github-pr-check-python-I_CHN4P6-py3.10
$ poetry install
Installing dependencies from lock file
No dependencies to install or update
$ functions_framework --target=jira_github_pr_check
...
Ngrok in a tool for providing a public access of a local development service.
When launched locally (see above), the cloud function is listening to port 8080 (default) on localhost. Ngrok can expose this port in ngrok.io:
$ ngrok htp 8080
Session Status online
Account Fred Barrau (Plan: Free)
Version 2.3.40
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding http://fe0b-217-128-118-2.ngrok.io -> http://localhost:8080
Forwarding https://fe0b-217-128-118-2.ngrok.io -> http://localhost:8080
Connections ttl opn rt1 rt5 p50 p90
31 0 0.00 0.00 0.65 15.16
HTTP Requests
-------------
POST / 502 Bad Gateway
POST / 200 OK
POST / 200 OK
POST / 404 NOT FOUND
POST / 404 NOT FOUND
POST / 200 OK
POST / 404 NOT FOUND
POST / 404 NOT FOUND
POST / 404 NOT FOUND
POST /
This exposition can then be used to test from a Github webhook configured on a test repository.
As mentionned above, the current folder contains a .vscode/launch.json
file which, when used in a dedicated workspace, provides the configuration for the debugger of the python vscode extension.