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

Sourcery refactored tuning branch #1

Open
wants to merge 1 commit into
base: tuning
Choose a base branch
from
Open

Conversation

sourcery-ai[bot]
Copy link

@sourcery-ai sourcery-ai bot commented Jan 18, 2023

Branch tuning refactored by Sourcery.

If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.

See our documentation here.

Run Sourcery locally

Reduce the feedback loop during development by using the Sourcery editor plugin:

Review changes via command line

To manually merge these changes, make sure you're on the tuning branch, then run:

git fetch origin sourcery/tuning
git merge --ff-only FETCH_HEAD
git reset HEAD^

Help us improve this pull request!

@sourcery-ai sourcery-ai bot requested a review from joamatab January 18, 2023 19:02
parameters = dict(
(k, expand_path(v))
for k, v in args(params_args)
)
parameters = {k: expand_path(v) for k, v in args(params_args)}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 24-27 refactored with the following changes:

Comment on lines -92 to +94
r = requests.get('https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=' + token)
r = requests.get(
f'https://www.googleapis.com/oauth2/v3/tokeninfo?access_token={token}'
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function radlabauth refactored with the following changes:

Comment on lines -144 to +153
elif (select_proj != '1' and select_proj != '2'):
elif select_proj != '1':
sys.exit(Fore.RED + "\nError Occured - INVALID choice.\n")
else:
projid = input(Fore.YELLOW + Style.BRIGHT + "\nEnter the Project ID for RAD Lab management" + Style.RESET_ALL + ': ').strip()
else:
pass

os.system("gcloud config set project " + projid)
os.system("gcloud auth application-default set-quota-project " + projid )
os.system(f"gcloud config set project {projid}")
os.system(f"gcloud auth application-default set-quota-project {projid}")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function set_proj refactored with the following changes:

Comment on lines -161 to +165
# Hardcoded Org level required RAD Lab Launcher roles
launcherorgroles = ['roles/iam.organizationRoleViewer']

credentials = GoogleCredentials.get_application_default()

service0 = discovery.build('cloudresourcemanager', 'v3', credentials=credentials)
request0 = service0.projects().getIamPolicy(resource='projects/' + projid)
request0 = service0.projects().getIamPolicy(resource=f'projects/{projid}')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function launcherperm refactored with the following changes:

This removes the following comments ( why? ):

# Check for Required roles on RAD Lab Management Project

Comment on lines -242 to +252
if 'folders' in parent:
credentials = GoogleCredentials.get_application_default()
s = discovery.build('cloudresourcemanager', 'v3', credentials=credentials)
req = s.folders().get(name=parent)
res = req.execute()
return findorg(res['parent'])
else:
if 'folders' not in parent:
# print(Fore.GREEN + "Org identified: " + Style.BRIGHT + parent + Style.RESET_ALL)
return parent
credentials = GoogleCredentials.get_application_default()
s = discovery.build('cloudresourcemanager', 'v3', credentials=credentials)
req = s.folders().get(name=parent)
res = req.execute()
return findorg(res['parent'])
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function findorg refactored with the following changes:

Comment on lines -42 to +61
print("----------> RUN FOR: " + dir)
print(f"----------> RUN FOR: {dir}")


try:
# IF MODULE EXISTS: Copying main directory in temp folder
shutil.copytree(GITHUB_WORKSPACE+'/'+dir, os.getcwd()+'/temp/'+dir)
shutil.copytree(f'{GITHUB_WORKSPACE}/{dir}', f'{os.getcwd()}/temp/{dir}')

# Deleting added/modified & removed files
for mfile in modified_files:
if os.path.exists(os.getcwd()+'/temp/'+mfile):
print("Deleting file: " + mfile)
os.remove(os.getcwd()+'/temp/'+mfile)
if os.path.exists(f'{os.getcwd()}/temp/{mfile}'):
print(f"Deleting file: {mfile}")
os.remove(f'{os.getcwd()}/temp/{mfile}')

for rfile in removed_files:
if os.path.exists(os.getcwd()+'/temp/'+rfile):
print("Deleting file: " + rfile)
os.remove(os.getcwd()+'/temp/'+rfile)
if os.path.exists(f'{os.getcwd()}/temp/{rfile}'):
print(f"Deleting file: {rfile}")
os.remove(f'{os.getcwd()}/temp/{rfile}')
except:
# IF MODULE DONOT EXISTS: Creating temp module folder
os.makedirs(os.getcwd()+'/temp/'+dir)
os.makedirs(f'{os.getcwd()}/temp/{dir}')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

Comment on lines -116 to +118
response = requests.get('https://api.github.com/repos/'+ GITHUB_REPOSITORY +'/pulls/'+ str(pr) +'/files')
response = requests.get(
f'https://api.github.com/repos/{GITHUB_REPOSITORY}/pulls/{str(pr)}/files'
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function pr_files refactored with the following changes:

Comment on lines -133 to +142
# print(path)
if not os.path.exists(path):
os.makedirs(path)
# print(path)
if not os.path.exists(path):
os.makedirs(path)

# print('Beginning file download with requests')
r = requests.get(raw)
with open(path + '/' + os.path.basename(file), 'wb') as f:
f.write(r.content)
# print('Beginning file download with requests')
r = requests.get(raw)
with open(f'{path}/{os.path.basename(file)}', 'wb') as f:
f.write(r.content)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function downloadprfiles refactored with the following changes:

Comment on lines -149 to +164
modified_files_dir = []
removed_files_dir = []

for file in modified_files:
modified_files_dir.append(os.path.dirname(file))

for file in removed_files:
removed_files_dir.append(os.path.dirname(file))

working_directories = modified_files_dir + removed_files_dir
working_directories = list(set(working_directories))
modified_files_dir = [os.path.dirname(file) for file in modified_files]
removed_files_dir = [os.path.dirname(file) for file in removed_files]
working_directories = modified_files_dir + removed_files_dir
working_directories = list(set(working_directories))

# print("Working Directories:")
# print(working_directories)

modules = [x for x in working_directories if x.startswith('modules/')]
modules = [x for x in modules if x.count('/') == 1]
print("Modules Updated:")
print(modules)

return modules
modules = [x for x in working_directories if x.startswith('modules/')]
modules = [x for x in modules if x.count('/') == 1]
print("Modules Updated:")
print(modules)

return modules
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_updated_modules refactored with the following changes:

Comment on lines -173 to +184
tr = Terraform(working_dir=dir)

return_code_init, stdout_init, stderr_init = tr.init_cmd(capture_output=False)
return_code_plan, stdout_plan, stderr_plan = tr.plan_cmd(capture_output=False,var={'billing_account_id':'ABCD-EFGH-IJKL-MNOP', 'organization_id':'1234567890', 'random_id': '1234'})
path = os.getcwd()+'/temp/'
if(return_code_init == 1):
comment = 'Terraform Init FAILED'
status = 'fail'
if(return_code_plan == 1):
comment = 'Terraform Plan FAILED'
status = 'fail'
else:
comment = 'Terraform Init & Terraform Plan SUCCESSFUL'
status = 'pass'
return comment, status
tr = Terraform(working_dir=dir)

return_code_init, stdout_init, stderr_init = tr.init_cmd(capture_output=False)
return_code_plan, stdout_plan, stderr_plan = tr.plan_cmd(capture_output=False,var={'billing_account_id':'ABCD-EFGH-IJKL-MNOP', 'organization_id':'1234567890', 'random_id': '1234'})

path = f'{os.getcwd()}/temp/'
if(return_code_init == 1):
comment = 'Terraform Init FAILED'
status = 'fail'
if(return_code_plan == 1):
comment = 'Terraform Plan FAILED'
status = 'fail'
else:
comment = 'Terraform Init & Terraform Plan SUCCESSFUL'
status = 'pass'

return comment, status
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function tf refactored with the following changes:

response = requests.post('https://api.github.com/repos/'+ GITHUB_REPOSITORY +'/issues/'+ str(pr) +'/comments', data=json.dumps(data), headers=headers)
# print(response.text)
response = requests.post(
f'https://api.github.com/repos/{GITHUB_REPOSITORY}/issues/{str(pr)}/comments',
data=json.dumps(data),
headers=headers,
)
# print(response.text)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function commentpr refactored with the following changes:

print('\n'.join(' - {}'.format(s) for s in warnings))
print('\n'.join(f' - {s}' for s in warnings))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

Comment on lines -56 to +65
for variable in variables:
if not variable.description:
errors.append(f'variable {variable.name} has no description')
errors.extend(f'variable {variable.name} has no description'
for variable in variables if not variable.description)
if sorted(variable_names) != variable_names:
message = f'variable order should be: {sorted(variable_names)}'
errors.append(message)

outputs = tfdoc.get_outputs(subpath)
output_names = [v.name for v in outputs]
for output in outputs:
if not output.description:
errors.append(f'output {output.name} has no description')
errors.extend(f'output {output.name} has no description'
for output in outputs if not output.description)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function check_path refactored with the following changes:

response = requests.get('https://api.github.com/repos/'+ GITHUB_REPOSITORY +'/issues')
return response
return requests.get(f'https://api.github.com/repos/{GITHUB_REPOSITORY}/issues')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function open_issue refactored with the following changes:

Comment on lines -106 to +107
response = requests.get('https://api.github.com/repos/'+ GITHUB_REPOSITORY +'/issues/'+ str(number) +'/comments')
response = requests.get(
f'https://api.github.com/repos/{GITHUB_REPOSITORY}/issues/{str(number)}/comments'
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function issuecommentcheck refactored with the following changes:

Comment on lines -202 to +205
for line in format_variables(variables):
buffer.append(line)
buffer.extend(iter(format_variables(variables)))
buffer.append('\n## Outputs\n')
for line in format_outputs(outputs):
buffer.append(line)
buffer.extend(iter(format_outputs(outputs)))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_doc refactored with the following changes:

Comment on lines -228 to +227
m = re.search('(?sm)%s.*%s' % (MARK_BEGIN, MARK_END), readme)
m = re.search(f'(?sm){MARK_BEGIN}.*{MARK_END}', readme)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function replace_doc refactored with the following changes:

Comment on lines -244 to +245
variables += [v for v in parse_items(
file.read(), RE_VARIABLES, VariableToken, Variable, VariableData)]
variables += list(
parse_items(file.read(), RE_VARIABLES, VariableToken, Variable,
VariableData))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_variables refactored with the following changes:

Comment on lines -254 to +256
outputs += [o for o in parse_items(
file.read(), RE_OUTPUTS, OutputToken, Output, OutputData)]
outputs += list(
parse_items(file.read(), RE_OUTPUTS, OutputToken, Output,
OutputData))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_outputs refactored with the following changes:

Comment on lines -268 to -271
m = re.search('(?sm)%s.*%s' % (MARK_BEGIN, MARK_END), readme)
if not m:
if m := re.search(f'(?sm){MARK_BEGIN}.*{MARK_END}', readme):
return get_doc(variables, outputs) in readme
else:
return
return get_doc(variables, outputs) in readme
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function check_state refactored with the following changes:

@sourcery-ai
Copy link
Author

sourcery-ai bot commented Jan 18, 2023

Sourcery Code Quality Report

✅  Merging this PR will increase code quality in the affected files by 0.04%.

Quality metrics Before After Change
Complexity 22.99 😞 22.65 😞 -0.34 👍
Method Length 99.50 🙂 99.56 🙂 0.06 👎
Working memory 12.02 😞 12.13 😞 0.11 👎
Quality 49.97% 😞 50.01% 🙂 0.04% 👍
Other metrics Before After Change
Lines 2026 2037 11
Changed files Quality Before Quality After Quality Change
modules/silicon_design/scripts/build/images/provision/papermill-launcher 84.26% ⭐ 83.60% ⭐ -0.66% 👎
radlab-launcher/radlab.py 36.38% 😞 36.66% 😞 0.28% 👍
radlab-launcher/terraform_installer.py 49.49% 😞 54.43% 🙂 4.94% 👍
tools/build_readme.py 66.54% 🙂 66.22% 🙂 -0.32% 👎
tools/check-license.py 76.08% ⭐ 76.18% ⭐ 0.10% 👍
tools/check-tf-plan.py 53.42% 🙂 52.23% 🙂 -1.19% 👎
tools/check_boilerplate.py 60.33% 🙂 60.41% 🙂 0.08% 👍
tools/check_documentation.py 50.05% 🙂 54.71% 🙂 4.66% 👍
tools/notifications.py 50.27% 🙂 49.94% 😞 -0.33% 👎
tools/service-catalog.py 48.95% 😞 48.86% 😞 -0.09% 👎
tools/tfdoc.py 76.66% ⭐ 76.03% ⭐ -0.63% 👎

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation
radlab-launcher/radlab.py moduleperm 128 ⛔ 928 ⛔ 24 ⛔ 2.28% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
radlab-launcher/radlab.py launcherperm 38 ⛔ 399 ⛔ 19 ⛔ 12.31% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
tools/check-tf-plan.py main 34 ⛔ 309 ⛔ 13 😞 22.06% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
tools/notifications.py main 28 😞 254 ⛔ 16 ⛔ 23.22% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
radlab-launcher/radlab.py module_deploy_common_settings 16 🙂 307 ⛔ 22 ⛔ 24.76% ⛔ Try splitting into smaller methods. Extract out complex expressions

Legend and Explanation

The emojis denote the absolute quality of the code:

  • ⭐ excellent
  • 🙂 good
  • 😞 poor
  • ⛔ very poor

The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request.


Please see our documentation here for details on how these metrics are calculated.

We are actively working on this report - lots more documentation and extra metrics to come!

Help us improve this quality report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants