Update string formatting

This commit is contained in:
Peter Evans 2019-12-28 17:09:58 +09:00
parent 4f8d5b9d3e
commit b3805d65e3
2 changed files with 32 additions and 40 deletions

View file

@ -63,55 +63,49 @@ def create_or_update_pull_request(
pull_request = github_repo.create_pull( pull_request = github_repo.create_pull(
title=title, body=body, base=base, head=branch title=title, body=body, base=base, head=branch
) )
print( print(f"Created pull request #{pull_request.number} ({branch} => {base})")
"Created pull request #%d (%s => %s)" % (pull_request.number, branch, base)
)
except GithubException as e: except GithubException as e:
if e.status == 422: if e.status == 422:
# Format the branch name # A pull request exists for this branch and base
head_branch = "%s:%s" % (github_repository.split("/")[0], branch) head_branch = "{}:{}".format(github_repository.split("/")[0], branch)
# Get the pull request # Get the pull request
pull_request = github_repo.get_pulls( pull_request = github_repo.get_pulls(
state="open", base=base, head=head_branch state="open", base=base, head=head_branch
)[0] )[0]
print( print(f"Updated pull request #{pull_request.number} ({branch} => {base})")
"Updated pull request #%d (%s => %s)"
% (pull_request.number, branch, base)
)
else: else:
print(str(e)) print(str(e))
raise raise
# Set the output variables # Set the output variables
os.system("echo ::set-env name=PULL_REQUEST_NUMBER::%d" % pull_request.number) os.system(f"echo ::set-env name=PULL_REQUEST_NUMBER::{pull_request.number}")
os.system("echo ::set-output name=pr_number::%d" % pull_request.number) os.system(f"echo ::set-output name=pr_number::{pull_request.number}")
# Set labels, assignees and milestone # Set labels, assignees and milestone
if labels is not None: if labels is not None:
print("Applying labels '%s'" % labels) print(f"Applying labels '{labels}'")
pull_request.as_issue().edit(labels=cs_string_to_list(labels)) pull_request.as_issue().edit(labels=cs_string_to_list(labels))
if assignees is not None: if assignees is not None:
print("Applying assignees '%s'" % assignees) print(f"Applying assignees '{assignees}'")
pull_request.as_issue().edit(assignees=cs_string_to_list(assignees)) pull_request.as_issue().edit(assignees=cs_string_to_list(assignees))
if milestone is not None: if milestone is not None:
print("Applying milestone '%s'" % milestone) print(f"Applying milestone '{milestone}'")
milestone = github_repo.get_milestone(int(milestone)) milestone = github_repo.get_milestone(int(milestone))
pull_request.as_issue().edit(milestone=milestone) pull_request.as_issue().edit(milestone=milestone)
# Set pull request reviewers # Set pull request reviewers
if reviewers is not None: if reviewers is not None:
print("Requesting reviewers '%s'" % reviewers) print(f"Requesting reviewers '{reviewers}'")
try: try:
pull_request.create_review_request(reviewers=cs_string_to_list(reviewers)) pull_request.create_review_request(reviewers=cs_string_to_list(reviewers))
except GithubException as e: except GithubException as e:
# Likely caused by "Review cannot be requested from pull request # Likely caused by "Review cannot be requested from pull request author."
# author."
if e.status == 422: if e.status == 422:
print("Requesting reviewers failed - %s" % e.data["message"]) print("Request reviewers failed - {}".format(e.data["message"]))
# Set pull request team reviewers # Set pull request team reviewers
if team_reviewers is not None: if team_reviewers is not None:
print("Requesting team reviewers '%s'" % team_reviewers) print(f"Requesting team reviewers '{team_reviewers}'")
pull_request.create_review_request( pull_request.create_review_request(
team_reviewers=cs_string_to_list(team_reviewers) team_reviewers=cs_string_to_list(team_reviewers)
) )
@ -126,5 +120,7 @@ def create_or_update_pull_request(
# Likely caused by "Project already has the associated issue." # Likely caused by "Project already has the associated issue."
if e.status == 422: if e.status == 422:
print( print(
"Create project card failed - %s" % e.data["errors"][0]["message"] "Create project card failed - {}".format(
e.data["errors"][0]["message"]
)
) )

View file

@ -63,55 +63,49 @@ def create_or_update_pull_request(
pull_request = github_repo.create_pull( pull_request = github_repo.create_pull(
title=title, body=body, base=base, head=branch title=title, body=body, base=base, head=branch
) )
print( print(f"Created pull request #{pull_request.number} ({branch} => {base})")
"Created pull request #%d (%s => %s)" % (pull_request.number, branch, base)
)
except GithubException as e: except GithubException as e:
if e.status == 422: if e.status == 422:
# Format the branch name # A pull request exists for this branch and base
head_branch = "%s:%s" % (github_repository.split("/")[0], branch) head_branch = "{}:{}".format(github_repository.split("/")[0], branch)
# Get the pull request # Get the pull request
pull_request = github_repo.get_pulls( pull_request = github_repo.get_pulls(
state="open", base=base, head=head_branch state="open", base=base, head=head_branch
)[0] )[0]
print( print(f"Updated pull request #{pull_request.number} ({branch} => {base})")
"Updated pull request #%d (%s => %s)"
% (pull_request.number, branch, base)
)
else: else:
print(str(e)) print(str(e))
raise raise
# Set the output variables # Set the output variables
os.system("echo ::set-env name=PULL_REQUEST_NUMBER::%d" % pull_request.number) os.system(f"echo ::set-env name=PULL_REQUEST_NUMBER::{pull_request.number}")
os.system("echo ::set-output name=pr_number::%d" % pull_request.number) os.system(f"echo ::set-output name=pr_number::{pull_request.number}")
# Set labels, assignees and milestone # Set labels, assignees and milestone
if labels is not None: if labels is not None:
print("Applying labels '%s'" % labels) print(f"Applying labels '{labels}'")
pull_request.as_issue().edit(labels=cs_string_to_list(labels)) pull_request.as_issue().edit(labels=cs_string_to_list(labels))
if assignees is not None: if assignees is not None:
print("Applying assignees '%s'" % assignees) print(f"Applying assignees '{assignees}'")
pull_request.as_issue().edit(assignees=cs_string_to_list(assignees)) pull_request.as_issue().edit(assignees=cs_string_to_list(assignees))
if milestone is not None: if milestone is not None:
print("Applying milestone '%s'" % milestone) print(f"Applying milestone '{milestone}'")
milestone = github_repo.get_milestone(int(milestone)) milestone = github_repo.get_milestone(int(milestone))
pull_request.as_issue().edit(milestone=milestone) pull_request.as_issue().edit(milestone=milestone)
# Set pull request reviewers # Set pull request reviewers
if reviewers is not None: if reviewers is not None:
print("Requesting reviewers '%s'" % reviewers) print(f"Requesting reviewers '{reviewers}'")
try: try:
pull_request.create_review_request(reviewers=cs_string_to_list(reviewers)) pull_request.create_review_request(reviewers=cs_string_to_list(reviewers))
except GithubException as e: except GithubException as e:
# Likely caused by "Review cannot be requested from pull request # Likely caused by "Review cannot be requested from pull request author."
# author."
if e.status == 422: if e.status == 422:
print("Requesting reviewers failed - %s" % e.data["message"]) print("Request reviewers failed - {}".format(e.data["message"]))
# Set pull request team reviewers # Set pull request team reviewers
if team_reviewers is not None: if team_reviewers is not None:
print("Requesting team reviewers '%s'" % team_reviewers) print(f"Requesting team reviewers '{team_reviewers}'")
pull_request.create_review_request( pull_request.create_review_request(
team_reviewers=cs_string_to_list(team_reviewers) team_reviewers=cs_string_to_list(team_reviewers)
) )
@ -126,5 +120,7 @@ def create_or_update_pull_request(
# Likely caused by "Project already has the associated issue." # Likely caused by "Project already has the associated issue."
if e.status == 422: if e.status == 422:
print( print(
"Create project card failed - %s" % e.data["errors"][0]["message"] "Create project card failed - {}".format(
e.data["errors"][0]["message"]
)
) )