-
Notifications
You must be signed in to change notification settings - Fork 105
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
Adding convenience methods for getting or creating objects #220
Open
gsnyder2007
wants to merge
4
commits into
master
Choose a base branch
from
gsnyder/client-with-convenience-functions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
VERSION = (1, 0, 7) | ||
VERSION = (1, 1, 0) | ||
|
||
__version__ = '.'.join(map(str, VERSION)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
from blackduck import Client | ||
from blackduck.constants import PROJECT_VERSION_SETTINGS, VERSION_DISTRIBUTION, VERSION_PHASES | ||
|
||
import argparse | ||
import logging | ||
import requests | ||
|
||
logging.basicConfig( | ||
level=logging.DEBUG, | ||
format="[%(asctime)s] {%(module)s:%(lineno)d} %(levelname)s - %(message)s" | ||
) | ||
|
||
parser = argparse.ArgumentParser("Create, read, update, and delete a project") | ||
parser.add_argument("--base-url", required=True, help="Hub server URL e.g. https://your.blackduck.url") | ||
parser.add_argument("--token-file", dest='token_file', required=True, help="containing access token") | ||
parser.add_argument("--no-verify", dest='verify', action='store_false', help="disable TLS certificate verification") | ||
parser.add_argument("project", help="Project name") | ||
parser.add_argument("version", help="Version name") | ||
args = parser.parse_args() | ||
|
||
with open(args.token_file, 'r') as tf: | ||
access_token = tf.readline().strip() | ||
|
||
bd = Client( | ||
base_url=args.base_url, | ||
token=access_token, | ||
verify=args.verify | ||
) | ||
|
||
project_name = args.project | ||
|
||
# POST project | ||
project_data = { | ||
'name': project_name, | ||
'description': "some description", | ||
'projectLevelAdjustments': True, | ||
} | ||
|
||
try: | ||
r = bd.session.post("/api/projects", json=project_data) | ||
r.raise_for_status() | ||
print(f"created project {r.links['project']['url']}") | ||
except requests.HTTPError as err: | ||
# more fine grained error handling here; otherwise: | ||
bd.http_error_handler(err) | ||
|
||
# GET project | ||
params = { | ||
'q': [f"name:{project_name}"] | ||
} | ||
|
||
project_obj = None | ||
project_url = None | ||
for project in bd.get_items("/api/projects", params=params): | ||
if project['name'] == project_name: | ||
project_obj = project | ||
project_url = bd.list_resources(project)['href'] | ||
print(f"project url: {project_url}") | ||
|
||
# POST version | ||
version_data = { | ||
'versionName': args.version, | ||
'distribution': 'EXTERNAL', | ||
'phase': 'PLANNING' | ||
} | ||
|
||
versions_url = project_url + "/versions" | ||
try: | ||
r = bd.session.post(versions_url, json=version_data) | ||
r.raise_for_status() | ||
version_url = r.headers['Location'] | ||
print(f"created version {version_url}") | ||
except requests.HTTPError as err: | ||
# more fine grained error handling here; otherwise: | ||
bd.http_error_handler(err) | ||
|
||
# GET or CREATE version | ||
|
||
version = bd.get_or_create_resource(field='versionName', value=args.version, name="versions", parent=project_obj) | ||
print(f"Version {version['versionName']} was either found or created after initial POST") | ||
|
||
# DELETE version | ||
try: | ||
r = bd.session.delete(version_url) | ||
r.raise_for_status() | ||
print(f"deleted version {args.version}") | ||
except requests.HTTPError as err: | ||
if err.response.status_code == 404: | ||
print("not found") | ||
else: | ||
bd.http_error_handler(err) | ||
|
||
# GET or CREATE version | ||
params = { | ||
'phase': 'PLANNING', | ||
'distribution': 'SAAS' | ||
} | ||
version = bd.get_or_create_resource(field='versionName', value=args.version, name="versions", parent=project_obj, params=params) | ||
print(f"Version {version['versionName']} was either found or created after deleting the version") | ||
|
||
# DELETE project | ||
try: | ||
r = bd.session.delete(project_url) | ||
r.raise_for_status() | ||
print("deleted project") | ||
except requests.HTTPError as err: | ||
if err.response.status_code == 404: | ||
print("not found") | ||
else: | ||
bd.http_error_handler(err) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, looking at this again - think there should be more here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh, yeah, makes me wonder how my tests even worked. Investigating....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some code got borked in transit. Fixing...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pushed an update with the (I think) now complete code. Still testing....