Example #1
0
 def fill_short_stats_commitrange(self, commitrange):
     to_ref, from_ref = commitrange
     if to_ref == 'HEAD' and from_ref is None:
         total_lines = 0
     else:
         total_lines = self.total_lines
     for commit in self.fill_shortstat(commitrange):
         files = commit['files']
         inserted = commit['additions']
         deleted = commit['deletions']
         total_lines += inserted
         total_lines -= deleted
         self.total_lines_added += inserted
         self.total_lines_removed += deleted
         stamp = commit['committer_time']
         author = commit['author_name']
         email = commit['author_email']
         author = get_author_by_email(email, author)
         if author in conf['merge_authors']:
             author = conf['merge_authors'][author]
         self.changes_by_date[stamp] = {
             'files': files,
             'ins': inserted,
             'del': deleted,
             'lines': total_lines
         }
         self.process_line_user(author, stamp, inserted, deleted)
     self.total_lines = total_lines
Example #2
0
 def commits_by_path(self, request):
     if not self.project:
         raise api_errors.NotFoundError("project")
     path = request.get_form_var('path', '')
     ref = request.get_form_var('ref') or 'HEAD'
     commits = {'r': 1}
     cs = {}
     try:
         tree = self.project.repo.get_tree(ref, path=path, with_commit=True)
         for v in tree:
             k = v['id']
             v['message_with_emoji'] = parse_emoji(
                 escape(v['commit']['message']))
             author = get_author_by_email(v['commit']['author']['email'])
             if author:
                 v['contributor'] = author
                 v['contributor_url'] = "%s/people/%s" % (DOMAIN, author)
             else:
                 v['contributor'] = v['commit']['author']['name']
             v['age'] = compute_relative_time(v['commit']['author']['time'])
             v['sha'] = v['commit']['sha']
             v['commit_id'] = v['commit']['sha']
             cs[k] = v
         commits['commits'] = cs
     except KeyError:
         commits = {'r': '0', 'err': 'Path not found'}
     except IOError:
         commits = {'r': '0', 'err': 'Path not found'}
     return json.dumps(commits)
Example #3
0
 def commits_by_path(self, request):
     if not self.project:
         raise api_errors.NotFoundError("project")
     path = request.get_form_var('path', '')
     ref = request.get_form_var('ref') or 'HEAD'
     commits = {'r': 1}
     cs = {}
     try:
         tree = self.project.repo.get_tree(ref, path=path, with_commit=True)
         for v in tree:
             k = v['id']
             v['message_with_emoji'] = parse_emoji(
                 escape(v['commit']['message']))
             author = get_author_by_email(
                 v['commit']['author']['email'])
             if author:
                 v['contributor'] = author
                 v['contributor_url'] = "%s/people/%s" % (DOMAIN, author)
             else:
                 v['contributor'] = v['commit']['author']['name']
             v['age'] = compute_relative_time(v['commit']['author']['time'])
             v['sha'] = v['commit']['sha']
             v['commit_id'] = v['commit']['sha']
             cs[k] = v
         commits['commits'] = cs
     except KeyError:
         commits = {'r': '0', 'err': 'Path not found'}
     except IOError:
         commits = {'r': '0', 'err': 'Path not found'}
     return json.dumps(commits)
Example #4
0
 def fill_short_stats_commitrange(self, commitrange):
     to_ref, from_ref = commitrange
     if to_ref == 'HEAD' and from_ref is None:
         total_lines = 0
     else:
         total_lines = self.total_lines
     for commit in self.fill_shortstat(commitrange):
         files = commit['files']
         inserted = commit['additions']
         deleted = commit['deletions']
         total_lines += inserted
         total_lines -= deleted
         self.total_lines_added += inserted
         self.total_lines_removed += deleted
         stamp = commit['committer_time']
         author = commit['author_name']
         email = commit['author_email']
         author = get_author_by_email(email, author)
         if author in conf['merge_authors']:
             author = conf['merge_authors'][author]
         self.changes_by_date[stamp] = {
             'files': files,
             'ins': inserted,
             'del': deleted,
             'lines': total_lines
         }
         self.process_line_user(author, stamp, inserted, deleted)
     self.total_lines = total_lines
Example #5
0
    def __init__(self, repo, commit):
        self.repo = repo
        self._commit = commit
        self.type = 'commit'
        self.repo_name = repo.name
        parent = commit['parent'][0] if commit['parent'] else None
        self.parent = parent
        self.parents = commit['parent']
        message = ("%s\n\n%s" % (
            commit['message'],
            remove_unknown_character(commit['body']))
        ).strip()
        self.message = message
        self.message_header = commit['message']
        self.message_body = commit['body']
        self.sha = commit['sha']
        self.tree = commit['tree']
        self.has_author_link = True

        # author
        author_name = commit['author']['name']
        self.author_name = author_name
        author_email = commit['author']['email']
        self.author_email = author_email
        self.email = author_email
        code_author_name = get_author_by_email(author_email, None)
        if code_author_name is None:
            self.has_author_link = False
            author = User(name=author_name, email=author_email)
        else:
            author = User(name=code_author_name, email=author_email)
        self.author = author
        author_date = datetime.fromtimestamp(
            commit['author']['time'],
            FixedOffset(commit['author']['offset']))
        self.author_time = author_date
        author_timestamp = str(commit['author']['time'])
        self.author_timestamp = author_timestamp
        self.time = author_date

        # committer
        committer_name = commit['committer']['name']
        committer_email = email_normalizer(committer_name,
                                           commit['committer']['email'])
        committer = User(name=committer_name, email=committer_email)
        self.committer = committer
        committer_date = datetime.fromtimestamp(
            commit['committer']['time'],
            FixedOffset(commit['committer']['offset']))
        self.committer_time = committer_date
        self.commit_time = committer_date  # FIXME: remove this!
Example #6
0
    def fill_rev_list_commitrange(self, commitrange):
        to_ref, from_ref = commitrange
        commits = self.gyt_repo.repo.rev_list(to_ref, from_ref)
        for commit in commits:
            author = commit.author.name
            # email = email_normalizer(commit.author.name,
            #                          commit.author.email)
            email = commit.author.email
            stamp = commit.committer.time
            date = datetime.fromtimestamp(
                commit.committer.time,
                FixedOffset(commit.committer.offset)
            )

            author = get_author_by_email(email, author)
            if author in conf['merge_authors']:
                author = conf['merge_authors'][author]

            # First and last commit stamp
            # (may be in any order because of cherry-picking and patches)
            if stamp > self.last_commit_stamp:
                self.last_commit_stamp = stamp
            if self.first_commit_stamp == 0 or stamp < self.first_commit_stamp:
                self.first_commit_stamp = stamp

            # yearly/weekly activity
            yyw = date.strftime('%Y-%W')
            self.year_week_act[yyw] += 1
            if self.year_week_act_peak < self.year_week_act[yyw]:
                self.year_week_act_peak = self.year_week_act[yyw]

            # author stats
            if author not in self.authors:
                self.authors[author] = {
                    'lines_added': 0,
                    'lines_removed': 0,
                    'commits': 0,
                }
            # commits, note again that commits may be in any date order
            # because of cherry-picking and patches
            if 'last_commit_stamp' not in self.authors[author]:
                self.authors[author]['last_commit_stamp'] = stamp
            if stamp > self.authors[author]['last_commit_stamp']:
                self.authors[author]['last_commit_stamp'] = stamp
            if 'first_commit_stamp' not in self.authors[author]:
                self.authors[author]['first_commit_stamp'] = stamp
            if stamp < self.authors[author]['first_commit_stamp']:
                self.authors[author]['first_commit_stamp'] = stamp
            if 'email' not in self.authors[author]:
                self.authors[author]['email'] = email
Example #7
0
    def __init__(self, repo, commit):
        self.repo = repo
        self._commit = commit
        self.type = 'commit'
        self.repo_name = repo.name
        parent = commit['parent'][0] if commit['parent'] else None
        self.parent = parent
        self.parents = commit['parent']
        message = ("%s\n\n%s" %
                   (commit['message'], remove_unknown_character(
                       commit['body']))).strip()
        self.message = message
        self.message_header = commit['message']
        self.message_body = commit['body']
        self.sha = commit['sha']
        self.tree = commit['tree']
        self.has_author_link = True

        # author
        author_name = commit['author']['name']
        self.author_name = author_name
        author_email = commit['author']['email']
        self.author_email = author_email
        self.email = author_email
        code_author_name = get_author_by_email(author_email, None)
        if code_author_name is None:
            self.has_author_link = False
            author = User(name=author_name, email=author_email)
        else:
            author = User(name=code_author_name, email=author_email)
        self.author = author
        author_date = datetime.fromtimestamp(
            commit['author']['time'], FixedOffset(commit['author']['offset']))
        self.author_time = author_date
        author_timestamp = str(commit['author']['time'])
        self.author_timestamp = author_timestamp
        self.time = author_date

        # committer
        committer_name = commit['committer']['name']
        committer_email = email_normalizer(committer_name,
                                           commit['committer']['email'])
        committer = User(name=committer_name, email=committer_email)
        self.committer = committer
        committer_date = datetime.fromtimestamp(
            commit['committer']['time'],
            FixedOffset(commit['committer']['offset']))
        self.committer_time = committer_date
        self.commit_time = committer_date  # FIXME: remove this!
Example #8
0
    def fill_rev_list_commitrange(self, commitrange):
        to_ref, from_ref = commitrange
        commits = self.gyt_repo.repo.rev_list(to_ref, from_ref)
        for commit in commits:
            author = commit.author.name
            # email = email_normalizer(commit.author.name,
            #                          commit.author.email)
            email = commit.author.email
            stamp = commit.committer.time
            date = datetime.fromtimestamp(commit.committer.time,
                                          FixedOffset(commit.committer.offset))

            author = get_author_by_email(email, author)
            if author in conf['merge_authors']:
                author = conf['merge_authors'][author]

            # First and last commit stamp
            # (may be in any order because of cherry-picking and patches)
            if stamp > self.last_commit_stamp:
                self.last_commit_stamp = stamp
            if self.first_commit_stamp == 0 or stamp < self.first_commit_stamp:
                self.first_commit_stamp = stamp

            # yearly/weekly activity
            yyw = date.strftime('%Y-%W')
            self.year_week_act[yyw] += 1
            if self.year_week_act_peak < self.year_week_act[yyw]:
                self.year_week_act_peak = self.year_week_act[yyw]

            # author stats
            if author not in self.authors:
                self.authors[author] = {
                    'lines_added': 0,
                    'lines_removed': 0,
                    'commits': 0,
                }
            # commits, note again that commits may be in any date order
            # because of cherry-picking and patches
            if 'last_commit_stamp' not in self.authors[author]:
                self.authors[author]['last_commit_stamp'] = stamp
            if stamp > self.authors[author]['last_commit_stamp']:
                self.authors[author]['last_commit_stamp'] = stamp
            if 'first_commit_stamp' not in self.authors[author]:
                self.authors[author]['first_commit_stamp'] = stamp
            if stamp < self.authors[author]['first_commit_stamp']:
                self.authors[author]['first_commit_stamp'] = stamp
            if 'email' not in self.authors[author]:
                self.authors[author]['email'] = email
Example #9
0
def async_push_notif(args):
    data = args
    repo = data.get('repository', {})
    repo_name = repo.get('name', '')
    repo_url = repo.get('url', '')
    ref = data.get('ref', '')
    pushername = data.get('user_name')
    branch = ref.split('/')[-1]
    commits = data.get('commits', [])
    commits_data = []
    proj = CodeDoubanProject.get_by_name(repo_name)
    for c in commits:
        author = c.get('author', {})
        email = author.get('email')
        username = get_author_by_email(email)
        if not username:
            continue
        commit_id = c.get('id')
        message = c.get('message')
        timestamp = c.get('timestamp')
        url = '%s/commit/%s' % (repo_url, commit_id)
        ProjectOwnLRUCounter(username).use(proj.id)
        ProjectWatchLRUCounter(username).use(proj.id)
        commits_data.append(
            dict(repo_name=repo_name,
                 repo_url=repo_url,
                 ref=ref,
                 branch=branch,
                 author=author,
                 username=username,
                 email=email,
                 commit_id=commit_id,
                 message=message,
                 timestamp=parser.parse(timestamp[:-5]),
                 url=url))

    before = data.get('before')
    after = data.get('after')
    push_signal.send('push',
                     repo_name=repo_name,
                     repo_url=repo_url,
                     ref=ref,
                     branch=branch,
                     commits=commits_data,
                     before=before,
                     after=after,
                     username=pushername)
Example #10
0
def async_push_notif(args):
    data = args
    repo = data.get('repository', {})
    repo_name = repo.get('name', '')
    repo_url = repo.get('url', '')
    ref = data.get('ref', '')
    pushername = data.get('user_name')
    branch = ref.split('/')[-1]
    commits = data.get('commits', [])
    commits_data = []
    proj = CodeDoubanProject.get_by_name(repo_name)
    for c in commits:
        author = c.get('author', {})
        email = author.get('email')
        username = get_author_by_email(email)
        if not username:
            continue
        commit_id = c.get('id')
        message = c.get('message')
        timestamp = c.get('timestamp')
        url = '%s/commit/%s' % (repo_url, commit_id)
        ProjectOwnLRUCounter(username).use(proj.id)
        ProjectWatchLRUCounter(username).use(proj.id)
        commits_data.append(dict(repo_name=repo_name,
                                 repo_url=repo_url,
                                 ref=ref,
                                 branch=branch,
                                 author=author,
                                 username=username,
                                 email=email,
                                 commit_id=commit_id,
                                 message=message,
                                 timestamp=parser.parse(timestamp[:-5]),
                                 url=url))

    before = data.get('before')
    after = data.get('after')
    push_signal.send('push',
                     repo_name=repo_name,
                     repo_url=repo_url,
                     ref=ref,
                     branch=branch,
                     commits=commits_data,
                     before=before,
                     after=after,
                     username=pushername)
Example #11
0
 def get_gitstats_data(self):
     assert self.project_name
     assert self.project
     repo = self.pygit2_repo
     data = GitDataCollector(self._gyt_repo)
     try:
         # FIXME: GitError: Reference 'refs/heads/master' not found
         head_sha = repo[repo.head.target].hex
         get_author = lambda u: get_author_by_email(
             u.email.encode('utf-8'), u.name.encode('utf-8'))
         authors = {get_author(c.author) for c in repo.walk(
             head_sha, GIT_SORT_TIME)}
         n_author = len(authors)
     except:
         return data
     data.collect(self.path, self.project_name, head_sha, n_author)
     return data
Example #12
0
 def get_gitstats_data(self):
     assert self.project_name
     assert self.project
     repo = self.pygit2_repo
     data = GitDataCollector(self._gyt_repo)
     try:
         # FIXME: GitError: Reference 'refs/heads/master' not found
         head_sha = repo[repo.head.target].hex
         get_author = lambda u: get_author_by_email(u.email.encode('utf-8'),
                                                    u.name.encode('utf-8'))
         authors = {
             get_author(c.author)
             for c in repo.walk(head_sha, GIT_SORT_TIME)
         }
         n_author = len(authors)
     except:
         return data
     data.collect(self.path, self.project_name, head_sha, n_author)
     return data
Example #13
0
def add_pull(ticket, pullreq, user):
    from dispatches import dispatch
    from vilya.libs.text import get_mentions_from_text
    from vilya.libs.signals import pullrequest_signal
    from vilya.models.user import get_author_by_email
    from vilya.models.user import User
    from vilya.models.trello.core import process_trello_notify

    reporter = user.username
    commits = pullreq.commits
    # TODO: refactory is! auto number how to?
    shas = [p.sha for p in commits]
    ticket_len = Ticket.get_count_by_proj_id(ticket.project_id)
    if ticket_len == 0:
        # 检查是否创建过新PR,若未创建过则以旧PR号为基准累加
        max_ticket_id = PullRequest.get_max_ticket_id(ticket.project_id)
        if max_ticket_id >= 0:
            ticket = Ticket.add(ticket.project_id,
                                ticket.title,
                                ticket.description,
                                ticket.author,
                                max_ticket_id + 1)
        else:
            ticket = Ticket.add(ticket.project_id,
                                ticket.title,
                                ticket.description,
                                ticket.author)
    else:
        ticket = Ticket.add(ticket.project_id,
                            ticket.title,
                            ticket.description,
                            ticket.author)
    pullreq = pullreq.insert(ticket.ticket_number)

    if shas:
        ticket.add_commits(','.join(shas), reporter)
    noti_receivers = [committer.name
                      for committer in CodeDoubanProject.get_committers_by_project(pullreq.to_proj.id)]  # noqa
    noti_receivers = noti_receivers + [pullreq.to_proj.owner.name]
    get_commit_author = lambda u: get_author_by_email(u.email.encode('utf-8'))
    commit_authors = {get_commit_author(c.author) for c in commits}
    commit_authors = {a for a in commit_authors if a}
    noti_receivers.extend(commit_authors)

    # diffs, author_by_file, authors = pullreq.get_diffs(with_blame=True)
    # FIXME: diffs没用到?
    # diffs = pullreq.get_diffs()

    # blame代码变更的原作者, 也加到at users
    at_users = get_mentions_from_text(ticket.description)
    # at_users.extend(authors)
    at_users.append(pullreq.to_proj.owner_id)
    at_users = set(at_users)

    # FIXME: invited_users is used on page /hub/my_pull_requests/
    invited_users = [User(u).add_invited_pull_request(ticket.id)
                     for u in at_users]

    ProjectOwnLRUCounter(user.username).use(pullreq.to_proj.id)
    ProjectWatchLRUCounter(user.username).use(pullreq.to_proj.id)

    # TODO: 重构Feed之后取消这个信号的发送
    pullrequest_signal.send(user.username,
                            extra_receivers=noti_receivers,
                            pullreq=pullreq,
                            comment=ticket.description,
                            ticket_id=ticket.ticket_id,
                            ticket=ticket,
                            status="unmerge",
                            new_version=True)

    dispatch('pullreq',
             data=dict(sender=user.username,
                       content=ticket.description,
                       ticket=ticket,
                       status='unmerge',
                       new_version=True,
                       extra_receivers=noti_receivers))

    dispatch('pr_actions',
             data=dict(type='pr_opened',
                       hooks=pullreq.to_proj.hooks,
                       author=user,
                       title=ticket.title,
                       body=ticket.description,
                       ticket=ticket,
                       pullreq=pullreq))

    # FIXME: move to dispatch
    process_trello_notify(user, ticket)
    return pullreq
Example #14
0
 def addrs_by_emails(cls, emails):
     return Mail.addrs_by_usernames(
         [get_author_by_email(email) for email in emails])
Example #15
0
def add_pull(ticket, pullreq, user):
    from dispatches import dispatch
    from vilya.libs.text import get_mentions_from_text
    from vilya.libs.signals import pullrequest_signal
    from vilya.models.user import get_author_by_email
    from vilya.models.user import User
    from vilya.models.trello.core import process_trello_notify

    reporter = user.username
    commits = pullreq.commits
    # TODO: refactory is! auto number how to?
    shas = [p.sha for p in commits]
    ticket_len = Ticket.get_count_by_proj_id(ticket.project_id)
    if ticket_len == 0:
        # 检查是否创建过新PR,若未创建过则以旧PR号为基准累加
        max_ticket_id = PullRequest.get_max_ticket_id(ticket.project_id)
        if max_ticket_id >= 0:
            ticket = Ticket.add(ticket.project_id, ticket.title,
                                ticket.description, ticket.author,
                                max_ticket_id + 1)
        else:
            ticket = Ticket.add(ticket.project_id, ticket.title,
                                ticket.description, ticket.author)
    else:
        ticket = Ticket.add(ticket.project_id, ticket.title,
                            ticket.description, ticket.author)
    pullreq = pullreq.insert(ticket.ticket_number)

    if shas:
        ticket.add_commits(','.join(shas), reporter)
    noti_receivers = [
        committer.name for committer in
        CodeDoubanProject.get_committers_by_project(pullreq.to_proj.id)
    ]  # noqa
    noti_receivers = noti_receivers + [pullreq.to_proj.owner.name]
    get_commit_author = lambda u: get_author_by_email(u.email.encode('utf-8'))
    commit_authors = {get_commit_author(c.author) for c in commits}
    commit_authors = {a for a in commit_authors if a}
    noti_receivers.extend(commit_authors)

    # diffs, author_by_file, authors = pullreq.get_diffs(with_blame=True)
    # FIXME: diffs没用到?
    # diffs = pullreq.get_diffs()

    # blame代码变更的原作者, 也加到at users
    at_users = get_mentions_from_text(ticket.description)
    # at_users.extend(authors)
    at_users.append(pullreq.to_proj.owner_id)
    at_users = set(at_users)

    # FIXME: invited_users is used on page /hub/my_pull_requests/
    invited_users = [
        User(u).add_invited_pull_request(ticket.id) for u in at_users
    ]

    ProjectOwnLRUCounter(user.username).use(pullreq.to_proj.id)
    ProjectWatchLRUCounter(user.username).use(pullreq.to_proj.id)

    # TODO: 重构Feed之后取消这个信号的发送
    pullrequest_signal.send(user.username,
                            extra_receivers=noti_receivers,
                            pullreq=pullreq,
                            comment=ticket.description,
                            ticket_id=ticket.ticket_id,
                            ticket=ticket,
                            status="unmerge",
                            new_version=True)

    dispatch('pullreq',
             data=dict(sender=user.username,
                       content=ticket.description,
                       ticket=ticket,
                       status='unmerge',
                       new_version=True,
                       extra_receivers=noti_receivers))

    dispatch('pr_actions',
             data=dict(type='pr_opened',
                       hooks=pullreq.to_proj.hooks,
                       author=user,
                       title=ticket.title,
                       body=ticket.description,
                       ticket=ticket,
                       pullreq=pullreq))

    # FIXME: move to dispatch
    process_trello_notify(user, ticket)
    return pullreq
Example #16
0
 def addrs_by_emails(cls, emails):
     return Mail.addrs_by_usernames(
         [get_author_by_email(email) for email in emails])