classAzureProvider_4.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. def comp_commit_file_and_create_fixpr(self, comp, files_to_patch):
  2. if len(files_to_patch) == 0:
  3. print('BD-Scan-Action: WARN: Unable to apply fix patch - cannot determine containing package file')
  4. return False
  5. new_branch_seed = '%030x' % random.randrange(16 ** 30)
  6. new_branch_name = f"synopsys-enablement-{new_branch_seed}"
  7. globals.printdebug(f"DEBUG: Get commit for head of {self.azure_build_source_branch}'")
  8. commits = self.azure_git_client.get_commits(self.azure_repo_id, None)
  9. head_commit = commits[0]
  10. globals.printdebug(f"DEBUG: Head commit={head_commit.commit_id}")
  11. globals.printdebug(f"DEBUG: Creating new ref 'refs/heads/{new_branch_name}'")
  12. self.azure_create_branch(head_commit.commit_id, new_branch_name)
  13. gitRefUpdate = GitRefUpdate()
  14. gitRefUpdate.name = f"refs/heads/{new_branch_name}"
  15. gitRefUpdate.old_object_id = head_commit.commit_id
  16. gitPush = GitPush()
  17. gitPush.commits = []
  18. gitPush.ref_updates = [gitRefUpdate]
  19. # for file_to_patch in globals.files_to_patch:
  20. for pkgfile in files_to_patch:
  21. globals.printdebug(f"DEBUG: Upload file '{pkgfile}'")
  22. try:
  23. with open(files_to_patch[pkgfile], 'r') as fp:
  24. new_contents = fp.read()
  25. except Exception as exc:
  26. print(f"BD-Scan-Action: ERROR: Unable to open package file '{files_to_patch[pkgfile]}'"
  27. f" - {str(exc)}")
  28. return False
  29. gitCommitRef = GitCommitRef()
  30. gitCommitRef.comment = "Added Synopsys pipeline template"
  31. gitCommitRef.changes = [
  32. {
  33. 'changeType': 'edit',
  34. 'item': {
  35. 'path': pkgfile
  36. },
  37. 'newContent': {
  38. 'content': new_contents,
  39. 'contentType': 'rawText'
  40. }
  41. }
  42. ]
  43. gitPush.commits.append(gitCommitRef)
  44. # globals.printdebug(f"DEBUG: Update file '{pkgfile}' with commit message '{commit_message}'")
  45. # file = repo.update_file(pkgfile, commit_message, new_contents, orig_contents.sha, branch=new_branch_name)
  46. push = self.azure_git_client.create_push(gitPush, self.azure_repo_id)
  47. if not push:
  48. print(f"BD-Scan-Action: ERROR: Create push failed")
  49. sys.exit(1)
  50. pr_title = f"Black Duck: Upgrade {comp.name} to version {comp.goodupgrade} fix known security vulerabilities"
  51. pr_body = f"\n# Synopsys Black Duck Auto Pull Request\n" \
  52. f"Upgrade {comp.name} from version {comp.version} to " \
  53. f"{comp.goodupgrade} in order to fix security vulnerabilities:\n\n"
  54. gitPullRequest = GitPullRequest()
  55. gitPullRequest.source_ref_name = f"refs/heads/{new_branch_name}"
  56. gitPullRequest.target_ref_name = self.azure_build_source_branch
  57. gitPullRequest.title = pr_title
  58. gitPullRequest.description = pr_body
  59. pull = self.azure_git_client.create_pull_request(gitPullRequest, self.azure_repo_id)
  60. if not pull:
  61. print(f"BD-Scan-Action: ERROR: Create pull request failed")
  62. sys.exit(1)
  63. return True