-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Pull request updater for azure client #3153
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
568495d
adding pull request updater for azure client
milind009 15f3849
renaming fetch_pull_request function
milind009 02e09c6
renaming function call in azure pull request updater
milind009 91c4d97
adding uuid to temp branch name
milind009 ee5f605
using securerandom instead of uuid
milind009 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
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,112 @@ | ||
# frozen_string_literal: true | ||
|
||
require "dependabot/clients/azure" | ||
require "securerandom" | ||
|
||
module Dependabot | ||
class PullRequestUpdater | ||
class Azure | ||
OBJECT_ID_FOR_BRANCH_DELETE = "0000000000000000000000000000000000000000" | ||
|
||
attr_reader :source, :files, :base_commit, :old_commit, :credentials, | ||
:pull_request_number | ||
|
||
def initialize(source:, files:, base_commit:, old_commit:, | ||
credentials:, pull_request_number:) | ||
@source = source | ||
@files = files | ||
@base_commit = base_commit | ||
@old_commit = old_commit | ||
@credentials = credentials | ||
@pull_request_number = pull_request_number | ||
end | ||
|
||
def update | ||
return unless pull_request_exists? && source_branch_exists? | ||
|
||
update_source_branch | ||
end | ||
|
||
private | ||
|
||
def azure_client_for_source | ||
@azure_client_for_source ||= | ||
Dependabot::Clients::Azure.for_source( | ||
source: source, | ||
credentials: credentials | ||
) | ||
end | ||
|
||
def pull_request_exists? | ||
pull_request | ||
rescue Dependabot::Clients::Azure::NotFound | ||
false | ||
end | ||
|
||
def source_branch_exists? | ||
azure_client_for_source.branch(source_branch_name) | ||
rescue Dependabot::Clients::Azure::NotFound | ||
false | ||
end | ||
|
||
# Currently the PR diff in ADO shows difference in commits instead of actual diff in files. | ||
# This workaround is done to get the target branch commit history on the source branch alongwith file changes | ||
def update_source_branch | ||
# 1) Push the file changes to a newly created temporary branch (from base commit) | ||
new_commit = create_temp_branch | ||
# 2) Update PR source branch to point to the temp branch head commit. | ||
update_branch(source_branch_name, old_source_branch_commit, new_commit) | ||
# 3) Delete temp branch | ||
update_branch(temp_branch_name, new_commit, OBJECT_ID_FOR_BRANCH_DELETE) | ||
end | ||
|
||
def pull_request | ||
@pull_request ||= | ||
azure_client_for_source.pull_request(pull_request_number.to_s) | ||
end | ||
|
||
def source_branch_name | ||
@source_branch_name ||= pull_request&.fetch("sourceRefName")&.gsub("refs/heads/", "") | ||
end | ||
|
||
def create_temp_branch | ||
response = azure_client_for_source.create_commit( | ||
temp_branch_name, | ||
base_commit, | ||
commit_message, | ||
files, | ||
nil | ||
) | ||
|
||
JSON.parse(response.body).fetch("refUpdates").first.fetch("newObjectId") | ||
end | ||
|
||
def temp_branch_name | ||
@temp_branch_name ||= | ||
"#{source_branch_name}-temp-#{SecureRandom.uuid[0..6]}" | ||
end | ||
|
||
def update_branch(branch_name, old_commit, new_commit) | ||
azure_client_for_source.update_ref( | ||
branch_name, | ||
old_commit, | ||
new_commit | ||
) | ||
end | ||
|
||
# For updating source branch, we require the latest commit for the source branch. | ||
def commit_being_updated | ||
@commit_being_updated ||= | ||
azure_client_for_source.commits(source_branch_name).first | ||
end | ||
|
||
def old_source_branch_commit | ||
commit_being_updated.fetch("commitId") | ||
end | ||
|
||
def commit_message | ||
commit_being_updated.fetch("comment") | ||
end | ||
end | ||
end | ||
end |
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
Oops, something went wrong.
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.
Not blocking for now, but we might want to handle non-200 responses 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.
I see we have some sort of handling in the get method of the azure client. Can you specify the scenarios that we might want to cover here?