Skip to content

Commit

Permalink
Update to v1.0.2 with added features
Browse files Browse the repository at this point in the history
  • Loading branch information
gnpaone committed Aug 23, 2024
1 parent 3074b7d commit e6026ec
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 35 deletions.
18 changes: 10 additions & 8 deletions .github/workflows/example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,24 @@ jobs:
uses: actions/checkout@v4

- name: Winget Version Badge
uses: gnpaone/[email protected].0
uses: gnpaone/[email protected].2
with:
id: "GitHub.cli,Git.Git"
marker_text: "EXAMPLE_1,EXAMPLE_2"
pkg_link: "https://github.com,"
id: "GitHub.cli;Git.Git;Git.MinGit;GitHub.GitHubDesktop;GitHub.GitLFS;Gitleaks.Gitleaks"
marker_text: "EXAMPLE_1;EXAMPLE_2;EXAMPLE_3;EXAMPLE_4;EXAMPLE_5;EXAMPLE_6"
pkg_link: "https://github.com;;;;https://docs.github.com/en/repositories/working-with-files/managing-large-files/installing-git-large-file-storage;"
style: "plastic"
label_color: ",red"
color: "blue,green"
label_color: ";red;orange;cyan;;"
color: "blue;green;yellow;eb4034;hsl(72, 100%, 50%);rgb(255, 0, 153, 1)"
newline: "true;true;false;false;false;true"
html: "false;false;false;false;false;true"
id: latestver

# Optional
- name: Version
run: |
version=${{ steps.latestver.outputs.winget_ver }}
version="${{ steps.latestver.outputs.winget_ver }}"
echo "ver=$version" >> $GITHUB_OUTPUT
IFS=',' read -r -a array <<< "$version"
IFS=';' read -r -a array <<< "$version"
for i in "${!array[@]}"; do
echo "ver$i=${array[$i]}" >> $GITHUB_OUTPUT
done
Expand Down
32 changes: 27 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ An unofficial shields.io badge with package version fetched from custom API.
<td>Package id - can be found using <code>winget search</code></td>
<td>Yes</td>
<td><em>Winget package identifier (ID)</em></td>
<td rowspan="9">All these parameters (related to workflow only) supports updating multiple package badges at a time without calling the workflow multiple times.<br><a href="https://github.com/gnpaone/winget-version-badge/blob/main/.github/workflows/example.yml">Example workflow</td>
<td rowspan="11">All these parameters (related to workflow only) supports updating multiple package badges at a time without calling the workflow multiple times.<br><a href="https://github.com/gnpaone/winget-version-badge/blob/main/.github/workflows/example.yml">Example workflow</td>
</tr>
<tr>
<td>style</td>
Expand Down Expand Up @@ -73,7 +73,7 @@ An unofficial shields.io badge with package version fetched from custom API.
<td><em>N/A</em></td>
<td>image</td>
<td>false</td>
<td>Return version as shields.io badge oor just as plain text</td>
<td>Return version as shields.io badge or just as plain text</td>
<td>No</td>
<td>true <em>or</em> 1, false</td>
</tr>
Expand Down Expand Up @@ -101,6 +101,22 @@ An unofficial shields.io badge with package version fetched from custom API.
<td>No</td>
<td><em>URL of the package repo</em></td>
</tr>
<tr>
<td>newline</td>
<td><em>N/A</em></td>
<td>true</td>
<td>Add \n after start marker (n/a when <code>html</code> parameter is true)</td>
<td>No</td>
<td>true, false</td>
</tr>
<tr>
<td>html</td>
<td><em>N/A</em></td>
<td>false</td>
<td>Return badge as html instead of markdown text</td>
<td>No</td>
<td>true, false</td>
</tr>
</tbody>
</table>

Expand All @@ -110,17 +126,23 @@ An unofficial shields.io badge with package version fetched from custom API.
* For fetching multiple package versions
* `id` and `marker_text` must contain equal number of elements.
* Other parameters can either be single element which applies to all the multiple badges or number of elements must be equal to `id` and `marker_text`.
* The multiple elements should be comma separated and overall wrapped in double quotes.
* The multiple elements should be semicolon separated and overall wrapped in double quotes.
* Empty elements are allowed for `style`, `label_color` and `pkg_link` parameters only.
* The README markdown file must contain start marker `<!-- EXAMPLE_MARKER_START -->` and end marker `<!-- EXAMPLE_MARKER_END -->` where "EXAMPLE_MARKER" is the input of `marker_text` parameter. Note that the `_START` and `_END` part is important.
* The fetched versions can be accessed via `outputs.winget_ver` for further usage in the workflow. If multiple versions are fetched then this contains versions separated with ",".
* The fetched versions can be accessed via `outputs.winget_ver` for further usage in the workflow. If multiple versions are fetched then this contains versions separated with ";".

## Usage
API calling example: `https://winget-version-badge.vercel.app/?id=Git.Git&image=true`<br>
Workflow examples: [Basic workflow](https://github.com/gnpaone/winget-version-badge/blob/main/examples/basic.yml) | [Multi versions](https://github.com/gnpaone/winget-version-badge/blob/main/examples/multi-ver.yml) <br>
<!-- EXAMPLE_1_START -->
[![GitHub.cli](https://img.shields.io/badge/Winget%20package-2.9.0-blue?style=plastic&labelColor=)](https://github.com)<!-- EXAMPLE_1_END --><!-- EXAMPLE_2_START -->
[![Git.Git](https://img.shields.io/badge/Winget%20package-2.46.0-green?style=plastic&labelColor=red)](https://github.com)<!-- EXAMPLE_2_END -->
[![Git.Git](https://img.shields.io/badge/Winget%20package-2.46.0-green?style=plastic&labelColor=red)]()<!-- EXAMPLE_2_END -->

|EXAMPLE_3|EXAMPLE_4|EXAMPLE_5|
|:----:|:----:|:----:|
|<!-- EXAMPLE_3_START -->[![Git.MinGit](https://img.shields.io/badge/Winget%20package-2.45.0-yellow?style=plastic&labelColor=orange)]()<!-- EXAMPLE_3_END -->|<!-- EXAMPLE_4_START -->[![GitHub.GitHubDesktop](https://img.shields.io/badge/Winget%20package-3.4.3-eb4034?style=plastic&labelColor=cyan)]()<!-- EXAMPLE_4_END -->|<!-- EXAMPLE_5_START -->[![GitHub.GitLFS](https://img.shields.io/badge/Winget%20package-3.4.1-hsl%2872%2C%20100%25%2C%2050%25%29?style=plastic&labelColor=)](https://docs.github.com/en/repositories/working-with-files/managing-large-files/installing-git-large-file-storage)<!-- EXAMPLE_5_END -->|

<!-- EXAMPLE_6_START --><a href=''><img src='https://img.shields.io/badge/Winget%20package-8.18.4-rgb%28255%2C%200%2C%20153%2C%201%29?style=plastic&labelColor=' alt='Gitleaks.Gitleaks' /></a><!-- EXAMPLE_6_END -->

## Limitations
- Since deployed in Vercel, all its limitations applied. Thus it is requested not to abuse the API usage and push it to limits.
Expand Down
8 changes: 8 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ inputs:
description: "Winget package repo link"
default: ""
required: false
newline:
description: 'Add \n after start marker'
default: "true"
required: false
html:
description: 'Return badge as html instead of markdown text'
default: "false"
required: false
commit_user:
description: "Git username"
default: "winget-badge-bot"
Expand Down
2 changes: 1 addition & 1 deletion examples/basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
uses: actions/checkout@v4

- name: Winget Version Badge
uses: gnpaone/[email protected].0
uses: gnpaone/[email protected].2
with:
id: "Git.Git"
marker_text: "TEST_PKG"
18 changes: 9 additions & 9 deletions examples/multi-ver.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ jobs:
uses: actions/checkout@v4

- name: Winget Version Badge
uses: gnpaone/[email protected].0
uses: gnpaone/[email protected].2
with:
id: "GitHub.cli,Git.Git"
marker_text: "TEST_PKG_1,TEST_PKG_2"
pkg_link: "https://github.com,"
id: "GitHub.cli;Git.Git"
marker_text: "TEST_PKG_1;TEST_PKG_2"
pkg_link: "https://github.com;"
label: "Winget package"
label_color: ",red"
color: "blue,green"
label_color: ";red"
color: "blue;green"
id: latestver

- name: Version
run: |
version=${{ steps.latestver.outputs.winget_ver }}
version="${{ steps.latestver.outputs.winget_ver }}"
echo "ver=$version" >> $GITHUB_OUTPUT
IFS=',' read -r -a array <<< "$version"
IFS=';' read -r -a array <<< "$version"
for i in "${!array[@]}"; do
echo "ver$i=${array[$i]}" >> $GITHUB_OUTPUT
done
36 changes: 24 additions & 12 deletions winget.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,31 @@ def run
readme_path = ENV['INPUT_README_PATH']
marker_text = ENV['INPUT_MARKER_TEXT']
pkg_link = ENV['INPUT_PKG_LINK']
newline = ENV['INPUT_NEWLINE']
html = ENV['INPUT_HTML']
git_username = ENV['INPUT_COMMIT_USER']
git_email = ENV['INPUT_COMMIT_EMAIL']
commit_message = ENV['INPUT_COMMIT_MESSAGE'] || 'Update README.md'

fetch_winget(id, style, label, labelColor, color, marker_text, pkg_link, readme_path, commit_message, git_username, git_email)
fetch_winget(id, style, label, labelColor, color, marker_text, pkg_link, newline, html, readme_path, commit_message, git_username, git_email)
rescue StandardError => e
puts "Error: #{e.message}"
exit 1
end

private

def fetch_winget(id, style, label, labelColor, color, marker_text, pkg_link, readme_path, commit_message, git_username, git_email)
id_array = id.split(',')
marker_text_array = marker_text.split(',')
def fetch_winget(id, style, label, labelColor, color, marker_text, pkg_link, newline, html, readme_path, commit_message, git_username, git_email)
id_array = id.split(';', -1)
marker_text_array = marker_text.split(';', -1)

if id_array.length != marker_text_array.length
puts "Error: 'id' and 'marker_text' must have the same array length."
exit 1
end

def handle_param_array(param, id_length, param_name)
param_array = param.split(',')
param_array = param.split(';', -1)
if param_array.length == 1
Array.new(id_length, param_array[0])
elsif param_array.length == id_length
Expand All @@ -53,6 +55,8 @@ def handle_param_array(param, id_length, param_name)
color_array = handle_param_array(color, id_array.length, 'color')
readme_path_array = handle_param_array(readme_path, id_array.length, 'readme_path')
pkg_link_array = handle_param_array(pkg_link, id_array.length, 'pkg_link')
newline_array = handle_param_array(newline, id_array.length, 'newline')
html_array = handle_param_array(html, id_array.length, 'html')

winget_ver_array = Array.new(id_array.length)

Expand All @@ -69,14 +73,14 @@ def handle_param_array(param, id_length, param_name)
pkg_ver = URI.open(api_url).read
winget_ver_array[i] = pkg_ver

update_readme_content(id_array[i], style_array[i], label_array[i], labelColor_array[i], color_array[i], marker_text_array[i], pkg_link_array[i], readme_path_array[i], pkg_ver)
update_readme_content(id_array[i], style_array[i], label_array[i], labelColor_array[i], color_array[i], marker_text_array[i], pkg_link_array[i], newline_array[i], html_array[i], readme_path_array[i], pkg_ver)
else
puts "Failed to retrieve: #{id_array[i]} - #{response.code} - #{response.message}"
exit 1
end
end

winget_ver = winget_ver_array.join(',')
winget_ver = winget_ver_array.join(';')

File.open(ENV['GITHUB_OUTPUT'], 'a') do |file|
file.puts("winget_ver=#{winget_ver}")
Expand All @@ -85,13 +89,21 @@ def handle_param_array(param, id_length, param_name)
update_git_repo(readme_path, commit_message, git_username, git_email)
end

def update_readme_content(id, style, label, labelColor, color, marker_text, pkg_link, readme_path, winget_ver)
def update_readme_content(id, style, label, labelColor, color, marker_text, pkg_link, newline, html, readme_path, winget_ver)
readme_content = File.read(readme_path)
start_marker = "<!-- #{marker_text}_START -->"
end_marker = "<!-- #{marker_text}_END -->"
shields_url = "https://img.shields.io/badge/#{ERB::Util.url_encode(label)}-#{winget_ver}-#{color}?style=#{style}&labelColor=#{labelColor}"

updated_readme_content = readme_content.gsub(/#{start_marker}.*#{end_marker}/m, "#{start_marker}\n[![#{id}](#{shields_url})](#{pkg_link})#{end_marker}")
shields_url = "https://img.shields.io/badge/#{ERB::Util.url_encode(label)}-#{winget_ver}-#{ERB::Util.url_encode(color)}?style=#{style}&labelColor=#{ERB::Util.url_encode(labelColor)}"

if html=="true"
updated_readme_content = readme_content.gsub(/#{start_marker}.*#{end_marker}/m, "#{start_marker}<a href='#{pkg_link}'><img src='#{shields_url}' alt='#{id}' /></a>#{end_marker}")
elsif html=="false"
if newline=="true"
updated_readme_content = readme_content.gsub(/#{start_marker}.*#{end_marker}/m, "#{start_marker}\n[![#{id}](#{shields_url})](#{pkg_link})#{end_marker}")
elsif newline=="false"
updated_readme_content = readme_content.gsub(/#{start_marker}.*#{end_marker}/m, "#{start_marker}[![#{id}](#{shields_url})](#{pkg_link})#{end_marker}")
end
end

File.write(readme_path, updated_readme_content)
end
Expand All @@ -110,4 +122,4 @@ def update_git_repo(readme_path, commit_message, git_username, git_email)
end
end

Winget.new.run
Winget.new.run

0 comments on commit e6026ec

Please sign in to comment.