comment-pr.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env python
  2. import os
  3. import json
  4. import argparse
  5. from github import Github, Auth as GithubAuth
  6. from github.PullRequest import PullRequest
  7. from gh_status import update_pr_comment_text
  8. def main():
  9. parser = argparse.ArgumentParser()
  10. parser.add_argument("--rewrite", dest="rewrite", action="store_true")
  11. parser.add_argument("--color", dest="color", default="white")
  12. parser.add_argument("--fail", dest="fail", action="store_true")
  13. parser.add_argument("--ok", dest="ok", action="store_true")
  14. parser.add_argument("text", type=argparse.FileType("r"), nargs="?", default="-")
  15. args = parser.parse_args()
  16. color = args.color
  17. if args.ok:
  18. color = 'green'
  19. elif args.fail:
  20. color = 'red'
  21. run_number = int(os.environ.get("GITHUB_RUN_NUMBER"))
  22. build_preset = os.environ["BUILD_PRESET"]
  23. gh = Github(auth=GithubAuth.Token(os.environ["GITHUB_TOKEN"]))
  24. with open(os.environ["GITHUB_EVENT_PATH"]) as fp:
  25. event = json.load(fp)
  26. pr = gh.create_from_raw_data(PullRequest, event["pull_request"])
  27. update_pr_comment_text(pr, build_preset, run_number, color, args.text.read().rstrip(), args.rewrite)
  28. if __name__ == "__main__":
  29. if os.environ.get('GITHUB_EVENT_NAME', '').startswith('pull_request'):
  30. main()