Skip to content

Commit

Permalink
added example
Browse files Browse the repository at this point in the history
  • Loading branch information
nleach999 committed Apr 26, 2024
1 parent 914f3e6 commit e5985af
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 31 deletions.
14 changes: 14 additions & 0 deletions api_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from cxone_api import AuthUS, ApiUS, paged_api, CxOneClient
from cxone_api.repo import ProjectRepoConfig
import os, asyncio


async def main():
cxone_client = CxOneClient.create_with_oauth(os.environ["OAUTH_CLIENTID"], os.environ["OAUTH_CLIENTSECRET"], "CxOneAPIExample",
AuthUS(os.environ["TENANT"]), ApiUS())

async for scan in paged_api(cxone_client.get_scans, "scans", statuses=['Completed']):
print(f"Scan Id: {scan['id']} ProjectId: {scan['projectId']} Branch: {scan['branch']}")
pass

asyncio.run(main())
31 changes: 0 additions & 31 deletions cxone_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,34 +444,3 @@ async def get_scan_workflow(self, scanid, **kwargs):
return await self.__exec_request(requests.get, url)


class ProjectRepoConfig:

def __init__(self, cxone_client, project_data):
self.__client = cxone_client
self.__project_data = project_data
self.__fetched_undocumented_config = False
self.__lock = asyncio.Lock()

async def __get_logical_repo_url(self):
# The documented project API seems to have a bug and does not return the repoUrl. The undocumented
# API used by the UI has it. The undocumented API will no longer be called when the project
# API is fixed.
async with self.__lock:
if len(self.__project_data['repoUrl']) == 0 and not self.__fetched_undocumented_config:
self.__fetched_undocumented_config = True
config = (await self.__client.get_project_configuration(self.__project_data['id'])).json()

for entry in config:
if entry['key'] == "scan.handler.git.repository":
self.__project_data['repoUrl'] = entry['value']

return self.__project_data['repoUrl']

@property
async def primary_branch(self):
return self.__project_data['mainBranch'] if len(self.__project_data['mainBranch']) > 0 else None

@property
async def repo_url(self):
url = await self.__get_logical_repo_url()
return url if len(url) > 0 else None
31 changes: 31 additions & 0 deletions cxone_api/repo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class ProjectRepoConfig:

def __init__(self, cxone_client, project_data):
self.__client = cxone_client
self.__project_data = project_data
self.__fetched_undocumented_config = False
self.__lock = asyncio.Lock()

async def __get_logical_repo_url(self):
# The documented project API seems to have a bug and does not return the repoUrl. The undocumented
# API used by the UI has it. The undocumented API will no longer be called when the project
# API is fixed.
async with self.__lock:
if len(self.__project_data['repoUrl']) == 0 and not self.__fetched_undocumented_config:
self.__fetched_undocumented_config = True
config = (await self.__client.get_project_configuration(self.__project_data['id'])).json()

for entry in config:
if entry['key'] == "scan.handler.git.repository":
self.__project_data['repoUrl'] = entry['value']

return self.__project_data['repoUrl']

@property
async def primary_branch(self):
return self.__project_data['mainBranch'] if len(self.__project_data['mainBranch']) > 0 else None

@property
async def repo_url(self):
url = await self.__get_logical_repo_url()
return url if len(url) > 0 else None

0 comments on commit e5985af

Please sign in to comment.