Skip to content

Commit 98fd0c8

Browse files
committed
bugzilla: Post comment using REST API
Signed-off-by: Mathieu Dubois-Briand <[email protected]>
1 parent c0d9dce commit 98fd0c8

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

swattool/bugzilla.py

+46-6
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@
77
import json
88
from typing import Optional
99

10+
import requests
11+
1012
from .webrequests import Session
13+
from . import utils
1114

1215
logger = logging.getLogger(__name__)
1316

1417
BASE_URL = "https://bugzilla.yoctoproject.org"
1518
REST_BASE_URL = f"{BASE_URL}/rest/"
1619
ISSUE_URL = f"{BASE_URL}/show_bug.cgi?id="
1720

21+
TOKENFILE = utils.DATADIR / 'bugzilla_token'
22+
1823

1924
class Bugzilla:
2025
"""Bugzilla server interaction class."""
@@ -97,13 +102,48 @@ def get_bug_title(cls, bugid: int) -> Optional[str]:
97102

98103
return jsondata[0]['summary']
99104

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+
100133
@classmethod
101134
def add_bug_comment(cls, bugid: int, comment: str):
102135
"""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+
}
104143

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

swattool/main.py

+8
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ def login(user: str, password: str):
107107
swatbotrest.login(user, password)
108108

109109

110+
@maingroup.command()
111+
@click.option('--user', '-u', prompt=True)
112+
@click.option('--password', '-p', prompt=True, hide_input=True)
113+
def bugzilla_login(user: str, password: str):
114+
"""Login to Yocto Project Bugzilla."""
115+
Bugzilla.login(user, password)
116+
117+
110118
failures_list_options = [
111119
click.option('--limit', '-l', type=click.INT, default=None,
112120
help="Only parse the n last failures"),

0 commit comments

Comments
 (0)