react-to-product-owners-yml-changes.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env python3
  2. import os
  3. import sys
  4. from os.path import dirname, join, realpath
  5. import yaml
  6. # Run from project root.
  7. os.chdir(realpath(join(dirname(sys.argv[0]), "..")))
  8. PREFIX = "Product Area: "
  9. LABELS_YAML = ".github/labels.yml"
  10. product_owners = yaml.safe_load(open(sys.argv[1]))
  11. labels = open(LABELS_YAML)
  12. areas = [
  13. # For `sentry` repo we don't want SDK or Docs areas, that stuff should
  14. # be transferred to other repos.
  15. x
  16. for x in product_owners["by_area"]
  17. if not x.startswith("SDK") and not x == "Docs"
  18. ]
  19. fastforward = False
  20. head = []
  21. area_lines = ["- name: 'Product Area: Unknown'\n", " color: '8D5494'\n"]
  22. tail = []
  23. current = head
  24. # Best to look the other way, Buck. This is just waaaay easier than trying to
  25. # use ruamel.yaml to preserve comments and other formatting. What this does is
  26. # loop through line by line, collecting everything before the Product Area
  27. # labels in `head` and everything after in `tail`. It "fast-forwards" past the
  28. # existing Product Area labels. Then we generate output for the new Product
  29. # Area labels, then emit the three parts (head, area_lines, tail) in order.
  30. for line in labels:
  31. if line == "\n":
  32. fastforward = False
  33. elif fastforward:
  34. continue
  35. elif line.startswith("- name: 'Product Area: "):
  36. fastforward = True
  37. current = tail
  38. continue
  39. current.append(line)
  40. for area in areas:
  41. if "'" in area:
  42. area_lines.append(f'- name: "Product Area: {area}"\n')
  43. else:
  44. area_lines.append(f"- name: 'Product Area: {area}'\n")
  45. area_lines.append(" color: '8D5494'\n")
  46. area_lines += ["- name: 'Product Area: Other'\n", " color: '8D5494'\n"]
  47. with open(".github/labels.yml", "w+") as fp:
  48. fp.writelines(head)
  49. fp.writelines(area_lines)
  50. fp.writelines(tail)
  51. # Now for issue templates. Same game. Hang in there.
  52. BEGINNING = (
  53. " # begin product areas - autogenerated by bin/react-to-product-owners-yml-changes.py\n"
  54. )
  55. END = " # end product areas\n"
  56. for template in ("bug", "feature"):
  57. filepath = f".github/ISSUE_TEMPLATE/{template}.yml"
  58. head = []
  59. area_lines = [BEGINNING, " - 'Unknown'\n"]
  60. tail = []
  61. current = head
  62. for line in open(filepath):
  63. if line == END:
  64. fastforward = False
  65. elif line == BEGINNING:
  66. fastforward = True
  67. current = tail
  68. elif not fastforward:
  69. current.append(line)
  70. for area in areas:
  71. if "'" in area:
  72. area_lines.append(f' - "{area}"\n')
  73. else:
  74. area_lines.append(f" - '{area}'\n")
  75. area_lines += [" - 'Other'\n", END]
  76. with open(filepath, "w+") as fp:
  77. fp.writelines(head)
  78. fp.writelines(area_lines)
  79. fp.writelines(tail)