Skip to content
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

feat: Add automatic tagging for github integration #4028

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

novakzaballa
Copy link
Contributor

@novakzaballa novakzaballa commented May 27, 2024

Thanks for submitting a PR! Please check the boxes below:

  • I have run pre-commit to check linting
  • I have added information to docs/ if required so people know about the feature!
  • I have filled in the "Changes" section below?
  • I have filled in the "How did you test this code" section below?
  • I have used a Conventional Commit title for this Pull Request

Changes

  • When a project is lilnked to a GitHub repository, the following tags are added to the project:
    The tags added are:
    PR Open
    PR Merged
    PR Closed
    PR Draft
    Issue Open
    Issue Closed

  • Add automatic tagging of features linked to GitHub resources. The tags are added on response to events in Flagsmith, like linking/unlinking PR/Issues, and also in response to events in GitHub like closing/reopening/marking as draft/merging PR/Issues. Each feature can have only one active PR tag and one active Issue tag at any time.

How did you test this code?

Unit tests added.

For local manual testing, the corresponding FE PR is #4035
It would be necessary to configure a local dev environment with these two branches (BE/FE) for testing until this BE PR is merged.

NOTE

For the case of multiple PRs/Issues linked to the same feature, we should consider tagging the feature as closed/merged only when all the linked Issues/PRs are closed/merged correspondingly.

Copy link

vercel bot commented May 27, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

3 Ignored Deployments
Name Status Preview Comments Updated (UTC)
docs ⬜️ Ignored (Inspect) Visit Preview Jun 7, 2024 1:46pm
flagsmith-frontend-preview ⬜️ Ignored (Inspect) Visit Preview Jun 7, 2024 1:46pm
flagsmith-frontend-staging ⬜️ Ignored (Inspect) Visit Preview Jun 7, 2024 1:46pm

@github-actions github-actions bot added the api Issue related to the REST API label May 27, 2024
@novakzaballa novakzaballa changed the title feat: Add automatic tagging for github integration feat: Add automatic tagging for github integration - WIP May 27, 2024
Copy link
Contributor

github-actions bot commented May 27, 2024

Uffizzi Ephemeral Environment deployment-52275

☁️ https://app.uffizzi.com/github.com/Flagsmith/flagsmith/pull/4028

📄 View Application Logs etc.

What is Uffizzi? Learn more!

Copy link

codecov bot commented May 27, 2024

Codecov Report

Attention: Patch coverage is 98.01325% with 3 lines in your changes missing coverage. Please review.

Project coverage is 96.44%. Comparing base (9e2ffd2) to head (2d5ce3e).
Report is 62 commits behind head on main.

Current head 2d5ce3e differs from pull request most recent head 6c3c76c

Please upload reports for the commit 6c3c76c to get more accurate results.

Files Patch % Lines
api/features/permissions.py 70.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4028      +/-   ##
==========================================
+ Coverage   96.42%   96.44%   +0.02%     
==========================================
  Files        1146     1150       +4     
  Lines       37386    37691     +305     
==========================================
+ Hits        36048    36351     +303     
- Misses       1338     1340       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@zachaysan zachaysan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of comments but nothing I don't trust that you'll handle.

api/conftest.py Outdated
Comment on lines 973 to 975
from tests.unit.features.test_unit_feature_external_resources_views import (
mocked_requests_post,
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think mocked_requests_post should be converted into a fixture and added to a conftest.py instead of importing it like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converted into a fixture in conftest.py

api/conftest.py Outdated
)
mock_generate_token.return_value = "mocked_token"

mocker.patch("requests.post", side_effect=mocked_requests_post)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is mocked_requests_post coming from?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converted into a fixture in conftest

Comment on lines +31 to +45
tag_by_event_type = {
"pull_request": {
"closed": GitHubTag.PR_CLOSED.value,
"converted_to_draft": GitHubTag.PR_DRAFT.value,
"opened": GitHubTag.PR_OPEN.value,
"reopened": GitHubTag.PR_OPEN.value,
"ready_for_review": GitHubTag.PR_OPEN.value,
"merged": GitHubTag.PR_MERGED.value,
},
"issues": {
"closed": GitHubTag.ISSUE_CLOSED.value,
"opened": GitHubTag.ISSUE_OPEN.value,
"reopened": GitHubTag.ISSUE_OPEN.value,
},
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section should probably live in the constants.py module.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was tempted to put it in constants, however, it would lose context and require extra comments since it's used only in the context of the github.py file. i.e. the event_type in its name refers to a webhook request event type.

Comment on lines 48 to 50
def handle_installation_deleted(payload: dict[str, typing.Any]) -> None:
installation_id = payload.get("installation", {}).get("id")
if installation_id:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want to fail without an exception if installation_id is None?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! At least initially we want an unhandled exception in case of these unexpected cases. GitHub should never send an "installation.delete" event without an installation ID, if that happens we want the alerts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines +70 to +60
if feature:
if (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would return early here.

if not feature:
    return
if ( #....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, it's the same, right? I will keep this since it is a positive condition, it is returning early anyway, and the code is readable in this way.

from django.db import migrations


class Migration(migrations.Migration):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to combine this migration with the other one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. Done.

Comment on lines +95 to +101
from integrations.github.constants import (
GitHubTag,
github_tag_description,
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you need to inline import these constants? Surely they would be topline imports since there should be no cyclical import issues.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I have a circular import issue otherwise.

Comment on lines 338 to 339

print(feature_external_resource)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want to remove this print statement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

url = reverse("api-v1:github-webhook")

# When
client = APIClient()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the api_client fixture instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 1056 to 1061
# def test_handle_installation_deleted_with_non_existing_installation(mocker: MockerFixture):
# payload = {"installation": {"id": 456}}
# handle_installation_deleted(payload)
# mock_logger.error.assert_called_once_with(
# "Github Configuration with installation_id 456 does not exist"
# )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra commented out code. Should be deleted, yes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. Thanks! Deleted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Issue related to the REST API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants