Skip to content

Commit 2cae150

Browse files
committed
improve readbility of code
1 parent 61fb944 commit 2cae150

File tree

1 file changed

+86
-46
lines changed

1 file changed

+86
-46
lines changed

scripts/automated_ingestion/eessi_task.py

Lines changed: 86 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ def _find_pr_for_branch(self, branch_name: str) -> Optional[PullRequest]:
832832
"""
833833
try:
834834
head_ref = f"{self.git_repo.owner.login}:{branch_name}"
835-
filter_prs = [16, 17, 18, 19, 20, 21] # TODO: remove this once the PR is merged
835+
filter_prs = [16, 17, 18, 19, 20, 21, 22] # TODO: remove this once the PR is merged
836836
prs = [pr for pr in list(self.git_repo.get_pulls(state='all', head=head_ref))
837837
if pr.number not in filter_prs]
838838
return prs[0] if prs else None
@@ -891,6 +891,85 @@ def _sync_task_state_file(self, source_branch: str, target_branch: str):
891891
log_message(LoggingScope.TASK_OPS, 'ERROR', "Error syncing task state file: %s", err)
892892
return None
893893

894+
@log_function_entry_exit()
895+
def _update_task_states(self, next_state: TaskState, default_branch_name: str,
896+
approved_state: TaskState, feature_branch_name: str):
897+
"""
898+
Update task states in default and feature branches
899+
900+
States have to be updated in a specific order and in particular the default branch has to be
901+
merged into the feature branch before the feature branch can be updated to avoid a merge conflict.
902+
903+
Args:
904+
next_state: next state to be applied to the default branch
905+
default_branch_name: name of the default branch
906+
approved_state: state to be applied to the feature branch
907+
feature_branch_name: name of the feature branch
908+
"""
909+
# TODO: add failure handling (capture failures and return them somehow)
910+
911+
# update TaskState file content
912+
# - next_state in default branch (interpreted as current state)
913+
# - approved_state in feature branch (interpreted as future state, ie, after
914+
# the PR corresponding to the feature branch will be merged)
915+
916+
# first, update the task state file in the default branch
917+
self._update_task_state_file(next_state, branch_name=default_branch_name)
918+
919+
# second, merge default branch into feature branch (to avoid a merge conflict)
920+
arch = self.description.task_object.arch
921+
commit_message = f"merge {default_branch_name} into {feature_branch_name} for {arch}"
922+
self.git_repo.merge(
923+
head=default_branch_name,
924+
base=feature_branch_name,
925+
commit_message=commit_message
926+
)
927+
928+
# last, update task state file in feature branch
929+
self._update_task_state_file(approved_state, branch_name=feature_branch_name)
930+
log_message(LoggingScope.TASK_OPS, 'INFO',
931+
"TaskState file updated to %s in default branch (%s) and to %s in feature branch (%s)",
932+
next_state, default_branch_name, approved_state, feature_branch_name)
933+
934+
@log_function_entry_exit()
935+
def _create_pull_request(self, feature_branch_name: str, default_branch_name: str):
936+
"""
937+
Create a PR from the feature branch to the default branch
938+
939+
Args:
940+
feature_branch_name: name of the feature branch
941+
default_branch_name: name of the default branch
942+
"""
943+
pr_title_format = self.config['github']['grouped_pr_title']
944+
pr_body_format = self.config['github']['grouped_pr_body']
945+
repo_name = self.description.get_repo_name()
946+
pr_number = self.description.get_pr_number()
947+
pr_url = f"https://github.com/{repo_name}/pull/{pr_number}"
948+
seq_num = self._determine_sequence_number()
949+
pr_title = pr_title_format.format(
950+
cvmfs_repo=self.cvmfs_repo,
951+
pr=pr_number,
952+
repo=repo_name,
953+
seq_num=seq_num,
954+
)
955+
pr_body = pr_body_format.format(
956+
cvmfs_repo=self.cvmfs_repo,
957+
pr=pr_number,
958+
pr_url=pr_url,
959+
repo=repo_name,
960+
seq_num=seq_num,
961+
contents="TO BE DONE",
962+
analysis="TO BE DONE",
963+
action="TO BE DONE",
964+
)
965+
pr = self.git_repo.create_pull(
966+
title=pr_title,
967+
body=pr_body,
968+
head=feature_branch_name,
969+
base=default_branch_name
970+
)
971+
log_message(LoggingScope.TASK_OPS, 'INFO', "PR created: %s", pr)
972+
894973
@log_function_entry_exit()
895974
def _handle_add_payload_staged(self):
896975
"""Handler for ADD action in PAYLOAD_STAGED state"""
@@ -922,52 +1001,13 @@ def _handle_add_payload_staged(self):
9221001
if not pull_request:
9231002
log_message(LoggingScope.TASK_OPS, 'INFO',
9241003
"no PR found for branch %s", feature_branch_name)
925-
# update TaskState file content
926-
# - next state in default branch (interpreted as current state)
927-
# - approved state in feature branch (interpreted as future state, ie, after the PR is merged)
928-
self._update_task_state_file(next_state, branch_name=default_branch_name)
929-
# merge default branch into feature branch (attempt to avoid merge conflicts)
930-
self.git_repo.merge(
931-
head=default_branch_name,
932-
base=feature_branch_name,
933-
commit_message=f"Merge {default_branch_name} into {feature_branch_name}"
934-
)
935-
# update task state file in feature branch
936-
self._update_task_state_file(approved_state, branch_name=feature_branch_name)
937-
log_message(LoggingScope.TASK_OPS, 'INFO',
938-
"TaskState file updated to %s in default branch (%s) and to %s in feature branch (%s)",
939-
next_state, default_branch_name, approved_state, feature_branch_name)
9401004

941-
# create PR
942-
pr_title_format = self.config['github']['grouped_pr_title']
943-
pr_body_format = self.config['github']['grouped_pr_body']
944-
repo_name = self.description.get_repo_name()
945-
pr_number = self.description.get_pr_number()
946-
pr_url = f"https://github.com/{repo_name}/pull/{pr_number}"
947-
seq_num = self._determine_sequence_number()
948-
pr_title = pr_title_format.format(
949-
cvmfs_repo=self.cvmfs_repo,
950-
pr=pr_number,
951-
repo=repo_name,
952-
seq_num=seq_num,
953-
)
954-
pr_body = pr_body_format.format(
955-
cvmfs_repo=self.cvmfs_repo,
956-
pr=pr_number,
957-
pr_url=pr_url,
958-
repo=repo_name,
959-
seq_num=seq_num,
960-
contents="TO BE DONE",
961-
analysis="TO BE DONE",
962-
action="TO BE DONE",
963-
)
964-
pr = self.git_repo.create_pull(
965-
title=pr_title,
966-
body=pr_body,
967-
head=feature_branch_name,
968-
base=default_branch_name
969-
)
970-
log_message(LoggingScope.TASK_OPS, 'INFO', "PR created: %s", pr)
1005+
# TODO: add failure handling (capture result and act on it)
1006+
self._update_task_states(next_state, default_branch_name, approved_state, feature_branch_name)
1007+
1008+
# TODO: add failure handling (capture result and act on it)
1009+
self._create_pull_request(feature_branch_name, default_branch_name)
1010+
9711011
return TaskState.PULL_REQUEST
9721012
else:
9731013
log_message(LoggingScope.TASK_OPS, 'INFO',

0 commit comments

Comments
 (0)