Example #1
0
def parse_githubdata(data, current_user):
    """
    data { 'repo': genymotion-libauth,
           detail: paginable,
           label: paginable,
           comment: paginable,
           json: json,
           review_comments: paginable, (optional)
           events: paginable, (optional)
           commits: paginable, (optional)
         }
    return Pr
    """

    now = timezone.now()
    feedback_ok = 0
    feedback_weak = 0
    feedback_ko = 0
    milestone = data['json']['milestone']
    labels = list()
    last_event = None
    last_commit = None

    if 'events' in data: last_event = practivity.get_latest_event(data['events'])
    if 'commits' in data: last_commit = practivity.get_latest_commit(data['commits'])
    last_activity = practivity.get_latest_activity(last_event, last_commit)

    for lbl in data['label']:
        label_style = 'light' if is_color_light(lbl['color']) else 'dark'
        labels.append({'name' : lbl['name'],
                       'color' : lbl['color'],
                       'style' : label_style,
        })

    date = dateparse.parse_datetime(data['json']['updated_at'])
    is_old = False
    if (now - date).days >= settings.OLD_PERIOD:
        if not labels and None in settings.OLD_LABELS:
            is_old = True
        else:
            for lbl in labels:
                if lbl['name'] in settings.OLD_LABELS:
                    is_old = True
                    break

    if current_user is not None:
        my_open_comment_count = get_open_comment_count(data['review_comments'], current_user)
    else:
        my_open_comment_count = 0

    # look for tags and activity only in main conversation and not in "file changed"
    for jcomment in data['comment']:
        body = jcomment['body']
        if "comments" in settings.LAST_ACTIVITY_FILTER:
            comment_activity = practivity.PrActivity(jcomment['updated_at'],
                                                     jcomment['user']['login'],
                                                     "commented")
            last_activity = practivity.get_latest_activity(last_activity, comment_activity)
        if re.search(settings.FEEDBACK_OK['keyword'], body):
            feedback_ok += 1
        if re.search(settings.FEEDBACK_WEAK['keyword'], body):
            feedback_weak += 1
        if re.search(settings.FEEDBACK_KO['keyword'], body):
            feedback_ko += 1
    if milestone:
        milestone = milestone['title']

    try:
        pr = PullRequest(url=data['json']['html_url'],
                         title=data['json']['title'],
                         updated_at=date,
                         user=data['json']['user']['login'],
                         my_open_comment_count=my_open_comment_count,
                         last_activity=last_activity,
                         repo=data['json']['base']['repo']['name'],
                         nbreview=int(data['detail']['comments']) +
                                  int(data['detail']['review_comments']),
                         feedback_ok=feedback_ok,
                         feedback_weak=feedback_weak,
                         feedback_ko=feedback_ko,
                         milestone=milestone,
                         labels=labels,
                         is_old=is_old)
    except Exception as e:
        logger.error("cannot create PullRequest: %s", e)

    return pr
Example #2
0
    def parsepr(self, current_user):
        """ Parse the data and return a _pr.pullrequest.PullRequest
        """
        assert self.__info is not None, "need to call addfragment on id %s" % self.__prid
        assert self.__details is not None, "need to call addfragment on id %s" % self.__prid
        assert self.__labels is not None, "need to call addfragment on id %s" % self.__prid
        assert self.__reviews is not None, "need to call addfragment on id %s" % self.__prid

        now = timezone.now()
        feedback_ok = 0
        feedback_ko = 0
        milestone = self.__info['milestone']
        labels = list()
        last_event = None
        last_commit = None
        targetbranch = None

        if self.__events:
            last_event = practivity.get_latest_event(self.__events)
        if self.__commits:
            last_commit = practivity.get_latest_commit(self.__commits)
        last_activity = practivity.get_latest_activity(last_event, last_commit)

        for lbl in self.__labels:
            label_style = 'light' if is_color_light(lbl['color']) else 'dark'
            labels.append({'name' : lbl['name'],
                           'color' : lbl['color'],
                           'style' : label_style,
            })

        date = dateparse.parse_datetime(self.__info['updated_at'])
        is_old = False
        if (now - date).days >= settings.OLD_PERIOD:
            if not labels and None in settings.OLD_LABELS:
                is_old = True
            else:
                for lbl in labels:
                    if lbl['name'] in settings.OLD_LABELS:
                        is_old = True
                        break

        if current_user is not None:
            my_open_comment_count = get_open_comment_count(self.__review_comments,
                                                           current_user)
        else:
            my_open_comment_count = 0

        # look for tags and activity only in main conversation and not in "file changed"
        if "comments" in settings.LAST_ACTIVITY_FILTER:
            for comment in self.__comments:
                comment_activity = practivity.PrActivity(comment['updated_at'],
                                                         comment['user']['login'],
                                                         "commented")
                last_activity = practivity.get_latest_activity(last_activity, comment_activity)

        review_by_user = {}
        for review in self.__reviews:
            login = review['user']['login']
            # only last review matter. Assume reviews are ordered and take only the last review
            # for each user, ignoring COMMENTED
            if review['state'] == "CHANGES_REQUESTED" or review['state'] == "APPROVED":
                review_by_user[login] = review['state']

            if "reviews" in settings.LAST_ACTIVITY_FILTER:
                review_activity = practivity.PrActivity(review['submitted_at'],
                                                        login,
                                                        "reviewed")
                last_activity = practivity.get_latest_activity(last_activity, review_activity)

        feedback_ok = sum(1 for k, v in review_by_user.items() if v == "APPROVED")
        feedback_ko = sum(1 for k, v in review_by_user.items() if v == "CHANGES_REQUESTED")

        if milestone:
            milestone = milestone['title']

        pr = PullRequest(url=self.__info['html_url'],
                         title=self.__info['title'],
                         updated_at=date,
                         user=self.__info['user']['login'],
                         my_open_comment_count=my_open_comment_count,
                         last_activity=last_activity,
                         repo=self.__info['base']['repo']['name'],
                         nbreview=int(self.__details['comments']) +
                                  int(self.__details['review_comments']),
                         feedback_ok=feedback_ok,
                         feedback_ko=feedback_ko,
                         milestone=milestone,
                         labels=labels,
                         is_old=is_old,
                         targetbranch=self.__info['base']['ref'],
        )

        return pr
Example #3
0
def fetch_data(repo_name, url, org, current_user):
    """ Celery task, call github api
    repo_name -- github repo name
    url -- base url for this repo
    org -- github organisation

    return a dict {'name' : repo_name, 'pr_list' : pr_list} with pr_list a list
    of models.Pr
    """
    pr_list = []
    repo = {"name": repo_name, "pr_list": pr_list}
    url = "%s/repos/%s/%s/pulls" % (url, org, repo_name)
    json_prlist = paginablejson.PaginableJson(url)
    now = timezone.now()
    if not json_prlist:
        return
    for json_pr in json_prlist:
        if json_pr["state"] == "open":
            conversation_json = paginablejson.PaginableJson(json_pr["comments_url"])
            issue_url = json_pr["issue_url"]
            last_event = None
            last_commit = None
            if "events" in settings.LAST_ACTIVITY_FILTER:
                last_event = practivity.get_latest_event(issue_url)
            if "commits" in settings.LAST_ACTIVITY_FILTER:
                last_commit = practivity.get_latest_commit("%s/%s" % (url, json_pr["number"]))
            last_activity = practivity.get_latest_activity(last_event, last_commit)

            detail_json = paginablejson.PaginableJson(json_pr["url"])
            feedback_ok = 0
            feedback_weak = 0
            feedback_ko = 0
            milestone = json_pr["milestone"]
            label_json = paginablejson.PaginableJson("%s/labels" % issue_url)
            labels = list()
            if label_json:
                for lbl in label_json:
                    label_style = "light" if is_color_light(lbl["color"]) else "dark"
                    labels.append({"name": lbl["name"], "color": lbl["color"], "style": label_style})

            date = dateparse.parse_datetime(json_pr["updated_at"])
            is_old = False
            if (now - date).days >= settings.OLD_PERIOD:
                if not labels and None in settings.OLD_LABELS:
                    is_old = True
                else:
                    for lbl in labels:
                        if lbl["name"] in settings.OLD_LABELS:
                            is_old = True
                            break

            if current_user is not None:
                my_open_comment_count = get_open_comment_count(json_pr["review_comments_url"], current_user)
            else:
                my_open_comment_count = 0
            # look for tags and activity only in main conversation and not in "file changed"
            for jcomment in conversation_json:
                body = jcomment["body"]
                if "comments" in settings.LAST_ACTIVITY_FILTER:
                    comment_activity = practivity.PrActivity(
                        jcomment["updated_at"], jcomment["user"]["login"], "commented"
                    )
                    last_activity = practivity.get_latest_activity(last_activity, comment_activity)
                if re.search(settings.FEEDBACK_OK["keyword"], body):
                    feedback_ok += 1
                if re.search(settings.FEEDBACK_WEAK["keyword"], body):
                    feedback_weak += 1
                if re.search(settings.FEEDBACK_KO["keyword"], body):
                    feedback_ko += 1
            if milestone:
                milestone = milestone["title"]

            pr = models.Pr(
                url=json_pr["html_url"],
                title=json_pr["title"],
                updated_at=date,
                user=json_pr["user"]["login"],
                my_open_comment_count=my_open_comment_count,
                last_activity=last_activity,
                repo=json_pr["base"]["repo"]["full_name"],
                nbreview=int(detail_json["comments"]) + int(detail_json["review_comments"]),
                feedback_ok=feedback_ok,
                feedback_weak=feedback_weak,
                feedback_ko=feedback_ko,
                milestone=milestone,
                labels=labels,
                is_old=is_old,
            )
            pr_list.append(pr)

    repo["pr_list"] = sorted(pr_list, key=attrgetter("updated_at"), reverse=True)

    if not pr_list:
        return None

    return repo
Example #4
0
def parse_githubdata(data, current_user):
    """
    data { 'repo': genymotion-libauth,
           detail: paginable,
           label: paginable,
           comment: paginable,
           json: json,
           review_comments: paginable, (optional)
           events: paginable, (optional)
           commits: paginable, (optional)
         }
    return Pr
    """

    now = timezone.now()
    feedback_ok = 0
    feedback_weak = 0
    feedback_ko = 0
    milestone = data['json']['milestone']
    labels = list()
    last_event = None
    last_commit = None

    if 'events' in data:
        last_event = practivity.get_latest_event(data['events'])
    if 'commits' in data:
        last_commit = practivity.get_latest_commit(data['commits'])
    last_activity = practivity.get_latest_activity(last_event, last_commit)

    for lbl in data['label']:
        label_style = 'light' if is_color_light(lbl['color']) else 'dark'
        labels.append({
            'name': lbl['name'],
            'color': lbl['color'],
            'style': label_style,
        })

    date = dateparse.parse_datetime(data['json']['updated_at'])
    is_old = False
    if (now - date).days >= settings.OLD_PERIOD:
        if not labels and None in settings.OLD_LABELS:
            is_old = True
        else:
            for lbl in labels:
                if lbl['name'] in settings.OLD_LABELS:
                    is_old = True
                    break

    if current_user is not None:
        my_open_comment_count = get_open_comment_count(data['review_comments'],
                                                       current_user)
    else:
        my_open_comment_count = 0

    # look for tags and activity only in main conversation and not in "file changed"
    for jcomment in data['comment']:
        body = jcomment['body']
        if "comments" in settings.LAST_ACTIVITY_FILTER:
            comment_activity = practivity.PrActivity(jcomment['updated_at'],
                                                     jcomment['user']['login'],
                                                     "commented")
            last_activity = practivity.get_latest_activity(
                last_activity, comment_activity)
        if re.search(settings.FEEDBACK_OK['keyword'], body):
            feedback_ok += 1
        if re.search(settings.FEEDBACK_WEAK['keyword'], body):
            feedback_weak += 1
        if re.search(settings.FEEDBACK_KO['keyword'], body):
            feedback_ko += 1
    if milestone:
        milestone = milestone['title']

    try:
        pr = PullRequest(url=data['json']['html_url'],
                         title=data['json']['title'],
                         updated_at=date,
                         user=data['json']['user']['login'],
                         my_open_comment_count=my_open_comment_count,
                         last_activity=last_activity,
                         repo=data['json']['base']['repo']['name'],
                         nbreview=int(data['detail']['comments']) +
                         int(data['detail']['review_comments']),
                         feedback_ok=feedback_ok,
                         feedback_weak=feedback_weak,
                         feedback_ko=feedback_ko,
                         milestone=milestone,
                         labels=labels,
                         is_old=is_old)
    except Exception as e:
        logger.error("cannot create PullRequest: %s", e)

    return pr
Example #5
0
def fetch_data(repo_name, url, org, current_user):
    """ Celery task, call github api
    repo_name -- github repo name
    url -- base url for this repo
    org -- github organisation

    return a dict {'name' : repo_name, 'pr_list' : pr_list} with pr_list a list
    of models.Pr
    """
    pr_list = []
    repo = {
        'name': repo_name,
        'pr_list': pr_list,
    }
    url = "%s/repos/%s/%s/pulls" % (url, org, repo_name)
    json_prlist = paginablejson.PaginableJson(url)
    now = timezone.now()
    if not json_prlist:
        return
    for json_pr in json_prlist:
        if json_pr['state'] == 'open':
            conversation_json = paginablejson.PaginableJson(
                json_pr['comments_url'])
            issue_url = json_pr['issue_url']
            last_event = None
            last_commit = None
            if "events" in settings.LAST_ACTIVITY_FILTER:
                last_event = practivity.get_latest_event(issue_url)
            if "commits" in settings.LAST_ACTIVITY_FILTER:
                last_commit = practivity.get_latest_commit(
                    "%s/%s" % (url, json_pr['number']))
            last_activity = practivity.get_latest_activity(
                last_event, last_commit)

            detail_json = paginablejson.PaginableJson(json_pr['url'])
            feedback_ok = 0
            feedback_weak = 0
            feedback_ko = 0
            milestone = json_pr['milestone']
            label_json = paginablejson.PaginableJson("%s/labels" % issue_url)
            labels = list()
            if label_json:
                for lbl in label_json:
                    label_style = 'light' if is_color_light(
                        lbl['color']) else 'dark'
                    labels.append({
                        'name': lbl['name'],
                        'color': lbl['color'],
                        'style': label_style,
                    })

            date = dateparse.parse_datetime(json_pr['updated_at'])
            is_old = False
            if (now - date).days >= settings.OLD_PERIOD:
                if not labels and None in settings.OLD_LABELS:
                    is_old = True
                else:
                    for lbl in labels:
                        if lbl['name'] in settings.OLD_LABELS:
                            is_old = True
                            break

            if current_user is not None:
                my_open_comment_count = get_open_comment_count(
                    json_pr['review_comments_url'], current_user)
            else:
                my_open_comment_count = 0
            # look for tags and activity only in main conversation and not in "file changed"
            for jcomment in conversation_json:
                body = jcomment['body']
                if "comments" in settings.LAST_ACTIVITY_FILTER:
                    comment_activity = practivity.PrActivity(
                        jcomment['updated_at'], jcomment['user']['login'],
                        "commented")
                    last_activity = practivity.get_latest_activity(
                        last_activity, comment_activity)
                if re.search(settings.FEEDBACK_OK['keyword'], body):
                    feedback_ok += 1
                if re.search(settings.FEEDBACK_WEAK['keyword'], body):
                    feedback_weak += 1
                if re.search(settings.FEEDBACK_KO['keyword'], body):
                    feedback_ko += 1
            if milestone:
                milestone = milestone['title']

            pr = models.Pr(url=json_pr['html_url'],
                           title=json_pr['title'],
                           updated_at=date,
                           user=json_pr['user']['login'],
                           my_open_comment_count=my_open_comment_count,
                           last_activity=last_activity,
                           repo=json_pr['base']['repo']['full_name'],
                           nbreview=int(detail_json['comments']) +
                           int(detail_json['review_comments']),
                           feedback_ok=feedback_ok,
                           feedback_weak=feedback_weak,
                           feedback_ko=feedback_ko,
                           milestone=milestone,
                           labels=labels,
                           is_old=is_old)
            pr_list.append(pr)

    repo['pr_list'] = sorted(pr_list,
                             key=attrgetter('updated_at'),
                             reverse=True)

    if not pr_list:
        return None

    return repo