-
Notifications
You must be signed in to change notification settings - Fork 10
feat: add Task Result API support with example and unit tests #156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TanyaSingh369-svg
wants to merge
3
commits into
next-1.0.0
Choose a base branch
from
feature/task-result-api
base: next-1.0.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import os | ||
|
|
||
| from pytfe import TFEClient | ||
|
|
||
|
|
||
| def main(): | ||
| token = os.getenv("TFE_TOKEN") | ||
| task_result_id = os.getenv("TFE_TASK_RESULT_ID") | ||
|
|
||
| if not token: | ||
| print("Set TFE_TOKEN") | ||
| return | ||
|
|
||
| if not task_result_id: | ||
| print("Set TFE_TASK_RESULT_ID") | ||
| return | ||
|
|
||
| client = TFEClient() | ||
|
|
||
| try: | ||
| result = client.task_results.read(task_result_id) | ||
|
|
||
| print("=== Task Result ===") | ||
| print(f"ID: {result.id}") | ||
| print(f"Status: {result.status}") | ||
| print(f"Message: {result.message}") | ||
| print(f"Task Name: {result.task_name}") | ||
| print(f"URL: {result.url}") | ||
|
|
||
| except Exception as e: | ||
| print(f"Error: {e}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # Copyright IBM Corp. 2025, 2026 | ||
| # SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from datetime import datetime | ||
| from enum import Enum | ||
|
|
||
| from pydantic import BaseModel, ConfigDict, Field | ||
|
|
||
| # Reuse, do NOT duplicate | ||
| from pytfe.models.task_stage import TaskStage | ||
|
|
||
|
|
||
| class TaskResultStatus(str, Enum): | ||
| passed = "passed" | ||
| failed = "failed" | ||
| pending = "pending" | ||
| running = "running" | ||
| unreachable = "unreachable" | ||
| errored = "errored" | ||
|
|
||
|
|
||
| class TaskEnforcementLevel(str, Enum): | ||
| advisory = "advisory" | ||
| mandatory = "mandatory" | ||
|
|
||
|
|
||
| class TaskResultStatusTimestamps(BaseModel): | ||
| model_config = ConfigDict(populate_by_name=True) | ||
|
|
||
| errored_at: datetime | None = Field(None, alias="errored-at") | ||
| running_at: datetime | None = Field(None, alias="running-at") | ||
| canceled_at: datetime | None = Field(None, alias="canceled-at") | ||
| failed_at: datetime | None = Field(None, alias="failed-at") | ||
| passed_at: datetime | None = Field(None, alias="passed-at") | ||
|
|
||
|
|
||
| class TaskResult(BaseModel): | ||
| model_config = ConfigDict(populate_by_name=True) | ||
|
|
||
| id: str | ||
|
|
||
| status: TaskResultStatus | None = Field(None, alias="status") | ||
| message: str | None = Field(None, alias="message") | ||
|
|
||
| status_timestamps: TaskResultStatusTimestamps | None = Field( | ||
| None, alias="status-timestamps" | ||
| ) | ||
|
|
||
| url: str | None = Field(None, alias="url") | ||
|
|
||
| created_at: datetime | None = Field(None, alias="created-at") | ||
| updated_at: datetime | None = Field(None, alias="updated-at") | ||
|
|
||
| task_id: str | None = Field(None, alias="task-id") | ||
| task_name: str | None = Field(None, alias="task-name") | ||
| task_url: str | None = Field(None, alias="task-url") | ||
|
|
||
| workspace_task_id: str | None = Field(None, alias="workspace-task-id") | ||
| workspace_task_enforcement_level: TaskEnforcementLevel | None = Field( | ||
| None, alias="workspace-task-enforcement-level" | ||
| ) | ||
|
|
||
| agent_pool_id: str | None = Field(None, alias="agent-pool-id") | ||
|
|
||
| # Relation (matches Go: *TaskStage) | ||
| task_stage: TaskStage | None = Field(None, alias="task-stage") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # Copyright IBM Corp. 2025, 2026 | ||
| # SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| from typing import Any | ||
|
|
||
| from pytfe.models.task_result import TaskResult | ||
| from pytfe.utils import valid_string_id | ||
|
|
||
| from ._base import _Service | ||
|
|
||
|
|
||
| class TaskResults(_Service): | ||
| def read(self, task_result_id: str) -> TaskResult: | ||
| if not valid_string_id(task_result_id): | ||
| raise ValueError("Invalid task_result_id") | ||
|
|
||
| path = f"/api/v2/task-results/{task_result_id}" | ||
|
|
||
| response = self.t.request("GET", path) | ||
| data = response.json() | ||
|
|
||
| if "data" not in data: | ||
| raise ValueError("Invalid response format") | ||
|
|
||
| return self._parse_task_result(data["data"]) | ||
|
|
||
| def _parse_task_result(self, data: dict[str, Any]) -> TaskResult: | ||
| attributes = data.get("attributes", {}) | ||
|
|
||
| attributes["id"] = data.get("id") | ||
|
|
||
| return TaskResult(**attributes) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| from unittest.mock import Mock | ||
|
|
||
| import pytest | ||
|
|
||
| from pytfe.models.task_result import TaskResult | ||
| from pytfe.resources.task_result import TaskResults | ||
|
|
||
|
|
||
| class TestTaskResults: | ||
| @pytest.fixture | ||
| def mock_transport(self): | ||
| return Mock() | ||
|
|
||
| @pytest.fixture | ||
| def service(self, mock_transport): | ||
| return TaskResults(mock_transport) | ||
|
|
||
| def test_read_success(self, service, mock_transport): | ||
| response = Mock() | ||
| response.json.return_value = { | ||
| "data": { | ||
| "id": "tr-123", | ||
| "attributes": { | ||
| "status": "passed", | ||
| "message": "ok", | ||
| "status-timestamps": {}, | ||
| "url": "url", | ||
| "created-at": "2024-01-01T00:00:00Z", | ||
| "updated-at": "2024-01-01T00:00:00Z", | ||
| "task-id": "t1", | ||
| "task-name": "name", | ||
| "task-url": "url", | ||
| "workspace-task-id": "wt1", | ||
| "workspace-task-enforcement-level": "advisory", | ||
| "agent-pool-id": None, | ||
| }, | ||
| } | ||
| } | ||
| mock_transport.request.return_value = response | ||
|
|
||
| result = service.read("tr-123") | ||
|
|
||
| assert isinstance(result, TaskResult) | ||
| assert result.id == "tr-123" | ||
| assert result.status == "passed" | ||
|
|
||
| def test_invalid_id(self, service): | ||
| with pytest.raises(ValueError): | ||
| service.read("") | ||
|
|
||
| def test_missing_data(self, service, mock_transport): | ||
| response = Mock() | ||
| response.json.return_value = {} | ||
|
|
||
| mock_transport.request.return_value = response | ||
|
|
||
| with pytest.raises(ValueError): | ||
| service.read("tr-123") | ||
|
|
||
| def test_missing_attributes(self, service, mock_transport): | ||
| response = Mock() | ||
| response.json.return_value = {"data": {"id": "tr-123"}} | ||
|
|
||
| mock_transport.request.return_value = response | ||
|
|
||
| result = service.read("tr-123") | ||
|
|
||
| assert result.id == "tr-123" | ||
|
|
||
| def test_optional_fields(self, service, mock_transport): | ||
| response = Mock() | ||
| response.json.return_value = { | ||
| "data": { | ||
| "id": "tr-123", | ||
| "attributes": { | ||
| "status": "passed", | ||
| "message": None, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| mock_transport.request.return_value = response | ||
|
|
||
| result = service.read("tr-123") | ||
|
|
||
| assert result.message is None | ||
|
|
||
| def test_status_enum(self, service, mock_transport): | ||
| response = Mock() | ||
| response.json.return_value = { | ||
| "data": { | ||
| "id": "tr-123", | ||
| "attributes": { | ||
| "status": "failed", | ||
| "message": "fail", | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| mock_transport.request.return_value = response | ||
|
|
||
| result = service.read("tr-123") | ||
|
|
||
| assert result.status == "failed" | ||
|
|
||
| def test_timestamps_parsing(self, service, mock_transport): | ||
| response = Mock() | ||
| response.json.return_value = { | ||
| "data": { | ||
| "id": "tr-123", | ||
| "attributes": { | ||
| "status": "passed", | ||
| "message": "ok", | ||
| "status-timestamps": {"passed-at": "2024-01-01T00:00:00Z"}, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| mock_transport.request.return_value = response | ||
|
|
||
| result = service.read("tr-123") | ||
|
|
||
| assert result.status_timestamps is not None |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
task stage relation was not mapped to the read output and the response shows None