in asfyaml/feature/github/__init__.py [0:0]
def run(self):
"""GitHub features"""
# Test if we need to process this (only works on the default branch)
if self.instance.branch != self.repository.default_branch:
print(
"[github] Saw GitHub meta-data in .asf.yaml, but not in default branch of repository, not updating..."
)
return
# Check if cached yaml exists, compare if changed
self.previous_yaml = {}
yaml_filepath = f"{BASE_CACHE_PATH}/ghsettings.{self.repository.name}.yml"
if not self.instance.no_cache:
try:
if os.path.exists(yaml_filepath):
self.previous_yaml = yaml.safe_load(open(yaml_filepath).read())
self.previous_yaml.pop("refname", "")
if self.previous_yaml == self.yaml:
if DEBUG:
print("[github] Saw no changes to GitHub settings, skipping this run.")
return
except yaml.YAMLError as _e: # Failed to parse old yaml? bah.
print("[github] Failed to parse previous GitHub settings, please notify users@infra.apache.org")
# Update items
print(f"[github] GitHub meta-data changed for {self.repository.name}, updating...")
gh_token = os.environ.get("GH_TOKEN")
if not self.noop("github"):
# if a GH_TOKEN is set as environment variable, use this, otherwise load it from file
if not gh_token:
gh_token = open(GH_TOKEN_FILE).read().strip()
self._gh = pygithub.Github(auth=pygithubAuth.Token(gh_token))
self._ghrepo = self.gh.get_repo(f"{self.repository.org_id}/{self.repository.name}")
elif gh_token: # If supplied from OS env, load the ghrepo object anyway
self._gh = pygithub.Github(auth=pygithubAuth.Token(gh_token))
self._ghrepo = self.gh.get_repo(f"{self.repository.org_id}/{self.repository.name}")
# For each sub-feature we see (with the @directive decorator on it), run it
for _feat in _features:
_feat(self)
# Save cached version of this YAML for next time.
if os.path.exists(BASE_CACHE_PATH):
with open(yaml_filepath, "w") as f:
f.write(yaml.dump(self.yaml_raw, default_flow_style=False))
else:
print(f"CACHE Path '{BASE_CACHE_PATH}' does not exist, skip caching")