def test_get_pull_requests_with_base_branch(self, mock_util, repo): self.init_github() mock_util.mock_pulls(base="master", head="TestOwner:some-branch") pull_requests = get_pull_requests_with_base_branch( repo, "master", head="some-branch" ) assert 0 == len(pull_requests) responses.reset() mock_util.mock_pulls(pulls=self._get_expected_pull_requests(3), base="master") pull_requests = get_pull_requests_with_base_branch(repo, "master") assert 3 == len(pull_requests)
def aggregate_child_change_notes(self, pull_request): """Given a pull request, aggregate all change notes from child pull requests. Child pull requests are pull requests that have a base branch equal to the the given pull request's head.""" self.change_notes = get_pull_requests_with_base_branch( self.repo, pull_request.head.ref, state="all") self.change_notes = [ note for note in self.change_notes if is_pull_request_merged(note) and note.head.ref != self.repo.default_branch ] if len(self.change_notes) == 0: return for change_note in self.change_notes: self._parse_change_note(change_note) body = [] for parser in self.parsers: if parser.title is None: parser.title = "Notes From Child PRs" parser_content = parser.render() if parser_content: body.append(parser_content) if self.empty_change_notes: body.extend(render_empty_pr_section(self.empty_change_notes)) new_body = "\r\n".join(body) if not pull_request.update(body=new_body): raise CumulusCIException( "Update of pull request #{} failed.".format( pull_request.number))
def update_unaggregated_pr_header(self, pull_request_to_update, branch_name_to_add): """Updates the 'Unaggregated Pull Requests' section header with a link to the new child branch pull request""" body = pull_request_to_update.body if self.UNAGGREGATED_SECTION_HEADER not in body: body += self.UNAGGREGATED_SECTION_HEADER pull_requests = get_pull_requests_with_base_branch( self.repo, branch_name_to_add.split("__")[0], branch_name_to_add) if len(pull_requests) == 0: raise CumulusCIException( "No pull request for branch {} found.".format( branch_name_to_add)) elif len(pull_requests) > 1: raise CumulusCIException( "Expected one pull request, found {} for branch {}".format( len(pull_requests), branch_name_to_add)) pull_request_link = markdown_link_to_pr(pull_requests[0]) if pull_request_link not in body: body += "\r\n* " + pull_request_link pull_request_to_update.update(body=body) return
def _get_parent_pull_request(self): """Attempts to retrieve a pull request for the given branch.""" requests = get_pull_requests_with_base_branch( self.repo, self.repo.default_branch, self.branch_name ) if len(requests) > 0: return requests[0] else: self.logger.info(f"Pull request not found for branch {self.branch_name}.")
def _get_parent_pull_request(self, branch_name): """Attempts to retrieve a pull request for the given branch. If one is not found, then it is created and the 'Build Change Notes' label is applied to it.""" requests = get_pull_requests_with_base_branch(self.repo, self.repo.default_branch, branch_name) if len(requests) == 0: self.logger.info( "Pull request not found. Creating pull request for branch: {} with base of 'master'." .format(branch_name)) parent_pull_request = create_pull_request(self.repo, branch_name) add_labels_to_pull_request(self.repo, parent_pull_request, self.build_notes_label) else: parent_pull_request = requests[0] return parent_pull_request
def _update_unaggregated_pr_header(self, pull_request_to_update, branch_name_to_add): """Updates the 'Unaggregated Pull Requests' section header with a link to the new child branch pull request""" self._add_header(pull_request_to_update) pull_requests = get_pull_requests_with_base_branch( self.repo, branch_name_to_add.split("__")[0], branch_name_to_add, state="all", ) if len(pull_requests) == 0: self.logger.info( f"No pull request for branch {branch_name_to_add} found.") elif len(pull_requests) > 1: self.logger.error( f"Expected one pull request, found {len(pull_requests)} for branch {branch_name_to_add}" ) else: self._add_link_to_pr(pull_request_to_update, pull_requests[0])
def _handle_parent_branch_name_option(self, generator, parent_branch_name): pull_requests = get_pull_requests_with_base_branch( self.repo, self.repo.default_branch, parent_branch_name) if len(pull_requests) == 0: self.logger.info( "No pull request found for branch: {}. Exiting...".format( parent_branch_name)) return elif len(pull_requests) > 1: self.logger.info( "More than one pull request returned with base='master' for branch {}" .format(parent_branch_name)) return else: # We can ONLY aggregate child change notes when given the parent_branch option # # We aren't able to append to the 'Unaggregated Pull Requests' header. # We don't know at what time the label was applied to the pull request; # and therefore, we cannot determine which child pull requests are already # aggregated into the parent pull request, and which ones should be # included in the 'Unaggregated Pull Requests' section. generator.aggregate_child_change_notes(pull_requests[0])