Skip to content

Commit

Permalink
Commit API
Browse files Browse the repository at this point in the history
  • Loading branch information
dianaclarke committed Jul 21, 2021
1 parent 57928cf commit d7c84a3
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 3 deletions.
1 change: 1 addition & 0 deletions conbench/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from ._errors import * # noqa
from .auth import * # noqa
from .benchmarks import * # noqa
from .commits import * # noqa
from .compare import * # noqa
from .contexts import * # noqa
from .distributions import * # noqa
Expand Down
3 changes: 3 additions & 0 deletions conbench/api/_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ def _201_created(example, schema=None):
spec.components.response("BenchmarkEntity", _200_ok(ex.BENCHMARK_ENTITY))
spec.components.response("BenchmarkList", _200_ok(ex.BENCHMARK_LIST))
spec.components.response("BenchmarkCreated", _201_created(ex.BENCHMARK_ENTITY))
spec.components.response("CommitEntity", _200_ok(ex.COMMIT_ENTITY))
spec.components.response("CommitList", _200_ok(ex.COMMIT_LIST))
spec.components.response("CompareEntity", _200_ok(ex.COMPARE_ENTITY))
spec.components.response("CompareList", _200_ok(ex.COMPARE_LIST))
spec.components.response("DistributionEntity", _200_ok(ex.DISTRIBUTION_ENTITY))
Expand All @@ -82,6 +84,7 @@ def _201_created(example, schema=None):
{"name": "Index", "description": "List of endpoints"},
{"name": "Users", "description": "Manage users"},
{"name": "Benchmarks", "description": "Record benchmarks"},
{"name": "Commits", "description": "Benchmarked Commits"},
{"name": "Compare", "description": "Compare benchmarks"},
{"name": "Contexts", "description": "Benchmark contexts"},
{"name": "Distributions", "description": "Benchmark distributions"},
Expand Down
18 changes: 15 additions & 3 deletions conbench/api/_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ def _api_benchmark_entity(summary_id, context_id, case_id, batch_id, run_id, nam
}


def _api_commit_entity(commit_id):
return {
def _api_commit_entity(commit_id, links=True):
result = {
"author_avatar": "https://avatars.githubusercontent.com/u/878798?v=4",
"author_login": "dianaclarke",
"author_name": "Diana Clarke",
Expand All @@ -99,7 +99,14 @@ def _api_commit_entity(commit_id):
"parent_sha": "4beb514d071c9beec69b8917b5265e77ade22fb3",
"parent_url": "https://github.com/apache/arrow/commit/4beb514d071c9beec69b8917b5265e77ade22fb3",
"timestamp": "2021-02-25T01:02:51",
"links": {
"list": "http://localhost/api/commits/",
"self": "http://localhost/api/commits/%s/" % commit_id,
},
}
if not links:
result.pop("links", None)
return result


def _api_compare_entity(benchmark_ids, batch_ids, run_ids, batch, benchmark, tags):
Expand Down Expand Up @@ -278,7 +285,7 @@ def _api_run_entity(run_id, commit_id, machine_id, now, baseline_id):
"id": run_id,
"name": "commit: 02addad336ba19a654f9c857ede546331be7b631",
"timestamp": now,
"commit": _api_commit_entity(commit_id),
"commit": _api_commit_entity(commit_id, links=False),
"machine": _api_machine_entity(machine_id, links=False),
"links": {
"self": "http://localhost/api/runs/%s/" % run_id,
Expand Down Expand Up @@ -316,6 +323,11 @@ def _api_run_entity(run_id, commit_id, machine_id, now, baseline_id):
"file-write",
),
]
COMMIT_ENTITY = _api_commit_entity("some-commit-uuid-1")
COMMIT_LIST = [
_api_commit_entity("some-commit-uuid-1"),
_api_commit_entity("some-commit-uuid-2"),
]
COMPARE_ENTITY = _api_compare_entity(
["some-benchmark-uuid-1", "some-benchmark-uuid-2"],
["some-batch-uuid-1", "some-batch-uuid-2"],
Expand Down
66 changes: 66 additions & 0 deletions conbench/api/commits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from ..api import rule
from ..api._endpoint import ApiEndpoint
from ..entities._entity import NotFound
from ..entities.commit import Commit, CommitSerializer


class CommitListAPI(ApiEndpoint):
serializer = CommitSerializer()

def get(self):
"""
---
description: Get a list of commits.
responses:
"200": "CommitList"
"401": "401"
tags:
- Commits
"""
commits = Commit.all(limit=500)
return self.serializer.many.dump(commits)


class CommitEntityAPI(ApiEndpoint):
serializer = CommitSerializer()

def _get(self, commit_id):
try:
commit = Commit.one(id=commit_id)
except NotFound:
self.abort_404_not_found()
return commit

def get(self, commit_id):
"""
---
description: Get a commit.
responses:
"200": "CommitEntity"
"401": "401"
"404": "404"
parameters:
- name: commit_id
in: path
schema:
type: string
tags:
- Commits
"""
commit = self._get(commit_id)
return self.serializer.one.dump(commit)


commit_entity_view = CommitEntityAPI.as_view("commit")
commit_list_view = CommitListAPI.as_view("commits")

rule(
"/commits/<commit_id>/",
view_func=commit_entity_view,
methods=["GET"],
)
rule(
"/commits/",
view_func=commit_list_view,
methods=["GET"],
)
5 changes: 5 additions & 0 deletions conbench/entities/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os

import dateutil.parser
import flask as f
import requests
import sqlalchemy as s

Expand Down Expand Up @@ -50,6 +51,10 @@ def _dump(self, commit):
"author_login": commit.author_login,
"author_avatar": commit.author_avatar,
"timestamp": commit.timestamp.isoformat(),
"links": {
"list": f.url_for("api.commits", _external=True),
"self": f.url_for("api.commit", commit_id=commit.id, _external=True),
},
}


Expand Down
97 changes: 97 additions & 0 deletions conbench/tests/api/_expected_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,73 @@
},
"description": "OK",
},
"CommitEntity": {
"content": {
"application/json": {
"example": {
"author_avatar": "https://avatars.githubusercontent.com/u/878798?v=4",
"author_login": "dianaclarke",
"author_name": "Diana Clarke",
"id": "some-commit-uuid-1",
"links": {
"list": "http://localhost/api/commits/",
"self": "http://localhost/api/commits/some-commit-uuid-1/",
},
"message": "ARROW-11771: [Developer][Archery] Move benchmark tests (so CI runs them)",
"parent_sha": "4beb514d071c9beec69b8917b5265e77ade22fb3",
"parent_url": "https://github.com/apache/arrow/commit/4beb514d071c9beec69b8917b5265e77ade22fb3",
"repository": "https://github.com/apache/arrow",
"sha": "02addad336ba19a654f9c857ede546331be7b631",
"timestamp": "2021-02-25T01:02:51",
"url": "https://github.com/apache/arrow/commit/02addad336ba19a654f9c857ede546331be7b631",
}
}
},
"description": "OK",
},
"CommitList": {
"content": {
"application/json": {
"example": [
{
"author_avatar": "https://avatars.githubusercontent.com/u/878798?v=4",
"author_login": "dianaclarke",
"author_name": "Diana Clarke",
"id": "some-commit-uuid-1",
"links": {
"list": "http://localhost/api/commits/",
"self": "http://localhost/api/commits/some-commit-uuid-1/",
},
"message": "ARROW-11771: [Developer][Archery] Move benchmark tests (so CI runs them)",
"parent_sha": "4beb514d071c9beec69b8917b5265e77ade22fb3",
"parent_url": "https://github.com/apache/arrow/commit/4beb514d071c9beec69b8917b5265e77ade22fb3",
"repository": "https://github.com/apache/arrow",
"sha": "02addad336ba19a654f9c857ede546331be7b631",
"timestamp": "2021-02-25T01:02:51",
"url": "https://github.com/apache/arrow/commit/02addad336ba19a654f9c857ede546331be7b631",
},
{
"author_avatar": "https://avatars.githubusercontent.com/u/878798?v=4",
"author_login": "dianaclarke",
"author_name": "Diana Clarke",
"id": "some-commit-uuid-2",
"links": {
"list": "http://localhost/api/commits/",
"self": "http://localhost/api/commits/some-commit-uuid-2/",
},
"message": "ARROW-11771: [Developer][Archery] Move benchmark tests (so CI runs them)",
"parent_sha": "4beb514d071c9beec69b8917b5265e77ade22fb3",
"parent_url": "https://github.com/apache/arrow/commit/4beb514d071c9beec69b8917b5265e77ade22fb3",
"repository": "https://github.com/apache/arrow",
"sha": "02addad336ba19a654f9c857ede546331be7b631",
"timestamp": "2021-02-25T01:02:51",
"url": "https://github.com/apache/arrow/commit/02addad336ba19a654f9c857ede546331be7b631",
},
]
}
},
"description": "OK",
},
"CompareEntity": {
"content": {
"application/json": {
Expand Down Expand Up @@ -1010,6 +1077,35 @@
"tags": ["Benchmarks"],
},
},
"/api/commits/": {
"get": {
"description": "Get a list of commits.",
"responses": {
"200": {"$ref": "#/components/responses/CommitList"},
"401": {"$ref": "#/components/responses/401"},
},
"tags": ["Commits"],
}
},
"/api/commits/{commit_id}/": {
"get": {
"description": "Get a commit.",
"parameters": [
{
"in": "path",
"name": "commit_id",
"required": True,
"schema": {"type": "string"},
}
],
"responses": {
"200": {"$ref": "#/components/responses/CommitEntity"},
"401": {"$ref": "#/components/responses/401"},
"404": {"$ref": "#/components/responses/404"},
},
"tags": ["Commits"],
}
},
"/api/compare/batches/{compare_ids}/": {
"get": {
"description": "Compare benchmark results.",
Expand Down Expand Up @@ -1329,6 +1425,7 @@
{"description": "List of endpoints", "name": "Index"},
{"description": "Manage users", "name": "Users"},
{"description": "Record benchmarks", "name": "Benchmarks"},
{"description": "Benchmarked Commits", "name": "Commits"},
{"description": "Compare benchmarks", "name": "Compare"},
{"description": "Benchmark contexts", "name": "Contexts"},
{"description": "Benchmark distributions", "name": "Distributions"},
Expand Down
40 changes: 40 additions & 0 deletions conbench/tests/api/test_commits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from ...api._examples import _api_commit_entity
from ...tests.api import _asserts
from ...tests.api import _fixtures


def _expected_entity(commit):
return _api_commit_entity(commit.id)


def create_commit():
summary = _fixtures.create_benchmark_summary()
return summary.run.commit


class TestCommitGet(_asserts.GetEnforcer):
url = "/api/commits/{}/"
public = True

def _create(self):
return create_commit()

def test_get_commit(self, client):
self.authenticate(client)
commit = self._create()
response = client.get(f"/api/commits/{commit.id}/")
self.assert_200_ok(response, _expected_entity(commit))


class TestCommitList(_asserts.ListEnforcer):
url = "/api/commits/"
public = True

def _create(self):
return create_commit()

def test_commit_list(self, client):
self.authenticate(client)
commit = self._create()
response = client.get("/api/commits/")
self.assert_200_ok(response, contains=_expected_entity(commit))

0 comments on commit d7c84a3

Please sign in to comment.