Skip to content

Commit

Permalink
Override hyperlink + fetch & display avatars in markdown (#481)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
nvuillam and nvuillam authored Dec 20, 2023
1 parent e009776 commit 519ec19
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 9 deletions.
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"docstates",
"docstrings",
"doctest",
"docurl",
"donotpresent",
"dont",
"dotfiles",
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ _________________
| -s<br/> --sort | String | _(optional)_ Sort order: name (default) or stars |
| -x<br/> --minstars | String | _(optional)_ If set, filters repositories to keep only those with more than X stars |
| -m<br/> --markdownfile | String | _(optional)_ Output markdown file file |
| -d<br/> --docurl | String | _(optional)_ Hyperlink to use when clicking on badge markdown file badge. (Default: link to markdown file) |
| -p<br/> --mergepackages | String | _(optional)_ In case of multiple packages, merge their stats in a single one in markdown and json output |
| -j<br/> --json | String | _(optional)_ Output in json format |
| -v<br/> --version | Boolean | _(optional)_ Displays version of github-dependents-info |
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 }}
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
7 changes: 6 additions & 1 deletion github_dependents_info/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def main(
help="""Path to markdown file to insert/update Used By badge between tags
<!-- gh-dependents-info-used-by-start --><!-- gh-dependents-info-used-by-end -->""",
),
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"),
Expand Down Expand Up @@ -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,
)
Expand All @@ -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()

Expand Down
25 changes: 23 additions & 2 deletions github_dependents_info/gh_dependents_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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'<img class="avatar mr-2" src="{img}" width="20" height="20" alt=""> '
md_lines += [f"|{image_md}[{repo_label}](https://github.com/{repo_label}) | {repo_stars} |"]
# Dependents by package
else:
for package in self.packages:
Expand All @@ -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'<img class="avatar mr-2" src="{img}" width="20" height="20" alt=""> '
md_lines += [f"|{image_md}[{repo_label}](https://github.com/{repo_label}) | {repo_stars} |"]
md_lines += [""]

# footer
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>"]
Expand Down
7 changes: 5 additions & 2 deletions tests/test_gh_dependents_info/test_gh_dependents_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 519ec19

Please sign in to comment.