Ejemplo n.º 1
0
 def test_as_guess(self):
     """Test the as_guess method"""
     # pylint: disable=protected-access
     ct = CommentTime(datetime.datetime(2020, 1, 2),
                      datetime.datetime(2020, 1, 4),
                      updated_time_is_guess=False)
     ct2 = ct.as_guess()
     self.assertEqual(ct2._creation_time, ct._creation_time)
     self.assertEqual(ct2._last_updated_time, ct._last_updated_time)
     self.assertTrue(ct2._updated_time_is_guess)
Ejemplo n.º 2
0
    def _simple_line_comment(comment_id,
                             content,
                             username=None,
                             creation_date=None,
                             updated_date=None,
                             path=None):
        """Return a PRLineComment object with some hard-coded pieces

        Args:
        comment_id (integer): used in URL
        content (string)
        username (string): if not given, uses a hard-coded username
        creation_date (datetime): if not given, uses a hard-coded creation_date
        updated_date (datetime): if not given, uses the same date as creation_date
        path (string): if not given, uses a hard-coded path
        """
        if username is None:
            username = "******"
        if creation_date is None:
            creation_date = datetime.datetime(2020, 1, 2)
        if updated_date is None:
            updated_date = creation_date
        if path is None:
            path = "path/to/file.py"
        time_info = CommentTime(creation_date, updated_date)
        return PRLineComment(
            username=username,
            time_info=time_info,
            url="https://github.com/org/repo/1#comment-{c_id}".format(
                c_id=comment_id),
            content=content,
            path=path)
Ejemplo n.º 3
0
    def _simple_comment(comment_type,
                        comment_id,
                        content,
                        username=None,
                        creation_date=None,
                        updated_date=None):
        """Return a Comment object with some hard-coded pieces

        Args:
        comment_type (one of the subclasses of Comment (e.g., ConversationComment))
        comment_id (integer): used in URL
        content (string)
        username (string): if not given, uses a hard-coded username
        creation_date (datetime): if not given, uses a hard-coded creation_date
        updated_date (datetime): if not given, uses the same date as creation_date
        """
        if username is None:
            username = "******"
        if creation_date is None:
            creation_date = datetime.datetime(2020, 1, 2)
        if updated_date is None:
            updated_date = creation_date
        time_info = CommentTime(creation_date, updated_date)
        return comment_type(
            username=username,
            time_info=time_info,
            url="https://github.com/org/repo/1#comment-{c_id}".format(
                c_id=comment_id),
            content=content)
Ejemplo n.º 4
0
 def test_repr_resultsInEqualObject(self):
     """repr of a CommentTime object should result in an equiv. object"""
     # This ability to recreate the object isn't a strict requirement, so if it gets
     # hard to maintain, we can drop it.
     ct = CommentTime(datetime.datetime(2020, 1, 1),
                      datetime.datetime(2020, 1, 2))
     # pylint: disable=eval-used
     ct2 = eval(repr(ct))
     self.assertEqual(ct2, ct)
Ejemplo n.º 5
0
    def _create_comment(content=None):
        """Returns a basic Comment object

        If content isn't specified, use some hard-coded content
        """
        if content is None:
            content = "My content"
        time_info = CommentTime(creation_time=datetime.datetime(2020, 1, 1),
                                last_updated_time=datetime.datetime(
                                    2020, 1, 2))
        return ConversationComment(
            username="******",
            time_info=time_info,
            url="https://github.com/org/repo/1#issuecomment-2",
            content=content)
Ejemplo n.º 6
0
    def _create_pr(body=None,
                   comments=None,
                   username=None,
                   creation_date=None,
                   updated_date=None):
        """Returns a basic PullRequest object

        If body is given, it should be a string; otherwise, a hard-coded body is used

        If comments is given, it should be a list of Comment objects; otherwise, a
        hard-coded list of comments is used.

        If username is given, it should be a string; otherwise, a hard-coded username is used

        If creation_date is given, it should be a datetime object; otherwise, a hard-coded
        creation_date is used.

        If updated_date is given, it should be a datetime object; otherwise, we use the
        same date as creation_date.
        """
        if body is None:
            body = "PR body"

        if comments is None:
            comments = (TestPullRequest._simple_comment(
                ConversationComment, 1, "comment"),
                        TestPullRequest._simple_line_comment(
                            2, "line comment"),
                        TestPullRequest._simple_comment(
                            PRReviewComment, 3, "review comment"))

        if username is None:
            username = "******"

        if creation_date is None:
            creation_date = datetime.datetime(2020, 1, 1)

        if updated_date is None:
            updated_date = creation_date

        time_info = CommentTime(creation_date, updated_date)
        return PullRequest(pr_number=17,
                           title="My title",
                           username=username,
                           time_info=time_info,
                           url="https://github.com/org/repo/1",
                           body=body,
                           comments=comments)
Ejemplo n.º 7
0
    def _create_comment_todo(text=None, extra_info=None, completed=False):
        """Returns a basic CommentTodo object

        If text is None, some default text will be used
        """
        if text is None:
            text = "My text"
        time_info = CommentTime(creation_time=datetime.datetime(2020, 1, 1),
                                last_updated_time=datetime.datetime(
                                    2020, 1, 2))
        return CommentTodo(username="******",
                           time_info=time_info,
                           url="https://github.com/org/repo/1#issuecomment-2",
                           text=text,
                           extra_info=extra_info,
                           completed=completed)
Ejemplo n.º 8
0
    def _create_comment(content=None, path=None):
        """Returns a basic PRLineComment object

        If content isn't specified, use some hard-coded content

        If path isn't specified, use some hard-coded path
        """
        if content is None:
            content = "My content"
        if path is None:
            path = "path/to/file.py"
        time_info = CommentTime(creation_time=datetime.datetime(2020, 1, 1),
                                last_updated_time=datetime.datetime(
                                    2020, 1, 2))
        return PRLineComment(
            username="******",
            time_info=time_info,
            url="https://github.com/org/repo/1#issuecomment-2",
            content=content,
            path=path)
Ejemplo n.º 9
0
 def test_updatedSince_false(self):
     """Test updated_since method when it should be false"""
     ct = CommentTime(datetime.datetime(2020, 1, 2),
                      datetime.datetime(2020, 1, 4))
     is_updated_since = ct.updated_since(datetime.datetime(2020, 1, 5))
     self.assertFalse(is_updated_since)
Ejemplo n.º 10
0
 def test_createdSince_true(self):
     """Test created_since method when it should be true"""
     ct = CommentTime(datetime.datetime(2020, 1, 2),
                      datetime.datetime(2020, 1, 4))
     is_created_since = ct.created_since(datetime.datetime(2020, 1, 1))
     self.assertTrue(is_created_since)
Ejemplo n.º 11
0
 def test_str_works(self):
     # pylint: disable=no-self-use
     """Just make sure that the str method runs successfully"""
     ct = CommentTime(datetime.datetime(2020, 1, 1),
                      datetime.datetime(2020, 1, 2))
     _ = str(ct)
Ejemplo n.º 12
0
def fetch_pull_request(repo, pr_number):
    """Fetch information about the given Pull Request, returning a PullRequest object

    Args:
    repo: string - in the format Org/Repo
    pr_number: integer - PR ID in this repo
    """
    gh_inst = _get_github_instance()
    gh_repo = gh_inst.get_repo(repo)
    gh_pr = gh_repo.get_pull(pr_number)

    # This is the time that *anything* in the PR was last updated. We use this as a
    # conservative guess of when comments were last updated if we don't have any other
    # last-updated information for a given comment.
    pr_last_updated = gh_pr.updated_at.astimezone()

    comments = []
    for gh_comment in gh_pr.get_issue_comments():
        time_info = CommentTime(
            creation_time=gh_comment.created_at.astimezone(),
            last_updated_time=gh_comment.updated_at.astimezone())
        this_comment = ConversationComment(username=gh_comment.user.login,
                                           time_info=time_info,
                                           url=gh_comment.html_url,
                                           content=gh_comment.body)
        comments.append(this_comment)

    for gh_comment in gh_pr.get_comments():
        time_info = CommentTime(
            creation_time=gh_comment.created_at.astimezone(),
            last_updated_time=gh_comment.updated_at.astimezone())
        this_comment = PRLineComment(username=gh_comment.user.login,
                                     time_info=time_info,
                                     url=gh_comment.html_url,
                                     content=gh_comment.body,
                                     path=gh_comment.path)
        comments.append(this_comment)

    for gh_comment in gh_pr.get_reviews():
        if gh_comment.body:
            # GitHub creates a Pull Request Review for any PR line comments that have been
            # made - even individual line comments made outside a review, or when you make
            # a set of line comments in a review but don't leave an overall
            # comment. Exclude empty reviews that are created in these circumstances.

            # Pull Request Reviews don't appear to support a last-updated time, so we use
            # the last updated time of the PR as a whole as a conservative guess.
            time_info = CommentTime(
                creation_time=gh_comment.submitted_at.astimezone(),
                last_updated_time=pr_last_updated,
                updated_time_is_guess=True)
            this_comment = PRReviewComment(username=gh_comment.user.login,
                                           time_info=time_info,
                                           url=gh_comment.html_url,
                                           content=gh_comment.body)
            comments.append(this_comment)

    time_info = CommentTime(creation_time=gh_pr.created_at.astimezone(),
                            last_updated_time=pr_last_updated)
    return PullRequest(pr_number=pr_number,
                       title=gh_pr.title,
                       username=gh_pr.user.login,
                       time_info=time_info,
                       url=gh_pr.html_url,
                       body=gh_pr.body,
                       comments=comments)