Skip to content

Commit 79ccaf0

Browse files
authored
Ensure project name is random. (#26)
We deterministically create a project on every test based on the test name, which might be a problem if the test fails to clean it up, potentially causing subsequent runs to fail. This change ensures that project names has a random suffix.
1 parent 4603377 commit 79ccaf0

File tree

4 files changed

+16
-20
lines changed

4 files changed

+16
-20
lines changed

resources/github.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
1-
import random
2-
import string
3-
41
from sh import gh
52
from robot.api.deco import keyword
63
from robot.api import logger
7-
8-
9-
def randstring(size=6, chars=string.ascii_uppercase + string.digits):
10-
return "".join(random.choice(chars) for _ in range(size)) # nosec B311
11-
12-
13-
def randint():
14-
return f"{random.randint(1000000, 9999999)}" # nosec B311
4+
from resources import helpers
155

166

177
class GitHub:
@@ -20,7 +10,7 @@ def __init__(self):
2010

2111
@keyword
2212
def random_repo_name(self, org, base_name):
23-
return f"{org}/{base_name}-{randint()}"
13+
return f"{org}/{base_name}-{helpers.randint()}"
2414

2515
@keyword
2616
def a_copy_of_repo(self, repo_template, repo_name):

resources/helpers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import random
23
import yaml
34
from sh import minder
45

@@ -10,6 +11,10 @@
1011
)
1112

1213

14+
def randint():
15+
return f"{random.randint(1000000, 9999999)}" # nosec B311
16+
17+
1318
def _get_url_from_config(server_type):
1419
"""Helper function to read config and return URL for a given server type."""
1520
minder_config_path = os.getenv(MINDER_CONFIG)

resources/minder_restapi_lib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def create_authorization_header(self):
1818
1919
Logs into Minder, extracts the Bearer token, and returns the Authorization header.
2020
"""
21-
log_into_minder()
21+
log_into_minder() # TODO remove this, it should be ensured by the above layer
2222
bearer_token = self._get_bearer_token()
2323

2424
# Return the Authorization header

resources/projects.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
from robot.api.deco import keyword
33
from robot.libraries.BuiltIn import BuiltIn
44
import re
5-
import hashlib
65
import os
76
from resources.minder_restapi_lib import MinderRestApiLib
7+
from resources import helpers
88

99

1010
class Projects:
@@ -132,11 +132,12 @@ def _convert_to_dns_safe_name(self, name):
132132
if name[0].isdigit():
133133
name = "p-" + name
134134

135-
# If the name is already 63 characters or less, return it as is
136-
if len(name) <= 63:
137-
return name
135+
# If the name is already 63 - 8 characters or less, add random
136+
# suffix and return it.
137+
suffix = helpers.randint()
138+
if len(name) <= 63 - 8:
139+
return f"{name}-{suffix}"
138140

139-
# Otherwise, truncate and add suffix
140-
suffix = hashlib.sha1(name.encode()).hexdigest()[:9] # nosec
141-
truncated_name = name[:53]
141+
# Otherwise truncate
142+
truncated_name = name[:55]
142143
return f"{truncated_name}-{suffix}"

0 commit comments

Comments
 (0)