get_env.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python3
  2. import ssl
  3. import json
  4. import os
  5. import shlex
  6. import re
  7. import string
  8. import random
  9. import argparse
  10. import datetime
  11. import urllib.request
  12. def id_gen(size=5, chars=string.ascii_uppercase + string.digits):
  13. return "".join(random.choice(chars) for _ in range(size))
  14. def parse_args():
  15. parser = argparse.ArgumentParser()
  16. parser.add_argument("--event_file", help="Current GitHub event file", required=True)
  17. parser.add_argument(
  18. "--type",
  19. help="Event file type",
  20. required=True,
  21. choices=["pull", "tag", "other"],
  22. )
  23. args = parser.parse_args()
  24. return args
  25. def get_commit_json(event):
  26. context = ssl._create_unverified_context()
  27. with urllib.request.urlopen(
  28. event["pull_request"]["_links"]["commits"]["href"], context=context
  29. ) as commit_file:
  30. commit_json = json.loads(commit_file.read().decode("utf-8"))
  31. return commit_json
  32. def get_details(event, args):
  33. data = {}
  34. current_time = datetime.datetime.utcnow().date()
  35. if args.type == "pull":
  36. commit_json = get_commit_json(event)
  37. data["commit_comment"] = shlex.quote(commit_json[-1]["commit"]["message"])
  38. data["commit_hash"] = commit_json[-1]["sha"]
  39. ref = event["pull_request"]["head"]["ref"]
  40. data["pull_id"] = event["pull_request"]["number"]
  41. data["pull_name"] = shlex.quote(event["pull_request"]["title"])
  42. elif args.type == "tag":
  43. data["commit_comment"] = shlex.quote(event["head_commit"]["message"])
  44. data["commit_hash"] = event["head_commit"]["id"]
  45. ref = event["ref"]
  46. else:
  47. data["commit_comment"] = shlex.quote(event["commits"][-1]["message"])
  48. data["commit_hash"] = event["commits"][-1]["id"]
  49. ref = event["ref"]
  50. data["commit_sha"] = data["commit_hash"][:8]
  51. data["branch_name"] = re.sub("refs/\w+/", "", ref)
  52. data["suffix"] = (
  53. data["branch_name"].replace("/", "_")
  54. + "-"
  55. + current_time.strftime("%d%m%Y")
  56. + "-"
  57. + data["commit_sha"]
  58. )
  59. if ref.startswith("refs/tags/"):
  60. data["suffix"] = data["branch_name"].replace("/", "_")
  61. return data
  62. def add_env(name, value, file):
  63. delimeter = id_gen()
  64. print(f"{name}<<{delimeter}", file=file)
  65. print(f"{value}", file=file)
  66. print(f"{delimeter}", file=file)
  67. def add_envs(data, env_file, args):
  68. add_env("COMMIT_MSG", data["commit_comment"], env_file)
  69. add_env("COMMIT_HASH", data["commit_hash"], env_file)
  70. add_env("COMMIT_SHA", data["commit_sha"], env_file)
  71. add_env("SUFFIX", data["suffix"], env_file)
  72. add_env("BRANCH_NAME", data["branch_name"], env_file)
  73. add_env("DIST_SUFFIX", data["suffix"], env_file)
  74. add_env("WORKFLOW_BRANCH_OR_TAG", data["branch_name"], env_file)
  75. if args.type == "pull":
  76. add_env("PULL_ID", data["pull_id"], env_file)
  77. add_env("PULL_NAME", data["pull_name"], env_file)
  78. def main():
  79. args = parse_args()
  80. event_file = open(args.event_file)
  81. event = json.load(event_file)
  82. env_file = open(os.environ["GITHUB_ENV"], "a")
  83. data = get_details(event, args)
  84. add_envs(data, env_file, args)
  85. event_file.close()
  86. env_file.close()
  87. if __name__ == "__main__":
  88. main()