Skip to content

Commit

Permalink
update PR with files
Browse files Browse the repository at this point in the history
  • Loading branch information
tamsinforbes committed Apr 19, 2024
1 parent dee5835 commit e4503a0
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 25 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.built-image
.DS_Store
__pycache__/
*.pytest_cache
*.pytest_cache
*venv
*.venv
2 changes: 1 addition & 1 deletion python-malformed-yaml/github_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def __init__(self, github_token, repository_name: str, pr_number: int):
self.repository_name = repository_name
self.client = Github(self.github_token)

def get_changed_files_from_pr(self) -> list:
def get_changed_files_from_pr(self) -> list:
return [file.filename for file in self.client.get_repo(
self.repository_name).get_pull(self.pr_number).get_files()]

Expand Down
31 changes: 25 additions & 6 deletions python-malformed-yaml/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import pathlib
from pathlib import Path
import re
import yaml
from github_service import GitHubService as github_service

Expand All @@ -17,16 +18,34 @@ def get_github_env() -> tuple:
return token, repo, pr_number


def get_changed_yaml_files_from_pr():
def get_changed_yaml_files_from_pr() -> list:
token, repository_name, pr = get_github_env()
github = github_service(token, repository_name, pr)
github = github_service(token, repository_name, int(pr))
changed_files = github.get_changed_files_from_pr()
print(changed_files)
pattern = re.compile("\\.yml$|\\.yaml$")
changed_yaml_files = [file for file in changed_files if pattern.search(file)]
return changed_yaml_files

def get_malformed_yaml_files(yaml_files: list) -> list:
print(f"WD: {os.getcwd()}")
# p = Path.cwd()
# os.chdir(p.parent)
# print(f"NEW WD: {os.getcwd()}")

malformed_yaml_files = []
for y in yaml_files:
with open(y) as stream:
try:
yaml.safe_load(stream)
except yaml.YAMLError as exc:
malformed_yaml_files.append(f"\n{str(y)}:\n{str(exc)}")
return malformed_yaml_files

def main():
print("!!!OUTPUT!!!")
get_changed_yaml_files_from_pr()
changed_yaml_files = get_changed_yaml_files_from_pr()
print(changed_yaml_files)
malformed_yaml_files = get_malformed_yaml_files(changed_yaml_files)
print(malformed_yaml_files)

# def main():

Expand Down
2 changes: 2 additions & 0 deletions python-malformed-yaml/requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pytest==8.1.1
pytest-mock==3.14.0
2 changes: 1 addition & 1 deletion python-malformed-yaml/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
PyGithub==2.2.0
PyYAML==6.0.1
PyYAML==6.0.1
28 changes: 28 additions & 0 deletions python-malformed-yaml/test/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pytest
import unittest
from unittest.mock import patch
from main import get_changed_yaml_files_from_pr, get_malformed_yaml_files, main
from github_service import GitHubService as github_service
# python_malformed_yaml_main = importlib.import_module(
# "python-malformed-yaml.main")
# main = python_malformed_yaml_main.main

def test_get_changed_yaml_files_from_pr(mocker):
# get_github_env_mock = mocker.patch("get_github_env")
# get_github_env_mock.return_value = ("fake_github_token", "fake_repo", "123")
# github_mock = mocker.patch("github")

get_changed_files_from_pr_mock = mocker.patch("get_changed_files_from_pr")
get_changed_files_from_pr_mock.return_value(["a.yml", "b.py", "c.yaml", "d.txt"])
assert get_changed_yaml_files_from_pr() == ["a.yml", "c.yaml"]


def test_main_exception():
with pytest.raises(Exception) as exc_info:
main()
expected_malformed_files = [
"test_python_malformed_yaml/test_yaml_files/bad.yml",
"test_python_malformed_yaml/test_yaml_files/bad.yaml"
]
assert all(file in str(exc_info.value)
for file in expected_malformed_files)
Empty file.
16 changes: 0 additions & 16 deletions python-malformed-yaml/test/test_python_malformed_yaml/test_main.py

This file was deleted.

0 comments on commit e4503a0

Please sign in to comment.