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

Fix bugs found when trying to uplift #1655

Merged
merged 1 commit into from
Feb 13, 2019
Merged
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
69 changes: 45 additions & 24 deletions script/pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ def parse_args():
parser.add_argument('--title',
help='title to use (instead of inferring one from the first commit)',
default=None)
parser.add_argument('-f', '--force', action='store_true',
help='use the script forcefully; ignore warnings')

return parser.parse_args()

Expand Down Expand Up @@ -193,11 +191,13 @@ def main():
if result != 0:
return result

# get all channel branches (starting at master)
brave_browser_version = get_remote_version('master')
remote_branches = get_remote_channel_branches(brave_browser_version)
top_level_base = 'master'

# if starting point is NOT nightly, remove options which aren't desired
# also, find the branch which should be used for diffs (for cherry-picking)
local_version = get_raw_version()
remote_branches = get_remote_channel_branches(local_version)
top_level_base = 'master'
if not is_nightly(args.start_from):
top_level_base = remote_branches[args.start_from]
try:
Expand All @@ -219,6 +219,8 @@ def main():
local_branch = 'pr' + str(pr_number) + '_' + head['ref']
head_sha = head['sha']
top_level_base = response['base']['ref']
top_level_sha = response['base']['sha']
merged_at = str(response['merged_at']).strip()

except Exception as e:
print('[ERROR] API returned an error when looking up pull request "' + str(pr_number) + '"\n' + str(e))
Expand All @@ -232,6 +234,12 @@ def main():
branch_index = remote_branches.index(top_level_base)
config.channels_to_process = config.channel_names[branch_index:]

# if PR was already merged, use the SHA it was PRed against
if len(merged_at) > 0:
print('pr was already merged at ' + merged_at + '; using "' + top_level_sha +
'" instead of "' + top_level_base + '"')
top_level_base = top_level_sha

# create local branch which matches the contents of the PR
with scoped_cwd(BRAVE_CORE_ROOT):
# check if branch exists already
Expand All @@ -241,38 +249,25 @@ def main():
branch_sha = ''
if len(branch_sha) > 0:
# branch exists; reset it
print('branch "' + local_branch + '" exists; resetting to origin/' + head['ref'] + ' (' + head_sha + ')')
print('branch "' + local_branch + '" exists; resetting to origin/' + head['ref'] +
' (' + head_sha + ')')
execute(['git', 'checkout', local_branch])
execute(['git', 'reset', '--hard', head_sha])
else:
# create the branch
print('creating branch "' + local_branch + '" using origin/' + head['ref'] + ' (' + head_sha + ')')
execute(['git', 'checkout', '-b', local_branch, head_sha])
# now that branch exists, switch back to previous local branch
execute(['git', 'checkout', '-'])

# get local version + latest version on remote (master)
# if they don't match, rebase is needed
remote_version = get_remote_version(top_level_base)
if local_version != remote_version:
if not args.force:
print('[ERROR] Your branch is out of sync (local=' + local_version +
', remote=' + remote_version + '); please rebase (ex: "git rebase origin/' +
top_level_base + '"). NOTE: You can bypass this check by using -f')
return 1
print('[WARNING] Your branch is out of sync (local=' + local_version +
', remote=' + remote_version + '); continuing since -f was provided')

# If title isn't set already, generate one from first commit
local_branch = get_local_branch_name(BRAVE_CORE_ROOT)
if not config.title:
if not config.title and not args.uplift_using_pr:
config.title = get_title_from_first_commit(BRAVE_CORE_ROOT, top_level_base)

# Create a branch for each channel
print('\nCreating branches...')
fancy_print('NOTE: Commits are being detected by diffing "' + local_branch + '" against "' + top_level_base + '"')
remote_branches = get_remote_channel_branches(local_version)
local_branches = {}
branch = ''
try:
for channel in config.channels_to_process:
branch = create_branch(channel, top_level_base, remote_branches[channel], local_branch)
Expand Down Expand Up @@ -304,6 +299,25 @@ def main():
return 0


def is_sha(ref):
global config
repo = GitHub(config.github_token).repos(BRAVE_CORE_REPO)
try:
repo.git.commits(str(ref)).get()
except Exception as e:
response = str(e)
try:
# Unsure of original intention, but github.py (on purpose) throws
# an exception if the response body contains `message`.
# This catch has the response (in JSON) in exception
json_response = json.loads(response)
if json_response['sha'] == ref:
return True
except Exception as e2:
return False
return False


def create_branch(channel, top_level_base, remote_base, local_branch):
global config

Expand All @@ -312,10 +326,17 @@ def create_branch(channel, top_level_base, remote_base, local_branch):

channel_branch = remote_base + '_' + local_branch

if is_sha(top_level_base):
compare_from = top_level_base
else:
compare_from = 'origin/' + top_level_base

with scoped_cwd(BRAVE_CORE_ROOT):
# get SHA for all commits (in order)
sha_list = execute(['git', 'log', 'origin/' + top_level_base + '..HEAD', '--pretty=format:%h', '--reverse'])
sha_list = execute(['git', 'log', compare_from + '..HEAD', '--pretty=format:%h', '--reverse'])
sha_list = sha_list.split('\n')
if len(sha_list) == 0:
raise Exception('No changes detected!')
try:
# check if branch exists already
try:
Expand Down Expand Up @@ -369,7 +390,7 @@ def submit_pr(channel, top_level_base, remote_base, local_branch):
return 0

print('(' + channel + ') creating pull request')
pr_title = config.title
pr_title = config.title or ''
pr_dst = remote_base
if is_nightly(channel):
pr_dst = 'master'
Expand Down