Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify the drawing of badges #2220

Merged
merged 13 commits into from
Jan 6, 2023
137 changes: 72 additions & 65 deletions .automation/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,17 +791,18 @@ def generate_descriptor_documentation(descriptor):
descriptor_md += [
"## Linters",
"",
"| Linter | Configuration key | Status |",
"| ------ | ----------------- | ------ |",
"| Linter | Additional |",
"| ------ | ---------- |",
]
for linter in descriptor.get("linters", []):
linter_name_lower = linter.get("linter_name").lower().replace("-", "_")
linter_doc_url = f"{lang_lower}_{linter_name_lower}.md"
badge = get_repository_badge_url(linter)
badges = get_badges(linter)
md_extra = " ".join(badges)
descriptor_md += [
f"| [{linter.get('linter_name')}]({doc_url(linter_doc_url)}) | "
f"[{linter.get('name', descriptor.get('descriptor_id'))}]({doc_url(linter_doc_url)}) |"
f" {badge} |"
f"| [{linter.get('linter_name')} ({linter.get('name', descriptor.get('descriptor_id'))})]"
f"({doc_url(linter_doc_url)}) "
f"| {md_extra} |"
]

# Criteria used by the descriptor to identify files to lint
Expand Down Expand Up @@ -950,8 +951,8 @@ def process_type(linters_by_type, type1, type_label, linters_tables_md):
linters_tables_md += [
f"### {type_label}",
"",
f"| <!-- --> | {col_header} | Linter | Configuration key | Additional |",
"| :---: | ----------------- | -------------- | ------------ | :-----: |",
f"| <!-- --> | {col_header} | Linter | Additional |",
"| :---: | ----------------- | -------------- | :-----: |",
]
descriptor_linters = linters_by_type[type1]
for linter in descriptor_linters:
Expand All @@ -977,32 +978,21 @@ def process_type(linters_by_type, type1, type_label, linters_tables_md):
descriptor_url = doc_url(f"{DOCS_URL_DESCRIPTORS_ROOT}/{lang_lower}.md")
descriptor_id_cell = f"[{descriptor_label}]({descriptor_url})"
# Build extra badges
md_extras = []
repo = get_github_repo(linter)
if repo is not None:
md_extras += [
f"[![GitHub stars](https://img.shields.io/github/stars/{repo}?cacheSeconds=3600)]"
f"(https://github.com/{repo})"
]
if hasattr(linter, "is_formatter") and linter.is_formatter is True:
md_extras += ["![formatter](https://shields.io/badge/-format-yellow)"]
elif linter.cli_lint_fix_arg_name is not None:
md_extras += ["![autofix](https://shields.io/badge/-autofix-green)"]
if hasattr(linter, "can_output_sarif") and linter.can_output_sarif is True:
md_extras += ["![sarif](https://shields.io/badge/-SARIF-orange)"]
md_extra = " ".join(md_extras)
badges = get_badges(linter)
md_extra = " ".join(badges)
# Build doc URL
linter_doc_url = (
f"{DOCS_URL_DESCRIPTORS_ROOT}/{lang_lower}_{linter_name_lower}.md"
)
# Build md table line
linters_tables_md += [
f"| {icon_html} <!-- linter-icon --> | {descriptor_id_cell} | "
f"[{linter.linter_name}]({doc_url(linter_doc_url)})"
f"| [{linter.name}]({doc_url(linter_doc_url)})"
f"| {md_extra} |"
f"| {icon_html} <!-- linter-icon --> | "
f"{descriptor_id_cell} | "
f"[{linter.linter_name} ({linter.name})]({doc_url(linter_doc_url)}) | "
f"{md_extra} |"
]

individual_badges = get_badges(linter, show_last_release=True, show_last_commit=True, show_commits_activity=True, show_contributors=True)
md_individual_extra = " ".join(individual_badges)
# Build individual linter doc
linter_doc_md = [
"---",
Expand All @@ -1014,7 +1004,6 @@ def process_type(linters_by_type, type1, type_label, linters_tables_md):
"<!-- markdownlint-disable MD033 MD041 -->",
f"<!-- {'@'}generated by .automation/build.py, please do not update manually -->",
]
badge = get_repository_badge_url(linter)
# Header image as title
if (
hasattr(linter, "linter_banner_image_url")
Expand All @@ -1029,7 +1018,7 @@ def process_type(linters_by_type, type1, type_label, linters_tables_md):
"center",
150,
),
"\n" + badge,
"\n" + md_individual_extra,
]
# Text + image as title
elif (
Expand All @@ -1045,14 +1034,14 @@ def process_type(linters_by_type, type1, type_label, linters_tables_md):
100,
)
+ linter.linter_name
+ " "
+ badge
+ "\n"
+ md_individual_extra
]
# Text as title
elif badge == "":
elif md_individual_extra == "":
linter_doc_md += [f"# {linter.linter_name}"]
else:
linter_doc_md += [f"# {linter.linter_name} {badge}"]
linter_doc_md += [f"# {linter.linter_name}\n{md_individual_extra}"]

# Indicate that a linter is disabled in this version
if hasattr(linter, "deprecated") and linter.deprecated is True:
Expand Down Expand Up @@ -2724,46 +2713,57 @@ def generate_documentation_all_users():
file.write("\n".join(linter_doc_md) + "\n")
logging.info(f"Generated {REPO_HOME}/docs/all_users.md")


# https://shields.io/category/activity
def get_repository_badge_url(linter):
repo_url = None
def get_badges(linter, show_last_release=False, show_last_commit=False, show_commits_activity=False, show_contributors=False):
badges = []
repo = get_github_repo(linter)

if (
hasattr(linter, "get")
and linter.get("linter_repo") is not None
and "github" in linter.get("linter_repo")
(hasattr(linter, "get") and linter.get("deprecated") is True) or
(hasattr(linter, "deprecated") and linter.deprecated is True)
):
repo_url = linter.get("linter_repo")
elif (
hasattr(linter, "get")
and linter.get("linter_url") is not None
and "github" in linter.get("linter_url")
badges += ["![deprecated](https://shields.io/badge/-deprecated-red)"]
if repo is not None:
badges += [
f"[![GitHub stars](https://img.shields.io/github/stars/{repo}?cacheSeconds=3600)]"
f"(https://github.com/{repo})"
]
if (
(hasattr(linter, "get") and linter.get("is_formatter") is True) or
(hasattr(linter, "is_formatter") and linter.is_formatter is True)
):
repo_url = linter.get("linter_url")
badges += ["![formatter](https://shields.io/badge/-format-yellow)"]
elif (
hasattr(linter, "linter_repo")
and linter.linter_repo is not None
and "github" in linter.linter_repo
(hasattr(linter, "get") and linter.get("cli_lint_fix_arg_name") is not None) or
(hasattr(linter, "cli_lint_fix_arg_name") and linter.cli_lint_fix_arg_name is not None)
):
repo_url = linter.linter_repo
elif (
hasattr(linter, "linter_url")
and linter.linter_url is not None
and "github" in linter.linter_url
badges += ["![autofix](https://shields.io/badge/-autofix-green)"]
if (
(hasattr(linter, "get") and linter.get("can_output_sarif") is True) or
(hasattr(linter, "can_output_sarif") and linter.can_output_sarif is True)
):
repo_url = linter.linter_url

badge = ""

if repo_url is not None:
match = re.search(r"https://github\.com/(.*)/(.*)", repo_url)

badge_url = f"https://img.shields.io/github/last-commit/{match.group(1)}/{match.group(2)}"
badge = f"[![GitHub last commit]({badge_url})]({repo_url}/commits)"

return badge
badges += ["![sarif](https://shields.io/badge/-SARIF-orange)"]

if show_last_release and repo is not None:
badges += [
f"[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/{repo}?sort=semver)]"
f"(https://github.com/{repo}/releases)"
]
if show_last_commit and repo is not None:
badges += [
f"[![GitHub last commit](https://img.shields.io/github/last-commit/{repo})]"
f"(https://github.com/{repo}/commits)"
]
if show_commits_activity and repo is not None:
badges += [
f"[![GitHub commit activity](https://img.shields.io/github/commit-activity/y/{repo})]"
f"(https://github.com/{repo}/graphs/commit-activity/)"
]
if show_contributors and repo is not None:
badges += [
f"[![GitHub contributors](https://img.shields.io/github/contributors/{repo})]"
f"(https://github.com/{repo}/graphs/contributors/)"
]
return badges

# get github repo info using api
def get_github_repo_info(repo):
Expand Down Expand Up @@ -2811,6 +2811,13 @@ def refresh_users_info():

def get_github_repo(linter):
if (
hasattr(linter, "get")
and linter.get("linter_repo") is not None
and linter.get("linter_repo").startswith("https://github.com")
):
repo = linter.get("linter_repo").split("https://github.com/", 1)[1]
return repo
elif (
hasattr(linter, "linter_repo")
and linter.linter_repo is not None
and linter.linter_repo.startswith("https://github.com")
Expand Down
Loading