Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: link the pr to solved jira #897

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions .github/workflows/add-jira-link-pr-open.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#
# SPDX-FileCopyrightText: 2024 Lifely
# SPDX-License-Identifier: EUPL-1.2+
#
name: Create Jira Link to the PR
on:
pull_request:
types:
- opened

jobs:
create-jira-web-link:
runs-on: ubuntu-latest
steps:
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.12'

- name: Run Python script
run: |
pip install requests
MY_PYTHON_SCRIPT=$(cat << EOF
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need MY_PYTHON_SCRIPT? Can we execute this directly with python as we anyway immediately do?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need some variable substitutions, but you might be right, and it could be executed directly, without going through a local variable.

import re
import requests
import os
import json


def extract_jira_key(pr_message):
pattern = r"[Ss]olves ([A-Z]+-\d+)"
match = re.search(pattern, pr_message)
if match:
return match.group(1)
return None


def get_pr_context():
event_path = '${{ github.event_path }}'
try:
with open(event_path, 'r', encoding='utf8') as event_file:
payload = json.load(event_file)
pr_description = payload.get('pull_request', {}).get('body')
pr_number = payload.get('pull_request', {}).get('number')
pr_url = payload.get('pull_request', {}).get('html_url')
issue_key = extract_jira_key(pr_description)
if issue_key:
return {
"id": pr_number,
"issue_key": issue_key,
"url": pr_url
}
except FileNotFoundError:
print(f"Error: File '{event_path}' not found.")
return None


def add_remote_link(pr_context):
jira_url = '${{ secrets.jira_api }}'
url = f"{jira_url}/issue/{pr_context['issue_key']}/remotelink"
headers = {
'Content-Type': 'application/json',
'Authorization': 'Basic ${{ secrets.jira_token }}'
}
payload = {
'object': {
'url': pr_context['url'],
'title': f"Pull Request #{pr_context['id']}"
}
}
response = requests.post(url, headers=headers, json=payload)
if response.status_code == 201:
print(f"Remote link added successfully to issue {pr_context['issue_key']}.")
else:
print(f"Error adding remote link: {response.status_code} - {response.text}")


pr_context = get_pr_context()
if pr_context:
add_remote_link(pr_context)
EOF
)
python -c "$MY_PYTHON_SCRIPT"
5 changes: 5 additions & 0 deletions .github/workflows/build-test-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ jobs:
- name: Gradle build
run: ./gradlew build -x test --info

- name: Upload Detekt scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: ${{ github.workspace }}/build/reports/detekt/detekt.sarif

- name: Cache Gradle build artefacts
uses: actions/cache/save@v4
with:
Expand Down
2 changes: 2 additions & 0 deletions scripts/github/.secrets-sample
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# Create you're own .secrets file and set all secrets that will be loaded by ACT
GITHUB_TOKEN=github-token
JIRA_API=https://jira.api
JIRA_TOKEN=jira-token
6 changes: 6 additions & 0 deletions scripts/github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ There's a script that kicks off act in the correct location:

This script can be used to test the whole flow, or you can adjust it so that it
only runs a single job.

# Sample GitHub events
The [sample-events](./sample-events) directory contains sample json events that
will be generated by GitHub in case of events. It only contains those samples
currently used for testing, more samples can be copied from the
[mixu/identify-github-event](https://github.com/mixu/identify-github-event) repository.
Loading
Loading