Skip to content

Commit

Permalink
Suppress PR body warning without a PR
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Aug 25, 2023
1 parent 42d8619 commit 772d11f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
24 changes: 1 addition & 23 deletions ckan_meta_tester/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import logging
from os import environ
from typing import Optional
from urllib.parse import urlparse
import requests
from exitstatus import ExitStatus

from .ckan_meta_tester import CkanMetaTester
Expand All @@ -22,27 +20,7 @@ def test_metadata() -> None:
environ.get('INPUT_GAME', 'KSP'))
sys.exit(ExitStatus.success
if ex.test_metadata(environ.get('INPUT_SOURCE', 'netkans'),
get_pr_body(github_token, environ.get('INPUT_PULL_REQUEST_URL')),
environ.get('INPUT_PULL_REQUEST_URL'),
github_token,
environ.get('INPUT_DIFF_META_ROOT'))
else ExitStatus.failure)


def get_pr_body(github_token: Optional[str], pr_url: Optional[str]) -> str:
# Get PR body text
if pr_url:
headers = { 'Accept': 'application/vnd.github.v3.raw+json' }
parsed_pr_url = urlparse(pr_url)
if github_token:
if parsed_pr_url.scheme == 'https' and parsed_pr_url.netloc == 'api.github.com' \
and parsed_pr_url.path.startswith('/repos/'):
headers['Authorization'] = f'token {github_token}'
else:
logging.warning('Invalid pull request url, omitting Authorization header')

resp = requests.get(pr_url, headers=headers, timeout=30)
if resp.ok:
# If the PR has an empty body, 'body' is set to None, not the empty string
return resp.json().get('body') or ''
logging.warning(resp.text)
return ''
33 changes: 30 additions & 3 deletions ckan_meta_tester/ckan_meta_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
from typing import Optional, Iterable, Set, List, Any, Tuple, OrderedDict as OD
from collections import OrderedDict
from tempfile import TemporaryDirectory
from urllib.parse import urlparse

from git import Repo, DiffIndex
from exitstatus import ExitStatus
import requests

from netkan.repos import CkanMetaRepo

Expand Down Expand Up @@ -65,7 +68,9 @@ def debug_action(self) -> None:
logging.debug('Files: %s', ', '.join([str(x) for x in working.glob('*')]))
logging.debug('Repo: %s', Repo('.').git_dir)

def test_metadata(self, source: str = 'netkans', pr_body: str = '', github_token: Optional[str] = None, diff_meta_root: Optional[str] = None) -> bool:
def test_metadata(self, source: str = 'netkans', pr_body_url: Optional[str] = None, github_token: Optional[str] = None, diff_meta_root: Optional[str] = None) -> bool:

pr_body = self.get_pr_body(github_token, pr_body_url)

# Work around issue noted in noted in KSP-CKAN/NetKAN#9527
Repo('.').git.execute(['git', 'config', '--global', '--add', 'safe.directory', '/github/workspace'])
Expand All @@ -75,7 +80,7 @@ def test_metadata(self, source: str = 'netkans', pr_body: str = '', github_token

# Escape hatch in case author replaces a download after a previous success
# (which will save it to the persistent cache)
overwrite_cache = ('#overwrite_cache' in pr_body)
overwrite_cache = False if pr_body is None else ('#overwrite_cache' in pr_body)
logging.debug('overwrite_cache: %s', overwrite_cache)

if not self.CACHE_PATH.exists():
Expand All @@ -85,7 +90,7 @@ def test_metadata(self, source: str = 'netkans', pr_body: str = '', github_token
meta_repo = CkanMetaRepo(Repo(Path(diff_meta_root))) if diff_meta_root else None

for file in self.files_to_test(source):
if len(pr_body) < 1:
if pr_body is not None and len(pr_body) < 1:
# Warn for empty PR body on every file so it's noticeable in the files changed tab
print(f'::warning file={file}::Pull requests should have a description with a summary of the changes')
if not self.test_file(file, overwrite_cache, github_token, meta_repo):
Expand Down Expand Up @@ -222,6 +227,28 @@ def install_identifiers(self, identifiers: List[str], pr_body: Optional[str]) ->
input_str=self.CKAN_INSTALL_IDENTIFIERS_TEMPLATE.substitute(
identifiers=' '.join(identifiers)))

@staticmethod
def get_pr_body(github_token: Optional[str], pr_url: Optional[str]) -> Optional[str]:
# Get PR body text
if pr_url:
headers = { 'Accept': 'application/vnd.github.v3.raw+json' }
parsed_pr_url = urlparse(pr_url)
if github_token:
if parsed_pr_url.scheme == 'https' and parsed_pr_url.netloc == 'api.github.com' \
and parsed_pr_url.path.startswith('/repos/'):
headers['Authorization'] = f'token {github_token}'
else:
logging.warning('Invalid pull request url, omitting Authorization header')

resp = requests.get(pr_url, headers=headers, timeout=30)
if resp.ok:
# If the PR has an empty body, 'body' is set to None, not the empty string
return resp.json().get('body') or ''
logging.warning(resp.text)
return ''
# Lacking a URL means we're not in a pull request, which is different from the body being empty
return None

def pr_body_versions(self, pr_body: Optional[str]) -> List[GameVersion]:
if not pr_body:
return []
Expand Down

0 comments on commit 772d11f

Please sign in to comment.