6
6
from django .core .management .base import BaseCommand
7
7
8
8
from apps .github .constants import GITHUB_ITEMS_PER_PAGE
9
- from apps .github .models import Issue , Organization , Release , Repository , sync_repository
9
+ from apps .github .models import Release , Repository , sync_repository
10
10
from apps .owasp .constants import OWASP_ORGANIZATION_NAME
11
11
from apps .owasp .models import Chapter , Committee , Event , Project
12
12
13
- BATCH_SIZE = 10
14
-
15
13
16
14
class Command (BaseCommand ):
17
15
help = "Updates OWASP entities based on their GitHub data."
@@ -20,17 +18,6 @@ def add_arguments(self, parser):
20
18
parser .add_argument ("--offset" , default = 0 , required = False , type = int )
21
19
22
20
def handle (self , * _args , ** options ):
23
- def save_data ():
24
- """Save data to DB."""
25
- Organization .bulk_save (organizations )
26
- Issue .bulk_save (issues )
27
- Release .bulk_save (releases )
28
-
29
- Chapter .bulk_save (chapters )
30
- Committee .bulk_save (committees )
31
- Event .bulk_save (events )
32
- Project .bulk_save (projects )
33
-
34
21
gh = github .Github (os .getenv ("GITHUB_TOKEN" ), per_page = GITHUB_ITEMS_PER_PAGE )
35
22
gh_owasp_organization = gh .get_organization (OWASP_ORGANIZATION_NAME )
36
23
remote_owasp_repositories_count = gh_owasp_organization .public_repos
@@ -41,27 +28,23 @@ def save_data():
41
28
chapters = []
42
29
committees = []
43
30
events = []
44
- issues = []
45
- organizations = []
46
31
projects = []
47
32
releases = []
48
33
49
34
offset = options ["offset" ]
50
- for idx , gh_repository in enumerate (
51
- gh_owasp_organization .get_repos (
52
- type = "public" ,
53
- sort = "created" ,
54
- direction = "asc" ,
55
- )[offset :]
56
- ):
57
- print (f"{ idx + offset + 1 :<4} { gh_repository .name } " )
35
+ gh_repositories = gh_owasp_organization .get_repos (
36
+ type = "public" ,
37
+ sort = "created" ,
38
+ direction = "desc" ,
39
+ )
40
+ total_count = gh_repositories .totalCount - offset
41
+ for idx , gh_repository in enumerate (gh_repositories [offset :]):
42
+ prefix = f"{ idx + offset + 1 } of { total_count } "
43
+ print (f"{ prefix :<12} { gh_repository .name } " )
58
44
59
45
owasp_organization , repository , new_releases = sync_repository (
60
46
gh_repository , organization = owasp_organization , user = owasp_user
61
47
)
62
- if not owasp_organization .id :
63
- owasp_organization .save ()
64
-
65
48
releases .extend (new_releases )
66
49
67
50
entity_key = gh_repository .name .lower ()
@@ -81,11 +64,13 @@ def save_data():
81
64
elif entity_key .startswith ("www-committee-" ):
82
65
committees .append (Committee .update_data (gh_repository , repository , save = False ))
83
66
84
- if idx % BATCH_SIZE == 0 :
85
- save_data ( )
67
+ # Bulk save data.
68
+ Release . bulk_save ( releases )
86
69
87
- # Save remaining data.
88
- save_data ()
70
+ Chapter .bulk_save (chapters )
71
+ Committee .bulk_save (committees )
72
+ Event .bulk_save (events )
73
+ Project .bulk_save (projects )
89
74
90
75
# Check repository counts.
91
76
local_owasp_repositories_count = Repository .objects .filter (
0 commit comments