Code Monkey home page Code Monkey logo

jira-pr-check-python's Introduction

Cloud Function: Jira Github PR Check

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.

Usage

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
...

Configuration

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/

Development

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.

Dependency management

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

Local development

Configure the dev/.env file

# 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

Running the cloud function locally

$ 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
...

Exposing the cloud function with ngrok

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.

Local debugging

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.

jira-pr-check-python's People

Contributors

fredericbarrau avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.