Remove ignore event logic
This commit is contained in:
parent
a0ad81c6c7
commit
263b5927d1
1 changed files with 87 additions and 100 deletions
|
@ -19,15 +19,6 @@ def get_github_event(github_event_path):
|
||||||
return github_event
|
return github_event
|
||||||
|
|
||||||
|
|
||||||
def ignore_event(event_name, event_data):
|
|
||||||
if event_name == "push":
|
|
||||||
ref = "{ref}".format(**event_data)
|
|
||||||
if not ref.startswith('refs/heads/'):
|
|
||||||
print("Ignoring events for tags and remotes.")
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def get_head_short_sha1(repo):
|
def get_head_short_sha1(repo):
|
||||||
return repo.git.rev_parse('--short', 'HEAD')
|
return repo.git.rev_parse('--short', 'HEAD')
|
||||||
|
|
||||||
|
@ -90,10 +81,7 @@ def cs_string_to_list(str):
|
||||||
return list(filter(None, l))
|
return list(filter(None, l))
|
||||||
|
|
||||||
|
|
||||||
def process_event(event_name, event_data, repo, branch, base, remote_exists):
|
def process_event(github_token, github_repository, repo, branch, base, remote_exists):
|
||||||
# Fetch required environment variables
|
|
||||||
github_token = os.environ['GITHUB_TOKEN']
|
|
||||||
github_repository = os.environ['GITHUB_REPOSITORY']
|
|
||||||
# Fetch optional environment variables with default values
|
# Fetch optional environment variables with default values
|
||||||
commit_message = os.getenv(
|
commit_message = os.getenv(
|
||||||
'COMMIT_MESSAGE',
|
'COMMIT_MESSAGE',
|
||||||
|
@ -111,9 +99,6 @@ def process_event(event_name, event_data, repo, branch, base, remote_exists):
|
||||||
pull_request_reviewers = os.environ.get('PULL_REQUEST_REVIEWERS')
|
pull_request_reviewers = os.environ.get('PULL_REQUEST_REVIEWERS')
|
||||||
pull_request_team_reviewers = os.environ.get('PULL_REQUEST_TEAM_REVIEWERS')
|
pull_request_team_reviewers = os.environ.get('PULL_REQUEST_TEAM_REVIEWERS')
|
||||||
|
|
||||||
# Update URL for the 'origin' remote
|
|
||||||
set_git_remote_url(repo.git, github_token, github_repository)
|
|
||||||
|
|
||||||
# Push the local changes to the remote branch
|
# Push the local changes to the remote branch
|
||||||
print("Pushing changes.")
|
print("Pushing changes.")
|
||||||
push_result = push_changes(repo.git, branch, commit_message)
|
push_result = push_changes(repo.git, branch, commit_message)
|
||||||
|
@ -161,97 +146,99 @@ def process_event(event_name, event_data, repo, branch, base, remote_exists):
|
||||||
team_reviewers=cs_string_to_list(pull_request_team_reviewers))
|
team_reviewers=cs_string_to_list(pull_request_team_reviewers))
|
||||||
|
|
||||||
|
|
||||||
# Get the JSON event data
|
# Fetch environment variables
|
||||||
|
github_token = os.environ['GITHUB_TOKEN']
|
||||||
|
github_repository = os.environ['GITHUB_REPOSITORY']
|
||||||
|
github_ref = os.environ['GITHUB_REF']
|
||||||
event_name = os.environ['GITHUB_EVENT_NAME']
|
event_name = os.environ['GITHUB_EVENT_NAME']
|
||||||
|
# Get the JSON event data
|
||||||
event_data = get_github_event(os.environ['GITHUB_EVENT_PATH'])
|
event_data = get_github_event(os.environ['GITHUB_EVENT_PATH'])
|
||||||
# Check if this event should be ignored
|
|
||||||
skip_ignore_event = bool(os.environ.get('SKIP_IGNORE'))
|
|
||||||
if skip_ignore_event or not ignore_event(event_name, event_data):
|
|
||||||
# Set the repo to the working directory
|
|
||||||
repo = Repo(os.getcwd())
|
|
||||||
# Get the default for author email and name
|
|
||||||
author_email, author_name = get_author_default(event_name, event_data)
|
|
||||||
# Set commit author overrides
|
|
||||||
author_email = os.getenv('COMMIT_AUTHOR_EMAIL', author_email)
|
|
||||||
author_name = os.getenv('COMMIT_AUTHOR_NAME', author_name)
|
|
||||||
# Set git configuration
|
|
||||||
set_git_config(repo.git, author_email, author_name)
|
|
||||||
|
|
||||||
# Fetch/Set the branch name
|
# Set the repo to the working directory
|
||||||
branch_prefix = os.getenv(
|
repo = Repo(os.getcwd())
|
||||||
'PULL_REQUEST_BRANCH',
|
# Get the default for author email and name
|
||||||
'create-pull-request/patch')
|
author_email, author_name = get_author_default(event_name, event_data)
|
||||||
# Fetch the git ref
|
# Set commit author overrides
|
||||||
github_ref = os.environ['GITHUB_REF']
|
author_email = os.getenv('COMMIT_AUTHOR_EMAIL', author_email)
|
||||||
# Fetch an optional base branch override
|
author_name = os.getenv('COMMIT_AUTHOR_NAME', author_name)
|
||||||
base_override = os.environ.get('PULL_REQUEST_BASE')
|
# Set git configuration
|
||||||
|
set_git_config(repo.git, author_email, author_name)
|
||||||
|
# Update URL for the 'origin' remote
|
||||||
|
set_git_remote_url(repo.git, github_token, github_repository)
|
||||||
|
|
||||||
# Set the base branch
|
# Fetch/Set the branch name
|
||||||
if base_override is not None:
|
branch_prefix = os.getenv(
|
||||||
base = base_override
|
'PULL_REQUEST_BRANCH',
|
||||||
checkout_branch(repo.git, True, base)
|
'create-pull-request/patch')
|
||||||
elif github_ref.startswith('refs/pull/'):
|
# Fetch an optional base branch override
|
||||||
# Switch to the merging branch instead of the merge commit
|
base_override = os.environ.get('PULL_REQUEST_BASE')
|
||||||
base = os.environ['GITHUB_HEAD_REF']
|
|
||||||
repo.git.checkout(base)
|
|
||||||
else:
|
|
||||||
base = github_ref[11:]
|
|
||||||
|
|
||||||
# Skip if the current branch is a PR branch created by this action.
|
# Set the base branch
|
||||||
# This may occur when using a PAT instead of GITHUB_TOKEN.
|
if base_override is not None:
|
||||||
if base.startswith(branch_prefix):
|
base = base_override
|
||||||
print("Branch '%s' was created by this action. Skipping." % base)
|
checkout_branch(repo.git, True, base)
|
||||||
sys.exit()
|
elif github_ref.startswith('refs/pull/'):
|
||||||
|
# Switch to the merging branch instead of the merge commit
|
||||||
|
base = os.environ['GITHUB_HEAD_REF']
|
||||||
|
repo.git.checkout(base)
|
||||||
|
else:
|
||||||
|
base = github_ref[11:]
|
||||||
|
|
||||||
# Fetch an optional environment variable to determine the branch suffix
|
# Skip if the current branch is a PR branch created by this action.
|
||||||
branch_suffix = os.getenv('BRANCH_SUFFIX', 'short-commit-hash')
|
# This may occur when using a PAT instead of GITHUB_TOKEN.
|
||||||
if branch_suffix == "short-commit-hash":
|
if base.startswith(branch_prefix):
|
||||||
# Suffix with the short SHA1 hash
|
print("Branch '%s' was created by this action. Skipping." % base)
|
||||||
branch = "%s-%s" % (branch_prefix, get_head_short_sha1(repo))
|
sys.exit()
|
||||||
elif branch_suffix == "timestamp":
|
|
||||||
# Suffix with the current timestamp
|
# Fetch an optional environment variable to determine the branch suffix
|
||||||
branch = "%s-%s" % (branch_prefix, int(time.time()))
|
branch_suffix = os.getenv('BRANCH_SUFFIX', 'short-commit-hash')
|
||||||
elif branch_suffix == "random":
|
if branch_suffix == "short-commit-hash":
|
||||||
# Suffix with the current timestamp
|
# Suffix with the short SHA1 hash
|
||||||
branch = "%s-%s" % (branch_prefix, get_random_suffix())
|
branch = "%s-%s" % (branch_prefix, get_head_short_sha1(repo))
|
||||||
elif branch_suffix == "none":
|
elif branch_suffix == "timestamp":
|
||||||
# Fixed branch name
|
# Suffix with the current timestamp
|
||||||
branch = branch_prefix
|
branch = "%s-%s" % (branch_prefix, int(time.time()))
|
||||||
else:
|
elif branch_suffix == "random":
|
||||||
|
# Suffix with the current timestamp
|
||||||
|
branch = "%s-%s" % (branch_prefix, get_random_suffix())
|
||||||
|
elif branch_suffix == "none":
|
||||||
|
# Fixed branch name
|
||||||
|
branch = branch_prefix
|
||||||
|
else:
|
||||||
|
print(
|
||||||
|
"Branch suffix '%s' is not a valid value." %
|
||||||
|
branch_suffix)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
# Check if the remote branch exists
|
||||||
|
remote_exists = remote_branch_exists(repo, branch)
|
||||||
|
|
||||||
|
if remote_exists:
|
||||||
|
if branch_suffix == 'short-commit-hash':
|
||||||
|
# A remote branch already exists for the HEAD commit
|
||||||
print(
|
print(
|
||||||
"Branch suffix '%s' is not a valid value." %
|
"Pull request branch '%s' already exists for this commit. Skipping." %
|
||||||
branch_suffix)
|
branch)
|
||||||
|
sys.exit()
|
||||||
|
elif branch_suffix in ['timestamp', 'random']:
|
||||||
|
# Generated branch name clash with an existing branch
|
||||||
|
print(
|
||||||
|
"Pull request branch '%s' already exists. Please re-run." %
|
||||||
|
branch)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Check if the remote branch exists
|
# Checkout branch
|
||||||
remote_exists = remote_branch_exists(repo, branch)
|
checkout_branch(repo.git, remote_exists, branch)
|
||||||
|
|
||||||
if remote_exists:
|
# Check if there are changes to pull request
|
||||||
if branch_suffix == 'short-commit-hash':
|
if repo.is_dirty() or len(repo.untracked_files) > 0:
|
||||||
# A remote branch already exists for the HEAD commit
|
print("Repository has modified or untracked files.")
|
||||||
print(
|
process_event(
|
||||||
"Pull request branch '%s' already exists for this commit. Skipping." %
|
github_token,
|
||||||
branch)
|
github_repository,
|
||||||
sys.exit()
|
repo,
|
||||||
elif branch_suffix in ['timestamp', 'random']:
|
branch,
|
||||||
# Generated branch name clash with an existing branch
|
base,
|
||||||
print(
|
remote_exists)
|
||||||
"Pull request branch '%s' already exists. Please re-run." %
|
else:
|
||||||
branch)
|
print("Repository has no modified or untracked files. Skipping.")
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
# Checkout branch
|
|
||||||
checkout_branch(repo.git, remote_exists, branch)
|
|
||||||
|
|
||||||
# Check if there are changes to pull request
|
|
||||||
if repo.is_dirty() or len(repo.untracked_files) > 0:
|
|
||||||
print("Repository has modified or untracked files.")
|
|
||||||
process_event(
|
|
||||||
event_name,
|
|
||||||
event_data,
|
|
||||||
repo,
|
|
||||||
branch,
|
|
||||||
base,
|
|
||||||
remote_exists)
|
|
||||||
else:
|
|
||||||
print("Repository has no modified or untracked files. Skipping.")
|
|
||||||
|
|
Loading…
Reference in a new issue