Skip to content

Commit

Permalink
Merge github.com:DSD-DBS/polarion-rest-api-client into add-documents
Browse files Browse the repository at this point in the history
  • Loading branch information
dahbar committed Jan 2, 2024
2 parents 17302c0 + 30a4b84 commit ff93c05
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 5 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build-test-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ name: Build
on:
push:
branches: ["*"]
pull_request: [master]
pull_request: [main]
tags: ["v*.*.*"]

jobs:
Expand All @@ -18,12 +18,12 @@ jobs:
matrix:
os: [ubuntu-latest]
python_version:
- "3.9"
- "3.10"
- "3.11"
- "3.12"
include:
- os: windows-latest
python_version: "3.9"
python_version: "3.12"
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{matrix.python_version}}
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ venv.bak/
.spyderproject
.spyproject

# VS Code settings
.vscode/

# Rope project settings
.ropeproject

Expand Down
65 changes: 65 additions & 0 deletions polarion_rest_api_client/data_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""Data model classes returned by the client."""
from __future__ import annotations

import base64
import dataclasses
import hashlib
import json
Expand Down Expand Up @@ -112,6 +113,70 @@ def __setattr__(self, key: str, value: t.Any):
else:
self.additional_attributes[key] = value

def __eq__(self, other: object) -> bool:
"""Compare only WorkItem attributes."""
if not isinstance(other, WorkItem):
return NotImplemented
if self.get_current_checksum() is None:
self.calculate_checksum()
if other.get_current_checksum() is None:
other.calculate_checksum()

return self.get_current_checksum() == other.get_current_checksum()

def to_dict(self) -> dict[str, t.Any]:
"""Return the content of the WorkItem as dictionary."""
sorted_links = sorted(
self.linked_work_items,
key=lambda x: f"{x.role}/{x.secondary_work_item_project}/{x.secondary_work_item_id}", # pylint: disable=line-too-long
)

sorted_attachments = sorted(
self.attachments, key=lambda x: x.file_name or ""
)

return {
"id": self.id,
"title": self.title,
"description_type": self.description_type,
"description": self.description,
"type": self.type,
"status": self.status,
"additional_attributes": dict(
sorted(self.additional_attributes.items())
),
"checksum": self._checksum,
"linked_work_items": [
dataclasses.asdict(lwi) for lwi in sorted_links
],
"attachments": [
dataclasses.asdict(at) for at in sorted_attachments
],
}

def calculate_checksum(self) -> str:
"""Calculate and return a checksum for this WorkItem.
In addition, the checksum will be written to self._checksum.
"""
data = self.to_dict()
del data["checksum"]
del data["id"]

for attachment in data["attachments"]:
try:
attachment["content_bytes"] = base64.b64encode(
attachment["content_bytes"]
).decode("utf8")
except TypeError:
pass

data = dict(sorted(data.items()))

converted = json.dumps(data).encode("utf8")
self._checksum = hashlib.sha256(converted).hexdigest()
return self._checksum


@dataclasses.dataclass
class WorkItemLink:
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dynamic = ["version"]
name = "polarion-rest-api-client"
description = "An API Client for the Polarion REST API"
readme = "README.md"
requires-python = ">=3.9, <3.12"
requires-python = ">=3.10, <3.13"
license = { text = "Apache-2.0" }
authors = [
{ name = "DB Netz AG" },
Expand All @@ -23,9 +23,9 @@ classifiers = [
"Natural Language :: English",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
]
dependencies = [
"httpx>=0.20.0,<0.25.0",
Expand Down

0 comments on commit ff93c05

Please sign in to comment.