Skip to content

Commit

Permalink
work for push events
Browse files Browse the repository at this point in the history
  • Loading branch information
miles-kt-inkeep committed Feb 14, 2024
1 parent 21ad33d commit 3ab52b5
Showing 1 changed file with 33 additions and 69 deletions.
102 changes: 33 additions & 69 deletions entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,95 +5,59 @@


def read_json(filepath):
"""
Read a json file as a dictionary.
Parameters
----------
filepath : str
Returns
-------
data : dict
"""
with open(filepath, 'r') as f:
with open(filepath, "r") as f:
return json.load(f)


def get_actions_input(input_name):
"""
Get a Github actions input by name.
Parameters
----------
input_name : str
Returns
-------
action_input : str
return os.getenv("INPUT_{}".format(input_name).upper())

Notes
-----
GitHub Actions creates an environment variable for the input with the name:

INPUT_<CAPITALIZED_VARIABLE_NAME> (e.g. "INPUT_FOO" for "foo")
def load_template(filename):
template_path = os.path.join(".github/workflows", filename)
with open(template_path, "r") as f:
return f.read()

References
----------
.. [1] https://help.github.com/en/actions/automating-your-workflow-with-github-actions/metadata-syntax-for-github-actions#example # noqa: E501

"""
return os.getenv('INPUT_{}'.format(input_name).upper())
def find_pr_by_sha(repo, sha):
"""Find a PR by commit SHA.
Parameters:
- repo: The repository object from PyGithub.
- sha: The commit SHA to search for in PRs.
def load_template(filename):
Returns:
- The pull request object if found, otherwise None.
"""
Load a template.
prs = repo.get_pulls(state="all")
for pr in prs:
if pr.merge_commit_sha == sha:
return pr
return None

Parameters
----------
filename : template file name

Returns
-------
template : str
def main():
gh = Github(os.getenv("GITHUB_TOKEN"))
event = read_json(os.getenv("GITHUB_EVENT_PATH"))
repo = gh.get_repo(event["repository"]["full_name"])
sha = event.get("after") # The commit SHA from the push event

"""
template_path = os.path.join('.github/workflows', filename)
with open(template_path, 'r') as f:
return f.read()
pr = find_pr_by_sha(repo, sha)
if pr is None:
print(f"No PR found for commit {sha}")
return

new_comment = "Inkeep sync has been started"

def main():
# search a pull request that triggered this action
gh = Github(os.getenv('GITHUB_TOKEN'))
event = read_json(os.getenv('GITHUB_EVENT_PATH'))
branch_label = event['pull_request']['head']['label'] # author:branch
branch_name = branch_label.split(':')[-1]
repo = gh.get_repo(event['repository']['full_name'])
prs = repo.get_pulls(state='open', sort='created', head=branch_label)
pr = prs[0]

# load template
template = load_template(get_actions_input('filename'))

# build a comment
pr_info = {
'pull_id': pr.number,
'branch_name': branch_name
}
new_comment = template.format(**pr_info)

# check if this pull request has a duplicated comment
# Check for duplicated comment
old_comments = [c.body for c in pr.get_issue_comments()]
if new_comment in old_comments:
print('This pull request already a duplicated comment.')
exit(0)
print("This pull request already has a duplicated comment.")
return

# add the comment
# Add the comment
pr.create_issue_comment(new_comment)


if __name__ == '__main__':
if __name__ == "__main__":
main()

0 comments on commit 3ab52b5

Please sign in to comment.