Skip to content

Commit

Permalink
Merge pull request #9 from sclorg/fix_merger_real_life
Browse files Browse the repository at this point in the history
Fix changing directory. Let's change it at the end.
  • Loading branch information
phracek authored Dec 18, 2024
2 parents 49ab8a0 + da35ba8 commit b69f9c3
Showing 1 changed file with 51 additions and 43 deletions.
94 changes: 51 additions & 43 deletions auto_merger/merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,30 +173,28 @@ def check_pr_to_merge(self) -> bool:
approval_count = self.check_pr_approvals(pr["reviews"])
if not self.check_pr_lifetime(pr=pr):
continue
self.pr_to_merge[self.container_name] = {
self.pr_to_merge[self.container_name].append({
"number": pr["number"],
"approvals": approval_count,
"pr_dict": {
"title": pr["title"],
}
}
})

def clone_repo(self):
self.temp_dir = utils.temporary_dir()
utils.run_command(
f"gh repo clone https://github.com/sclorg/{self.container_name} {self.temp_dir}/{self.container_name}"
)
self.container_dir = Path(self.temp_dir) / f"{self.container_name}"
if self.container_dir.exists():
os.chdir(self.container_dir)

def merge_pull_requests(self):
for container in UPSTREAM_REPOS:
self.container_name = container
self.container_dir = Path(self.temp_dir) / f"{self.container_name}"
with cwd(self.container_dir) as _:
self.merge_pr()
self.clean_dirs()
os.chdir(self.current_dir)
self.clean_dirs()


def clean_dirs(self):
Expand All @@ -206,34 +204,40 @@ def clean_dirs(self):

def merge_pr(self):
for pr in self.pr_to_merge[self.container_name]:
self.logger.info(f"Let's try to merge {pr['number']}....")
try:
output = utils.run_command(f"gh pr merge {pr['number']}", return_output=True)
self.logger.debug(f"The output from merging command '{output}'")
except subprocess.CalledProcessError as cpe:
self.logger.error(f"Merging pr {pr} failed with reason {cpe.output}")
if int(pr["approvals"]) < self.approvals:
continue

self.logger.info(f"Let's try to merge {pr['number']}....")
# try:
# output = utils.run_command(f"gh pr merge {pr['number']}", return_output=True)
# self.logger.debug(f"The output from merging command '{output}'")
# except subprocess.CalledProcessError as cpe:
# self.logger.error(f"Merging pr {pr} failed with reason {cpe.output}")
# continue

def check_all_containers(self) -> int:
if not self.is_authenticated():
return 1
self.temp_dir = utils.temporary_dir()
for container in UPSTREAM_REPOS:
self.container_name = container
self.repo_data = []
self.clone_repo()
if not self.is_correct_repo():
self.logger.error(f"This is not correct repo {self.container_name}.")
self.clean_dirs()
continue
if self.container_name not in self.pr_to_merge:
self.pr_to_merge[self.container_name] = []
try:
self.get_gh_pr_list()
self.check_pr_to_merge()
except subprocess.CalledProcessError:
self.clean_dirs()
self.logger.error(f"Something went wrong {self.container_name}.")
if not self.container_dir.exists():
continue
with cwd(self.container_dir) as _:
if not self.is_correct_repo():
self.logger.error(f"This is not correct repo {self.container_name}.")
continue
if self.container_name not in self.pr_to_merge:
self.pr_to_merge[self.container_name] = []
try:
self.get_gh_pr_list()
self.check_pr_to_merge()
except subprocess.CalledProcessError:
self.logger.error(f"Something went wrong {self.container_name}.")
continue
os.chdir(self.current_dir)
return 0

def print_pull_request_to_merge(self):
Expand All @@ -242,24 +246,25 @@ def print_pull_request_to_merge(self):
return 0
to_approval: bool = False
pr_body: List = []
for container, pr in self.pr_to_merge.items():
if not pr:
continue
if int(pr["approvals"]) < self.approvals:
continue
to_approval = True
result_pr = f"CAN BE MERGED"
pr_body.append(
f"<tr><td>https://github.com/sclorg/{container}/pull/{pr['number']}</td>"
f"<td>{pr['pr_dict']['title']}</td><td><p style='color:red;'>{result_pr}</p></td></tr>"
)
if to_approval:
self.approval_body.append(f"Pull requests that can be merged.")
self.approval_body.append("<table><tr><th>Pull request URL</th><th>Title</th><th>Approval status</th></tr>")
self.approval_body.extend(pr_body)
self.approval_body.append("</table><br>")
else:
self.approval_body.append("There are not pull requests to be merged.")
is_empty: bool = False
for container, pr_list in self.pr_to_merge.items():
for pr in pr_list:
if not pr:
continue
if int(pr["approvals"]) < self.approvals:
continue
to_approval = True
result_pr = f"CAN BE MERGED"
pr_body.append(
f"<tr><td>https://github.com/sclorg/{container}/pull/{pr['number']}</td>"
f"<td>{pr['pr_dict']['title']}</td><td><p style='color:red;'>{result_pr}</p></td></tr>"
)
if to_approval:
self.approval_body.append(f"Pull requests that can be merged.")
self.approval_body.append("<table><tr><th>Pull request URL</th><th>Title</th><th>Approval status</th></tr>")
self.approval_body.extend(pr_body)
self.approval_body.append("</table><br>")
is_empty = True
print('\n'.join(self.approval_body))

def send_results(self, recipients):
Expand All @@ -268,4 +273,7 @@ def send_results(self, recipients):
return 1
sender_class = EmailSender(recipient_email=list(recipients))
subject_msg = "Pull request statuses for organization https://gibhub.com/sclorg"
sender_class.send_email(subject_msg, self.approval_body)
if self.approval_body:
sender_class.send_email(subject_msg, self.approval_body)
else:
self.logger.info("Nothing to send.")

0 comments on commit b69f9c3

Please sign in to comment.