Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
chore: Checking github-action variable to see auto-merge functional… (
Browse files Browse the repository at this point in the history
#355)

* chore: Checking `github-action` variable to see auto-merge functionality is enabled.
  • Loading branch information
awais786 authored Mar 21, 2023
1 parent d79cbb8 commit ba5ce34
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 6 deletions.
29 changes: 25 additions & 4 deletions jenkins/github_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import re
import time
from ast import literal_eval

import requests
from git import Git, Repo
Expand All @@ -23,6 +24,7 @@ def __init__(self):
self._set_github_token()
self._set_user_email()
self._set_github_instance()
self.AUTOMERGE_ACTION_VAR = 'AUTOMERGE_PYTHON_DEPENDENCIES_UPGRADES_PR'

# FIXME: Does nothing, sets variable to None if env var missing
def _set_github_token(self):
Expand Down Expand Up @@ -284,16 +286,35 @@ def verify_upgrade_packages(self, pull_request):
self._add_comment_about_reqs(pull_request, "List of packages in the PR without any issue", valid_reqs)

if not suspicious_reqs and valid_reqs:
# right now this scripts is adding labels on all prs and other scripts
# merge these labeled prs. We need to do this only for under arch-bom ownership repos.
# pull_request.set_labels('Ready to Merge')
logger.info("Total valid upgrades are %s", valid_reqs)
if self.check_automerge_variable_value(location):
pull_request.set_labels('Ready to Merge')
logger.info("Total valid upgrades are %s", valid_reqs)
else:
self._add_comment_about_reqs(pull_request, "These Packages need manual review.", suspicious_reqs)

else:
logger.info("No package available for comparison.")

def check_automerge_variable_value(self, location):
"""
Check whether repository has the `AUTOMERGE_PYTHON_DEPENDENCIES_UPGRADES_PR` variable
with `True` value exists.
"""
link = location.split('pulls')
get_repo_variable = link[0] + 'actions/variables/' + self.AUTOMERGE_ACTION_VAR
logger.info('Hitting repository to check AUTOMERGE_ACTION_VAR settings.')

headers = {"Accept": "application/vnd.github+json", "Authorization": f'Bearer {self.github_token}'}
load_content = requests.get(get_repo_variable, headers=headers)
time.sleep(1)

if load_content.status_code == 200:
val = literal_eval(load_content.json()['value'])
logger.info(f"AUTOMERGE_ACTION_VAR value is {val}")
return val

return False

def compare_pr_differnce(self, txt):
""" Parse the content and extract packages for comparison. """
regex = re.compile(r"(?P<change>[\-\+])(?P<name>[\w][\w\-\[\]]+)==(?P<version>\d+\.\d+(\.\d+)?(\.[\w]+)?)")
Expand Down
2 changes: 1 addition & 1 deletion jenkins/pull_request_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,4 @@ def main(


if __name__ == '__main__':
main(auto_envvar_prefix="PR_CREATOR") # pylint: disable=no-value-for-parameter, unexpected-keyword-arg
main(auto_envvar_prefix="PR_CREATOR") # pylint: disable=no-value-for-parameter
17 changes: 17 additions & 0 deletions jenkins/tests/test_data/minor_diff.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
diff --git a/requirements/base.txt b/requirements/base.txt
index e94bd48..132b72b 100644
--- a/requirements/base.txt
+++ b/requirements/base.txt
@@ -1,6 +1,6 @@
#
-# This file is autogenerated by pip-compile with python 3.8
-# To update, run:
+# This file is autogenerated by pip-compile with Python 3.8
+# by the following command:
#
# make upgrade
#
@@ -12,14 +12,12 @@ iniconfig==1.1.1
-packaging==21.3
+packaging==21.6

107 changes: 106 additions & 1 deletion jenkins/tests/test_upgrade_python_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def test_branch_deletion(self, create_branch_mock, create_pr_mock,
assert update_files_mock.called
assert create_pr_mock.called

def test_compare_upgrade_difference(self):
def test_compare_upgrade_difference_with_major_changes(self):
basepath = path.dirname(__file__)
filepath = path.abspath(path.join(basepath, "test_data", "diff.txt"))
with open(filepath, "r") as f:
Expand All @@ -209,3 +209,108 @@ def test_compare_upgrade_difference(self):
assert sorted(
['cachetools', 'six', 'tox', 'pyproject-api', 'colorama', 'py', 'chardet', 'pyparsing', 'packaging']
) == [g['name'] for g in suspicious]

def test_compare_upgrade_difference_with_minor_changes(self):
basepath = path.dirname(__file__)
filepath = path.abspath(path.join(basepath, "test_data", "minor_diff.txt"))
with open(filepath, "r") as f:
valid, suspicious = GitHubHelper().compare_pr_differnce(f.read())
assert sorted(
['packaging']
) == [g['name'] for g in valid]

assert sorted(
[]
) == [g['name'] for g in suspicious]

def test_check_automerge_variable_value(self):
with patch('requests.get') as mock_request:
mock_request.return_value.status_code = 200
mock_request.return_value.json.return_value = {
'name': 'ENABLE_AUTOMERGE_FOR_DEPENDENCIES_PRS', 'value': 'True',
'created_at': '2023-03-17T12:58:50Z', 'updated_at': '2023-03-17T13:01:12Z'
}
self.assertTrue(
GitHubHelper().check_automerge_variable_value(
'https://foo/bar/testrepo/pulls/1'
)
)

# in case of false value of variable.
mock_request.return_value.json.return_value = {
'name': 'ENABLE_AUTOMERGE_FOR_DEPENDENCIES_PRS', 'value': 'False',
'created_at': '2023-03-17T12:58:50Z', 'updated_at': '2023-03-17T13:01:12Z'
}
self.assertFalse(
GitHubHelper().check_automerge_variable_value(
'https://foo/bar/testrepo/pulls/1'
)
)
# in case of no variable exists.
mock_request.return_value.status_code = 404
mock_request.return_value.json.return_value = {
'name': 'ENABLE_AUTOMERGE_FOR_DEPENDENCIES_PRS', 'value': 'False',
'created_at': '2023-03-17T12:58:50Z', 'updated_at': '2023-03-17T13:01:12Z'
}
self.assertFalse(
GitHubHelper().check_automerge_variable_value(
'https://foo/bar/testrepo/pulls/1'
)
)

@patch('jenkins.pull_request_creator.PullRequestCreator.github_helper.close_existing_pull_requests',
return_value=[])
@patch('jenkins.pull_request_creator.PullRequestCreator.github_helper.get_github_instance',
return_value=Mock())
@patch('jenkins.pull_request_creator.PullRequestCreator.github_helper.repo_from_remote', return_value=Mock())
@patch('jenkins.pull_request_creator.PullRequestCreator.github_helper.get_updated_files_list',
return_value=["requirements/edx/base.txt", "requirements/edx/coverage.txt"])
@patch('jenkins.pull_request_creator.PullRequestCreator.github_helper.get_current_commit', return_value='1234567')
@patch('jenkins.pull_request_creator.PullRequestCreator.github_helper.branch_exists', return_value=False)
@patch('jenkins.pull_request_creator.PullRequestCreator.github_helper.update_list_of_files', return_value=None)
@patch('jenkins.pull_request_creator.PullRequestCreator.github_helper.create_pull_request')
@patch('jenkins.pull_request_creator.PullRequestCreator.github_helper.create_branch', return_value=None)
@patch('jenkins.pull_request_creator.PullRequestCreator._get_user',
return_value=Mock(name="fake name", login="fake login"))
@patch('jenkins.github_helpers.GitHubHelper.delete_branch', return_value=None)
def test_changes_with_minor_versions_and_variable(
self, delete_branch_mock, get_user_mock, create_branch_mock, create_pr_mock,
update_files_mock, branch_exists_mock, current_commit_mock,
modified_list_mock, repo_mock, authenticate_mock,
close_existing_prs_mock
):
"""
Ensure a merge with no changes to db files will not result in any updates.
"""
pull_request_creator = PullRequestCreator('--repo_root=../../edx-platform', 'upgrade-branch', [],
[], 'Upgrade python requirements', 'Update python requirements',
'make upgrade PR')
pull_request_creator.create(True)

assert branch_exists_mock.called
assert create_branch_mock.called
self.assertEqual(create_branch_mock.call_count, 1)
assert update_files_mock.called
self.assertEqual(update_files_mock.call_count, 1)
assert create_pr_mock.called

create_pr_mock.title = "Python Requirements Update"
create_pr_mock.diff_url = "/"
create_pr_mock.repository.name = 'repo-health-data'

basepath = path.dirname(__file__)

filepath = path.abspath(path.join(basepath, "test_data", "minor_diff.txt"))
with open(filepath, "r") as f:
content = f.read().encode('utf-8')
with patch('requests.get') as mock_request:
mock_request.return_value.content = content
mock_request.return_value.status_code = 200

# in case of `check_automerge_variable_value` false value label will not added.
with patch(
'jenkins.github_helpers.GitHubHelper.check_automerge_variable_value'
) as check_automerge_variable_value:
check_automerge_variable_value.return_value = False
GitHubHelper().verify_upgrade_packages(create_pr_mock)
assert not create_pr_mock.set_labels.called

0 comments on commit ba5ce34

Please sign in to comment.