예제 #1
0
async def test_GitLabManager_post_comment_invalid_line_code(
    mock_call_provider, monkeypatch
):
    manager = GitLabManager(access_token="valid")

    async def fake_file_diff(*args):
        # Fake that the 20 first lines are identical
        return [difflib.Match(a=0, b=0, size=20)]

    monkeypatch.setattr(manager, "_get_file_diff", fake_file_diff)

    body = NewComment("New discussion on plain text", "README.md", 17, None)

    mock_call_provider.side_effect = [
        read_sample_response("get_pr.json"),
        HTTPClientError(
            HTTPStatus.BAD_REQUEST,
            None,
            MagicMock(
                body=bytes(
                    json.dumps({"message": 'line_code=>["must be a valid line code"]'}),
                    encoding="utf-8",
                )
            ),
        ),
        read_sample_response("posted_new_file_comment.json"),
    ]

    result = await manager.post_comment("mergerequest-id", body)

    assert mock_call_provider.call_count == 3
    # 1 = last call; 0 = args of call; 0 = first argument
    request = mock_call_provider.call_args_list[-1][0][0]

    assert request.url == f"{manager.base_api_url}mergerequest-id/discussions"
    assert json.loads(request.body.decode("utf-8")) == {
        "body": body.text,
        "position": {
            "position_type": "text",
            "new_line": 17,
            "old_line": 17,
            "new_path": "README.md",
            "old_path": None,
            "base_sha": "e616d1a1a2a95416178b1494fa08a69694132c96",
            "head_sha": "5cbd51cf3b89aaa1a2444cd4d4ce68fae299f592",
            "start_sha": "e616d1a1a2a95416178b1494fa08a69694132c96",
        },
    }
    assert request.method == "POST"
    assert result == {
        "id": 49157,
        "text": "New discussion on plain text",
        "updatedAt": "2021-03-04T14:02:21.258Z",
        "userName": "******",
        "userPicture": "https://gitlab.example.com/uploads/-/system/user/avatar/149/avatar.png",
        "inReplyTo": "7cd51262f86ad99f85f5a2ad30f76aeb39de7696",
    }
예제 #2
0
async def test_GitHubManager_post_comment_valid_new(mock_call_provider,
                                                    pr_valid_github_manager):
    mock_call_provider.side_effect = [
        read_sample_response("github_pr_links.json"),
        read_sample_response("github_comments_post.json"),
    ]
    body = NewComment(text="test text",
                      filename="test.ipynb",
                      line=3,
                      originalLine=None)

    result = await pr_valid_github_manager.post_comment(
        "https://api.github.com/repos/octocat/repo/pulls/1", body)

    expected_result = {
        "id": 299659626,
        "text": "test",
        "updatedAt": "2019-07-02T19:58:38Z",
        "userName": "******",
        "userPicture": "https://avatars1.githubusercontent.com/u/9003282?v=4",
        # FIXME use a different sample set without in_reply_to_id
        "inReplyToId": 296364299,
    }

    assert mock_call_provider.call_count == 2
    # 1 = 2nd call; 0 = args of call; 0 = first argument
    request = mock_call_provider.call_args_list[1][0][0]
    assert request.url == "https://api.github.com/repos/octocat/repo/pulls/1/comments"
    assert json.loads(request.body.decode("utf-8")) == {
        "body": "test text",
        "commit_id": "02fb374e022fbe7aaa4cd69c0dc3928e6422dfaa",
        "path": "test.ipynb",
        "line": 3,
        "side": "RIGHT",
    }
    assert request.method == "POST"
    assert result == expected_result
예제 #3
0
    mock_call_provider.assert_called_once()
    assert (
        mock_call_provider.call_args[0][0].url
        == f"{manager.base_api_url}mergerequests-id/discussions?per_page=100"
    )

    assert result == expected


@pytest.mark.asyncio
@pytest.mark.parametrize(
    "filename, body, position, response, expected",
    (
        (
            None,
            NewComment("New discussion at merge request level", None, None, None),
            {"commit_id": "5cbd51cf3b89aaa1a2444cd4d4ce68fae299f592"},
            "posted_new_pr_comment.json",
            {
                "id": 49294,
                "text": "New discussion at merge request level",
                "updatedAt": "2021-03-05T09:37:26.241Z",
                "userName": "******",
                "userPicture": "https://gitlab.example.com/uploads/-/system/user/avatar/149/avatar.png",
                "inReplyTo": "eca92cf74cfb9f4ad369cdadaa7209131bc1c648",
            },
        ),
        (
            None,
            CommentReply("Reply to a discussion at PR level", None, "discussion-id"),
            {},