def repo_isempty(repo: github.Repository) -> bool: """ is a GitHub repo empty? Parameters ---------- repo : github.Repository handle to GitHub repo Results ------- empty : bool GitHub repo empty """ try: repo.get_contents('/') empty = False except github.GithubException as e: logging.error(f'{repo.name} is empty. \n') empty = True logging.info(str(e)) return empty
def guess_title(repo: Repository, path: str, branch_name: str): """Guesses the title of a file in a repo or branch.""" if path.endswith('.md'): # If we are dealing with a markdown file, then we will interpret # the first header in the file as the file's title. contents = repo.get_contents(path, ref=branch_name) text = base64.b64decode(contents.content).decode('UTF-8') lines = text.split('\n') for line in lines: stripped = line.strip() if stripped.startswith('#'): return stripped.strip('#').strip() # Turn the file name into a title. return path.split('/')[-1].split('.')[0].replace('-', ' ').replace( '_', ' ').capitalize()
def copy_markdown_images(root: str, file: str, repo: Repository, markdown: str) -> str: template = "![{name}]({path})" paths = [result["path"] for result in parse.findall(template, markdown)] parent = Path(file).parent for path in paths: if path.startswith("http"): continue img_path = Path(parent / path).resolve().relative_to( Path(".").resolve()) img = repo.get_contents(str(img_path)) destination = os.path.realpath(f"{root}/gen_/{img_path}") os.makedirs(os.path.dirname(destination), exist_ok=True) urllib.request.urlretrieve(img.download_url, destination) markdown = markdown.replace(path, f"gen_/{img_path}") return markdown
def _scan_file(self, results: ScanResults, user: NamedUser, org: Organization, repo: Repository, commitsha: str, filename: str, filelist: List[str]) -> None: file: Optional(ContentFile) = None content: str = None for scanner in self._scanners: if scanner.want(filename): if not file: file = repo.get_contents(filename, ref=commitsha) content = file.decoded_content.decode("utf-8") reposlug = "{}/{}".format(org.login, repo.name) result = scanner.check(reposlug, commitsha, file.sha, file.path, content, filelist) results.add(result)
def update_changelog(version: str, github_repository: Repository) -> None: """ Add a version title to the changelog. """ changelog_path = Path('CHANGELOG.rst') branch = 'master' changelog_content_file = github_repository.get_contents( path=str(changelog_path), ref=branch, ) changelog_bytes = changelog_content_file.decoded_content changelog_contents = changelog_bytes.decode('utf-8') new_changelog_contents = changelog_contents.replace( 'Next\n----', f'Next\n----\n\n{version}\n------------', ) github_repository.update_file( path=str(changelog_path), message=f'Update for release {version}', content=new_changelog_contents, sha=changelog_content_file.sha, )
def _explore_repo(self, repo: Repository, release_ids: Optional[list[int]] = None): logging.info("Explore {}".format(repo.name)) repo_object = self._extract_existing_repo(repo) releases = self._repo_releases(repo, release_ids) if not repo_object and releases: content = {c.name for c in repo.get_contents("")} repo_name = repo.name for repo_class, check_func in self._repo_type_dict.items(): if check_func(content, repo_name): repo_object = repo_class(repo_name, repo.html_url) logging.info("Added {}".format(repo_object)) break if not repo_object or not releases: return if releases[0] not in repo_object.releases: repo_object.releases = self._filter_releases_by_py_ver( repo, releases, repo_object.releases) if isinstance(repo_object, Package): self._packages.add(repo_object) else: self._shells.add(repo_object)
def analyze_github_file(repo: Repository, path_to_file: str, context: DocCheckerContext) -> FileAnalysis: """ This will actually load the file and the commit information to get things like if it was changed recently and who the owner (is taken from the frontmatter). :param context: :param repo: The repo object in the API client :param path_to_file: The path to the file in the repo (as in "lib/myfile.md") :return: """ analysis = FileAnalysis() info(f"Checking file {path_to_file}...") try: commits = repo.get_commits(path=path_to_file) no_earlier_than = datetime.datetime.now() - datetime.timedelta( days=context.doc_is_stale_after_days) if commits.totalCount > 0: commit_date = commits[0].commit.committer.date analysis.file_changed_recently = commit_date >= no_earlier_than analysis.last_change = commit_date analysis.changed_by_email = commits[0].commit.committer.email analysis.changed_by_name = commits[0].commit.committer.name content = repo.get_contents(path_to_file, ref=context.github_branch) analysis.file_link = content.html_url analysis.file_identifier = path_to_file if content.decoded_content: doc = frontmatter.loads(content.decoded_content) if not doc and not doc.metadata: error( f"There was a problem when reading the frontmatter for {path_to_file}", 1) else: if 'title' in doc.metadata: analysis.doc_name = doc.metadata['title'] else: analysis.doc_name = path_to_file if 'owner' in doc.metadata: analysis.owner = doc.metadata['owner'] try: valid = validate_email(analysis.owner) analysis.owner = valid.email except EmailNotValidError as e: warning( f"Found an owner but the email {analysis.owner} is not valid: " + str(e), 1) analysis.owner = None info(f"Owner: {analysis.owner if analysis.owner else 'Not found'}", 1) info( f"Changed On: {analysis.last_change if analysis.last_change else 'Not found'}", 1) info(f"Is Stale: {'No' if analysis.file_changed_recently else 'Yes'}", 1) info( f"Changed By: {analysis.changed_by_email if analysis.changed_by_email else 'Not found'}", 1) except Exception as e: error(f"Unable to load analysis due to exception: {str(e)} ", 1) return analysis