Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
272 lines (253 loc) · 8.21 KB
/
Copy pathmain.py
File metadata and controls
272 lines (253 loc) · 8.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import argparse
import logging
import traceback
import sys
from src import commits_parser
from src import contributors_parser
from src import export_sheets
from src import git_logger
from src import invites_parser
from src import issues_parser
from src import pull_requests_parser
from src import wikipars
from src import workflow_runs_parser
from src.utils import logger, parse_time, validate_and_normalize_cell
from src.api.utils import APITypes
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--debug", action="store_true", help="Enable detailed console logs")
parser.add_argument(
"--source",
choices=list(APITypes),
default=APITypes.github,
required=False,
help="Data source: github API (by default), forgejo API, forgejo db (direct access)",
)
parser.add_argument("--invites", help="print pending invites", action="store_true")
parser.add_argument(
"--forgejo-db-organization",
help="Forgejo DB only: exact repository owner login (case-insensitive)",
)
parser.add_argument(
"--forgejo-db-repo-name-contains",
nargs="+",
help=(
"Forgejo DB only: select repositories whose name contains any of "
"these literal substrings (case-insensitive), e.g. 2026 2027"
),
)
parser.add_argument("-c", "--commits", help="log commits", action="store_true")
parser.add_argument(
"-p", "--pull_requests", help="log pull requests", action="store_true"
)
parser.add_argument(
"--graphql",
help="use graphql for requesting data (work only with --pull_requests) ",
action="store_true",
)
parser.add_argument("-i", "--issues", help="log issues", action="store_true")
parser.add_argument("-w", "--wikis", help="log wikis", action="store_true")
parser.add_argument("--contributors", help="log contributors", action="store_true")
parser.add_argument(
"--workflow_runs", help="log workflow runs", action="store_true"
)
parser.add_argument(
"--forks_include", help="logging data from forks", action="store_true"
)
parser.add_argument(
"-e",
"--export_google_sheets",
help="export table to google sheets",
action="store_true",
)
parser.add_argument(
"--start_cell",
type=str,
required=False,
help='Starting cell for Google Sheets export (e.g., "A1", "B3")',
default="A1",
)
parser.add_argument(
"--base_url",
type=str,
required=False,
help="Base URL for Forgejo instance (if using Forgejo)",
)
token = parser.add_mutually_exclusive_group(required=True)
token.add_argument("-t", "--token", type=str, help="account access token")
token.add_argument("--tokens", type=str, help="path to your tokens")
parser.add_argument(
"-l",
"--list",
type=str,
required=True,
help=(
"Path to the file containing the list of repositories. "
"Repositories should be separated by a line break. "
"Names must be in the format <organization or owner>/<name> "
),
)
parser.add_argument(
"--download_repos",
type=str,
help="path to downloaded repositories",
default="./",
)
parser.add_argument("-o", "--out", type=str, required=True, help="output filename")
parser.add_argument(
"--pr_comments", help="log comments for PR", action="store_true"
)
parser.add_argument(
"-s",
"--start",
type=str,
required=False,
help="start time",
default="2000/01/01-00:00:00",
)
parser.add_argument(
"-f",
"--finish",
type=str,
required=False,
help="finish time",
default="2400/01/01-00:00:00",
)
parser.add_argument(
"-b",
"--branch",
type=str,
required=False,
help=(
"branch to select commits, "
'by default use "default" repository branch, '
'use "all" to get all commits from all branches',
),
default=None,
)
parser.add_argument(
"--google_token",
type=str,
required=False,
help="Specify path to google token file",
)
parser.add_argument(
"--table_id",
type=str,
required=False,
help="Specify Google sheet document id (can find in url)",
)
parser.add_argument(
"--sheet_name",
type=str,
required=False,
help="Specify title for a sheet in a document in which data will be printed",
)
parser.add_argument(
"--clear_sheet",
action="store_true",
required=False,
help="Specify to clear sheet content before printing",
)
args = parser.parse_args()
if args.export_google_sheets:
for action in parser._actions:
if action.dest == "google_token":
action.required = True
if action.dest == "table_id":
action.required = True
if action.dest == "sheet_name":
action.required = True
return parser.parse_args()
def run(args, binded_repos, repos_for_wiki=None):
start = parse_time(args.start.split("-"))
finish = parse_time(args.finish.split("-"))
if args.commits:
commits_parser.log_commits(
binded_repos, args.out, start, finish, args.branch, args.forks_include
)
if args.pull_requests:
if args.graphql:
pull_requests_parser.log_pull_requests_by_graphql(
binded_repos=binded_repos, csv_name=args.out
)
else:
pull_requests_parser.log_pull_requests(
binded_repos,
args.out,
start,
finish,
args.forks_include,
args.pr_comments,
)
if args.issues:
issues_parser.log_issues(
binded_repos,
args.out,
start,
finish,
args.forks_include,
args.base_url,
)
if args.invites:
invites_parser.log_invitations(
binded_repos,
args.out,
)
if args.contributors:
contributors_parser.log_contributors(binded_repos, args.out, args.forks_include)
if args.workflow_runs:
workflow_runs_parser.log_workflow_runs(
binded_repos, args.out, args.forks_include
)
if args.wikis:
wikipars.wikiparser(repos_for_wiki, args.download_repos, args.out)
if args.export_google_sheets:
logger.close_csv_files()
export_sheets.write_data_to_table(
csv_path=args.out,
google_token=args.google_token,
table_id=args.table_id,
sheet_name=args.sheet_name,
start_cell=args.start_cell,
clear_content=args.clear_sheet,
)
def main():
args = parse_args()
logging.getLogger().setLevel(logging.DEBUG if args.debug else logging.WARNING)
try:
args.start_cell = validate_and_normalize_cell(args.start_cell)
except ValueError as e:
logging.error("Error in start_cell argument: %s", e)
sys.exit(1)
if args.token:
tokens = [args.token]
else:
tokens = git_logger.get_tokens_from_file(args.tokens)
repositories = git_logger.get_repos_from_file(args.list)
try:
clients = git_logger.Clients(
api_type=args.source,
auth_data=[
dict(
token=token,
base_url=args.base_url,
forgejo_db=dict(
repo_name_contains=args.forgejo_db_repo_name_contains,
organization=args.forgejo_db_organization,
),
)
for token in tokens
],
)
binded_repos = git_logger.get_next_binded_repo(clients, repositories)
except Exception as e:
logging.error("Failed to initialize any clients: %s", e)
logging.debug(traceback.format_exc())
return
try:
run(args, binded_repos, repositories)
finally:
logger.close_csv_files()
if __name__ == "__main__":
main()