get_env.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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_set_output_var(name, value, file):
  68. print(f"{name}={value}", file=file)
  69. def add_envs(data, gh_env_file, gh_out_file, args):
  70. add_env("COMMIT_MSG", data["commit_comment"], gh_env_file)
  71. add_env("COMMIT_HASH", data["commit_hash"], gh_env_file)
  72. add_env("COMMIT_SHA", data["commit_sha"], gh_env_file)
  73. add_env("SUFFIX", data["suffix"], gh_env_file)
  74. add_env("BRANCH_NAME", data["branch_name"], gh_env_file)
  75. add_env("DIST_SUFFIX", data["suffix"], gh_env_file)
  76. add_env("WORKFLOW_BRANCH_OR_TAG", data["branch_name"], gh_env_file)
  77. add_set_output_var("commit_msg", data["commit_comment"], gh_out_file)
  78. add_set_output_var("commit_hash", data["commit_hash"], gh_out_file)
  79. add_set_output_var("commit_sha", data["commit_sha"], gh_out_file)
  80. add_set_output_var("suffix", data["suffix"], gh_out_file)
  81. add_set_output_var("branch_name", data["branch_name"], gh_out_file)
  82. add_set_output_var("dist_suffix", data["suffix"], gh_out_file)
  83. add_set_output_var("default_target", os.getenv("DEFAULT_TARGET"), gh_out_file)
  84. if args.type == "pull":
  85. add_env("PULL_ID", data["pull_id"], gh_env_file)
  86. add_env("PULL_NAME", data["pull_name"], gh_env_file)
  87. add_set_output_var("pull_id", data["pull_id"], gh_out_file)
  88. add_set_output_var("pull_name", data["pull_name"], gh_out_file)
  89. def main():
  90. args = parse_args()
  91. event_file = open(args.event_file, "r")
  92. event = json.load(event_file)
  93. gh_env_file = open(os.environ["GITHUB_ENV"], "a")
  94. gh_out_file = open(os.environ["GITHUB_OUTPUT"], "a")
  95. data = get_details(event, args)
  96. add_envs(data, gh_env_file, gh_out_file, args)
  97. event_file.close()
  98. gh_env_file.close()
  99. gh_out_file.close()
  100. if __name__ == "__main__":
  101. main()