Ejemplo n.º 1
0
def test_known_pull_request_task(app, owner, repository, session):
    """Task pull_request /pulls/1 that already exists."""
    httpretty.reset()
    cs1 = CommitStatus(repository, "1", "https://github.com/pulls/1", {
        "message": [],
        "files": {}
    })
    cs2 = CommitStatus(repository, "2", "https://github.com/pulls/2", {
        "message": [],
        "files": {}
    })
    session.add(cs1)
    session.add(cs2)
    session.commit()

    bs = BranchStatus(cs2, "test:my-branch", "https://github.com/pulls/1", {
        "commits": [cs1, cs2],
        "files": {}
    })
    session.add(bs)
    session.commit()
    assert_that(bs.is_pending(), equal_to(False))

    httpretty.enable()
    pull_request(bs.id, "https://api.github.com/pulls/1",
                 "http://kwalitee.invenio-software.org/status/2",
                 {"ACCESS_TOKEN": "deadbeef"})
    httpretty.disable()

    latest_requests = httpretty.HTTPretty.latest_requests
    assert_that(len(latest_requests), equal_to(0), "No requests are expected")
Ejemplo n.º 2
0
def commits(owner, repository, session, request):
    """Create a couple of commits for a test."""
    cs = [
        {"sha": "ef01234"},
        {"sha": "789abcd"},
        {"sha": "0123456"},
    ]
    url_template = "https://github.com/commits/{sha}"

    commits = []
    for commit in cs:
        cs = CommitStatus(repository,
                          commit["sha"],
                          url_template.format(**commit),
                          {"message": [], "files": None})
        commits.append(cs)
        session.add(cs)
    session.commit()

    def teardown():
        for commit in commits:
            session.delete(commit)
        session.commit()

    request.addfinalizer(teardown)
    return commits
Ejemplo n.º 3
0
def test_push_wip_commit(app, repository):
    """Worker push /commits/1 has wip as a component and is ignored"""
    httpretty.reset()
    commit = {
        "sha":
        1,
        "url":
        "https://api.github.com/commits/1",
        "html_url":
        "https://github.com/commits/1",
        "comments_url":
        "https://api.github.com/commits/1/comments",
        "commit": {
            "message": "wip: herp derp\n\nBy: John Doe <*****@*****.**>",
        },
        "files": [{
            "filename": "spam/__init__.py",
            "status": "added",
            "raw_url": "https://github.com/raw/1/spam/__init__.py"
        }]
    }
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/commits/1",
                           body=json.dumps(commit),
                           content_type="application/json")
    status = {"id": 1, "state": "success"}
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/statuses/1",
                           status=201,
                           body=json.dumps(status),
                           content_type="application/json")

    cs = CommitStatus.find_or_create(repository, commit["sha"], commit["url"])
    assert_that(cs.is_pending())

    httpretty.enable()
    push(
        cs.id, "https://api.github.com/commits/1",
        "https://api.github.com/statuses/1", {
            "COMPONENTS": ["comp"],
            "SIGNATURES": ["By"],
            "TRUSTED_DEVELOPERS": ["*****@*****.**"],
            "CHECK_LICENSE": False,
            "repository": repository.id
        })
    httpretty.disable()

    latest_requests = httpretty.HTTPretty.latest_requests
    assert_that(len(latest_requests), equal_to(2), "1x GET, 1x POST")

    expected_requests = ["", "success"]
    for expected, request in zip(expected_requests, latest_requests):
        assert_that(str(request.parsed_body), contains_string(expected))

    cs = CommitStatus.query.filter_by(repository_id=repository.id,
                                      sha=commit["sha"]).first()
    assert_that(cs)
    assert_that(cs.state, equal_to("success"))
    assert_that(cs.errors, equal_to(0))
Ejemplo n.º 4
0
def test_push_wip_commit(app, repository):
    """Worker push /commits/1 has wip as a component and is ignored"""
    httpretty.reset()
    commit = {
        "sha": 1,
        "url": "https://api.github.com/commits/1",
        "html_url": "https://github.com/commits/1",
        "comments_url": "https://api.github.com/commits/1/comments",
        "commit": {
            "message": "wip: herp derp\n\nBy: John Doe <*****@*****.**>",
        },
        "files": [{
            "filename": "spam/__init__.py",
            "status": "added",
            "raw_url": "https://github.com/raw/1/spam/__init__.py"
        }]
    }
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/commits/1",
                           body=json.dumps(commit),
                           content_type="application/json")
    status = {"id": 1, "state": "success"}
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/statuses/1",
                           status=201,
                           body=json.dumps(status),
                           content_type="application/json")

    cs = CommitStatus.find_or_create(repository,
                                     commit["sha"],
                                     commit["url"])
    assert_that(cs.is_pending())

    httpretty.enable()
    push(cs.id,
         "https://api.github.com/commits/1",
         "https://api.github.com/statuses/1",
         {"COMPONENTS": ["comp"],
          "SIGNATURES": ["By"],
          "TRUSTED_DEVELOPERS": ["*****@*****.**"],
          "CHECK_LICENSE": False,
          "repository": repository.id})
    httpretty.disable()

    latest_requests = httpretty.HTTPretty.latest_requests
    assert_that(len(latest_requests), equal_to(2), "1x GET, 1x POST")

    expected_requests = [
        "",
        "success"
    ]
    for expected, request in zip(expected_requests, latest_requests):
        assert_that(str(request.parsed_body), contains_string(expected))

    cs = CommitStatus.query.filter_by(repository_id=repository.id,
                                      sha=commit["sha"]).first()
    assert_that(cs)
    assert_that(cs.state, equal_to("success"))
    assert_that(cs.errors, equal_to(0))
Ejemplo n.º 5
0
def test_push_broken_commit_message(app, repository):
    """Worker push /commits/1 is invalid (message)"""
    httpretty.reset()
    commit = {
        "sha":
        1,
        "url":
        "https://api.github.com/commits/1",
        "html_url":
        "https://github.com/commits/1",
        "comments_url":
        "https://api.github.com/commits/1/comments",
        "commit": {
            "message": "Fix all the bugs!"
        },
        "files": [{
            "filename": "spam/eggs.py",
            "status": "modified",
            "raw_url": "https://github.com/raw/1/spam/eggs.py"
        }]
    }
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/commits/1",
                           body=json.dumps(commit),
                           content_type="application/json")
    eggs_py = '"""Eggs are boiled."""\n'
    httpretty.register_uri(httpretty.GET,
                           "https://github.com/raw/1/spam/eggs.py",
                           body=eggs_py,
                           content_type="text/plain")
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/commits/1/comments",
                           status=201,
                           body=json.dumps({"id": 1}),
                           content_type="application/json")
    status = {"id": 1, "state": "success"}
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/statuses/1",
                           status=201,
                           body=json.dumps(status),
                           content_type="application/json")

    cs = CommitStatus.find_or_create(repository, commit["sha"], commit["url"])
    assert_that(cs.is_pending())

    httpretty.enable()
    push(cs.id, "https://api.github.com/commits/1",
         "https://api.github.com/statuses/1", {
             "CHECK_LICENSE": False,
             "repository": repository.id
         })
    httpretty.disable()

    latest_requests = httpretty.HTTPretty.latest_requests
    assert_that(len(latest_requests), equal_to(4), "2x GET, 2x POST")

    expected_requests = ["", "needs more reviewers", "", "error"]
    for expected, request in zip(expected_requests, latest_requests):
        assert_that(str(request.parsed_body), contains_string(expected))
Ejemplo n.º 6
0
def test_push_known_commit(repository, session):
    """Worker push /commits/1 is not rechecked if known"""
    httpretty.reset()
    commit = {
        "sha":
        1,
        "url":
        "https://api.github.com/commits/1",
        "html_url":
        "https://github.com/commits/1",
        "comments_url":
        "https://api.github.com/commits/1/comments",
        "commit": {
            "message": "Fix all the bugs!"
        },
        "files": [{
            "filename": "spam/eggs.py",
            "status": "modified",
            "raw_url": "https://github.com/raw/1/spam/eggs.py"
        }]
    }
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/commits/1",
                           body=json.dumps(commit),
                           content_type="application/json")

    cs = CommitStatus(repository, "1", "https://github.com/commits/1", {
        "message": ["error 1", "error 2"],
        "files": {}
    })
    session.add(cs)
    session.commit()
    assert_that(cs.is_pending(), equal_to(False))

    httpretty.enable()
    body = push(cs.id, "https://api.github.com/commits/1",
                "https://api.github.com/statuses/1",
                {"repository": repository.id})
    httpretty.disable()

    latest_requests = httpretty.HTTPretty.latest_requests
    assert_that(len(latest_requests), equal_to(1), "1x GET")

    assert_that(body["description"], contains_string("[error] 2 errors"))
Ejemplo n.º 7
0
def test_push_known_commit(repository, session):
    """Worker push /commits/1 is not rechecked if known"""
    httpretty.reset()
    commit = {
        "sha": 1,
        "url": "https://api.github.com/commits/1",
        "html_url": "https://github.com/commits/1",
        "comments_url": "https://api.github.com/commits/1/comments",
        "commit": {
            "message": "Fix all the bugs!"
        },
        "files": [{
            "filename": "spam/eggs.py",
            "status": "modified",
            "raw_url": "https://github.com/raw/1/spam/eggs.py"
        }]
    }
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/commits/1",
                           body=json.dumps(commit),
                           content_type="application/json")

    cs = CommitStatus(repository,
                      "1",
                      "https://github.com/commits/1",
                      {"message": ["error 1", "error 2"], "files": {}})
    session.add(cs)
    session.commit()
    assert_that(cs.is_pending(), equal_to(False))

    httpretty.enable()
    body = push(cs.id,
                "https://api.github.com/commits/1",
                "https://api.github.com/statuses/1",
                {"repository": repository.id})
    httpretty.disable()

    latest_requests = httpretty.HTTPretty.latest_requests
    assert_that(len(latest_requests), equal_to(1), "1x GET")

    assert_that(body["description"],
                contains_string("[error] 2 errors"))
Ejemplo n.º 8
0
def commit(repository, session, request):
    sha = "060fcc4e4856b1bc7dc0eba0b07d3c734bbf24fd"
    url_template = "https://github.com/invenio/test/commits/{sha}"

    c = CommitStatus(repository,
                     sha,
                     url_template.format(sha=sha),
                     {"message": ["foo", "bar"],
                      "files": {"spam": {"errors": ["spam"]},
                                "eggs": {"errors": ["eggs"]}}})
    session.add(c)
    session.commit()

    def teardown():
        session.delete(c)
        session.commit()

    request.addfinalizer(teardown)
    return c
Ejemplo n.º 9
0
def branch(owner, repository, session, request):
    cs = [
        {
            "sha": "ef01234"
        },
        {
            "sha": "789abcd"
        },
        {
            "sha": "0123456"
        },
    ]
    name = "spam:wip/my-branch"
    url_template = "https://github.com/invenio/test/commits/{sha}"

    commits = []
    branches = []
    for commit in cs:
        cs = CommitStatus(repository, commit["sha"],
                          url_template.format(**commit))
        commits.append(cs)
        session.add(cs)
        session.commit()

        bs = BranchStatus(commits[-1], name, "http://github.com/pulls/1", {
            "commits": commits,
            "files": {}
        })
        branches.append(bs)
        session.add(bs)
        session.commit()

    def teardown():
        for bs in branches:
            session.delete(bs)
        session.commit()
        for commit in commits:
            session.delete(commit)
        session.commit()

    request.addfinalizer(teardown)
    return branches[-1]
Ejemplo n.º 10
0
def test_remove(session):
    """remove {account} drops everything tied to it"""
    add("invenio")

    acc = Account.query.filter_by(name="invenio").first()
    rep = Repository.find_or_create(acc, "foo")
    commit = CommitStatus(rep, "1", "http://")
    session.add(commit)
    session.commit()
    bs = BranchStatus(commit, "1", "http://")
    session.add(bs)
    session.commit()

    remove("invenio")

    assert_that(Account.query.filter_by(name="invenio").count(), equal_to(0))
    assert_that(Repository.query.filter_by(name="foo").count(), equal_to(0))
    assert_that(
        CommitStatus.query.filter_by(repository_id=rep.id).count(),
        equal_to(0))
    assert_that(
        BranchStatus.query.filter_by(commit_id=commit.id).count(), equal_to(0))
Ejemplo n.º 11
0
def test_push_broken_files(repository):
    """Worker push /commits/1 is invalid (files)"""
    httpretty.reset()
    commit = {
        "sha":
        1,
        "url":
        "https://api.github.com/commits/1",
        "html_url":
        "https://github.com/commits/1",
        "comments_url":
        "https://api.github.com/commits/1/comments",
        "commit": {
            "message": "comp: bob\n\nBy: John <*****@*****.**>"
        },
        "files": [{
            "filename": "spam/eggs.py",
            "status": "modified",
            "raw_url": "https://github.com/raw/1/spam/eggs.py"
        }]
    }
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/commits/1",
                           body=json.dumps(commit),
                           content_type="application/json")
    eggs_py = "if foo == bar:\n  print('derp')\n"
    httpretty.register_uri(httpretty.GET,
                           "https://github.com/raw/1/spam/eggs.py",
                           body=eggs_py,
                           content_type="text/plain")
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/commits/1/comments",
                           status=201,
                           body=json.dumps({"id": 1}),
                           content_type="application/json")
    status = {"id": 1, "state": "success"}
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/statuses/1",
                           status=201,
                           body=json.dumps(status),
                           content_type="application/json")

    cs = CommitStatus.find_or_create(repository, commit["sha"], commit["url"])
    assert_that(cs.is_pending())

    httpretty.enable()
    push(
        cs.id, "https://api.github.com/commits/1",
        "https://api.github.com/statuses/1", {
            "COMPONENTS": ["comp"],
            "SIGNATURES": ["By"],
            "TRUSTED_DEVELOPERS": ["*****@*****.**"],
            "repository": repository.id
        })
    httpretty.disable()

    latest_requests = httpretty.HTTPretty.latest_requests
    assert_that(len(latest_requests), equal_to(4), "2x GET, 2x POST")

    expected_requests = ["", "", "F821 undefined name 'foo'", "error"]
    for expected, request in zip(expected_requests, latest_requests):
        assert_that(str(request.parsed_body), contains_string(expected))
Ejemplo n.º 12
0
def test_okay_pull_request_task(app, owner, repository, session):
    """Task pull_request /pulls/1 with pep8 errors"""
    httpretty.reset()
    pull = {
        "title": "Lorem ipsum",
        "url": "https://api.github.com/pulls/1",
        "html_url": "https://github.com/pulls/1",
        "commits_url": "https://api.github.com/pulls/1/commits",
        "statuses_url": "https://api.github.com/statuses/1",
        "issue_url": "https://api.github.com/issues/1",
        "review_comments_url": "https://api.github.com/pulls/1/comments",
        "head": {
            "sha": "1",
            "label": "test:my-branch"
        },
        "base": {
            "repo": {
                "full_name": "kwalitee/test"
            },
            "ref": "testref"
        },
    }
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/pulls/1",
                           body=json.dumps(pull),
                           content_type="application/json")

    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/repos/kwalitee/test/"
                           "contents/.kwalitee.yml?ref=testref",
                           status=404)

    issue = {
        "url": "https://apigithub.com/issues/1",
        "html_url": "https://github.com/issues/1",
        "labels_url": "https://api.github.com/issues/1/labels{/name}",
        "id": "42",
        "number": "1",
        "labels": [{"name": "foo"},
                   {"name": "in_work"}],
        "state": "open"
    }
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/issues/1",
                           body=json.dumps(issue),
                           content_type="application/json")
    labels = [{
        "url": "https://github.com/labels/foo",
        "name": "foo",
        "color": "000000"
    }, {
        "url": "https://github.com/labels/in_review",
        "name": "in_review",
        "color": "ff0000"
    }]
    httpretty.register_uri(httpretty.PUT,
                           "https://api.github.com/issues/1/labels",
                           status=200,
                           body=json.dumps(labels),
                           content_type="application/json")
    commits = [
        {
            "url": "https://api.github.com/commits/1",
            "sha": "1",
            "html_url": "https://github.com/commits/1",
            "comments_url": "https://api.github.com/commits/1/comments",
            "commit": {
                "message": "herp: derp\r\n\r\nSigned-off-by: John Doe "
                           "<*****@*****.**>"
            }
        }
    ]
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/pulls/1/commits",
                           body=json.dumps(commits),
                           content_type="application/json")
    files = [{
        "filename": "eggs/__init__.py",
        "status": "added",
        "raw_url": "https://github.com/raw/1/eggs/__init__.py",
        "contents_url": "https://api.github.com/eggs/__init__.py?ref=1"
    }]
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/pulls/1/files",
                           body=json.dumps(files),
                           content_type="application/json")
    init_py = GPL.format(datetime.now().year, '#')
    httpretty.register_uri(httpretty.GET,
                           "https://github.com/raw/1/eggs/__init__.py",
                           body=init_py,
                           content_type="text/plain")
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/commits/1/comments",
                           status=201,
                           body=json.dumps({"id": 1}),
                           content_type="application/json")
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/pulls/1/comments",
                           status=201,
                           body=json.dumps({"id": 3}),
                           content_type="application/json")
    status = {"id": 1, "state": "success"}
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/statuses/1",
                           status=201,
                           body=json.dumps(status),
                           content_type="application/json")

    cs = []
    for commit in commits:
        cs.append(CommitStatus.find_or_create(repository,
                                              commit["sha"],
                                              commit["url"]))
    bs = BranchStatus(cs[-1],
                      "test:my-branch",
                      "http://github.com/pulls/1",
                      {"commits": cs, "files": None})
    session.add(bs)
    session.commit()

    httpretty.enable()
    pull_request(bs.id,
                 "https://api.github.com/pulls/1",
                 "http://kwalitee.invenio-software.org/status/1",
                 {"TRUSTED_DEVELOPERS": ["*****@*****.**"],
                  "COMPONENTS": ["herp"],
                  "SIGNATURES": ["Signed-off-by"],
                  "IGNORE": ["E265", "D100"]})
    httpretty.disable()

    latest_requests = httpretty.HTTPretty.latest_requests
    # 5x GET pull, .kwalitee.yml, issue, commits, 1 file, spam/eggs.py
    # 2x POST status, label
    assert_that(len(latest_requests), equal_to(8), "6x GET + 2x POST")

    expected_requests = [
        "",
        "",
        "",
        "",
        "",
        "/status/1",
        "",
        "in_integration",
    ]
    for expected, request in zip(expected_requests, latest_requests):
        assert_that(str(request.body), contains_string(expected))

    body = json.loads(latest_requests[-3].body)
    assert_that(latest_requests[-3].headers["authorization"],
                equal_to("token {0}".format(owner.token)))
    assert_that(body["state"], equal_to("success"))

    cs = CommitStatus.query.filter_by(repository_id=repository.id).all()
    assert_that(cs, has_length(1))
    assert_that(cs[0].content["files"] is None)
    assert_that(cs[0].is_pending(), equal_to(False))

    bs = BranchStatus.query.filter_by(commit_id=cs[0].id,
                                      name="test:my-branch").first()

    assert_that(bs.errors, equal_to(0))
    assert_that(bs.content["commits"], has_length(1))
    assert_that(bs.content["files"]["eggs/__init__.py"]["errors"],
                has_length(0))
Ejemplo n.º 13
0
def test_push_broken_commit_message(app, repository):
    """Worker push /commits/1 is invalid (message)"""
    httpretty.reset()
    commit = {
        "sha": 1,
        "url": "https://api.github.com/commits/1",
        "html_url": "https://github.com/commits/1",
        "comments_url": "https://api.github.com/commits/1/comments",
        "commit": {
            "message": "Fix all the bugs!"
        },
        "files": [{
            "filename": "spam/eggs.py",
            "status": "modified",
            "raw_url": "https://github.com/raw/1/spam/eggs.py"
        }]
    }
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/commits/1",
                           body=json.dumps(commit),
                           content_type="application/json")
    eggs_py = '"""Eggs are boiled."""\n'
    httpretty.register_uri(httpretty.GET,
                           "https://github.com/raw/1/spam/eggs.py",
                           body=eggs_py,
                           content_type="text/plain")
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/commits/1/comments",
                           status=201,
                           body=json.dumps({"id": 1}),
                           content_type="application/json")
    status = {"id": 1, "state": "success"}
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/statuses/1",
                           status=201,
                           body=json.dumps(status),
                           content_type="application/json")

    cs = CommitStatus.find_or_create(repository,
                                     commit["sha"],
                                     commit["url"])
    assert_that(cs.is_pending())

    httpretty.enable()
    push(cs.id,
         "https://api.github.com/commits/1",
         "https://api.github.com/statuses/1",
         {"CHECK_LICENSE": False,
          "repository": repository.id})
    httpretty.disable()

    latest_requests = httpretty.HTTPretty.latest_requests
    assert_that(len(latest_requests), equal_to(4), "2x GET, 2x POST")

    expected_requests = [
        "",
        "needs more reviewers",
        "",
        "error"
    ]
    for expected, request in zip(expected_requests, latest_requests):
        assert_that(str(request.parsed_body),
                    contains_string(expected))
Ejemplo n.º 14
0
def test_push_broken_files(repository):
    """Worker push /commits/1 is invalid (files)"""
    httpretty.reset()
    commit = {
        "sha": 1,
        "url": "https://api.github.com/commits/1",
        "html_url": "https://github.com/commits/1",
        "comments_url": "https://api.github.com/commits/1/comments",
        "commit": {
            "message": "comp: bob\n\nBy: John <*****@*****.**>"
        },
        "files": [{
            "filename": "spam/eggs.py",
            "status": "modified",
            "raw_url": "https://github.com/raw/1/spam/eggs.py"
        }]
    }
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/commits/1",
                           body=json.dumps(commit),
                           content_type="application/json")
    eggs_py = "if foo == bar:\n  print('derp')\n"
    httpretty.register_uri(httpretty.GET,
                           "https://github.com/raw/1/spam/eggs.py",
                           body=eggs_py,
                           content_type="text/plain")
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/commits/1/comments",
                           status=201,
                           body=json.dumps({"id": 1}),
                           content_type="application/json")
    status = {"id": 1, "state": "success"}
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/statuses/1",
                           status=201,
                           body=json.dumps(status),
                           content_type="application/json")

    cs = CommitStatus.find_or_create(repository,
                                     commit["sha"],
                                     commit["url"])
    assert_that(cs.is_pending())

    httpretty.enable()
    push(cs.id,
         "https://api.github.com/commits/1",
         "https://api.github.com/statuses/1",
         {"COMPONENTS": ["comp"],
          "SIGNATURES": ["By"],
          "TRUSTED_DEVELOPERS": ["*****@*****.**"],
          "repository": repository.id})
    httpretty.disable()

    latest_requests = httpretty.HTTPretty.latest_requests
    assert_that(len(latest_requests), equal_to(4), "2x GET, 2x POST")

    expected_requests = [
        "",
        "",
        "F821 undefined name 'foo'",
        "error"
    ]
    for expected, request in zip(expected_requests, latest_requests):
        assert_that(str(request.parsed_body),
                    contains_string(expected))
Ejemplo n.º 15
0
def test_okay_pull_request_task(app, owner, repository, session):
    """Task pull_request /pulls/1 with pep8 errors"""
    httpretty.reset()
    pull = {
        "title": "Lorem ipsum",
        "url": "https://api.github.com/pulls/1",
        "html_url": "https://github.com/pulls/1",
        "commits_url": "https://api.github.com/pulls/1/commits",
        "statuses_url": "https://api.github.com/statuses/1",
        "issue_url": "https://api.github.com/issues/1",
        "review_comments_url": "https://api.github.com/pulls/1/comments",
        "head": {
            "sha": "1",
            "label": "test:my-branch"
        },
        "base": {
            "repo": {
                "full_name": "kwalitee/test"
            },
            "ref": "testref"
        },
    }
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/pulls/1",
                           body=json.dumps(pull),
                           content_type="application/json")

    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/repos/kwalitee/test/"
                           "contents/.kwalitee.yml?ref=testref",
                           status=404)

    issue = {
        "url": "https://apigithub.com/issues/1",
        "html_url": "https://github.com/issues/1",
        "labels_url": "https://api.github.com/issues/1/labels{/name}",
        "id": "42",
        "number": "1",
        "labels": [{
            "name": "foo"
        }, {
            "name": "in_work"
        }],
        "state": "open"
    }
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/issues/1",
                           body=json.dumps(issue),
                           content_type="application/json")
    labels = [{
        "url": "https://github.com/labels/foo",
        "name": "foo",
        "color": "000000"
    }, {
        "url": "https://github.com/labels/in_review",
        "name": "in_review",
        "color": "ff0000"
    }]
    httpretty.register_uri(httpretty.PUT,
                           "https://api.github.com/issues/1/labels",
                           status=200,
                           body=json.dumps(labels),
                           content_type="application/json")
    commits = [{
        "url": "https://api.github.com/commits/1",
        "sha": "1",
        "html_url": "https://github.com/commits/1",
        "comments_url": "https://api.github.com/commits/1/comments",
        "commit": {
            "message":
            "herp: derp\r\n\r\nSigned-off-by: John Doe "
            "<*****@*****.**>"
        }
    }]
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/pulls/1/commits",
                           body=json.dumps(commits),
                           content_type="application/json")
    files = [{
        "filename": "eggs/__init__.py",
        "status": "added",
        "raw_url": "https://github.com/raw/1/eggs/__init__.py",
        "contents_url": "https://api.github.com/eggs/__init__.py?ref=1"
    }]
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/pulls/1/files",
                           body=json.dumps(files),
                           content_type="application/json")
    init_py = GPL.format(datetime.now().year, '#')
    httpretty.register_uri(httpretty.GET,
                           "https://github.com/raw/1/eggs/__init__.py",
                           body=init_py,
                           content_type="text/plain")
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/commits/1/comments",
                           status=201,
                           body=json.dumps({"id": 1}),
                           content_type="application/json")
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/pulls/1/comments",
                           status=201,
                           body=json.dumps({"id": 3}),
                           content_type="application/json")
    status = {"id": 1, "state": "success"}
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/statuses/1",
                           status=201,
                           body=json.dumps(status),
                           content_type="application/json")

    cs = []
    for commit in commits:
        cs.append(
            CommitStatus.find_or_create(repository, commit["sha"],
                                        commit["url"]))
    bs = BranchStatus(cs[-1], "test:my-branch", "http://github.com/pulls/1", {
        "commits": cs,
        "files": None
    })
    session.add(bs)
    session.commit()

    httpretty.enable()
    pull_request(
        bs.id, "https://api.github.com/pulls/1",
        "http://kwalitee.invenio-software.org/status/1", {
            "TRUSTED_DEVELOPERS": ["*****@*****.**"],
            "COMPONENTS": ["herp"],
            "SIGNATURES": ["Signed-off-by"],
            "IGNORE": ["E265", "D100"]
        })
    httpretty.disable()

    latest_requests = httpretty.HTTPretty.latest_requests
    # 5x GET pull, .kwalitee.yml, issue, commits, 1 file, spam/eggs.py
    # 2x POST status, label
    assert_that(len(latest_requests), equal_to(8), "6x GET + 2x POST")

    expected_requests = [
        "",
        "",
        "",
        "",
        "",
        "/status/1",
        "",
        "in_integration",
    ]
    for expected, request in zip(expected_requests, latest_requests):
        assert_that(str(request.body), contains_string(expected))

    body = json.loads(latest_requests[-3].body)
    assert_that(latest_requests[-3].headers["authorization"],
                equal_to("token {0}".format(owner.token)))
    assert_that(body["state"], equal_to("success"))

    cs = CommitStatus.query.filter_by(repository_id=repository.id).all()
    assert_that(cs, has_length(1))
    assert_that(cs[0].content["files"] is None)
    assert_that(cs[0].is_pending(), equal_to(False))

    bs = BranchStatus.query.filter_by(commit_id=cs[0].id,
                                      name="test:my-branch").first()

    assert_that(bs.errors, equal_to(0))
    assert_that(bs.content["commits"], has_length(1))
    assert_that(bs.content["files"]["eggs/__init__.py"]["errors"],
                has_length(0))
Ejemplo n.º 16
0
def test_wip_pull_request_task(app, owner, repository, session):
    """Task pull_request /pulls/1 is work in progress"""
    httpretty.reset()
    pull = {
        "title": "WIP Lorem ipsum",
        "url": "https://api.github.com/pulls/1",
        "html_url": "https://github.com/pulls/1",
        "commits_url": "https://api.github.com/pulls/1/commits",
        "statuses_url": "https://api.github.com/statuses/1",
        "review_comments_url": "https://api.github.com/pulls/1/comments",
        "issue_url": "https://api.github.com/issues/1",
        "head": {
            "sha": "2",
            "label": "test:my-branch"
        },
        "base": {
            "repo": {
                "full_name": "kwalitee/test"
            },
            "ref": "testref"
        },
    }
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/pulls/1",
                           body=json.dumps(pull),
                           content_type="application/json")

    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/repos/kwalitee/test/"
                           "contents/.kwalitee.yml?ref=testref",
                           status=404)

    commits = [{
        "url": "https://api.github.com/commits/1",
        "sha": "1",
        "html_url": "https://github.com/commits/1",
        "comments_url": "https://api.github.com/commits/1/comments",
        "commit": {
            "message": "fix all the bugs!"
        }
    }, {
        "url": "https://api.github.com/commits/2",
        "sha": "2",
        "html_url": "https://github.com/commits/2",
        "comments_url": "https://api.github.com/commits/2/comments",
        "commit": {
            "message": "herp derp"
        }
    }]
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/pulls/1/commits",
                           body=json.dumps(commits),
                           content_type="application/json")
    issue = {
        "url": "https://api.github.com/issues/1",
        "html_url": "https://github.com/issues/1",
        "labels_url": "https://api.github.com/issues/1/labels{/name}",
        "id": "42",
        "number": "1",
        "labels": [{
            "name": "foo"
        }, {
            "name": "in_integration"
        }],
        "state": "open"
    }
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/issues/1",
                           body=json.dumps(issue),
                           content_type="application/json")
    labels = [{
        "url": "https://github.com/labels/foo",
        "name": "foo",
        "color": "000000"
    }, {
        "url": "https://github.com/labels/in_review",
        "name": "in_review",
        "color": "ff0000"
    }]
    httpretty.register_uri(httpretty.PUT,
                           "https://api.github.com/issues/1/labels",
                           status=200,
                           body=json.dumps(issue),
                           content_type="application/json")

    css = []
    for commit in commits:
        css.append(
            CommitStatus.find_or_create(repository, commit["sha"],
                                        commit["url"]))
    bs = BranchStatus(css[-1], "test:my-branch", "https://github.com/pulls/1",
                      {
                          "commits": css,
                          "files": None
                      })
    session.add(bs)
    session.commit()
    assert_that(bs.is_pending())

    httpretty.enable()
    pull_request(bs.id, "https://api.github.com/pulls/1",
                 "http://kwalitee.invenio-software.org/status/1",
                 {"repository": repository.id})
    httpretty.disable()

    latest_requests = httpretty.HTTPretty.latest_requests
    # 4x GET pull, .kwalitee.yml, commits, issue
    # 1x POST labels
    assert_that(len(latest_requests), equal_to(5), "4x GET + 1x POST")

    expected_requests = ["", "", "", "", "in_work"]
    for expected, request in zip(expected_requests, latest_requests):
        assert_that(str(request.body), contains_string(expected))

    labels = json.loads(latest_requests[-1].body)
    assert_that(labels, has_items("in_work", "foo"))
    assert_that(labels, is_not(has_item("in_review")))
Ejemplo n.º 17
0
def test_pull_request_task(app, owner, repository, session):
    """Task pull_request /pulls/1"""
    httpretty.reset()
    pull = {
        "title": "Lorem ipsum",
        "url": "https://api.github.com/pulls/1",
        "html_url": "https://github.com/pulls/1",
        "commits_url": "https://api.github.com/pulls/1/commits",
        "statuses_url": "https://api.github.com/statuses/2",
        "review_comments_url": "https://api.github.com/pulls/1/comments",
        "issue_url": "https://api.github.com/issues/1",
        "head": {
            "sha": "2",
            "label": "test:my-branch"
        },
        "base": {
            "repo": {
                "full_name": "kwalitee/test"
            },
            "ref": "testref"
        },
    }
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/pulls/1",
                           body=json.dumps(pull),
                           content_type="application/json")

    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/repos/kwalitee/test/"
                           "contents/.kwalitee.yml?ref=testref",
                           status=404)

    issue = {
        "url": "https://api.github.com/issues/1",
        "html_url": "https://github.com/issues/1",
        "labels_url": "https://api.github.com/issues/1/labels{/name}",
        "id": "42",
        "number": "1",
        "labels": [{
            "name": "foo"
        }, {
            "name": "in_work"
        }],
        "state": "open"
    }
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/issues/1",
                           body=json.dumps(issue),
                           content_type="application/json")
    labels = [{
        "url": "https://github.com/labels/foo",
        "name": "foo",
        "color": "000000"
    }, {
        "url": "https://github.com/labels/in_review",
        "name": "in_review",
        "color": "ff0000"
    }]
    httpretty.register_uri(httpretty.PUT,
                           "https://api.github.com/issues/1/labels",
                           status=200,
                           body=json.dumps(labels),
                           content_type="application/json")
    commits = [{
        "url": "https://api.github.com/commits/1",
        "sha": "1",
        "html_url": "https://github.com/commits/1",
        "comments_url": "https://api.github.com/commits/1/comments",
        "commit": {
            "message": "fix all the bugs!"
        }
    }, {
        "url": "https://api.github.com/commits/2",
        "sha": "2",
        "html_url": "https://github.com/commits/2",
        "comments_url": "https://api.github.com/commits/1/comments",
        "commit": {
            "message": "herp derp"
        }
    }]
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/pulls/1/commits",
                           body=json.dumps(commits),
                           content_type="application/json")
    files = [{
        "filename": "spam/eggs.py",
        "status": "added",
        "raw_url": "https://github.com/raw/2/spam/eggs.py",
        "contents_url": "https://api.github.com/spam/eggs.py?ref=2"
    }, {
        "filename": "spam/herp.html",
        "status": "added",
        "raw_url": "https://github.com/raw/2/spam/herp.html",
        "contents_url": "https://api.github.com/spam/herp.html?ref=2"
    }]
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/pulls/1/files",
                           body=json.dumps(files),
                           content_type="application/json")
    eggs_py = "if foo == bar:\n  print('derp')\n"
    httpretty.register_uri(httpretty.GET,
                           "https://github.com/raw/2/spam/eggs.py",
                           body=eggs_py,
                           content_type="text/plain")
    herp_html = "<!DOCTYPE html><html><title>Hello!</title></html>"
    httpretty.register_uri(httpretty.GET,
                           "https://github.com/raw/2/spam/herp.html",
                           body=herp_html,
                           content_type="text/html")
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/commits/1/comments",
                           status=201,
                           body=json.dumps({"id": 1}),
                           content_type="application/json")
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/commits/2/comments",
                           status=201,
                           body=json.dumps({"id": 2}),
                           content_type="application/json")
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/pulls/1/comments",
                           status=201,
                           body=json.dumps({"id": 3}),
                           content_type="application/json")
    status = {"id": 1, "state": "success"}
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/statuses/2",
                           status=201,
                           body=json.dumps(status),
                           content_type="application/json")

    css = []
    for commit in commits:
        css.append(
            CommitStatus.find_or_create(repository, commit["sha"],
                                        commit["url"]))

    bs = BranchStatus(css[-1], "test:my-branch", "https://github.com/pulls/1",
                      {
                          "commits": css,
                          "files": None
                      })
    session.add(bs)
    session.commit()

    assert_that(css[0].is_pending())
    assert_that(css[1].is_pending())
    assert_that(bs.is_pending())

    httpretty.enable()
    pull_request(bs.id, "https://api.github.com/pulls/1",
                 "http://kwalitee.invenio-software.org/status/2",
                 {"repository": repository.id})
    httpretty.disable()

    latest_requests = httpretty.HTTPretty.latest_requests
    # 7x GET pull, .kwalitee.yml, issue, commits, files, spam/eggs.py,
    # spam/herp.html
    # 5x POST comments (1 message + 2 files), status
    # 1x PUT labels
    assert_that(len(latest_requests), equal_to(12), "7x GET + 4x POST + 1 PUT")

    expected_requests = [
        "",
        "",
        "",
        "missing component name",  # "signature is missing",
        "",
        "",
        "",
        "F821 undefined name",
        "L101 copyright is missing",
        "/status/2",
        "",
        "in_review"
    ]
    for expected, request in zip(expected_requests, latest_requests):
        assert_that(str(request.body), contains_string(expected))

    body = json.loads(latest_requests[-3].body)
    assert_that(latest_requests[-3].headers["authorization"],
                equal_to("token {0}".format(owner.token)))
    assert_that(body["state"], equal_to("error"))

    cs = CommitStatus.query.filter_by(repository_id=repository.id).all()

    assert_that(cs, has_length(2))
    assert_that(cs[0].content["message"],
                has_item("1: M110 missing component name"))
    assert_that(cs[1].content["message"],
                has_item("1: M100 needs more reviewers"))

    bs = BranchStatus.query.filter_by(commit_id=cs[1].id,
                                      name="test:my-branch").first()

    assert_that(bs)
    assert_that(bs.content["commits"], has_items("1", "2"))
    assert_that(bs.errors, equal_to(12))
    assert_that(bs.content["files"]["spam/eggs.py"]["errors"],
                has_item("2:3: E111 indentation is not a multiple of four"))
Ejemplo n.º 18
0
def test_wip_pull_request_task(app, owner, repository, session):
    """Task pull_request /pulls/1 is work in progress"""
    httpretty.reset()
    pull = {
        "title": "WIP Lorem ipsum",
        "url": "https://api.github.com/pulls/1",
        "html_url": "https://github.com/pulls/1",
        "commits_url": "https://api.github.com/pulls/1/commits",
        "statuses_url": "https://api.github.com/statuses/1",
        "review_comments_url": "https://api.github.com/pulls/1/comments",
        "issue_url": "https://api.github.com/issues/1",
        "head": {
            "sha": "2",
            "label": "test:my-branch"
        },
        "base": {
            "repo": {
                "full_name": "kwalitee/test"
            },
            "ref": "testref"
        },
    }
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/pulls/1",
                           body=json.dumps(pull),
                           content_type="application/json")

    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/repos/kwalitee/test/"
                           "contents/.kwalitee.yml?ref=testref",
                           status=404)

    commits = [
        {
            "url": "https://api.github.com/commits/1",
            "sha": "1",
            "html_url": "https://github.com/commits/1",
            "comments_url": "https://api.github.com/commits/1/comments",
            "commit": {
                "message": "fix all the bugs!"
            }
        }, {

            "url": "https://api.github.com/commits/2",
            "sha": "2",
            "html_url": "https://github.com/commits/2",
            "comments_url": "https://api.github.com/commits/2/comments",
            "commit": {
                "message": "herp derp"
            }
        }
    ]
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/pulls/1/commits",
                           body=json.dumps(commits),
                           content_type="application/json")
    issue = {
        "url": "https://api.github.com/issues/1",
        "html_url": "https://github.com/issues/1",
        "labels_url": "https://api.github.com/issues/1/labels{/name}",
        "id": "42",
        "number": "1",
        "labels": [{"name": "foo"},
                   {"name": "in_integration"}],
        "state": "open"
    }
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/issues/1",
                           body=json.dumps(issue),
                           content_type="application/json")
    labels = [{
        "url": "https://github.com/labels/foo",
        "name": "foo",
        "color": "000000"
    }, {
        "url": "https://github.com/labels/in_review",
        "name": "in_review",
        "color": "ff0000"
    }]
    httpretty.register_uri(httpretty.PUT,
                           "https://api.github.com/issues/1/labels",
                           status=200,
                           body=json.dumps(issue),
                           content_type="application/json")

    css = []
    for commit in commits:
        css.append(CommitStatus.find_or_create(repository,
                                               commit["sha"],
                                               commit["url"]))
    bs = BranchStatus(css[-1],
                      "test:my-branch",
                      "https://github.com/pulls/1",
                      {"commits": css, "files": None})
    session.add(bs)
    session.commit()
    assert_that(bs.is_pending())

    httpretty.enable()
    pull_request(bs.id,
                 "https://api.github.com/pulls/1",
                 "http://kwalitee.invenio-software.org/status/1",
                 {"repository": repository.id})
    httpretty.disable()

    latest_requests = httpretty.HTTPretty.latest_requests
    # 4x GET pull, .kwalitee.yml, commits, issue
    # 1x POST labels
    assert_that(len(latest_requests), equal_to(5), "4x GET + 1x POST")

    expected_requests = [
        "",
        "",
        "",
        "",
        "in_work"
    ]
    for expected, request in zip(expected_requests, latest_requests):
        assert_that(str(request.body), contains_string(expected))

    labels = json.loads(latest_requests[-1].body)
    assert_that(labels, has_items("in_work", "foo"))
    assert_that(labels, is_not(has_item("in_review")))
Ejemplo n.º 19
0
def test_push_half_known_commit(repository, session):
    """Worker push /commits/1 checks the files if none"""
    httpretty.reset()
    commit = {
        "sha": "1",
        "url": "https://api.github.com/commits/1",
        "html_url": "https://github.com/commits/1",
        "comments_url": "https://api.github.com/commits/1/comments",
        "commit": {
            "message": "Fix all the bugs!"
        },
        "files": [{
            "filename": "spam/eggs.py",
            "status": "modified",
            "raw_url": "https://github.com/raw/1/spam/eggs.py"
        }]
    }
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/commits/1",
                           body=json.dumps(commit),
                           content_type="application/json")
    eggs_py = "if foo == bar:\n  print('derp')\n"
    httpretty.register_uri(httpretty.GET,
                           "https://github.com/raw/1/spam/eggs.py",
                           body=eggs_py,
                           content_type="text/plain")
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/commits/1/comments",
                           status=201,
                           body=json.dumps({"id": 1}),
                           content_type="application/json")
    status = {"id": 1, "state": "success"}
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/statuses/1",
                           status=201,
                           body=json.dumps(status),
                           content_type="application/json")

    cs = CommitStatus(repository,
                      "1",
                      "https://github.com/commits/1",
                      {"message": [], "files": None})
    session.add(cs)
    session.commit()
    assert_that(cs.is_pending(), equal_to(False))

    httpretty.enable()
    push(cs.id,
         "https://api.github.com/commits/1",
         "https://api.github.com/statuses/1",
         {"repository": repository.id})
    httpretty.disable()

    latest_requests = httpretty.HTTPretty.latest_requests
    assert_that(len(latest_requests), equal_to(4), "2x GET, 2x POST")

    expected_requests = [
        "",
        "",
        "F821 undefined name 'foo'",
        "error"
    ]
    for expected, request in zip(expected_requests, latest_requests):
        assert_that(str(request.parsed_body),
                    contains_string(expected))

    cs = CommitStatus.query.filter_by(id=cs.id).first()
    assert_that(cs)
    assert_that(cs.is_pending(), equal_to(False))
    assert_that(cs.content["files"]["spam/eggs.py"]["errors"],
                has_item("1: D100 Missing docstring in public module"))
Ejemplo n.º 20
0
def test_push_half_known_commit(repository, session):
    """Worker push /commits/1 checks the files if none"""
    httpretty.reset()
    commit = {
        "sha":
        "1",
        "url":
        "https://api.github.com/commits/1",
        "html_url":
        "https://github.com/commits/1",
        "comments_url":
        "https://api.github.com/commits/1/comments",
        "commit": {
            "message": "Fix all the bugs!"
        },
        "files": [{
            "filename": "spam/eggs.py",
            "status": "modified",
            "raw_url": "https://github.com/raw/1/spam/eggs.py"
        }]
    }
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/commits/1",
                           body=json.dumps(commit),
                           content_type="application/json")
    eggs_py = "if foo == bar:\n  print('derp')\n"
    httpretty.register_uri(httpretty.GET,
                           "https://github.com/raw/1/spam/eggs.py",
                           body=eggs_py,
                           content_type="text/plain")
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/commits/1/comments",
                           status=201,
                           body=json.dumps({"id": 1}),
                           content_type="application/json")
    status = {"id": 1, "state": "success"}
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/statuses/1",
                           status=201,
                           body=json.dumps(status),
                           content_type="application/json")

    cs = CommitStatus(repository, "1", "https://github.com/commits/1", {
        "message": [],
        "files": None
    })
    session.add(cs)
    session.commit()
    assert_that(cs.is_pending(), equal_to(False))

    httpretty.enable()
    push(cs.id, "https://api.github.com/commits/1",
         "https://api.github.com/statuses/1", {"repository": repository.id})
    httpretty.disable()

    latest_requests = httpretty.HTTPretty.latest_requests
    assert_that(len(latest_requests), equal_to(4), "2x GET, 2x POST")

    expected_requests = ["", "", "F821 undefined name 'foo'", "error"]
    for expected, request in zip(expected_requests, latest_requests):
        assert_that(str(request.parsed_body), contains_string(expected))

    cs = CommitStatus.query.filter_by(id=cs.id).first()
    assert_that(cs)
    assert_that(cs.is_pending(), equal_to(False))
    assert_that(cs.content["files"]["spam/eggs.py"]["errors"],
                has_item("1: D100 Missing docstring in public module"))
Ejemplo n.º 21
0
def test_pull_request_task(app, owner, repository, session):
    """Task pull_request /pulls/1"""
    httpretty.reset()
    pull = {
        "title": "Lorem ipsum",
        "url": "https://api.github.com/pulls/1",
        "html_url": "https://github.com/pulls/1",
        "commits_url": "https://api.github.com/pulls/1/commits",
        "statuses_url": "https://api.github.com/statuses/2",
        "review_comments_url": "https://api.github.com/pulls/1/comments",
        "issue_url": "https://api.github.com/issues/1",
        "head": {
            "sha": "2",
            "label": "test:my-branch"
        },
        "base": {
            "repo": {
                "full_name": "kwalitee/test"
            },
            "ref": "testref"
        },
    }
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/pulls/1",
                           body=json.dumps(pull),
                           content_type="application/json")

    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/repos/kwalitee/test/"
                           "contents/.kwalitee.yml?ref=testref",
                           status=404)

    issue = {
        "url": "https://api.github.com/issues/1",
        "html_url": "https://github.com/issues/1",
        "labels_url": "https://api.github.com/issues/1/labels{/name}",
        "id": "42",
        "number": "1",
        "labels": [{"name": "foo"},
                   {"name": "in_work"}],
        "state": "open"
    }
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/issues/1",
                           body=json.dumps(issue),
                           content_type="application/json")
    labels = [{
        "url": "https://github.com/labels/foo",
        "name": "foo",
        "color": "000000"
    }, {
        "url": "https://github.com/labels/in_review",
        "name": "in_review",
        "color": "ff0000"
    }]
    httpretty.register_uri(httpretty.PUT,
                           "https://api.github.com/issues/1/labels",
                           status=200,
                           body=json.dumps(labels),
                           content_type="application/json")
    commits = [
        {
            "url": "https://api.github.com/commits/1",
            "sha": "1",
            "html_url": "https://github.com/commits/1",
            "comments_url": "https://api.github.com/commits/1/comments",
            "commit": {
                "message": "fix all the bugs!"
            }
        }, {

            "url": "https://api.github.com/commits/2",
            "sha": "2",
            "html_url": "https://github.com/commits/2",
            "comments_url": "https://api.github.com/commits/1/comments",
            "commit": {
                "message": "herp derp"
            }
        }
    ]
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/pulls/1/commits",
                           body=json.dumps(commits),
                           content_type="application/json")
    files = [{
        "filename": "spam/eggs.py",
        "status": "added",
        "raw_url": "https://github.com/raw/2/spam/eggs.py",
        "contents_url": "https://api.github.com/spam/eggs.py?ref=2"
    }, {
        "filename": "spam/herp.html",
        "status": "added",
        "raw_url": "https://github.com/raw/2/spam/herp.html",
        "contents_url": "https://api.github.com/spam/herp.html?ref=2"
    }]
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/pulls/1/files",
                           body=json.dumps(files),
                           content_type="application/json")
    eggs_py = "if foo == bar:\n  print('derp')\n"
    httpretty.register_uri(httpretty.GET,
                           "https://github.com/raw/2/spam/eggs.py",
                           body=eggs_py,
                           content_type="text/plain")
    herp_html = "<!DOCTYPE html><html><title>Hello!</title></html>"
    httpretty.register_uri(httpretty.GET,
                           "https://github.com/raw/2/spam/herp.html",
                           body=herp_html,
                           content_type="text/html")
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/commits/1/comments",
                           status=201,
                           body=json.dumps({"id": 1}),
                           content_type="application/json")
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/commits/2/comments",
                           status=201,
                           body=json.dumps({"id": 2}),
                           content_type="application/json")
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/pulls/1/comments",
                           status=201,
                           body=json.dumps({"id": 3}),
                           content_type="application/json")
    status = {"id": 1, "state": "success"}
    httpretty.register_uri(httpretty.POST,
                           "https://api.github.com/statuses/2",
                           status=201,
                           body=json.dumps(status),
                           content_type="application/json")

    css = []
    for commit in commits:
        css.append(CommitStatus.find_or_create(repository,
                                               commit["sha"],
                                               commit["url"]))

    bs = BranchStatus(css[-1],
                      "test:my-branch",
                      "https://github.com/pulls/1",
                      {"commits": css, "files": None})
    session.add(bs)
    session.commit()

    assert_that(css[0].is_pending())
    assert_that(css[1].is_pending())
    assert_that(bs.is_pending())

    httpretty.enable()
    pull_request(bs.id,
                 "https://api.github.com/pulls/1",
                 "http://kwalitee.invenio-software.org/status/2",
                 {"repository": repository.id})
    httpretty.disable()

    latest_requests = httpretty.HTTPretty.latest_requests
    # 7x GET pull, .kwalitee.yml, issue, commits, files, spam/eggs.py,
    # spam/herp.html
    # 5x POST comments (1 message + 2 files), status
    # 1x PUT labels
    assert_that(len(latest_requests), equal_to(12), "7x GET + 4x POST + 1 PUT")

    expected_requests = [
        "",
        "",
        "",
        "missing component name",  # "signature is missing",
        "",
        "",
        "",
        "F821 undefined name",
        "L101 copyright is missing",
        "/status/2",
        "",
        "in_review"
    ]
    for expected, request in zip(expected_requests, latest_requests):
        assert_that(str(request.body), contains_string(expected))

    body = json.loads(latest_requests[-3].body)
    assert_that(latest_requests[-3].headers["authorization"],
                equal_to("token {0}".format(owner.token)))
    assert_that(body["state"], equal_to("error"))

    cs = CommitStatus.query.filter_by(repository_id=repository.id).all()

    assert_that(cs, has_length(2))
    assert_that(cs[0].content["message"],
                has_item("1: M110 missing component name"))
    assert_that(cs[1].content["message"],
                has_item("1: M100 needs more reviewers"))

    bs = BranchStatus.query.filter_by(commit_id=cs[1].id,
                                      name="test:my-branch").first()

    assert_that(bs)
    assert_that(bs.content["commits"], has_items("1", "2"))
    assert_that(bs.errors, equal_to(12))
    assert_that(
        bs.content["files"]["spam/eggs.py"]["errors"],
        has_item("2:3: E111 indentation is not a multiple of four"))