feat: enhance service logic

This commit is contained in:
HugoPBrito committed 2024-12-03 12:02:02 +01:00
1 parent 817ab62fe7
commit c7ba87faba
2 files changed
+39 -25

No files matched your search

@@ -10,7 +10,7 @@
"ResourceType": "Other",
"Description": "Ensure that repositories require at least 2 code changes approvals before merging a pull request.",
"Risk": "If repositories do not require at least 2 code changes approvals before merging a pull request, it is possible that code changes are not being reviewed by multiple people, which could lead to the introduction of bugs or security vulnerabilities.",
"RelatedUrl": "https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/about-protected-branches#require-pull-request-reviews-before-merging",
"RelatedUrl": "https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/approving-a-pull-request-with-required-reviews",
"Remediation": {
"Code": {
"CLI": "",
@@ -20,7 +20,7 @@
},
"Recommendation": {
"Text": "To require at least 2 code changes approvals before merging a pull request, navigate to the repository settings, click on 'Branches', and then 'Add rule'.",
"Url": "https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/approving-a-pull-request-with-required-reviews"
"Url": "https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/about-protected-branches#require-pull-request-reviews-before-merging"
}
},
"Categories": [],
@@ -17,31 +17,45 @@ class Repository(GithubService):
try:
for client in self.clients:
for repo in client.get_user().get_repos():
default_branch = repo.get_branch(repo.default_branch)
try:
securitymd_exists = repo.get_contents("SECURITY.md") is not None
except Exception:
securitymd_exists = False
if not repo.private:
default_branch = repo.get_branch(repo.default_branch).name
try:
securitymd_exists = (
repo.get_contents("SECURITY.md") is not None
)
except Exception:
securitymd_exists = False
branch = repo.get_branch(default_branch)
protection = branch.get_protection()
require_pr = protection.required_pull_request_reviews is not None
approval_cnt = (
protection.required_pull_request_reviews.required_approving_review_count
if require_pr
else 0
)
require_pr = False
try:
branch = repo.get_branch(default_branch)
protection = branch.get_protection()
require_pr = protection.required_pull_request_reviews
approval_cnt = (
protection.required_pull_request_reviews.required_approving_review_count
if require_pr
else 0
)
print(
f"Protección de rama encontrada para {branch.name}: {approval_cnt} aprobaciones requeridas."
)
except Exception as e:
logger.error(
f"Error getting branch protection for {repo.name}: {e}"
)
require_pr = False
approval_cnt = 0
repos[repo.id] = Repo(
id=repo.id,
name=repo.name,
full_name=repo.full_name,
default_branch=repo.default_branch,
private=repo.private,
securitymd=securitymd_exists,
require_pull_request=require_pr,
approval_count=approval_cnt,
)
repos[repo.id] = Repo(
id=repo.id,
name=repo.name,
full_name=repo.full_name,
default_branch=repo.default_branch,
private=repo.private,
securitymd=securitymd_exists,
require_pull_request=require_pr,
approval_count=approval_cnt,
)
except Exception as error:
logger.error(
f"{error.__class__.__name__}[{error.__traceback__.tb_lineno}]: {error}"