Skip to content

Commit cda46e7

Browse files
committed
Fix: Unicode encoding and logging errors in lftools
1. Fix UnicodeEncodeError in Gerrit/Nexus API requests: - Encode JSON string data as UTF-8 in RestApi._request() - Change Nexus encoding from latin-1 to utf-8 (5 locations) - Handles Unicode characters in responses (e.g., user names with 'š') - Fixes: 'latin-1' codec can't encode character '\u0161' 2. Fix logging format errors (3 locations): - GitHub create-repo: Use f-string instead of comma in log.info() - GitHub votes: Use f-string for approval list logging - Gerrit vote_on_change: Use f-string with parameters - Fixes: TypeError: not all arguments converted during string formatting Based on: modeseven-lfit/lftools-uv#53 Tested with: lftools gerrit addgithubrights git.opendaylight.org gnmi Change-Id: Ief4a4ffa9bcc1d0a31342ac49b448fd097dda879 Signed-off-by: Anil Belur <[email protected]>
1 parent 9909973 commit cda46e7

File tree

5 files changed

+27
-8
lines changed

5 files changed

+27
-8
lines changed

lftools/api/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ def _request(
4848
self, url: str, method: str, data: Optional[Any] = None, timeout: int = 30
4949
) -> requests.Response | Tuple[requests.Response, Optional[Dict[str, Any] | str]]:
5050
"""Execute the request."""
51+
# Encode string data as UTF-8 to handle Unicode characters
52+
if isinstance(data, str):
53+
data = data.encode("utf-8")
54+
5155
resp: requests.Response = self.r.request(method, self.endpoint + url, data=data, timeout=timeout)
5256

5357
# Some massaging to make our gerrit python code work

lftools/api/endpoints/gerrit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def vote_on_change(self, fqdn, gerrit_project, changeid, **kwargs):
171171
172172
POST /changes/{change-id}/revisions/{revision-id}/review
173173
"""
174-
log.info(fqdn, gerrit_project, changeid)
174+
log.info(f"Voting on change: fqdn={fqdn}, project={gerrit_project}, changeid={changeid}")
175175
access_str = "changes/{}/revisions/2/review".format(changeid)
176176
headers = {"Content-Type": "application/json; charset=UTF-8"}
177177
self.r.headers.update(headers)

lftools/cli/github_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def submit_pr(ctx, organization, repo, pr):
6868
def votes(ctx, organization, repo, pr):
6969
"""Helper for votes."""
7070
approval_list = prvotes(organization, repo, pr)
71-
log.info("Approvals:", approval_list)
71+
log.info(f"Approvals: {approval_list}")
7272

7373

7474
@click.command(name="list")
@@ -109,7 +109,7 @@ def createrepo(ctx, organization, repository, description, has_issues, has_proje
109109
has_issues = has_issues or False
110110
has_wiki = has_wiki or False
111111
has_projects = has_projects or False
112-
log.info("Creating repo under organization: ", orgName)
112+
log.info(f"Creating repo under organization: {orgName}")
113113
try:
114114
org = g.get_organization(orgName)
115115
except GithubException as ghe:

lftools/nexus/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def create_target(self, name, patterns):
9696
}
9797
}
9898

99-
json_data = json.dumps(target).encode(encoding="latin-1")
99+
json_data = json.dumps(target).encode(encoding="utf-8")
100100

101101
r = requests.post(url, auth=self.auth, headers=self.headers, data=json_data)
102102

@@ -148,7 +148,7 @@ def create_priv(self, name, target_id, priv):
148148
}
149149
}
150150

151-
json_data = json.dumps(privileges).encode(encoding="latin-1")
151+
json_data = json.dumps(privileges).encode(encoding="utf-8")
152152
r = requests.post(url, auth=self.auth, headers=self.headers, data=json_data)
153153
privileges = r.json()
154154

@@ -188,7 +188,7 @@ def create_role(self, name, privs, role_id="", description="", roles=[]):
188188
}
189189
}
190190

191-
json_data = json.dumps(role).encode(encoding="latin-1")
191+
json_data = json.dumps(role).encode(encoding="utf-8")
192192
log.debug("Sending role {} to Nexus".format(json_data))
193193

194194
r = requests.post(url, auth=self.auth, headers=self.headers, data=json_data)
@@ -243,7 +243,7 @@ def create_user(self, name, domain, role_id, password, extra_roles=[]):
243243
for role in extra_roles:
244244
user["data"]["roles"].append(self.get_role(role))
245245

246-
json_data = json.dumps(user).encode(encoding="latin-1")
246+
json_data = json.dumps(user).encode(encoding="utf-8")
247247

248248
user = requests.post(url, auth=self.auth, headers=self.headers, data=json_data)
249249

@@ -274,7 +274,7 @@ def update_repo_group_details(self, repoId, data):
274274

275275
repo = {"data": data}
276276

277-
json_data = json.dumps(repo).encode(encoding="latin-1")
277+
json_data = json.dumps(repo).encode(encoding="utf-8")
278278

279279
requests.put(url, auth=self.auth, headers=self.headers, data=json_data)
280280

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
fixes:
3+
- |
4+
Fix UnicodeEncodeError in Gerrit API requests when responses contain
5+
Unicode characters. JSON string data is now properly encoded as UTF-8
6+
in the RestApi._request() method, preventing 'latin-1' codec errors
7+
when user names or group names contain special characters (e.g., 'š').
8+
9+
This fixes failures in commands like:
10+
lftools gerrit addgithubrights <fqdn> <project>
11+
12+
- |
13+
Fix logging TypeError in GitHub create-repo command. Changed from
14+
using comma-separated arguments (which caused format string errors)
15+
to using f-string formatting for proper message construction.

0 commit comments

Comments
 (0)