def get_linked_task_ids(pull_request: PullRequest) -> List[str]: """ Extracts linked task ids from the body of the PR. We expect linked tasks to be in the description in a line under the line containing "Asana tasks:". :return: Returns a list of task ids. """ body_lines = pull_request.body().splitlines() stripped_body_lines = (line.strip() for line in body_lines) task_url_line = None seen_asana_tasks_line = False for line in stripped_body_lines: if seen_asana_tasks_line: task_url_line = line break if line.startswith("Asana tasks:"): seen_asana_tasks_line = True if task_url_line: task_urls = task_url_line.split() task_ids = [] for url in task_urls: maybe_id = re.search("\d+(?!.*\d)", url) if maybe_id is not None: task_ids.append(maybe_id.group()) return task_ids else: return []
def _add_asana_task_to_pull_request(pull_request: PullRequest, task_id: str): owner = pull_request.repository_owner_handle() task_url = asana_helpers.task_url_from_task_id(task_id) new_body = github_logic.inject_asana_task_into_pull_request_body( pull_request.body(), task_url) github_client.edit_pr_description(owner, pull_request.repository_name(), pull_request.number(), new_body) # Update the PullRequest object to represent the new body, so we don't have # to query it again pull_request.set_body(new_body)
def maybe_automerge_pull_request(pull_request: PullRequest) -> bool: if _is_pull_request_ready_for_automerge(pull_request): logger.info( f"Pull request {pull_request.id()} is able to be automerged, automerging now" ) github_client.merge_pull_request( pull_request.repository_owner_handle(), pull_request.repository_name(), pull_request.number(), pull_request.title(), pull_request.body(), ) return True else: return False
def _task_description_from_pull_request(pull_request: PullRequest) -> str: link_to_pr = _link(pull_request.url()) github_author = pull_request.author() author = _asana_user_url_from_github_user_handle(github_author.login()) if author is None: author = _asana_display_name_for_github_user(github_author) status_reason = _task_completion_from_pull_request(pull_request) status = "complete" if status_reason.is_complete else "incomplete" return _wrap_in_tag( "body" )(_wrap_in_tag("em") ("This is a one-way sync from GitHub to Asana. Do not edit this task or comment on it!" ) + f"\n\n\uD83D\uDD17 {link_to_pr}" + "\n✍️ " + author + _generate_assignee_description(pull_request.assignee()) + f"\n❗️Task is {status} because {status_reason.reason}" + _wrap_in_tag("strong")("\n\nDescription:\n") + _format_github_text_for_asana(pull_request.body()))
def upsert_pull_request(pull_request: PullRequest): pull_request_id = pull_request.id() task_id = dynamodb_client.get_asana_id_from_github_node_id(pull_request_id) if task_id is None: task_id = asana_controller.create_task(pull_request.repository_id()) if task_id is None: # TODO: Handle this case return logger.info( f"Task created for pull request {pull_request_id}: {task_id}") dynamodb_client.insert_github_node_to_asana_id_mapping( pull_request_id, task_id) asana_helpers.create_attachments(pull_request.body(), task_id) _add_asana_task_to_pull_request(pull_request, task_id) else: logger.info( f"Task found for pull request {pull_request_id}, updating task {task_id}" ) asana_controller.update_task(pull_request, task_id)
def _pull_request_body_mentions(pull_request: PullRequest) -> List[str]: return _extract_mentions(pull_request.body())