Skip to content

Commit

Permalink
feat: Create "Flagsmith Flag" label in linked repo
Browse files Browse the repository at this point in the history
  • Loading branch information
novakzaballa committed Jun 4, 2024
1 parent d290f53 commit 2d5ce3e
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 105 deletions.
21 changes: 11 additions & 10 deletions api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -987,14 +987,20 @@ def flagsmith_environments_v2_table(dynamodb: DynamoDBServiceResource) -> Table:


@pytest.fixture()
def feature_external_resource(
feature: Feature, post_request_mock: MagicMock, mocker: MockerFixture
) -> FeatureExternalResource:
mocker.patch(
def mock_github_client_generate_token(mocker: MockerFixture) -> MagicMock:
return mocker.patch(
"integrations.github.client.generate_token",
return_value="mocked_token",
)


@pytest.fixture()
def feature_external_resource(
feature: Feature,
post_request_mock: MagicMock,
mocker: MockerFixture,
mock_github_client_generate_token: MagicMock,
) -> FeatureExternalResource:
return FeatureExternalResource.objects.create(
url="https://github.com/repositoryownertest/repositorynametest/issues/11",
type="GITHUB_ISSUE",
Expand All @@ -1007,13 +1013,8 @@ def feature_external_resource(
def feature_with_value_external_resource(
feature_with_value: Feature,
post_request_mock: MagicMock,
mocker: MockerFixture,
mock_github_client_generate_token: MagicMock,
) -> FeatureExternalResource:
mocker.patch(
"integrations.github.client.generate_token",
return_value="mocked_token",
)

return FeatureExternalResource.objects.create(
url="https://github.com/repositoryownertest/repositorynametest/issues/11",
type="GITHUB_ISSUE",
Expand Down
18 changes: 18 additions & 0 deletions api/integrations/github/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,21 @@ def fetch_github_repo_contributors(
]

return build_paginated_response(results, response)


# Create "Flagsmith Flag" label in linked repo
def create_flagsmith_flag_label(
installation_id: str, owner: str, repo: str
) -> dict[str, Any]:
url = f"{GITHUB_API_URL}repos/{owner}/{repo}/labels"
headers = build_request_headers(installation_id)
payload = {
"name": "Flagsmith Flag",
"color": "6633FF",
"description": "This GitHub Issue/PR is linked to a Flagsmith Feature Flag",
}
response = requests.post(
url, json=payload, headers=headers, timeout=GITHUB_API_CALLS_TIMEOUT
)
response.raise_for_status()
return response.json()
12 changes: 11 additions & 1 deletion api/integrations/github/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from integrations.github.client import (
ResourceType,
create_flagsmith_flag_label,
delete_github_installation,
fetch_github_repo_contributors,
fetch_github_repositories,
Expand Down Expand Up @@ -142,7 +143,16 @@ def get_queryset(self):
def create(self, request, *args, **kwargs):

try:
return super().create(request, *args, **kwargs)
response = super().create(request, *args, **kwargs)
github_configuration = GithubConfiguration.objects.get(
id=self.kwargs["github_pk"]
)
create_flagsmith_flag_label(
installation_id=github_configuration.installation_id,
owner=request.data.get("repository_owner"),
repo=request.data.get("repository_name"),
)
return response

except IntegrityError as e:
if re.search(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,9 @@ def test_create_feature_external_resource(
github_configuration: GithubConfiguration,
github_repository: GithubRepository,
post_request_mock: MagicMock,
mocker: MockerFixture,
mock_github_client_generate_token: MagicMock,
) -> None:
# Given
mocker.patch(
"integrations.github.client.generate_token",
return_value="mocked_token",
)

feature_external_resource_data = {
"type": "GITHUB_ISSUE",
"url": "https://github.com/repoowner/repo-name/issues/35",
Expand Down Expand Up @@ -305,7 +300,7 @@ def test_delete_feature_external_resource(
post_request_mock.assert_called_with(
"https://api.github.com/repos/repositoryownertest/repositorynametest/issues/11/comments",
json={
"body": "### The feature flag `Test Feature1` was unlinked from the issue/PR"
"body": "**The feature flag `Test Feature1` was unlinked from the issue/PR**"
},
headers={
"Accept": "application/vnd.github.v3+json",
Expand All @@ -328,12 +323,9 @@ def test_get_feature_external_resources(
github_configuration: GithubConfiguration,
github_repository: GithubRepository,
feature_external_resource: FeatureExternalResource,
mocker: MockerFixture,
mock_github_client_generate_token: MagicMock,
) -> None:
# Given
mocker.patch(
"integrations.github.client.generate_token",
)
url = reverse(
"api-v1:projects:feature-external-resources-list",
kwargs={"project_pk": project.id, "feature_pk": feature.id},
Expand Down Expand Up @@ -413,7 +405,7 @@ def test_create_github_comment_on_feature_state_updated(
).updated_at.strftime(get_format("DATETIME_INPUT_FORMATS")[0])

expected_body_comment = (
"Flagsmith Feature `Test Feature1` has been updated:\n"
"**Flagsmith Feature `Test Feature1` has been updated:**\n"
+ expected_default_body(
project.id,
environment.api_key,
Expand Down Expand Up @@ -447,14 +439,9 @@ def test_create_github_comment_on_feature_was_deleted(
github_repository: GithubRepository,
feature_external_resource: FeatureExternalResource,
post_request_mock: MagicMock,
mocker: MockerFixture,
mock_github_client_generate_token: MagicMock,
) -> None:
# Given
mocker.patch(
"integrations.github.client.generate_token",
return_value="mocked_token",
)

url = reverse(
viewname="api-v1:projects:project-features-detail",
kwargs={"project_pk": project.id, "pk": feature.id},
Expand All @@ -468,7 +455,7 @@ def test_create_github_comment_on_feature_was_deleted(

post_request_mock.assert_called_with(
"https://api.github.com/repos/repositoryownertest/repositorynametest/issues/11/comments",
json={"body": "### The Feature Flag `Test Feature1` was deleted"},
json={"body": "**The Feature Flag `Test Feature1` was deleted**"},
headers={
"Accept": "application/vnd.github.v3+json",
"X-GitHub-Api-Version": GITHUB_API_VERSION,
Expand All @@ -485,18 +472,13 @@ def test_create_github_comment_on_segment_override_updated(
github_configuration: GithubConfiguration,
github_repository: GithubRepository,
post_request_mock: MagicMock,
mocker: MockerFixture,
environment: Environment,
admin_client: APIClient,
feature_with_value_external_resource: FeatureExternalResource,
mock_github_client_generate_token: MagicMock,
) -> None:
# Given
feature_state = segment_override_for_feature_with_value
mocker.patch(
"integrations.github.client.generate_token",
return_value="mocked_token",
)

payload = dict(WritableNestedFeatureStateSerializer(instance=feature_state).data)

payload["enabled"] = not feature_state.enabled
Expand All @@ -516,7 +498,7 @@ def test_create_github_comment_on_segment_override_updated(
).updated_at.strftime(get_format("DATETIME_INPUT_FORMATS")[0])

expected_comment_body = (
"Flagsmith Feature `feature_with_value` has been updated:\n"
"**Flagsmith Feature `feature_with_value` has been updated:**\n"
+ "\n"
+ expected_segment_comment_body(
project.id,
Expand Down Expand Up @@ -548,16 +530,11 @@ def test_create_github_comment_on_segment_override_deleted(
github_configuration: GithubConfiguration,
github_repository: GithubRepository,
post_request_mock: MagicMock,
mocker: MockerFixture,
admin_client_new: APIClient,
feature_with_value_external_resource: FeatureExternalResource,
mock_github_client_generate_token: MagicMock,
) -> None:
# Given
mocker.patch(
"integrations.github.client.generate_token",
return_value="mocked_token",
)

url = reverse(
viewname="api-v1:features:feature-segment-detail",
kwargs={"pk": feature_with_value_segment.id},
Expand All @@ -573,7 +550,7 @@ def test_create_github_comment_on_segment_override_deleted(
post_request_mock.assert_called_with(
"https://api.github.com/repos/repositoryownertest/repositorynametest/issues/11/comments",
json={
"body": "### The Segment Override `segment` for Feature Flag `feature_with_value` was deleted"
"body": "**The Segment Override `segment` for Feature Flag `feature_with_value` was deleted**"
},
headers={
"Accept": "application/vnd.github.v3+json",
Expand Down Expand Up @@ -631,7 +608,7 @@ def test_create_github_comment_using_v2(
response_data["updated_at"], format
).strftime(get_format("DATETIME_INPUT_FORMATS")[0])
expected_comment_body = (
"Flagsmith Feature `Test Feature1` has been updated:\n"
"**Flagsmith Feature `Test Feature1` has been updated:**\n"
+ "\n"
+ expected_segment_comment_body(
project.id,
Expand Down Expand Up @@ -712,16 +689,11 @@ def test_create_feature_external_resource_on_environment_with_v2(
segment_override_for_feature_with_value: FeatureState,
environment_v2_versioning: Environment,
post_request_mock: MagicMock,
mocker: MockerFixture,
mock_github_client_generate_token: MagicMock,
) -> None:
# Given
feature_id = segment_override_for_feature_with_value.feature_id

mocker.patch(
"integrations.github.client.generate_token",
return_value="mocked_token",
)

feature_external_resource_data = {
"type": "GITHUB_ISSUE",
"url": "https://github.com/repoowner/repo-name/issues/35",
Expand Down
Loading

0 comments on commit 2d5ce3e

Please sign in to comment.