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

Test dev #4

Closed
wants to merge 41 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
aa2a3b2
Update .gitignore
chkim-usgs Mar 7, 2023
23b5d47
Auto pr labeling first attempt
chkim-usgs Mar 8, 2023
739c1c9
Merge pull request #2 from chkim-usgs/test_bugfix_1
chkim-usgs Mar 8, 2023
db36b7e
Update name
chkim-usgs Mar 8, 2023
c2c28dd
Merge branch 'test_dev' of github.com:chkim-usgs/ISIS3 into test_dev
chkim-usgs Mar 8, 2023
cb88056
Update .gitignore
chkim-usgs Mar 8, 2023
2434bd2
Pull all PRs for testing first
chkim-usgs Mar 8, 2023
686efd0
Move script
chkim-usgs Mar 8, 2023
66535cf
Install with requirements.txt
chkim-usgs Mar 8, 2023
9af8186
Rename to requirements.txt
chkim-usgs Mar 8, 2023
d30376c
Fix headers case
chkim-usgs Mar 8, 2023
f45fc02
Give re module an alias
chkim-usgs Mar 9, 2023
96614e3
import sys
chkim-usgs Mar 9, 2023
ea39e6d
Traceback for update request
chkim-usgs Mar 9, 2023
c4e6d58
View update response json
chkim-usgs Mar 9, 2023
6839acd
Give permission to write to pull-requests
chkim-usgs Mar 9, 2023
594b158
Rename script and clean up exception handling
chkim-usgs Mar 9, 2023
b26d5f7
Update yaml to reflect script name change
chkim-usgs Mar 9, 2023
095578a
True to inner
chkim-usgs Mar 9, 2023
dfe93bc
Change True case
chkim-usgs Mar 9, 2023
982c127
Change path of requirements.txt and script
chkim-usgs Mar 9, 2023
46366aa
Refactor
chkim-usgs Mar 9, 2023
47aba6d
Use new output method
chkim-usgs Mar 9, 2023
09890fd
Change name
chkim-usgs Mar 9, 2023
1accef5
Change to logging
chkim-usgs Mar 9, 2023
4be2080
Change variable name
chkim-usgs Mar 9, 2023
a3e3964
check if_pr_bugfix() outside
chkim-usgs Mar 9, 2023
ee92a44
Check if strings output
chkim-usgs Mar 9, 2023
898fc7d
Try print
chkim-usgs Mar 9, 2023
a289abb
Log outputs
chkim-usgs Mar 9, 2023
7657484
Print instead of logging
chkim-usgs Mar 9, 2023
eb56344
print boolean
chkim-usgs Mar 9, 2023
276e9f1
Check if output is passed on
chkim-usgs Mar 9, 2023
5024357
This commit should not trigger the release_pull_request job
chkim-usgs Mar 9, 2023
27c0680
Merge pull request #3 from chkim-usgs/test_bugfix_1
chkim-usgs Mar 9, 2023
56ea159
Fix commits url, should not complete release job
chkim-usgs Mar 9, 2023
4d5a609
Merge branch 'test_dev' of github.com:chkim-usgs/ISIS3 into test_dev
chkim-usgs Mar 9, 2023
eac41cb
Print response json
chkim-usgs Mar 9, 2023
e1a6488
wrap dict in str for print
chkim-usgs Mar 9, 2023
ae50d62
Run python script outside echo to get print stmts
chkim-usgs Mar 9, 2023
68a9c20
This commit should not trigger release job
chkim-usgs Mar 9, 2023
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
150 changes: 150 additions & 0 deletions .github/pr_label_checker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import logging
import os
import re as rgx
from requests import get, post
from requests import Response
from requests.exceptions import HTTPError, RequestException
import sys

# Constants
GITHUB_TOKEN=os.environ["GITHUB_TOKEN"]
GITHUB_API_URL=os.environ["GITHUB_API_URL"]
GITHUB_SHA=os.environ["GITHUB_SHA"]

BASE_URL=f'{GITHUB_API_URL}/repos/chkim-usgs/ISIS3'
PULLS_URL=f'{BASE_URL}/pulls'
COMMITS_URL=f'{BASE_URL}/commits'
ISSUES_URL=f'{BASE_URL}/issues'
HEADERS = {
"Accept": "application/vnd.github+json",
"Authorization" : f"Bearer {GITHUB_TOKEN}",
"X-GitHub-Api-Version": "2022-11-28"
}

def get_prs_associated_with_commit() -> Response:
# Get list of PRs associated with commitSHA
try:
# TODO: Remove below pull all PRs [TESTING ONLY]
# GET_PR_LIST_URL=f'{BASE_URL}/pulls?state=all'
# response = get(GET_PR_LIST_URL, headers=HEADERS)

response = get(f'{COMMITS_URL}/{GITHUB_SHA}/pulls', headers=HEADERS)
response.raise_for_status()
return response
except HTTPError as he:
raise HTTPError("HTTPError in retrieving list of PRs", he)
except RequestException as re:
raise RequestException("Unable to retrieve list of PRs associated with commit.", re)

def get_pr_attributes(response: Response) -> tuple:
# Get necessary PR attributes
pull_response_json = response.json()
# print("Pull response json: " + str(pull_response_json))
if len(pull_response_json) == 0:
print(False)
sys.exit(1)
pull_number = pull_response_json.get("number")
pull_body = pull_response_json.get("body")
return (pull_number, pull_body)

def search_for_linked_issues(pull_body: str) -> list:
"""
Search for linked issues in PR body.
Regex examples:
'#1' - matches
'#123456' - 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('#',''))
# print("ISSUE NUMBERS: " + str(issue_numbers))
return issue_numbers

def get_linked_issues(issue_numbers: list) -> Response:
# Verify issues exists and has labels
for issue_number in issue_numbers:
# Get labels from issue
try:
response = get(f'{ISSUES_URL}/{issue_number}', headers=HEADERS)
response.raise_for_status()
return response
except HTTPError as he:
raise HTTPError("HTTPError in retrieving issues", he)
except RequestException as re:
raise RequestException("Unable to retrieve issues.", re)

def get_issue_labels(response: Response) -> list:
"""
Get labels from all linked issues.
"""
combined_issue_labels = []
# Combine labels into a list
issue_response_json = response.json()
issue_labels = issue_response_json.get("labels")
for issue_label in issue_labels:
# Get name of each label object
label_name = issue_label.get("name")
combined_issue_labels.append(label_name)
# print("COMBINED ISSUE LABELS: " + str(combined_issue_labels))
return combined_issue_labels

def convert_issue_list_to_dict(combined_issue_labels: list) -> dict:
# Convert label list into JSON-formatted dict
labels_data = {}
labels_data["labels"] = combined_issue_labels
return labels_data

def update_pr_labels(pull_number: str, labels_data: dict):

# Update pull request with labels
# Source: https://stackoverflow.com/q/68459601
try:
response = post(f'{ISSUES_URL}/{pull_number}/labels', json=labels_data, headers=HEADERS)
logging.info("UPDATED RESPONSE: " + str(response.json()))
response.raise_for_status()
except HTTPError as he:
raise HTTPError("HTTPError in updating PR.", he)
except RequestException as re:
raise RequestException("Unable to update PR.", re)

def get_pr(pull_number: str) -> Response:
try:
response = get(f'{PULLS_URL}/{pull_number}', headers=HEADERS)
response.raise_for_status()
return response
except HTTPError as he:
raise HTTPError("HTTPError in retrieving issues", he)
except RequestException as re:
raise RequestException("Unable to retrieve issues.", re)

def is_pr_bugfix(response: Response):
labels = response.json().get("labels")
logging.info("Labels: " + str(labels))
for label in labels:
if label.get("name") == "bug":
logging.info("PR is a bugfix")
return True
logging.info("PR is not a bugfix")
return False

if __name__ == "__main__":
try:
response = get_prs_associated_with_commit()
pull_number, pull_body = get_pr_attributes(response)
issue_numbers = search_for_linked_issues(pull_body)
response = get_linked_issues(issue_numbers)
combined_issue_labels = get_issue_labels(response)
labels_data = convert_issue_list_to_dict(combined_issue_labels)
update_pr_labels(pull_number, labels_data)

# Check PR labels for 'bug'
response = get_pr(pull_number)
print(is_pr_bugfix(response))
except (HTTPError, RequestException, Exception):
raise

1 change: 1 addition & 0 deletions .github/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests==2.28.2
89 changes: 61 additions & 28 deletions .github/workflows/autopr_bugfix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,83 @@ name: Cherrypick bugfixes to release branch
on:
push:
branches:
- dev
- test_dev
jobs:
# Check if the PR is a bugfix
check_labels:
runs-on: ubuntu-latest
permissions:
pull-requests: write
name: Check PR labels action step
# Make the output of this job available to other jobs
outputs:
result: ${{steps.check_pr_labels.outputs.result}}
result: ${{steps.execute_py_script.outputs.result}}
steps:
- id: check_pr_labels
uses: 3pointer/[email protected]
- name: checkout repo content
uses: actions/checkout@v2 # checkout the repository content
- name: setup python
uses: actions/setup-python@v4
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
labels: '["bug"]'
python-version: '3.10'
cache: 'pip'
- name: install python packages
run: |
python -m pip install --upgrade pip
pip install -r ./.github/requirements.txt
- name: Set output
id: execute_py_script
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_API_URL: ${{ secrets.GITHUB_API_URL }}
GITHUB_SHA: ${{ secrets.GITHUB_SHA }}
run: |
echo "result=$(python ./.github/pr_label_checker.py)" >> $GITHUB_OUTPUT
# Print the result to the log
- name: See result
run: echo "${{ steps.check_pr_labels.outputs.result }}"
run: echo "${{ steps.execute_py_script.outputs.result }}"
# check_labels:
# runs-on: ubuntu-latest
# name: Check PR labels action step
# # Make the output of this job available to other jobs
# outputs:
# result: ${{steps.check_pr_labels.outputs.result}}
# steps:
# - id: check_pr_labels
# uses: 3pointer/[email protected]
# with:
# github-token: ${{ secrets.GITHUB_TOKEN }}
# labels: '["bug"]'
# # Print the result to the log
# - name: See result
# run: echo "${{ steps.check_pr_labels.outputs.result }}"
release_pull_request:
needs: check_labels
# Only run this step if the code was a bugfix
if: contains(needs.check_labels.outputs.result, 'true')
runs-on: ubuntu-latest
name: release_pull_request
steps:
- name: checkout
uses: actions/[email protected]
# Fetch all info including branches + tags
with:
fetch-depth: 0
- name: 'Get Previous tag'
id: get_latest_tag
uses: "WyriHaximus/github-action-get-previous-tag@v1"
- id: get_short_version
# Get the major / minor version without patch info i.e. 6.0 from 6.0.1
run: |
major_minor=$(echo ${{steps.get_latest_tag.outputs.tag}} | cut -d '.' -f 1,2)
echo "::set-output name=major_minor::$major_minor"
- name: Create PR to branch
uses: adamtharani/github-action-cherry-pick@master
# PR to the latest feature release. Merges are enabled
with:
pr_branch: ${{steps.get_short_version.outputs.major_minor}}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DRY_RUN: false
- name: "Check if check_labels worked"
run: echo "Auto-labeling PR worked!!"
# - name: checkout
# uses: actions/[email protected]
# # Fetch all info including branches + tags
# with:
# fetch-depth: 0
# - name: 'Get Previous tag'
# id: get_latest_tag
# uses: "WyriHaximus/github-action-get-previous-tag@v1"
# - id: get_short_version
# # Get the major / minor version without patch info i.e. 6.0 from 6.0.1
# run: |
# major_minor=$(echo ${{steps.get_latest_tag.outputs.tag}} | cut -d '.' -f 1,2)
# echo "::set-output name=major_minor::$major_minor"
# - name: Create PR to branch
# uses: adamtharani/github-action-cherry-pick@master
# # PR to the latest feature release. Merges are enabled
# with:
# pr_branch: ${{steps.get_short_version.outputs.major_minor}}
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# DRY_RUN: false

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Test bug 1
# Testing again

# Ignore all
*

Expand Down