-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create new actions pipeline; add-jira-link-pr-open.yml, that will add a link in Jira back to the PR that this is triggered on. Add some sample .secrets-sample and a sample pull-request-event.json for testing the pipeline code. And mention this in the relevant README.md.
- Loading branch information
1 parent
28e81f5
commit 0f48ad3
Showing
4 changed files
with
506 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# | ||
# 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: Set Up Python | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: 3.12 | ||
|
||
- name: Build Python script | ||
run: | | ||
MY_PYTHON_SCRIPT=$(cat << EOF | ||
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 jira_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_url }}' | ||
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=standard_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) | ||
echo "MY_PYTHON_SCRIPT=$MY_PYTHON_SCRIPT" >> $GITHUB_ENV | ||
- name: Execute Python script | ||
run: | | ||
pip install requests | ||
python -c "$MY_PYTHON_SCRIPT" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_URL=jira-url | ||
JIRA_TOKEN=jira-token |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.