Skip to content

Commit 6a143ca

Browse files
committed
Added. job_compassplus/jira_dump_issues.py
1 parent 4baac94 commit 6a143ca

1 file changed

Lines changed: 132 additions & 0 deletions

File tree

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
__author__ = "ipetrash"
5+
6+
7+
import time
8+
import sqlite3
9+
10+
from pathlib import Path
11+
from typing import Any
12+
13+
from root_config import JIRA_HOST
14+
from root_common import session
15+
16+
17+
DIR: Path = Path(__file__).resolve().parent
18+
FILE_NAME_DB: Path = DIR / "jira_local.sqlite"
19+
20+
URL_SEARCH = f"{JIRA_HOST}/rest/api/latest/search"
21+
22+
23+
def init_sqlite(filename: Path) -> sqlite3.Connection:
24+
conn = sqlite3.connect(filename)
25+
conn.execute(
26+
"""
27+
CREATE TABLE IF NOT EXISTS Task (
28+
id INTEGER PRIMARY KEY,
29+
key TEXT,
30+
summary TEXT,
31+
description TEXT,
32+
status TEXT,
33+
issue_type TEXT,
34+
created TEXT,
35+
is_indexed INTEGER DEFAULT 0
36+
)
37+
"""
38+
)
39+
conn.commit()
40+
return conn
41+
42+
43+
def get_last_id(conn: sqlite3.Connection, project: str) -> int:
44+
res = conn.execute(
45+
"SELECT MAX(id) FROM Task WHERE key LIKE ?", (f"{project}-%",)
46+
).fetchone()
47+
return res[0] if res[0] else 0
48+
49+
50+
def sync_jira_to_sqlite(conn: sqlite3.Connection, projects: list[str]):
51+
max_results: int = 100
52+
53+
for project in projects:
54+
last_id: int = get_last_id(conn, project)
55+
56+
while True:
57+
print(f"[*] Поиск задач в Jira проекта {projects} от ID > {last_id}...")
58+
59+
filter_id: str = f"AND id > {last_id}" if last_id > 0 else ""
60+
jql: str = f"project = {project} {filter_id} ORDER BY id ASC"
61+
62+
query: dict[str, Any] = {
63+
"jql": jql,
64+
"fields": "key,summary,description,status,issuetype,created",
65+
"maxResults": max_results,
66+
}
67+
68+
rs = session.get(URL_SEARCH, params=query)
69+
try:
70+
rs.raise_for_status()
71+
except Exception as e:
72+
print(rs.json())
73+
raise e
74+
75+
issues: list[dict] = rs.json().get("issues")
76+
if not issues:
77+
print("Больше нет задач")
78+
break
79+
80+
for issue in issues:
81+
print(issue)
82+
83+
issue_id: int = int(issue["id"])
84+
issue_key: str = issue["key"]
85+
issue_summary: str = issue["fields"].get("summary", "")
86+
issue_desc: str = str(issue["fields"].get("description"))
87+
issue_status: str = issue["fields"]["status"]["name"]
88+
issue_type: str = issue["fields"]["issuetype"]["name"]
89+
issue_created: str = issue["fields"]["created"]
90+
91+
conn.execute(
92+
"""
93+
INSERT OR IGNORE INTO Task
94+
(id, key, summary, description, status, issue_type, created)
95+
VALUES
96+
(?, ?, ?, ?, ?, ?, ?)
97+
""",
98+
(
99+
issue_id,
100+
issue_key,
101+
issue_summary,
102+
issue_desc,
103+
issue_status,
104+
issue_type,
105+
issue_created,
106+
),
107+
)
108+
109+
last_id = issue_id
110+
111+
conn.commit()
112+
113+
print(f"[+] Загружено {len(issues)} задач...")
114+
115+
time.sleep(5)
116+
117+
118+
if __name__ == "__main__":
119+
# TODO: Возможность задавать аргументом
120+
projects: list[str] = [
121+
# "OPTT",
122+
"TXI",
123+
"TWRBS",
124+
"TXCORE",
125+
"TXACQ",
126+
]
127+
128+
# TODO: Возможность задавать аргументом путь к БД
129+
# FILE_NAME_DB: Path = DIR / "jira_local_OPTT.sqlite"
130+
131+
db_conn = init_sqlite(FILE_NAME_DB)
132+
sync_jira_to_sqlite(db_conn, projects)

0 commit comments

Comments
 (0)