Skip to content

Commit d6ba8fe

Browse files
abikouoQalthos
authored andcommitted
linters fixes
1 parent 0baef43 commit d6ba8fe

File tree

4 files changed

+67
-12
lines changed

4 files changed

+67
-12
lines changed

.github/actions/checkout_repository/resolve_dependency.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/python
2+
"""Script to check if a depends-on pull request has been defined into pull request body."""
23

34
import logging
45
import os
@@ -15,6 +16,13 @@
1516

1617

1718
def get_pr_merge_commit_sha(repository: str, pr_number: int) -> str:
19+
"""Retrieve pull request merge commit sha.
20+
21+
:param repository: The repository name
22+
:param pr_number: The pull request number
23+
:returns: The pull request merge commit sha if it exists
24+
:raises ValueError: if the pull request is not mergeable
25+
"""
1826
access_token = os.environ.get("GITHUB_TOKEN")
1927
gh_obj = Github(access_token)
2028
repo = gh_obj.get_repo(repository)
@@ -29,6 +37,12 @@ def get_pr_merge_commit_sha(repository: str, pr_number: int) -> str:
2937

3038

3139
def resolve_ref(pr_body: str, repository: str) -> int:
40+
"""Get pull request reference number defined with Depends-On.
41+
42+
:param pr_body: the pull request body
43+
:param repository: The repository name
44+
:returns: pull request number if it is defined else 0
45+
"""
3246
pr_regx = re.compile(
3347
rf"^Depends-On:[ ]*https://github.com/{repository}/pull/(\d+)\s*$",
3448
re.MULTILINE | re.IGNORECASE,
@@ -39,6 +53,7 @@ def resolve_ref(pr_body: str, repository: str) -> int:
3953

4054

4155
def main() -> None:
56+
"""Run the script."""
4257
pr_body = os.environ.get("RESOLVE_REF_PR_BODY") or ""
4358
repository = os.environ.get("RESOLVE_REF_REPOSITORY") or ""
4459

.github/actions/checkout_repository/test_resolve_dependency.py

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#!/usr/bin/env python3
2+
"""Module used to test resolve_dependency.py script."""
23

34
import os
45
import string
56

7+
from pathlib import PosixPath
68
from random import choice
79
from unittest.mock import MagicMock
810
from unittest.mock import patch
@@ -38,33 +40,55 @@
3840
("Depends-On: https://github.com/my_org/my_collection/pull", False),
3941
],
4042
)
41-
def test_resolve_ref(pr_body, match):
43+
def test_resolve_ref(pr_body: str, match: bool) -> None:
44+
"""Test resolve_ref function.
45+
46+
:param pr_body: pull request body
47+
:param match: whether a depends-on should be found or not
48+
"""
4249
expected = 12345 if match else 0
4350
assert resolve_ref(pr_body, "my_org/my_collection") == expected
4451

4552

4653
class FakePullRequest:
47-
def __init__(self, mergeable):
54+
# pylint: disable=too-few-public-methods
55+
"""Class to simulate PullRequest Object."""
56+
57+
def __init__(self, mergeable: bool) -> None:
58+
"""Class constructor.
59+
60+
:param mergeable: whether the pull request is mergeable or not
61+
"""
4862
self.mergeable = mergeable
4963
self.merge_commit_sha = self.generate_commit_sha()
5064

5165
@staticmethod
52-
def generate_commit_sha(length=16):
66+
def generate_commit_sha(length: int = 16) -> str:
67+
"""Generate random commit sha.
68+
69+
:param length: The length of the generated string
70+
:returns: The generated commit sha
71+
"""
5372
data = string.ascii_letters + string.digits
5473
return "".join([choice(data) for _ in range(length)])
5574

5675

5776
@pytest.mark.parametrize("mergeable", [True, False])
5877
@patch("resolve_dependency.Github")
59-
def test_get_pr_merge_commit_sha(m_Github, mergeable):
60-
m_github_obj = MagicMock()
61-
m_Github.return_value = m_github_obj
78+
def test_get_pr_merge_commit_sha(m_github: MagicMock, mergeable: bool) -> None:
79+
"""Test get_pr_merge_commit_sha function.
80+
81+
:param m_github: The github module
82+
:param mergeable: whether the pull request is mergeable or not
83+
"""
84+
github_obj = MagicMock()
85+
m_github.return_value = github_obj
6286

6387
os.environ["GITHUB_TOKEN"] = "unittest_github_token"
6488

6589
m_github_repo = MagicMock()
66-
m_github_obj.get_repo = MagicMock()
67-
m_github_obj.get_repo.return_value = m_github_repo
90+
github_obj.get_repo = MagicMock()
91+
github_obj.get_repo.return_value = m_github_repo
6892

6993
local_pr = FakePullRequest(mergeable=mergeable)
7094
m_github_repo.get_pull = MagicMock()
@@ -79,16 +103,30 @@ def test_get_pr_merge_commit_sha(m_Github, mergeable):
79103
with pytest.raises(ValueError):
80104
get_pr_merge_commit_sha(repository, pr_number)
81105

82-
m_Github.assert_called_once_with("unittest_github_token")
83-
m_github_obj.get_repo.assert_called_once_with(repository)
106+
m_github.assert_called_once_with("unittest_github_token")
107+
github_obj.get_repo.assert_called_once_with(repository)
84108
m_github_repo.get_pull.assert_called_once_with(pr_number)
85109

86110

87111
@pytest.mark.parametrize("repository", [True, False])
88112
@pytest.mark.parametrize("resolve_ref_pr", [0, 1])
89113
@patch("resolve_dependency.get_pr_merge_commit_sha")
90114
@patch("resolve_dependency.resolve_ref")
91-
def test_main(m_resolve_ref, m_get_pr_merge_commit_sha, repository, resolve_ref_pr, tmp_path):
115+
def test_main(
116+
m_resolve_ref: MagicMock,
117+
m_get_pr_merge_commit_sha: MagicMock,
118+
repository: bool,
119+
resolve_ref_pr: int,
120+
tmp_path: PosixPath,
121+
) -> None:
122+
"""Test main function.
123+
124+
:param m_resolve_ref: The resolve_ref mock function
125+
:param m_get_pr_merge_commit_sha: The get_pr_merge_commit_sha mock function
126+
:param repository: whether the repository is defined on environment variable or not
127+
:param resolve_ref_pr: The pull request number
128+
:param tmp_path: The temporary path for file to create for test
129+
"""
92130
pr_body = "My pull request body - this is a sample for unit tests"
93131
repository_name = "my_test_repository"
94132
os.environ["RESOLVE_REF_PR_BODY"] = pr_body

.pre-commit-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ repos:
6363
additional_dependencies:
6464
- types-PyYAML
6565
- pygithub
66+
- pytest
6667

6768
- repo: https://github.com/pycqa/pylint
6869
rev: v2.17.0
@@ -71,6 +72,7 @@ repos:
7172
additional_dependencies:
7273
- PyYAML
7374
- pygithub
75+
- pytest
7476

7577
- repo: local
7678
hooks:

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[flake8]
22
max-line-length = 160
3-
ignore = D103,D100
3+
ignore = D100,D101,D102,D103,D104,D105,D106,D107
44
exclude =

0 commit comments

Comments
 (0)