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

Update parsing #21

Merged
merged 1 commit into from
Aug 22, 2023
Merged
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
46 changes: 32 additions & 14 deletions .github/pr_label_checker.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import re as rgx
from itertools import chain
from requests import get, post
from requests import Response
from requests.exceptions import HTTPError, RequestException
Expand All @@ -8,12 +9,15 @@
# Constants
GITHUB_TOKEN=os.environ["GITHUB_TOKEN"]
GITHUB_API_URL=os.environ["GITHUB_API_URL"]
GITHUB_SERVER_URL=os.environ["GITHUB_SERVER_URL"]
GITHUB_SHA=os.environ["GITHUB_SHA"]

BASE_URL=f'{GITHUB_API_URL}/repos/DOI-USGS/ISIS3'
PULLS_URL=f'{BASE_URL}/pulls'
COMMITS_URL=f'{BASE_URL}/commits'
ISSUES_URL=f'{BASE_URL}/issues'
REPO_URL_PATH='chkim-usgs/ISIS3' #DOI-USGS/ISIS3
API_BASE_URL=f'{GITHUB_API_URL}/repos/{REPO_URL_PATH}'
API_PULLS_URL=f'{API_BASE_URL}/pulls'
API_COMMITS_URL=f'{API_BASE_URL}/commits'
API_ISSUES_URL=f'{API_BASE_URL}/issues'
ISSUES_URL=f'{GITHUB_SERVER_URL}/{REPO_URL_PATH}/issues/'
HEADERS = {
"Accept": "application/vnd.github+json",
"Authorization" : f"Bearer {GITHUB_TOKEN}",
Expand All @@ -25,7 +29,7 @@ def get_prs_associated_with_commit() -> Response:
Get list of PRs associated with commit.
"""
try:
response = get(f'{COMMITS_URL}/{GITHUB_SHA}/pulls', headers=HEADERS)
response = get(f'{API_COMMITS_URL}/{GITHUB_SHA}/pulls', headers=HEADERS)
response.raise_for_status()
return response
except HTTPError as he:
Expand All @@ -52,16 +56,30 @@ def search_for_linked_issues(pull_body: str) -> list:
Regex examples:
'#1' - matches
'#123456' - matches
'https://github.com/DOI-USGS/ISIS3/issues/25' - matches
'# 123' - fails
'#ABC' - fails
'## ABC'- fails
"""
linked_issues = rgx.findall('#[^\D]\d*', pull_body)
issue_numbers = []
for linked_issue in linked_issues:
# Strip linked issue text of '#'
issue_numbers.append(linked_issue.replace('#',''))
return issue_numbers
# Split the PR body by heading
pull_body_list = pull_body.split('##')
regex_pattern = rf'{ISSUES_URL}(\d)|(#[^\D]\d*)'
for section in pull_body_list:
# Find section with heading 'Related Issue'
if section != None and 'Related Issue' in section:
# Find items that match the regex patters
matched_items = rgx.findall(regex_pattern, section)
# Convert list of tuples to list of all items
flattened_list = list(chain.from_iterable(matched_items))
# Remove items of empty values
filtered_list = list(filter(None, flattened_list))
# Remove '#' from items
issue_numbers = list(map(lambda item: item.replace('#', ''), filtered_list))
return issue_numbers
# No linked issues
print(False)
sys.exit(0)


def get_linked_issues(issue_numbers: list) -> list:
"""
Expand All @@ -71,7 +89,7 @@ def get_linked_issues(issue_numbers: list) -> list:
for issue_number in issue_numbers:
# Get labels from issue
try:
response = get(f'{ISSUES_URL}/{issue_number}', headers=HEADERS)
response = get(f'{API_ISSUES_URL}/{issue_number}', headers=HEADERS)
response.raise_for_status()
response_list.append(response)
except HTTPError as he:
Expand Down Expand Up @@ -109,7 +127,7 @@ def update_pr_labels(pull_number: str, combined_issue_labels: list):
# Convert label list into JSON-formatted dict
labels_data = {"labels": combined_issue_labels}
try:
response = post(f'{ISSUES_URL}/{pull_number}/labels', json=labels_data, headers=HEADERS)
response = post(f'{API_ISSUES_URL}/{pull_number}/labels', json=labels_data, headers=HEADERS)
response.raise_for_status()
except HTTPError as he:
raise HTTPError("HTTPError in updating PR.", he)
Expand All @@ -121,7 +139,7 @@ def get_pr(pull_number: str) -> Response:
Get pull request
"""
try:
response = get(f'{PULLS_URL}/{pull_number}', headers=HEADERS)
response = get(f'{API_PULLS_URL}/{pull_number}', headers=HEADERS)
response.raise_for_status()
return response
except HTTPError as he:
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/autopr_bugfix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Cherrypick bugfixes to release branch
on:
push:
branches:
- dev
- test-dev
jobs:
# Check if the PR is a bugfix
check_labels:
Expand Down Expand Up @@ -30,6 +30,7 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_API_URL: ${{ secrets.GITHUB_API_URL }}
GITHUB_API_URL: ${{ secrets.GITHUB_SERVER_URL }}
GITHUB_SHA: ${{ secrets.GITHUB_SHA }}
run: |
echo "result=$(python ./.github/pr_label_checker.py)" >> $GITHUB_OUTPUT
Expand Down