|
7 | 7 | import json
|
8 | 8 | from typing import Optional
|
9 | 9 |
|
| 10 | +import requests |
| 11 | + |
10 | 12 | from .webrequests import Session
|
| 13 | +from . import utils |
11 | 14 |
|
12 | 15 | logger = logging.getLogger(__name__)
|
13 | 16 |
|
14 | 17 | BASE_URL = "https://bugzilla.yoctoproject.org"
|
15 | 18 | REST_BASE_URL = f"{BASE_URL}/rest/"
|
16 | 19 | ISSUE_URL = f"{BASE_URL}/show_bug.cgi?id="
|
17 | 20 |
|
| 21 | +TOKENFILE = utils.DATADIR / 'bugzilla_token' |
| 22 | + |
18 | 23 |
|
19 | 24 | class Bugzilla:
|
20 | 25 | """Bugzilla server interaction class."""
|
@@ -97,13 +102,48 @@ def get_bug_title(cls, bugid: int) -> Optional[str]:
|
97 | 102 |
|
98 | 103 | return jsondata[0]['summary']
|
99 | 104 |
|
| 105 | + @classmethod |
| 106 | + def login(cls, user: str, password: str) -> bool: |
| 107 | + """Login to bugzilla REST API.""" |
| 108 | + session = Session() |
| 109 | + |
| 110 | + logger.info("Sending logging request...") |
| 111 | + params = { |
| 112 | + 'login': user, |
| 113 | + 'password': password, |
| 114 | + } |
| 115 | + |
| 116 | + fparams = urllib.parse.urlencode(params) |
| 117 | + req = f"{REST_BASE_URL}login?{fparams}" |
| 118 | + |
| 119 | + try: |
| 120 | + data = session.get(req, 0) |
| 121 | + except requests.exceptions.HTTPError: |
| 122 | + logger.error("Login failed") |
| 123 | + return False |
| 124 | + |
| 125 | + token = json.loads(data)['token'] |
| 126 | + logger.info("Logging success") |
| 127 | + |
| 128 | + with TOKENFILE.open('w') as file: |
| 129 | + file.write(token) |
| 130 | + |
| 131 | + return True |
| 132 | + |
100 | 133 | @classmethod
|
101 | 134 | def add_bug_comment(cls, bugid: int, comment: str):
|
102 | 135 | """Publish a new comment to a bugzilla issue."""
|
103 |
| - bugurl = cls.get_bug_url(bugid) |
| 136 | + with TOKENFILE.open('r') as file: |
| 137 | + token = file.read() |
| 138 | + |
| 139 | + data = { |
| 140 | + 'token': token, |
| 141 | + 'comment': comment, |
| 142 | + } |
104 | 143 |
|
105 |
| - # TODO: remove and publish using REST API |
106 |
| - print(f"\nPlease update {bugurl} ticket id with:\n" |
107 |
| - f"{'-'*40}\n" |
108 |
| - f"{comment}\n" |
109 |
| - f"{'-'*40}\n") |
| 144 | + url = f"{REST_BASE_URL}bug/{bugid}/comment" |
| 145 | + try: |
| 146 | + Session().post(url, data) |
| 147 | + except requests.exceptions.HTTPError: |
| 148 | + logging.error("Failed to post comment on Bugzilla, please login") |
| 149 | + raise |
0 commit comments