Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
linters fixes
  • Loading branch information
abikouo authored and Qalthos committed Apr 5, 2023
commit 70a4bd023a393394b9550b4cfdd195a7c5653623
15 changes: 15 additions & 0 deletions .github/actions/checkout_repository/resolve_dependency.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/python
"""Script to check if a depends-on pull request has been defined into pull request body."""

import logging
import os
Expand All @@ -15,6 +16,13 @@


def get_pr_merge_commit_sha(repository: str, pr_number: int) -> str:
"""Retrieve pull request merge commit sha.

:param repository: The repository name
:param pr_number: The pull request number
:returns: The pull request merge commit sha if it exists
:raises ValueError: if the pull request is not mergeable
"""
access_token = os.environ.get("GITHUB_TOKEN")
gh_obj = Github(access_token)
repo = gh_obj.get_repo(repository)
Expand All @@ -29,6 +37,12 @@ def get_pr_merge_commit_sha(repository: str, pr_number: int) -> str:


def resolve_ref(pr_body: str, repository: str) -> int:
"""Get pull request reference number defined with Depends-On.

:param pr_body: the pull request body
:param repository: The repository name
:returns: pull request number if it is defined else 0
"""
pr_regx = re.compile(
rf"^Depends-On:[ ]*https://github.com/{repository}/pull/(\d+)\s*$",
re.MULTILINE | re.IGNORECASE,
Expand All @@ -39,6 +53,7 @@ def resolve_ref(pr_body: str, repository: str) -> int:


def main() -> None:
"""Run the script."""
pr_body = os.environ.get("RESOLVE_REF_PR_BODY") or ""
repository = os.environ.get("RESOLVE_REF_REPOSITORY") or ""

Expand Down
60 changes: 49 additions & 11 deletions .github/actions/checkout_repository/test_resolve_dependency.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env python3
"""Module used to test resolve_dependency.py script."""

import os
import string

from pathlib import PosixPath
from random import choice
from unittest.mock import MagicMock
from unittest.mock import patch
Expand Down Expand Up @@ -38,33 +40,55 @@
("Depends-On: https://github.com/my_org/my_collection/pull", False),
],
)
def test_resolve_ref(pr_body, match):
def test_resolve_ref(pr_body: str, match: bool) -> None:
"""Test resolve_ref function.

:param pr_body: pull request body
:param match: whether a depends-on should be found or not
"""
expected = 12345 if match else 0
assert resolve_ref(pr_body, "my_org/my_collection") == expected


class FakePullRequest:
def __init__(self, mergeable):
# pylint: disable=too-few-public-methods
"""Class to simulate PullRequest Object."""

def __init__(self, mergeable: bool) -> None:
"""Class constructor.

:param mergeable: whether the pull request is mergeable or not
"""
self.mergeable = mergeable
self.merge_commit_sha = self.generate_commit_sha()

@staticmethod
def generate_commit_sha(length=16):
def generate_commit_sha(length: int = 16) -> str:
"""Generate random commit sha.

:param length: The length of the generated string
:returns: The generated commit sha
"""
data = string.ascii_letters + string.digits
return "".join([choice(data) for _ in range(length)])


@pytest.mark.parametrize("mergeable", [True, False])
@patch("resolve_dependency.Github")
def test_get_pr_merge_commit_sha(m_Github, mergeable):
m_github_obj = MagicMock()
m_Github.return_value = m_github_obj
def test_get_pr_merge_commit_sha(m_github: MagicMock, mergeable: bool) -> None:
"""Test get_pr_merge_commit_sha function.

:param m_github: The github module
:param mergeable: whether the pull request is mergeable or not
"""
github_obj = MagicMock()
m_github.return_value = github_obj

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

m_github_repo = MagicMock()
m_github_obj.get_repo = MagicMock()
m_github_obj.get_repo.return_value = m_github_repo
github_obj.get_repo = MagicMock()
github_obj.get_repo.return_value = m_github_repo

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

m_Github.assert_called_once_with("unittest_github_token")
m_github_obj.get_repo.assert_called_once_with(repository)
m_github.assert_called_once_with("unittest_github_token")
github_obj.get_repo.assert_called_once_with(repository)
m_github_repo.get_pull.assert_called_once_with(pr_number)


@pytest.mark.parametrize("repository", [True, False])
@pytest.mark.parametrize("resolve_ref_pr", [0, 1])
@patch("resolve_dependency.get_pr_merge_commit_sha")
@patch("resolve_dependency.resolve_ref")
def test_main(m_resolve_ref, m_get_pr_merge_commit_sha, repository, resolve_ref_pr, tmp_path):
def test_main(
m_resolve_ref: MagicMock,
m_get_pr_merge_commit_sha: MagicMock,
repository: bool,
resolve_ref_pr: int,
tmp_path: PosixPath,
) -> None:
"""Test main function.

:param m_resolve_ref: The resolve_ref mock function
:param m_get_pr_merge_commit_sha: The get_pr_merge_commit_sha mock function
:param repository: whether the repository is defined on environment variable or not
:param resolve_ref_pr: The pull request number
:param tmp_path: The temporary path for file to create for test
"""
pr_body = "My pull request body - this is a sample for unit tests"
repository_name = "my_test_repository"
os.environ["RESOLVE_REF_PR_BODY"] = pr_body
Expand Down
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ repos:
additional_dependencies:
- types-PyYAML
- pygithub
- pytest

- repo: https://github.com/pycqa/pylint
rev: v3.0.0a6
Expand All @@ -71,6 +72,7 @@ repos:
additional_dependencies:
- PyYAML
- pygithub
- pytest

- repo: local
hooks:
Expand Down
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[flake8]
max-line-length = 160
ignore = D100,D101,D102,D103,D104,D105,D106,D107
exclude =