-
Notifications
You must be signed in to change notification settings - Fork 10
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 functions to get/set apps to commits #469
Merged
giovanni-guidini
merged 2 commits into
main
from
gio/pin-commits/get-set-apps-for-commits
May 28, 2024
Merged
Changes from all commits
Commits
Show all changes
2 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 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 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,71 @@ | ||
from unittest.mock import MagicMock | ||
|
||
import pytest | ||
from redis import RedisError | ||
|
||
from database.models.core import GithubAppInstallation, Owner | ||
from database.tests.factories.core import CommitFactory | ||
from services.github import get_github_app_for_commit, set_github_app_for_commit | ||
|
||
|
||
class TestGetSetGithubAppsToCommits(object): | ||
def _get_commit(self, dbsession): | ||
commit = CommitFactory(repository__owner__service="github") | ||
dbsession.add(commit) | ||
dbsession.flush() | ||
return commit | ||
|
||
def _get_app(self, owner: Owner, dbsession): | ||
app = GithubAppInstallation( | ||
owner=owner, installation_id=1250, app_id=250, pem_path="some_path" | ||
) | ||
dbsession.add(app) | ||
dbsession.flush() | ||
return app | ||
|
||
@pytest.fixture | ||
def mock_redis(self, mocker): | ||
fake_redis = MagicMock(name="fake_redis") | ||
mock_conn = mocker.patch("services.github.get_redis_connection") | ||
mock_conn.return_value = fake_redis | ||
return fake_redis | ||
|
||
def test_set_app_for_commit_no_app(self, mock_redis, dbsession): | ||
commit = self._get_commit(dbsession) | ||
assert set_github_app_for_commit(None, commit) == False | ||
mock_redis.set.assert_not_called() | ||
|
||
def test_set_app_for_commit_redis_success(self, mock_redis, dbsession): | ||
commit = self._get_commit(dbsession) | ||
app = self._get_app(commit.repository.owner, dbsession) | ||
assert set_github_app_for_commit(app.id, commit) == True | ||
mock_redis.set.assert_called_with( | ||
f"app_to_use_for_commit_{commit.id}", str(app.id), ex=(60 * 60 * 2) | ||
) | ||
|
||
def test_set_app_for_commit_redis_error(self, mock_redis, dbsession): | ||
commit = self._get_commit(dbsession) | ||
mock_redis.set.side_effect = RedisError | ||
assert set_github_app_for_commit("1000", commit) == False | ||
mock_redis.set.assert_called_with( | ||
f"app_to_use_for_commit_{commit.id}", "1000", ex=(60 * 60 * 2) | ||
) | ||
|
||
def test_get_app_for_commit(self, mock_redis): | ||
redis_keys = { | ||
"app_to_use_for_commit_12": "1200", | ||
"app_to_use_for_commit_10": "1000", | ||
} | ||
fake_commit_12 = MagicMock(name="fake_commit", **{"id": 12}) | ||
fake_commit_10 = MagicMock(name="fake_commit", **{"id": 10}) | ||
fake_commit_50 = MagicMock(name="fake_commit", **{"id": 50}) | ||
mock_redis.get.side_effect = lambda key: redis_keys.get(key) | ||
assert get_github_app_for_commit(fake_commit_12) == "1200" | ||
assert get_github_app_for_commit(fake_commit_10) == "1000" | ||
assert get_github_app_for_commit(fake_commit_50) == None | ||
|
||
def test_get_app_for_commit_error(self, mock_redis): | ||
mock_redis.get.side_effect = RedisError | ||
fake_commit_12 = MagicMock(name="fake_commit", **{"id": 12}) | ||
assert get_github_app_for_commit(fake_commit_12) == None | ||
mock_redis.get.assert_called_with("app_to_use_for_commit_12") |
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.
Hmm,
installation_id
has a lot of different types. In what circumstances should we be expecting astr
vsint
vsNone
type? Should this be documented?