Skip to content

Commit 73348a3

Browse files
authored
get available SCMs support (#128)
* added an scm get operation in the client class * added testing app endpoint, fixed client.create for external scms * added very basic test
1 parent c590385 commit 73348a3

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

Algorithmia/algorithm.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,15 @@ def set_options(self, timeout=300, stdout=False, output=OutputType.default, **qu
3939
return self
4040

4141
# Create a new algorithm
42-
def create(self, details={}, settings={}, version_info={}, source={}, scmsCredentials={}):
42+
def create(self, details, settings, version_info=None, source=None, scmsCredentials=None):
4343
url = "/v1/algorithms/" + self.username
44-
create_parameters = {"name": self.algoname, "details": details, "settings": settings,
45-
"version_info": version_info, "source": source, "scmsCredentials": scmsCredentials}
44+
create_parameters = {"name": self.algoname, "details": details, "settings": settings}
45+
if version_info:
46+
create_parameters['version_info'] = version_info
47+
if source:
48+
create_parameters['source'] = source
49+
if scmsCredentials:
50+
create_parameters['scmsCredentials'] = scmsCredentials
4651

4752
api_response = self.client.postJsonHelper(url, create_parameters, parse_response_as_json=True)
4853
return api_response

Algorithmia/client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ def username(self):
7171
username = next(self.dir("").list()).path
7272
return username
7373

74+
def scms(self):
75+
url = "/v1/scms"
76+
response = self.getJsonHelper(url)
77+
return response
78+
7479
def file(self, dataUrl, cleanup=False):
7580
if dataUrl.startswith('file://'):
7681
return LocalDataFile(self, dataUrl)

Test/api/app.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ async def process_get_algo(request: Request, username, algoname):
8080
"resource_type": "algorithm"}
8181
elif algoname == "echo":
8282
return JSONResponse(content={"error": {"id": "1cfb98c5-532e-4cbf-9192-fdd45b86969c", "code": 2001,
83-
"message": "Caller is not authorized to perform the operation"}}, status_code=403)
83+
"message": "Caller is not authorized to perform the operation"}},
84+
status_code=403)
8485
else:
8586
return JSONResponse(content={"error": "No such algorithm"}, status_code=404)
8687

@@ -102,6 +103,34 @@ async def get_scm_status(username, algoname):
102103
return {"scm_connection_status": "active"}
103104

104105

106+
@regular_app.get("/v1/scms")
107+
async def get_scms():
108+
return {'results': [{'default': True, 'enabled': True, 'id': 'internal', 'name': '', 'provider': 'internal'},
109+
{'default': False, 'enabled': True, 'id': 'github', 'name': 'https://github.com',
110+
'provider': 'github', 'scm': {'client_id': '0ff25ba21ec67dbed6e2'},
111+
'oauth': {'client_id': '0ff25ba21ec67dbed6e2'},
112+
'urls': {'web': 'https://github.com', 'api': 'https://api.github.com',
113+
'ssh': 'ssh://[email protected]'}},
114+
{'default': False, 'enabled': True, 'id': 'aadebe70-007f-48ff-ba38-49007c6e0377',
115+
'name': 'https://gitlab.com', 'provider': 'gitlab',
116+
'scm': {'client_id': 'ca459576279bd99ed480236a267cc969f8322caad292fa5147cc7fdf7b530a7e'},
117+
'oauth': {'client_id': 'ca459576279bd99ed480236a267cc969f8322caad292fa5147cc7fdf7b530a7e'},
118+
'urls': {'web': 'https://gitlab.com', 'api': 'https://gitlab.com',
119+
'ssh': 'ssh://[email protected]'}},
120+
{'default': False, 'enabled': True, 'id': '24ad1496-5a1d-43e2-9d96-42fce8e5484f',
121+
'name': 'IQIVA Public GitLab', 'provider': 'gitlab',
122+
'scm': {'client_id': '3341c989f9d28043d2597388aa4f43ce60a74830b981c4b7d79becf641959376'},
123+
'oauth': {'client_id': '3341c989f9d28043d2597388aa4f43ce60a74830b981c4b7d79becf641959376'},
124+
'urls': {'web': 'https://gitlab.com', 'api': 'https://gitlab.com',
125+
'ssh': 'ssh://[email protected]'}},
126+
{'default': False, 'enabled': False, 'id': '83cd96ae-b1f4-4bd9-b9ca-6f7f25c37708',
127+
'name': 'GitlabTest', 'provider': 'gitlab',
128+
'scm': {'client_id': '5e257d6e168d579d439b7d38cdfa647e16573ae1dace6d93a30c5c60b4e5dd32'},
129+
'oauth': {'client_id': '5e257d6e168d579d439b7d38cdfa647e16573ae1dace6d93a30c5c60b4e5dd32'},
130+
'urls': {'web': 'https://gitlab.com', 'api': 'https://gitlab.com',
131+
'ssh': 'ssh://[email protected]'}}]}
132+
133+
105134
@regular_app.get("/v1/algorithms/{algo_id}/errors")
106135
async def get_algo_errors(algo_id):
107136
return JSONResponse(content={"error": {"message": "not found"}}, status_code=404)

Test/regular/client_test.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class ClientDummyTest(unittest.TestCase):
2020
@classmethod
2121
def setUpClass(cls):
2222
cls.client = Algorithmia.client(api_address="http://localhost:8080", api_key="simabcd123")
23+
2324
admin_username = "a_Mrtest"
2425
admin_org_name = "a_myOrg"
2526
environment_name = "Python 3.9"
@@ -28,7 +29,6 @@ def setUp(self):
2829
self.admin_username = self.admin_username + str(int(random() * 10000))
2930
self.admin_org_name = self.admin_org_name + str(int(random() * 10000))
3031

31-
3232
def test_create_user(self):
3333
response = self.client.create_user(
3434
{"username": self.admin_username, "email": self.admin_username + "@algo.com", "passwordHash": "",
@@ -60,6 +60,12 @@ def test_get_environment(self):
6060
if u'error' not in response:
6161
self.assertTrue(response is not None and u'environments' in response)
6262

63+
def test_get_scms(self):
64+
response = self.client.scms()
65+
results = response['results']
66+
internal = [result for result in results if result['id'] == 'internal']
67+
self.assertTrue(len(internal) == 1)
68+
6369
def test_edit_org(self):
6470
org_name = "a_myOrg84"
6571

@@ -120,7 +126,6 @@ def test_get_algorithm_errors(self):
120126
except AlgorithmException as e:
121127
self.assertTrue(e.message == "No such algorithm")
122128

123-
124129
def test_no_auth_client(self):
125130

126131
key = os.environ.get('ALGORITHMIA_API_KEY', "")
@@ -135,6 +140,7 @@ def test_no_auth_client(self):
135140
error = e
136141
finally:
137142
os.environ['ALGORITHMIA_API_KEY'] = key
138-
self.assertEqual(str(error), str(AlgorithmException(message="authorization required", stack_trace=None, error_type=None)))
143+
self.assertEqual(str(error), str(AlgorithmException(message="authorization required", stack_trace=None,
144+
error_type=None)))
139145
if __name__ == '__main__':
140146
unittest.main()

0 commit comments

Comments
 (0)