Commit 28e6b1b2 authored by Christine Kim's avatar Christine Kim
Browse files

Add updated workflow files

parent f056bce8
Loading
Loading
Loading
Loading
+32 −14
Original line number Diff line number Diff line
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
@@ -8,12 +9,15 @@ import sys
# 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='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}",
@@ -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:
@@ -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('#',''))
    # 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 pattern
            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:
    """
@@ -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:
@@ -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)
@@ -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:
+2 −1
Original line number Diff line number Diff line
@@ -33,7 +33,8 @@
- [ ] My change requires a change to the documentation and I have updated the documentation accordingly.
- [ ] I have added tests to cover my changes.
- [ ] I have added myself to the [.zenodo.json](https://github.com/USGS-Astrogeology/ISIS3/blob/dev/.zenodo.json) document.
- [ ] I have added any user impacting changes to the [CHANGELOG.md](https://github.com/USGS-Astrogeology/ISIS3/blob/dev/CHANGELOG.md) document.
- [ ] I have added my user impacting change to the [CHANGELOG.md](https://github.com/USGS-Astrogeology/ISIS3/blob/dev/CHANGELOG.md) document. <!--- NOTE: You can only have one changelog entry per PR, see https://github.com/DOI-USGS/ISIS3/blob/dev/CONTRIBUTING.md -->


## Licensing
This project is mostly composed of free and unencumbered software released into the public domain, and we are unlikely to accept contributions that are not also released into the public domain. Somewhere near the top of each file should have these words:
+1 −1
Original line number Diff line number Diff line
requests==2.28.2
 No newline at end of file
requests==2.32.0
 No newline at end of file
+1 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ jobs:
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        GITHUB_API_URL: ${{ secrets.GITHUB_API_URL }}
        GITHUB_SERVER_URL: ${{ secrets.GITHUB_SERVER_URL }}
        GITHUB_SHA: ${{ secrets.GITHUB_SHA }}
      run: |
        echo "result=$(python ./.github/pr_label_checker.py)" >> $GITHUB_OUTPUT
+45 −0
Original line number Diff line number Diff line
name: Github to Gitlab CI - Run CodeBuild

env:
  PR_NUMBER: ${{ github.event.number }}
  GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }}

on:
  pull_request_target:
    branches: 
      - '**'

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
      - name: Run script
        run: |
          git config --global user.name "Github_CI"
          git config --global user.email "project_14468_bot_3f7d8e1a392afd88ead5f3f3154e809d@noreply.gitlab.com"
          git clone https://isis-codebuild-ci:$GITLAB_TOKEN@code.usgs.gov/astrogeology/isis-codebuild-ci.git
          cd isis-codebuild-ci
          git checkout -b PR_$PR_NUMBER
          echo -e "\nenv: \n  shell: bash \n  variables: \n    PR_NUMBER: $PR_NUMBER \n    MERGE_BRANCH: $GITHUB_BASE_REF" >> buildspec.yml
          git commit -a -m "$PR_NUMBER"
          git push origin PR_$PR_NUMBER --force
          
  comment-bot:
      permissions: write-all
      runs-on: [ubuntu-latest]
      needs: [build]
      steps:
      - name: Check out code
        uses: actions/checkout@v4 
      - name: Comment PR
        uses: thollander/actions-comment-pull-request@v2
        with:
          message: |
            The build and test suite have started for your pull request. 
            
            To view your [build log](https://us-west-2.codebuild.aws.amazon.com/project/eyJlbmNyeXB0ZWREYXRhIjoiNDJNZ2MxbHFKTkwxV1RyQUxJekdJY3FIanNqU29rMHB4Nk1YUzk4REIrZUZDeEtEaW9HQlZ1dTZOSHpML2VUTGVDekYydmVFcU9sUHJKN20wQzd1Q0UzSzJscnB0MElDb1M3Ti9GTlJYR1RuMWJTV3V1SkJTa3NoYmc9PSIsIml2UGFyYW1ldGVyU3BlYyI6IjF3U2NTSGlDcEtCc29YVnEiLCJtYXRlcmlhbFNldFNlcmlhbCI6MX0%3D), please reference the build with source version: "PR_${{ github.event.number }}". 
            
            Additionally, check the latest "dev" source version to identify existing test failures. Please note that you are not responsible for the test failures that exist on both your PR and the dev branch.
          
 No newline at end of file
Loading