-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
180 lines (145 loc) · 5.12 KB
/
__init__.py
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
"""Simple Jira extension."""
import os
import traceback
from pathlib import Path
import re
import albertv0 as v0
# initial configuration
__iid__ = "PythonInterface/v0.2"
__simplename__ = "jira"
__version__ = "0.1"
__trigger__ = "jira "
__author__ = "Gabriel Cesar"
__dependencies__ = []
__prettyname__ = "Simple Jira extension"
__homepage__ = "https://github.com/gabrielczar/albert-jira-extension"
file_dirname = os.path.dirname(__file__)
icon_path = os.path.join(file_dirname, "static", "images", "jira_blue")
cache_path = Path(v0.cacheLocation()) / __simplename__
config_path = Path(v0.configLocation()) / __simplename__
data_path = Path(v0.dataLocation()) / __simplename__
server_path = config_path / "server"
issue_regex = re.compile(r"\w+\/", re.IGNORECASE)
def initialize():
for p in (cache_path, config_path, data_path):
p.mkdir(parents=False, exist_ok=True)
def finalize():
pass
def handleQuery(query):
results = []
if query.isTriggered:
try:
results_setup = setup(query)
if results_setup:
return results_setup
query_string = query.string
if "remove server" in query_string:
results.append(
v0.Item(
id=__prettyname__,
icon=icon_path,
text="Remove server",
subtext="press [ENTER]",
actions=[
v0.FuncAction(
f"Removing stored server", remove_server())
]
)
)
else:
results.append(
v0.Item(
id=__prettyname__,
icon=icon_path,
text="Open issue...",
actions=[v0.UrlAction(
f"Open issue in Jira", get_issue_path(query_string))],
)
)
results.append(
v0.Item(
id=__prettyname__,
icon=icon_path,
text="Search for issue...",
actions=[v0.UrlAction(
f"Search for issue in Jira", get_search_path(query_string))],
)
)
except Exception:
results.insert(
0,
v0.Item(
id=__prettyname__,
icon=icon_path,
text="Something went wrong! Press [ENTER] to copy error and report it",
actions=[
v0.ClipAction(
f"Copied error - report this problem",
f"{traceback.format_exc()}",
),
v0.UrlAction(
f"Report error!", "https://github.com/gabrielczar/albert-jira-extension/issues/new")
],
),
)
return results
def get_server_path():
server = load_data("server")
if not "https://" in server:
server = "https://" + server
return server
def get_issue_path(raw_issue):
server = get_server_path()
issue = issue_regex.sub("", raw_issue)
return server + "/browse/" + issue
def get_search_path(text):
server = get_server_path()
return server + "/issues/?jql=text%20~%20\"" + text + "\""
def setup(query):
results = []
try:
if not server_path.is_file():
results.append(
v0.Item(
id=__prettyname__,
icon=icon_path,
text=f"Please specify the JIRA server to connect to",
subtext="Fill and press [ENTER]",
actions=[
v0.FuncAction(
"Save JIRA server", lambda: save_data(
query.string, "server")
)
],
)
)
except Exception:
remove_server()
results.insert(
0,
v0.Item(
id=__prettyname__,
icon=icon_path,
text="Something went wrong! Please try again!",
actions=[
v0.ClipAction(
f"Copy error - report this problem",
f"{traceback.format_exc()}",
),
v0.UrlAction(
f"Report error!", "https://github.com/gabrielczar/albert-jira-extension/issues/new")
],
),
)
return results
def save_data(data: str, data_name: str):
"""Save a piece of data in the configuration directory."""
with open(config_path / data_name, "w") as f:
f.write(data)
def load_data(data_name) -> str:
"""Load a piece of data from the configuration directory."""
with open(config_path / data_name, "r") as f:
data = f.readline().strip().split()[0]
return data
def remove_server():
os.remove(config_path / "server")