Пример #1
0
def build_release_notes(repository: Repository.Repository,
                        tag: GitRef.GitRef) -> str:
    signature = (
        "\n---\nReleased with [mozilla/task-boot](https://github.com/mozilla/task-boot)"
    )

    # Get all commits between both versions using the comparison endpoint
    try:
        latest_release = repository.get_latest_release()
        diff = repository.compare(latest_release.tag_name, tag.ref)
        commits = diff.commits
    except UnknownObjectException:
        logger.info(
            "No previous release available, will use all commits on repo")
        commits = [commit for commit in repository.get_commits()]

    # List existing tags sha
    tags = [tag.commit.sha for tag in repository.get_tags()]

    # Use first line of every commit in between versions
    lines = [
        "- {}".format(commit.commit.message.splitlines()[0])
        for commit in commits if not is_release_commit(commit, tags)
    ]

    return "\n".join(lines) + signature
Пример #2
0
def get_repo_commits(repo: Repository.Repository) -> List[Dict[str, Any]]:
    """
    Fetches and formats all commits for a specific Github repository.
    Runs in multiple threads, which requires specific retrying logic.

    :param repo: Github Repository object
    :return: list of dicts containing commits information
    """

    results = []

    try:

        commits = repo.get_commits()
        for c in commits:
            r = {
                "repo": repo.name,
                "author": c.commit.author.name,
                "date": c.commit.author.date,
            }
            results.append(r)

    except GithubException as e:

        if e.args[0] == 409 \
                and e.args[1]["message"] == "Git Repository is empty.":
            logger.warning(f"Repository {repo.name} is empty, "
                           f"returning empty list")
            return []

        else:
            raise e

    return results
Пример #3
0
 def _format_repo(self, repository: Repository):
     commits = repository.get_commits()
     return {
         "url": repository.html_url,
         "name": repository.name,
         "owner": {
             "login": repository.owner.login,
             "url": repository.owner.html_url,
             "avatar_url": repository.owner.avatar_url,
         },
         "latest_commit":
         self._format_commit(commits[0]) if commits else {},
     }
Пример #4
0
 def _format_repo(repository: Repository):
     commits = repository.get_commits()
     return {
         'url': repository.html_url,
         'name': repository.name,
         'owner': {
             'login': repository.owner.login,
             'url': repository.owner.html_url,
             'avatar_url': repository.owner.avatar_url,
         },
         'latest_commit':
         SearchService._format_commit(commits[0]) if commits else {},
     }
Пример #5
0
def get_latest_pr_from_readme(rest_repo: Repository, service_html: str):
    commit_url = service_html.split('main/')[-1]
    commits = rest_repo.get_commits(path=commit_url)
    latest_commit = None
    for commit in commits:
        latest_commit = commit
        break
    latest_pr_brief = latest_commit.commit.message
    latest_pr_number = re.findall('\(\#[0-9]+\)', latest_pr_brief)
    latest_pr_number_int = []
    for number in latest_pr_number:
        number = int(re.search('\d+', number).group())
        latest_pr_number_int.append(number)
    latest_pr_number_int.sort()

    return latest_pr_number_int[-1]
Пример #6
0
 def _create_finding_from_repo(self, repo: Repository) -> FINDING:
     latest_commit: str = None
     if self._annotate_latest_commit_id:
         try:
             latest_commit = repo.get_commits()[0].sha
         except Exception:
             self.LOGGER.warn(
                 "Could not identify the latest commit ID - repository without commits?"
             )
             latest_commit = ""
     return super()._create_finding(
         str(repo.id), repo.html_url, repo.full_name, repo.owner.type,
         str(repo.owner.id), repo.owner.name,
         repo.created_at.strftime("%Y-%m-%dT%H:%M:%SZ"),
         repo.updated_at.strftime("%Y-%m-%dT%H:%M:%SZ"),
         'private' if repo.private else 'public', repo.archived,
         latest_commit)
Пример #7
0
def query_contributors(repo: Repository, filepath: str) -> set[str]:
    """Query github for all contributors of a source

    Parameters
    ----------
    repo : Repository
        The repository to query
    filepath : str
        Path in ELI that should be queried

    Returns
    -------
    set[str]
        Contributors of source
    """
    contributors = set()
    commits = repo.get_commits(path=filepath)
    for commit in commits:
        if "github-actions" not in commit.author.login:
            contributors.add(commit.author.login)
    return contributors
Пример #8
0
def protect_pr_branch_with_tests_if_any_exist(
        org: Organization, repo: Repository,
        branches: Dict[str, Branch]) -> List[Change[str]]:
    def execute_test_protection(change: Change[str], branch: Branch,
                                existing_checks: Set[str],
                                known_checks: Set[str]) -> Change[str]:
        print_debug("[%s] Changing status checks on branch '%s' to [%s]" %
                    (highlight(repo.name), highlight(branch.name),
                     highlight(", ".join(list(known_checks)))))
        try:
            if existing_checks:
                branch.edit_required_status_checks(strict=True,
                                                   contexts=list(known_checks))
            else:
                safe_branch_edit_protection(
                    branch,
                    strict=True,
                    contexts=list(known_checks),
                )
        except GithubException as e:
            print_error(
                "Can't edit required status checks on repo %s branch %s: %s" %
                (repo.name, branch.name, str(e)))
            return change.failure()
        return change.success()

    prb = get_pr_branch(repo, branches)
    if not prb:
        return []

    existing_checks = set()  # type: Set[str]
    try:
        rqs = prb.get_required_status_checks()
    except GithubException:
        # the repository has currently no status checks
        pass
    else:
        if len(rqs.contexts) > 0:
            # The repository already has some status checks, in that case we do nothing
            existing_checks = set(rqs.contexts)
            print_debug("Branch %s on repo %s already has status checks [%s]" %
                        (highlight(prb.name), highlight(
                            repo.name), highlight(", ".join(existing_checks))))

    # the repository currently has no status checks, let's see if any came in within the last 7 days
    sevendaysago = datetime.now() - timedelta(days=7)
    known_checks = set()  # type: Set[str]
    for commit in repo.get_commits(prb.name, since=sevendaysago):
        for status in commit.get_statuses():  # type: CommitStatus
            known_checks.add(status.context)

    if known_checks and known_checks != existing_checks:
        # add all known checks as required checks
        print_debug('Adding checks [%s] to branch %s on repo %s' %
                    (highlight(", ".join(known_checks)), highlight(
                        prb.name), highlight(repo.name)))
        return [
            Change(
                meta=ChangeMetadata(
                    executor=execute_test_protection,
                    params=[prb, existing_checks, known_checks]),
                action=ChangeActions.REPLACE
                if existing_checks else ChangeActions.ADD,
                before="%s checks" %
                len(existing_checks) if existing_checks else "No checks",
                after="%s checks" % len(known_checks),
            )
        ]
    return []