Skip to content

Commit 133f6c5

Browse files
implement support for forgejo commit statuses
Signed-off-by: Olamidepeterojo <[email protected]>
1 parent 271bba4 commit 133f6c5

File tree

3 files changed

+55
-30
lines changed

3 files changed

+55
-30
lines changed

ogr/services/forgejo/__init__.py

Lines changed: 2 additions & 0 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 ogr.services.forgejo.commit_flag import ForgejoCommitFlag
45
from ogr.services.forgejo.issue import ForgejoIssue
56
from ogr.services.forgejo.project import ForgejoProject
67
from ogr.services.forgejo.pull_request import ForgejoPullRequest
78
from ogr.services.forgejo.service import ForgejoService
89

910
__all__ = [
11+
ForgejoCommitFlag.__name__,
1012
ForgejoPullRequest.__name__,
1113
ForgejoIssue.__name__,
1214
ForgejoProject.__name__,

tests/integration/forgejo/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,11 @@ def service():
1515
instance_url="https://v10.next.forgejo.org",
1616
api_key=api_key,
1717
)
18+
19+
20+
@pytest.fixture
21+
def project(service):
22+
return service.get_project(
23+
repo="ogr",
24+
namespace="packit-service",
25+
)

tests/integration/forgejo/test_commit_flag.py

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,46 @@
22
# SPDX-License-Identifier: MIT
33

44
import datetime
5+
56
import responses
6-
import pytest
7-
from typing import Any, Dict, List, Optional
8-
import requests
97

10-
from ogr.abstract import CommitStatus, CommitFlag
8+
from ogr.abstract import CommitFlag, CommitStatus
119
from ogr.services.forgejo.commit_flag import ForgejoCommitFlag
1210

11+
1312
class MockProject:
1413
forge_api_url = "http://dummy-forgejo/api/v1"
1514
owner = "dummy_owner"
1615
repo = "dummy_repo"
1716

18-
def get_auth_header(self) -> Dict[str, str]:
17+
def get_auth_header(self) -> dict[str, str]:
1918
return {"Authorization": "Bearer dummy_token"}
2019

20+
2121
@responses.activate
2222
def test_get_commit_flag_integration():
2323
project = MockProject()
2424
commit = "abcdef123456"
2525
url = f"{project.forge_api_url}/repos/{project.owner}/{project.repo}/commits/{commit}/statuses"
26-
26+
2727
# Dummy response data simulating Forgejo API output.
28-
dummy_response = [{
29-
"commit": commit,
30-
"state": "success",
31-
"context": "CI",
32-
"comment": "All tests passed",
33-
"id": "123",
34-
"url": "http://dummy-forgejo/commit/abcdef123456/status",
35-
"created": "2023-01-01T12:00:00Z",
36-
"updated": "2023-01-01T12:30:00Z"
37-
}]
28+
dummy_response = [
29+
{
30+
"commit": commit,
31+
"state": "success",
32+
"context": "CI",
33+
"comment": "All tests passed",
34+
"id": "123",
35+
"url": "http://dummy-forgejo/commit/abcdef123456/status",
36+
"created": "2023-01-01T12:00:00Z",
37+
"updated": "2023-01-01T12:30:00Z",
38+
},
39+
]
3840
responses.add(responses.GET, url, json=dummy_response, status=200)
39-
41+
4042
# Call the method under test.
41-
flags: List[CommitFlag] = ForgejoCommitFlag.get(project, commit)
42-
43+
flags: list[CommitFlag] = ForgejoCommitFlag.get(project, commit)
44+
4345
# Assertions using CommitStatus from packit.ogr.abstract.
4446
assert len(flags) == 1
4547
flag = flags[0]
@@ -48,18 +50,25 @@ def test_get_commit_flag_integration():
4850
assert flag.context == "CI"
4951
assert flag.comment == "All tests passed"
5052
assert flag.uid == "123"
51-
52-
expected_created = datetime.datetime.strptime("2023-01-01T12:00:00Z", "%Y-%m-%dT%H:%M:%SZ")
53-
expected_updated = datetime.datetime.strptime("2023-01-01T12:30:00Z", "%Y-%m-%dT%H:%M:%SZ")
53+
54+
expected_created = datetime.datetime.strptime(
55+
"2023-01-01T12:00:00Z",
56+
"%Y-%m-%dT%H:%M:%SZ",
57+
)
58+
expected_updated = datetime.datetime.strptime(
59+
"2023-01-01T12:30:00Z",
60+
"%Y-%m-%dT%H:%M:%SZ",
61+
)
5462
assert flag.created == expected_created
5563
assert flag.edited == expected_updated
5664

65+
5766
@responses.activate
5867
def test_set_commit_flag_integration():
5968
project = MockProject()
6069
commit = "abcdef123456"
6170
url = f"{project.forge_api_url}/repos/{project.owner}/{project.repo}/commits/{commit}/statuses"
62-
71+
6372
# Dummy response for setting a commit status.
6473
dummy_response = {
6574
"commit": commit,
@@ -69,28 +78,34 @@ def test_set_commit_flag_integration():
6978
"id": "456",
7079
"url": "http://dummy-forgejo/commit/abcdef123456/status",
7180
"created": "2023-02-01T12:00:00Z",
72-
"updated": "2023-02-01T12:30:00Z"
81+
"updated": "2023-02-01T12:30:00Z",
7382
}
7483
responses.add(responses.POST, url, json=dummy_response, status=200)
75-
84+
7685
# Call the set method to create a new commit flag.
7786
flag = ForgejoCommitFlag.set(
7887
project=project,
7988
commit=commit,
8089
state=CommitStatus.success,
8190
target_url="http://dummy-target",
8291
description="Build succeeded",
83-
context="CI"
92+
context="CI",
8493
)
85-
94+
8695
# Assertions to verify correct mapping using CommitStatus.
8796
assert flag.commit == commit
8897
assert flag.state == CommitStatus.success
8998
assert flag.context == "CI"
9099
assert flag.comment == "Build succeeded"
91100
assert flag.uid == "456"
92-
93-
expected_created = datetime.datetime.strptime("2023-02-01T12:00:00Z", "%Y-%m-%dT%H:%M:%SZ")
94-
expected_updated = datetime.datetime.strptime("2023-02-01T12:30:00Z", "%Y-%m-%dT%H:%M:%SZ")
101+
102+
expected_created = datetime.datetime.strptime(
103+
"2023-02-01T12:00:00Z",
104+
"%Y-%m-%dT%H:%M:%SZ",
105+
)
106+
expected_updated = datetime.datetime.strptime(
107+
"2023-02-01T12:30:00Z",
108+
"%Y-%m-%dT%H:%M:%SZ",
109+
)
95110
assert flag.created == expected_created
96111
assert flag.edited == expected_updated

0 commit comments

Comments
 (0)