Coverage for src/couchers/servicers/bugs.py: 97%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import grpc
2import requests
4from couchers.config import config
5from couchers.db import session_scope
6from couchers.models import User
7from couchers.sql import couchers_select as select
8from proto import bugs_pb2, bugs_pb2_grpc
11class Bugs(bugs_pb2_grpc.BugsServicer):
12 def _version(self):
13 return config["VERSION"]
15 def Version(self, request, context):
16 return bugs_pb2.VersionInfo(version=self._version())
18 def ReportBug(self, request, context):
19 if not config["BUG_TOOL_ENABLED"]:
20 context.abort(grpc.StatusCode.UNAVAILABLE, "Bug tool disabled")
22 repo = config["BUG_TOOL_GITHUB_REPO"]
23 auth = (config["BUG_TOOL_GITHUB_USERNAME"], config["BUG_TOOL_GITHUB_TOKEN"])
25 if context.user_id:
26 with session_scope() as session:
27 username = session.execute(select(User.username).where(User.id == context.user_id)).scalar_one()
28 user_details = f"{username} ({context.user_id})"
29 else:
30 user_details = "<not logged in>"
32 issue_title = request.subject
33 issue_body = (
34 f"Subject: {request.subject}\n"
35 f"Description:\n"
36 f"{request.description}\n"
37 f"\n"
38 f"Results:\n"
39 f"{request.results}\n"
40 f"\n"
41 f"Backend version: {self._version()}\n"
42 f"Frontend version: {request.frontend_version}\n"
43 f"User Agent: {request.user_agent}\n"
44 f"Page: {request.page}\n"
45 f"User: {user_details}"
46 )
47 issue_labels = ["bug tool"]
49 json_body = {"title": issue_title, "body": issue_body, "labels": issue_labels}
51 r = requests.post(f"https://api.github.com/repos/{repo}/issues", auth=auth, json=json_body)
52 if not r.status_code == 201:
53 context.abort(grpc.StatusCode.INTERNAL, "Request failed")
55 issue_number = r.json()["number"]
57 return bugs_pb2.ReportBugRes(
58 bug_id=f"#{issue_number}", bug_url=f"https://github.com/{repo}/issues/{issue_number}"
59 )