Exemple #1
0
def add_labels_based_on_size(pr: MergeRequest,
                             size_scheme: str = 'size/{size}'):
    """
    Labels the pull request with size labels according to the amount of
    code touched, commits and files involved. Helps plan the review in
    advance.
    """
    sizes = {'XXL', 'XL', 'L', 'M', 'S', 'XS'}
    with lock_igitt_object('label mr', pr):
        labels = pr.labels.difference(
            {size_scheme.format(size=size)
             for size in sizes})

        lines_added, lines_deleted = pr.diffstat
        commit_score = 4 * len(pr.commits)
        file_score = 4 * len(pr.affected_files)

        if commit_score + file_score + lines_added + lines_deleted <= 100:
            pr.labels = {size_scheme.format(size='XS')}.union(labels)

        elif commit_score + file_score + lines_added + lines_deleted <= 250:
            pr.labels = {size_scheme.format(size='S')}.union(labels)

        elif commit_score + file_score + lines_added + lines_deleted <= 500:
            pr.labels = {size_scheme.format(size='M')}.union(labels)

        elif commit_score + file_score + lines_added + lines_deleted <= 1000:
            pr.labels = {size_scheme.format(size='L')}.union(labels)

        elif commit_score + file_score + lines_added + lines_deleted <= 1500:
            pr.labels = {size_scheme.format(size='XL')}.union(labels)

        else:
            pr.labels = {size_scheme.format(size='XXL')}.union(labels)
Exemple #2
0
def remove_stale_label_from_merge_requests(
        pr: MergeRequest,
        *args,
        stale_label: str = 'Label to be used for marking stale'
):
    """
    Unassigns the chosen label from pull requests when they are updated again.
    """
    if len(args) > 0 and args[0] == stale_label:
        # LABELED and UNLABELED events return the label used, skip action if
        # the label was ``stale_label``
        return

    with lock_igitt_object('label mr', pr):
        pr.labels = pr.labels - {stale_label}
Exemple #3
0
def mark_pending_review_or_wip_accordingly(
        pr: MergeRequest,
        wip_label: str = 'Work in progress',
        pending_review_label: str = 'Review pending'):
    """
    Labels the pull request as pending review and removes work in
    progress on every changed PR accordingly. But retains work in progress
    label, if title of the pull request begins with "wip".
    """
    with lock_igitt_object('label mr', pr):
        labels = pr.labels
        # Allows [wip] and WIP:
        if not 'wip' in pr.title.lower()[:4]:
            labels.add(pending_review_label)
            labels.discard(wip_label)
        else:
            labels.add(wip_label)
            labels.discard(pending_review_label)

        pr.labels = labels
Exemple #4
0
def sync_updated_pr_with_issue(pr: MergeRequest,
                               sync_assignees: bool='Synchronize Assignees'):
    issues = pr.closes_issues
    repo = Repository.from_igitt_repo(pr.repository)
    pr_obj = MergeRequestModel.objects.get_or_create(
        repo=repo, number=pr.number)[0]
    data = defaultdict(dict)

    with lock_igitt_object('label mr', pr):
        labels = pr.labels
        for issue in issues:
            labels = issue.labels | labels
        pr.labels = labels

    if sync_assignees:
        with lock_igitt_object('assign mr', pr):
            assignees = pr.assignees
            for issue in issues:
                assignees |= issue.assignees
                data[str(issue.number)]['assignees'] = True
            pr.assignees = assignees

    pr_obj.closes_issues = data
    pr_obj.save()