forked from pycontribs/jira
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_local_jira_user.py
55 lines (46 loc) · 1.69 KB
/
make_local_jira_user.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
"""Attempts to create a test user, as the empty JIRA instance isn't provisioned with one."""
from __future__ import annotations
import sys
import time
from os import environ
import requests
from jira import JIRA
CI_JIRA_URL = environ["CI_JIRA_URL"]
def add_user_to_jira():
try:
JIRA(
CI_JIRA_URL,
basic_auth=(environ["CI_JIRA_ADMIN"], environ["CI_JIRA_ADMIN_PASSWORD"]),
).add_user(
username=environ["CI_JIRA_USER"],
email="[email protected]",
fullname=environ["CI_JIRA_USER_FULL_NAME"],
password=environ["CI_JIRA_USER_PASSWORD"],
)
print("user", environ["CI_JIRA_USER"])
except Exception as e:
if "username already exists" not in str(e):
raise e
if __name__ == "__main__":
if environ.get("CI_JIRA_TYPE", "Server").upper() == "CLOUD":
print("Do not need to create a user for Jira Cloud CI, quitting.")
sys.exit()
start_time = time.time()
timeout_mins = 15
print(
"waiting for instance of jira to be running, to add a user for CI system:\n"
f" timeout = {timeout_mins} mins"
)
while True:
try:
requests.get(CI_JIRA_URL + "rest/api/2/permissions")
print("JIRA IS REACHABLE")
add_user_to_jira()
break
except (requests.exceptions.Timeout, requests.exceptions.ConnectionError) as ex:
print(f"encountered {ex} while waiting for the JiraServer docker")
time.sleep(20)
if start_time + 60 * timeout_mins < time.time():
raise TimeoutError(
f"Jira server wasn't reachable within timeout {timeout_mins}"
)