Skip to content

Commit

Permalink
Path.relative_to
Browse files Browse the repository at this point in the history
  • Loading branch information
blablatdinov committed Jul 1, 2024
1 parent c2c62f8 commit 0f75836
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 43 deletions.
3 changes: 1 addition & 2 deletions src/main/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.contrib import admin

from main.models import GhInstallation, GhRepo, TouchRecord
from main.models import GhRepo, TouchRecord

admin.site.register(GhInstallation)
admin.site.register(GhRepo)
admin.site.register(TouchRecord)
24 changes: 19 additions & 5 deletions src/main/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from collections import defaultdict
from os import PathLike
from pathlib import Path
from pprint import pprint

from git import Repo

Expand Down Expand Up @@ -53,25 +54,38 @@ def files_sorted_by_last_changes(
for file in files_for_check:
last_touch = next(repo.iter_commits(paths=file)).committed_datetime
file_last_commit[file] = (now - last_touch).days
print(f'files_sorted_by_last_changes')
pprint(file_last_commit)
return file_last_commit


def files_sorted_by_last_changes_from_db(
repo_id: int,
real_points: dict[PathLike[str], int],
relative_to: PathLike,
):
"""Count days after last file changing."""
today = datetime.datetime.now().date()
touch_records = {
Path(str_path): (today - date).days
for str_path, date in TouchRecord.objects.filter(gh_repo_id=repo_id).values_list('path', 'date')
}
touch_records = {}
for str_path, date in TouchRecord.objects.filter(gh_repo_id=repo_id).values_list('path', 'date'):
print(str_path)
touch_records[Path(str_path)] = (today - date).days
# touch_records = {
# Path(str_path).relative_to(relative_to): (today - date).days
# for str_path, date in TouchRecord.objects.filter(gh_repo_id=repo_id).values_list('path', 'date')
# }
print(f'Exist touches:')
pprint(touch_records)
res = {}
pprint(real_points.items())
for key, value in real_points.items():
# print(f'touch record: {touch_records.get(key)}')
res[key] = min(
touch_records.get(key, float('inf')),
touch_records.get(key.relative_to(relative_to), float('inf')),
value,
)
print(f'files_sorted_by_last_changes:')
pprint(res)
return res


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 5.0.6 on 2024-07-01 22:13

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('main', '0006_touchrecord'),
]

operations = [
migrations.RemoveField(
model_name='ghrepo',
name='gh_installation',
),
migrations.AddField(
model_name='ghrepo',
name='installation_id',
field=models.BigIntegerField(default=1),
preserve_default=False,
),
migrations.DeleteModel(
name='GhInstallation',
),
]
11 changes: 1 addition & 10 deletions src/main/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,12 @@
from django.db import models


@final
class GhInstallation(models.Model):

installation_id = models.BigIntegerField(unique=True)

class Meta:
db_table = 'gh_installations'


@final
class GhRepo(models.Model):

full_name = models.CharField(max_length=512, unique=True)
gh_installation = models.ForeignKey(GhInstallation, on_delete=models.PROTECT)
has_webhook = models.BooleanField()
installation_id = models.BigIntegerField()

class Meta:
db_table = 'gh_repos'
Expand Down
16 changes: 9 additions & 7 deletions src/main/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,35 @@
from main.models import GhRepo, TouchRecord


def pygithub_client(installation_id: int):
auth = Auth.AppAuth(874924, Path('revive-code-bot.2024-04-11.private-key.pem').read_text())
return Github(auth=auth.get_installation_auth(installation_id))


def process_repo(repo_id: int):
gh_repo = GhRepo.objects.get(id=repo_id)
auth = Auth.AppAuth(874924, Path('revive-code-bot.2024-04-11.private-key.pem').read_text())
gh = Github(auth=auth.get_installation_auth(gh_repo.gh_installation.installation_id))
gh = pygithub_client(gh_repo.installation_id)
repo = gh.get_repo(gh_repo.full_name)
gh.close()
with tempfile.TemporaryDirectory() as tmpdirname:
print(tmpdirname)
Repo.clone_from(repo.clone_url, tmpdirname)
print('Repo cloned')
# time.sleep(60)
# assert False
# tmpdirname = '/Users/almazilaletdinov/code/quranbot/old'
repo_path = Path(tmpdirname)
files_for_search = list(repo_path.glob('**/*.py')) # FIXME from config
got = files_sorted_by_last_changes_from_db(
repo_id,
files_sorted_by_last_changes(repo_path, files_for_search),
tmpdirname,
)
limit = 10 # FIXME read from config
stripped_file_list = sorted(
[
(
Path(str(path).replace(
str(path).replace(
'{0}/'.format(tmpdirname),
'',
)),
),
points,
)
for path, points in got.items()
Expand Down
9 changes: 4 additions & 5 deletions src/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
from github import Auth, Github
from django.views.decorators.csrf import csrf_exempt

from main.models import GhInstallation, GhRepo
from main.models import GhRepo
from main.service import pygithub_client


def healthcheck(request):
Expand All @@ -41,14 +42,12 @@ def webhook(request: HttpRequest): # FIXME add secret
if request.headers['X-GitHub-Event'] == 'installation_repositories':
request_json = json.loads(request.body)
installation_id = request_json['installation']['id']
gh_instn, _ = GhInstallation.objects.get_or_create(installation_id=installation_id)
new_repos = []
auth = Auth.AppAuth(874924, Path('revive-code-bot.2024-04-11.private-key.pem').read_text())
gh = Github(auth=auth.get_installation_auth(installation_id))
gh = pygithub_client(installation_id)
for repo in request_json['repositories_added']:
new_repos.append(GhRepo(
full_name=repo['full_name'],
gh_installation=gh_instn,
installation_id=installation_id,
has_webhook=False),
)
GhRepo.objects.bulk_create(new_repos)
Expand Down
39 changes: 25 additions & 14 deletions src/tests/test_create_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,29 @@
def gh_repo(mixer):
return mixer.blend(
'main.GhRepo',
full_name='blablatdinov/ramadan2020marathon_bot',
gh_installation__installation_id=52326552,
full_name='blablatdinov/iman-game-bot',
installation_id=52326552,
)


def test(gh_repo, time_machine):
time_machine.move_to('2024-07-01')
# time_machine.move_to('2024-07-01')
process_repo(gh_repo.id)
from pprint import pprint
# pprint(list(TouchRecord.objects.values_list('path', flat=True)))
# pprint(list(TouchRecord.objects.values_list('date', flat=True)))

# assert False, TouchRecord.objects.all()
assert list(TouchRecord.objects.values_list('path', flat=True)) == [
'manage.py',
'config/asgi.py',
'config/wsgi.py',
'game/__init__.py',
'game/apps.py',
'game/views.py',
'bot_init/__init__.py',
'bot_init/migrations/__init__.py',
'dialog/__init__.py',
'dialog/migrations/__init__.py',
'marathon/__init__.py',
'marathon/migrations/__init__.py',
'bot_init/tests.py',
'dialog/models.py',
'dialog/tests.py',
'dialog/views.py',
'bot_init/apps.py',
'bot_init/urls.py',
'game/migrations/__init__.py',
]
assert list(TouchRecord.objects.values_list('date', flat=True)) == [
datetime.date(2024, 7, 1),
Expand All @@ -46,7 +48,16 @@ def test(gh_repo, time_machine):
datetime.date(2024, 7, 1),
datetime.date(2024, 7, 1),
datetime.date(2024, 7, 1),
datetime.date(2024, 7, 1),
datetime.date(2024, 7, 1)
]

process_repo(gh_repo.id)
# pprint(list(TouchRecord.objects.values_list('path', flat=True)))
# pprint(list(TouchRecord.objects.values_list('date', flat=True)))

assert list(TouchRecord.objects.values_list('path', flat=True)) == [
]
assert list(TouchRecord.objects.values_list('date', flat=True)) == [
]


Expand Down

0 comments on commit 0f75836

Please sign in to comment.