From 519ec190650978dc047a9bb1a46bf35396771870 Mon Sep 17 00:00:00 2001 From: Nicolas Vuillamy Date: Wed, 20 Dec 2023 19:58:50 +0100 Subject: [PATCH] Override hyperlink + fetch & display avatars in markdown (#481) * Fetch images + --docurl option - Allow to override hyperlink on README badge using option **--docurl** - Also fetch GitHub repositories avatars, for a nicer result in markdown :) * Prepare 1.4.0 release * black * cspell * image in html --------- Co-authored-by: Nicolas Vuillamy --- .cspell.json | 1 + CHANGELOG.md | 5 ++++ README.md | 4 +-- action.yml | 2 +- github_dependents_info/__main__.py | 7 +++++- github_dependents_info/gh_dependents_info.py | 25 +++++++++++++++++-- pyproject.toml | 2 +- .../test_gh_dependents_info.py | 7 ++++-- 8 files changed, 44 insertions(+), 9 deletions(-) diff --git a/.cspell.json b/.cspell.json index be0644c..5b955e4 100644 --- a/.cspell.json +++ b/.cspell.json @@ -99,6 +99,7 @@ "docstates", "docstrings", "doctest", + "docurl", "donotpresent", "dont", "dotfiles", diff --git a/CHANGELOG.md b/CHANGELOG.md index 206e233..b0734ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - Add your updates here :) +## [1.4.0] 2023-12-20 + +- Allow to override hyperlink on README badge using option **--docurl** +- Also fetch GitHub repositories avatars, for a nicer result in markdown :) + ## [1.3.2] 2023-12-18 - Fix markdown table diff --git a/README.md b/README.md index df6fd0c..35f10ab 100644 --- a/README.md +++ b/README.md @@ -235,6 +235,7 @@ _________________ | -s
--sort | String | _(optional)_ Sort order: name (default) or stars | | -x
--minstars | String | _(optional)_ If set, filters repositories to keep only those with more than X stars | | -m
--markdownfile | String | _(optional)_ Output markdown file file | +| -d
--docurl | String | _(optional)_ Hyperlink to use when clicking on badge markdown file badge. (Default: link to markdown file) | | -p
--mergepackages | String | _(optional)_ In case of multiple packages, merge their stats in a single one in markdown and json output | | -j
--json | String | _(optional)_ Output in json format | | -v
--version | Boolean | _(optional)_ Displays version of github-dependents-info | @@ -278,7 +279,6 @@ Create a file **.github/workflows/github-dependents-info.yml** in your repositor If will generate a new Pull Request (or replace the pending one) every time the usage stats will have changed :) ```yaml ---- # GitHub Dependents Info workflow # More info at https://github.com/nvuillam/github-dependents-info/ name: GitHub Dependents Info @@ -317,7 +317,7 @@ jobs: # Collect data & generate markdown - name: GitHub Dependents Info - uses: nvuillam/github-dependents-info@v1.3.2 + uses: nvuillam/github-dependents-info@v1.4.0 # If you trust me enough you can replace version by "main" :) # See documentation for variables details: https://github.com/nvuillam/github-dependents-info?tab=readme-ov-file#%EF%B8%8F-usage with: repo: ${{ github.repository }} diff --git a/action.yml b/action.yml index 290edf4..b45b3b9 100644 --- a/action.yml +++ b/action.yml @@ -28,7 +28,7 @@ inputs: runs: using: "docker" - image: "docker://nvuillam/github-dependents-info:v1.3.2" + image: "docker://nvuillam/github-dependents-info:v1.4.0" args: - --repo - ${{ inputs.repo }} diff --git a/github_dependents_info/__main__.py b/github_dependents_info/__main__.py index 227ccba..ed7f78c 100644 --- a/github_dependents_info/__main__.py +++ b/github_dependents_info/__main__.py @@ -32,6 +32,9 @@ def main( help="""Path to markdown file to insert/update Used By badge between tags """, ), + doc_url: str = typer.Option( + None, "-d", "--docurl", help="Hyperlink to use when clicking on badge markdown file badge" + ), badge_color: str = typer.Option("informational", "-c", "--markdownbadgecolor", help="Markdown badge color"), sort_key: str = typer.Option(None, "-s", "--sort", help="Sort of name(default) or stars"), min_stars: int = typer.Option(None, "-x", "--minstars", help="Filter dependents with less than X stars"), @@ -98,6 +101,8 @@ def main( json_output=json_output, csv_directory=csv_directory, badge_markdown_file=badge_markdown_file, + doc_url=doc_url, + markdown_file=markdown_file, badge_color=badge_color, merge_packages=merge_packages, ) @@ -108,7 +113,7 @@ def main( gh_deps_info.build_markdown(file=markdown_file) # Update existing markdown to add badge if badge_markdown_file is not None: - gh_deps_info.write_badge(badge_markdown_file) + gh_deps_info.write_badge(badge_markdown_file, "total_doc_url") # Print text or json result gh_deps_info.print_result() diff --git a/github_dependents_info/gh_dependents_info.py b/github_dependents_info/gh_dependents_info.py index f94504d..944d5cd 100644 --- a/github_dependents_info/gh_dependents_info.py +++ b/github_dependents_info/gh_dependents_info.py @@ -21,6 +21,8 @@ def __init__(self, repo, **options) -> None: self.min_stars = None if "min_stars" not in options else options["min_stars"] self.json_output = True if "json_output" in options and options["json_output"] is True else False self.merge_packages = True if "merge_packages" in options and options["merge_packages"] is True else False + self.doc_url = options["doc_url"] if "doc_url" in options else None + self.markdown_file = options["markdown_file"] if "markdown_file" in options else None self.badge_color = options["badge_color"] if "badge_color" in options else "informational" self.debug = True if "debug" in options and options["debug"] is True else False self.overwrite_progress = ( @@ -96,6 +98,10 @@ def collect(self): t.find("svg", {"class": "octicon-star"}).parent.text.strip().replace(",", "") ), } + # Collect avatar image + image = t.findAll("img", {"class": "avatar"}) + if len(image) > 0 and image[0].attrs and "src" in image[0].attrs: + result_item["img"] = image[0].attrs["src"] # Skip result if less than minimum stars if self.min_stars is not None and result_item["stars"] < self.min_stars: continue @@ -174,6 +180,13 @@ def collect(self): self.all_public_dependent_repos = sorted(self.all_public_dependent_repos, key=lambda d: d["name"]) # Build total badges + doc_url_to_use = "https://github.com/nvuillam/github-dependents-info" + if self.doc_url is not None: + doc_url_to_use = self.doc_url + elif self.markdown_file is not None: + doc_url_to_use = f"https://github.com/{self.repo}/blob/main/{self.markdown_file}" + self.badges["total_doc_url"] = self.build_badge("Used%20by", self.total_sum, url=doc_url_to_use) + self.badges["total"] = self.build_badge("Used%20by", self.total_sum) self.badges["public"] = self.build_badge("Used%20by%20(public)", self.total_public_sum) self.badges["private"] = self.build_badge("Used%20by%20(private)", self.total_private_sum) @@ -336,7 +349,11 @@ def build_markdown(self, **options) -> str: for repo1 in self.all_public_dependent_repos: repo_label = repo1["name"] repo_stars = repo1["stars"] - md_lines += [f"|[{repo_label}](https://github.com/{repo_label}) | {repo_stars} |"] + image_md = "" + if "img" in repo1: + img = repo1["img"] + image_md = f' ' + md_lines += [f"|{image_md}[{repo_label}](https://github.com/{repo_label}) | {repo_stars} |"] # Dependents by package else: for package in self.packages: @@ -355,7 +372,11 @@ def build_markdown(self, **options) -> str: for repo1 in package["public_dependents"]: repo_label = repo1["name"] repo_stars = repo1["stars"] - md_lines += [f"|[{repo_label}](https://github.com/{repo_label}) | {repo_stars} |"] + image_md = "" + if "img" in repo1: + img = repo1["img"] + image_md = f' ' + md_lines += [f"|{image_md}[{repo_label}](https://github.com/{repo_label}) | {repo_stars} |"] md_lines += [""] # footer diff --git a/pyproject.toml b/pyproject.toml index 2492c1c..04a299a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "github-dependents-info" -version = "1.3.2" +version = "1.4.0" description = "Collect information about dependencies between a github repo and other repositories. Results available in JSON, markdown and badges." readme = "README.md" authors = ["nvuillam "] diff --git a/tests/test_gh_dependents_info/test_gh_dependents_info.py b/tests/test_gh_dependents_info/test_gh_dependents_info.py index e67614b..13146e5 100644 --- a/tests/test_gh_dependents_info/test_gh_dependents_info.py +++ b/tests/test_gh_dependents_info/test_gh_dependents_info.py @@ -8,10 +8,13 @@ def test_collect_stats_single_package(): repo = "nvuillam/npm-groovy-lint" - gh_deps_info = GithubDependentsInfo(repo, debug=True, sort_key="stars", badge_color="pink") + tmp_md_file = tempfile.gettempdir() + os.path.sep + str(uuid.uuid4()) + "-test-single.md" + gh_deps_info = GithubDependentsInfo( + repo, debug=True, sort_key="stars", badge_color="pink", markdown_file=tmp_md_file + ) repo_stats = gh_deps_info.collect() assert repo_stats["public_dependents_number"] > 10 - tmp_md_file = tempfile.gettempdir() + os.path.sep + str(uuid.uuid4()) + "-test-single.md" + md = gh_deps_info.build_markdown(file=tmp_md_file) assert md.count("\n") > 10 assert "pink" in md