Skip to content

Commit 1775434

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 294d43d commit 1775434

File tree

2 files changed

+63
-37
lines changed

2 files changed

+63
-37
lines changed

ogr/services/forgejo/commit_flag.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# Copyright Contributors to the Packit project.
22
# SPDX-License-Identifier: MIT
33

4+
from datetime import datetime
5+
from typing import Any, List
6+
47
import requests
58

6-
from datetime import datetime
7-
from typing import Any, List, Optional
89
from ogr.abstract import CommitFlag, CommitStatus
910

11+
1012
class ForgejoCommitFlag(CommitFlag):
1113
"""
1214
CommitFlag implementation for Forgejo.
@@ -18,17 +20,20 @@ def _state_from_str(cls, state: str) -> CommitStatus:
1820
state = state.lower()
1921
if state == "success":
2022
return CommitStatus.success
21-
elif state == "failure":
23+
if state == "failure":
2224
return CommitStatus.failure
23-
elif state == "pending":
25+
if state == "pending":
2426
return CommitStatus.pending
25-
else:
26-
raise ValueError(f"Unknown commit state from Forgejo: {state}")
27+
raise ValueError(f"Unknown commit state from Forgejo: {state}")
2728

2829
@classmethod
2930
def _validate_state(cls, state: CommitStatus) -> CommitStatus:
3031
# Validate that the provided state is acceptable for Forgejo.
31-
valid_states = {CommitStatus.success, CommitStatus.failure, CommitStatus.pending}
32+
valid_states = {
33+
CommitStatus.success,
34+
CommitStatus.failure,
35+
CommitStatus.pending,
36+
}
3237
if state in valid_states:
3338
return state
3439
raise ValueError(f"Invalid commit state for Forgejo: {state}")
@@ -62,7 +67,11 @@ def get(project: Any, commit: str) -> List["CommitFlag"]:
6267
response = requests.get(url, headers=headers)
6368
flags = []
6469
for raw_flag in response.json():
65-
flags.append(ForgejoCommitFlag(raw_commit_flag=raw_flag, project=project, commit=commit))
70+
flags.append(
71+
ForgejoCommitFlag(
72+
raw_commit_flag=raw_flag, project=project, commit=commit,
73+
),
74+
)
6675
return flags
6776

6877
@staticmethod
@@ -86,7 +95,9 @@ def set(
8695
}
8796
headers = project.get_auth_header()
8897
response = requests.post(url, json=payload, headers=headers)
89-
return ForgejoCommitFlag(raw_commit_flag=response.json(), project=project, commit=commit)
98+
return ForgejoCommitFlag(
99+
raw_commit_flag=response.json(), project=project, commit=commit,
100+
)
90101

91102
@property
92103
def created(self) -> datetime:
Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
# Copyright Contributors to the Packit project.
2+
# SPDX-License-Identifier: MIT
3+
14
import datetime
5+
from typing import Dict, List
6+
27
import responses
3-
import pytest
4-
from typing import Any, Dict, List, Optional
5-
import requests
68

7-
from ogr.abstract import CommitStatus, CommitFlag
9+
from ogr.abstract import CommitFlag, CommitStatus
810
from ogr.services.forgejo.commit_flag import ForgejoCommitFlag
911

12+
1013
class MockProject:
1114
forge_api_url = "http://dummy-forgejo/api/v1"
1215
owner = "dummy_owner"
@@ -15,28 +18,31 @@ class MockProject:
1518
def get_auth_header(self) -> Dict[str, str]:
1619
return {"Authorization": "Bearer dummy_token"}
1720

21+
1822
@responses.activate
1923
def test_get_commit_flag_integration():
2024
project = MockProject()
2125
commit = "abcdef123456"
2226
url = f"{project.forge_api_url}/repos/{project.owner}/{project.repo}/commits/{commit}/statuses"
23-
27+
2428
# Dummy response data simulating Forgejo API output.
25-
dummy_response = [{
26-
"commit": commit,
27-
"state": "success",
28-
"context": "CI",
29-
"comment": "All tests passed",
30-
"id": "123",
31-
"url": "http://dummy-forgejo/commit/abcdef123456/status",
32-
"created": "2023-01-01T12:00:00Z",
33-
"updated": "2023-01-01T12:30:00Z"
34-
}]
29+
dummy_response = [
30+
{
31+
"commit": commit,
32+
"state": "success",
33+
"context": "CI",
34+
"comment": "All tests passed",
35+
"id": "123",
36+
"url": "http://dummy-forgejo/commit/abcdef123456/status",
37+
"created": "2023-01-01T12:00:00Z",
38+
"updated": "2023-01-01T12:30:00Z",
39+
},
40+
]
3541
responses.add(responses.GET, url, json=dummy_response, status=200)
36-
42+
3743
# Call the method under test.
3844
flags: List[CommitFlag] = ForgejoCommitFlag.get(project, commit)
39-
45+
4046
# Assertions using CommitStatus from packit.ogr.abstract.
4147
assert len(flags) == 1
4248
flag = flags[0]
@@ -45,18 +51,23 @@ def test_get_commit_flag_integration():
4551
assert flag.context == "CI"
4652
assert flag.comment == "All tests passed"
4753
assert flag.uid == "123"
48-
49-
expected_created = datetime.datetime.strptime("2023-01-01T12:00:00Z", "%Y-%m-%dT%H:%M:%SZ")
50-
expected_updated = datetime.datetime.strptime("2023-01-01T12:30:00Z", "%Y-%m-%dT%H:%M:%SZ")
54+
55+
expected_created = datetime.datetime.strptime(
56+
"2023-01-01T12:00:00Z", "%Y-%m-%dT%H:%M:%SZ",
57+
)
58+
expected_updated = datetime.datetime.strptime(
59+
"2023-01-01T12:30:00Z", "%Y-%m-%dT%H:%M:%SZ",
60+
)
5161
assert flag.created == expected_created
5262
assert flag.edited == expected_updated
5363

64+
5465
@responses.activate
5566
def test_set_commit_flag_integration():
5667
project = MockProject()
5768
commit = "abcdef123456"
5869
url = f"{project.forge_api_url}/repos/{project.owner}/{project.repo}/commits/{commit}/statuses"
59-
70+
6071
# Dummy response for setting a commit status.
6172
dummy_response = {
6273
"commit": commit,
@@ -66,28 +77,32 @@ def test_set_commit_flag_integration():
6677
"id": "456",
6778
"url": "http://dummy-forgejo/commit/abcdef123456/status",
6879
"created": "2023-02-01T12:00:00Z",
69-
"updated": "2023-02-01T12:30:00Z"
80+
"updated": "2023-02-01T12:30:00Z",
7081
}
7182
responses.add(responses.POST, url, json=dummy_response, status=200)
72-
83+
7384
# Call the set method to create a new commit flag.
7485
flag = ForgejoCommitFlag.set(
7586
project=project,
7687
commit=commit,
7788
state=CommitStatus.success,
7889
target_url="http://dummy-target",
7990
description="Build succeeded",
80-
context="CI"
91+
context="CI",
8192
)
82-
93+
8394
# Assertions to verify correct mapping using CommitStatus.
8495
assert flag.commit == commit
8596
assert flag.state == CommitStatus.success
8697
assert flag.context == "CI"
8798
assert flag.comment == "Build succeeded"
8899
assert flag.uid == "456"
89-
90-
expected_created = datetime.datetime.strptime("2023-02-01T12:00:00Z", "%Y-%m-%dT%H:%M:%SZ")
91-
expected_updated = datetime.datetime.strptime("2023-02-01T12:30:00Z", "%Y-%m-%dT%H:%M:%SZ")
100+
101+
expected_created = datetime.datetime.strptime(
102+
"2023-02-01T12:00:00Z", "%Y-%m-%dT%H:%M:%SZ",
103+
)
104+
expected_updated = datetime.datetime.strptime(
105+
"2023-02-01T12:30:00Z", "%Y-%m-%dT%H:%M:%SZ",
106+
)
92107
assert flag.created == expected_created
93108
assert flag.edited == expected_updated

0 commit comments

Comments
 (0)