Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Release Group Updater #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed python/__init__.py
Empty file.
19 changes: 0 additions & 19 deletions python/main.py

This file was deleted.

35 changes: 35 additions & 0 deletions python/scripts/generate_upload_custom_build_from_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from python.utils import api, locators, urls


def upload_custom_build(params={}, data={}, api_key=None):
data = {
"N"
}
...


def get_project_dependencies(revision_dict):
""" Gets array containing locators of all dependencies of a project. """
deps = []
for dep in revision_dict['dependencies']:
deps.append(dep['locator'])
return deps


def main(api_key):
print("Which revision would you like to replace?")
print("(Please enter the link to the revision")
revision_link = input("> ")
revision_loc = urls.project_locator_from_url(revision_link)
revision = api.revisions(revision_loc, api_key)
parent_loc = revision['parent_locator']
print(f'Would you like to replace this with {parent_loc}?')
yn = input("> ")
if yn.lower() == 'y':
# Get the parent revision and upload custom build to current revision
ploc = locators.project_locator_dict_from_str(parent_loc)
parent_rev = api.revisions(ploc, api_key)
parent_deps = get_project_dependencies(parent_rev)
print(parent_deps)
else:
print("Aborting...")
File renamed without changes.
56 changes: 56 additions & 0 deletions python/scripts/latest_revision_for_release_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Create a new release using the latest revision of each project

# Get release group
# Query for latest revision of each project in the group
# Create a new release using those revisions

# POST release
# title: string, projects: [o]
# p: {branch, projectId, revisionId}

import requests
import json

base_url = 'https://app.fossa.com/api'

def encode(project_locator):
encoded = project_locator.replace('/', '%2F').replace('+', '%2B').replace('$', '%24')
return encoded

def get_real_latest_revision(headers):
def tmp(project_locator, branch):
proj = requests.get(url=f'{base_url}/projects/{encode(project_locator)}', headers=headers).json()['references']
latest = list(filter(lambda x: x['name'] == branch, proj))
return latest[0]['revision_id']
return tmp

def get_all_projects(headers, group_id):
url = f'{base_url}/project_group/{group_id}'
group = requests.get(url, headers=headers).json()
latest = list(sorted(group['releases'], key=(lambda x: x['id']), reverse=True))[0]
projects = requests.get(url=f'{url}/release/{latest["id"]}', headers=headers).json()['projects']
return list(map(lambda x: (x["projectId"], x["branch"]),projects))

def main():
release_group_id = input("Please enter your release group ID. It can be found in between group and releases in the URL for your release group\n")
api_key = input("Please enter your API key\n")
title = input("Please enter the title of the new release\n")

header = {'Authorization': f'Bearer {api_key}', 'Content-Type': 'application/json'}
projects = get_all_projects(header, release_group_id)
latest_revisions = list(map(lambda x: (get_real_latest_revision(header)(x[0], x[1]), x[1]), projects))
p_list = list(map(lambda x: {'branch': x[1], 'revisionId': x[0], 'projectId': x[0].split('$')[0]}, latest_revisions))
payload = json.dumps({
'title': title,
'projects': p_list
})
# post
update = requests.post(url=f'{base_url}/project_group/{release_group_id}/release', headers=header, data=payload)
if update.status_code == 200:
print('Successfully created new release')
else:
print('Error!')
# print(update.text)

if __name__ == '__main__':
main()
4 changes: 4 additions & 0 deletions python/scripts/policy_id_from_link_to_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ def main(api_key) -> None:
for scan in scans:
print(f'The policy ID of scan {scan["id"]} ({scan["scanned_at"]} UTC) is:')
print(scan['licensingPolicyVersionId'])

if __name__ == '__main__':
fossa_api_key = api.get_api_key()
main(fossa_api_key)
17 changes: 16 additions & 1 deletion python/utils/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def project(project_locator_dict, api_key=None) -> Optional[dict]:

def revisions(project_locator: dict, api_key: Optional[str] = None) -> dict:
"""
Get all revisions for a project.
Get a specific revision for a project.

:param project_locator: {fetcher, package}
:param api_key: Your FOSSA API key (optional)
Expand All @@ -112,6 +112,21 @@ def revisions(project_locator: dict, api_key: Optional[str] = None) -> dict:
return response.json()


def revision_resolve(project_locator: dict, api_key: Optional[str] = None) -> dict:
"""
Get a specific revision for a project.

:param project_locator: {fetcher, package}
:param api_key: Your FOSSA API key (optional)

:return: The revisions list
"""
locator_url = f'{project_locator["fetcher"]}%2F{project_locator["package"]}%24{project_locator["revision"]}'
response = request(f'revisions/{locator_url}/resolves', 'GET', data=None, api_key=api_key)
return response.json()



def get_api_key() -> str:
"""
Detect the API key from the environment or asks the user for one
Expand Down
6 changes: 6 additions & 0 deletions python/utils/locators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@ def locator_dict_from_str(loc: str) -> Optional[dict]:
return pattern.match(loc).groupdict() # type: ignore


def project_locator_dict_from_str(loc: str) -> Optional[dict]:
r = '(?P<fetcher>(?:custom|archive)(?:%2B|\+)\d{4,5})(?:%2F|\/)(?P<package>.+)(?:%24|\$)(?P<revision>.+\\b)'
pattern = re.compile(r, re.I)
return pattern.match(loc).groupdict() # type: ignore


def locator_str_from_dict(d: dict) -> str:
return f"{d['fetcher']}+{d['package']}${d['version']}"
27 changes: 24 additions & 3 deletions python/utils/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def project_locator_from_url(project_summary_page_url: str) -> dict[str, str]:
:param project_summary_page_url:
:return:
"""
r = r"https:\/\/app\.fossa\.com\/projects\/(?P<fetcher>custom%2B\d{5})%2F(?P<package>.+?)(?:\/refs\/branch\/).+?" \
"\/(?P<revision>\d+\\b)"
r = 'https:\/\/app\.fossa\.com\/projects\/(?P<fetcher>(?:custom|archive)%2B\d{4,5})%2F(?P<package>.+)' \
'(?:\/refs\/branch\/.+?\/)(?P<revision>\d+\\b)'
pattern = re.compile(r, re.I)
match = pattern.match(project_summary_page_url)
if match is not None:
Expand All @@ -18,7 +18,28 @@ def project_locator_from_url(project_summary_page_url: str) -> dict[str, str]:
raise ValueError(f"Could not parse project locator from url: {project_summary_page_url}")


def encode_project_locator(project_locator: dict[str, str]) -> str:
"""
Encodes project locator as string

:param project_locator:
:return:
"""
# %2B = +
# %24 = $
# %2F = /

encoded = f"{project_locator['fetcher']}%2B{project_locator['package']}%24{project_locator['revision']}"
return encoded.replace('/', '%2F')


if __name__ == '__main__':
print('enter url')
print('enter url:')
url = input('> ')
print(project_locator_from_url(url))
# locator = {
# "fetcher": "archive",
# "package": "2146/msrest-0.6.10",
# "revision": "1645628883888"
# }
# print(encode_project_locator(locator))