diff --git a/.github/workflows/example.yml b/.github/workflows/example.yml index 69df4c7..b315447 100644 --- a/.github/workflows/example.yml +++ b/.github/workflows/example.yml @@ -14,7 +14,7 @@ jobs: uses: actions/checkout@v4 - name: Winget Version Badge - uses: gnpaone/winget-version-badge@v1.0.2 + uses: gnpaone/winget-version-badge@v1.0.3 with: 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" diff --git a/README.md b/README.md index ab45ae5..823cb0b 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,7 @@ An unofficial shields.io badge with package version fetched from custom API. #### Notes for the workflow: * Make sure to change the following in your GitHub repo settings: `Actions` > `General` > `Workflow permissions` > Choose `Read and write permissions` > Check `Allow GitHub Actions to create and approve pull requests` > `Save`. -* Other parameters `commit_user`, `commit_email`, `commit_message` related to action workflow are optional. +* Other parameters `commit_user`, `commit_email`, `commit_message` and `confirm_and_push` related to action workflow are optional. * 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`. @@ -133,7 +133,7 @@ An unofficial shields.io badge with package version fetched from custom API. ## Usage API calling example: `https://winget-version-badge.vercel.app/?id=Git.Git&image=true`
-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)
+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) | [PR only worflow](https://github.com/gnpaone/winget-version-badge/blob/main/examples/pr-only.yml)
[![GitHub.cli](https://img.shields.io/badge/Winget%20package-2.9.0-blue?style=plastic&labelColor=)](https://github.com) [![Git.Git](https://img.shields.io/badge/Winget%20package-2.46.0-green?style=plastic&labelColor=red)]() diff --git a/action.yml b/action.yml index 541a716..c710cfd 100644 --- a/action.yml +++ b/action.yml @@ -52,9 +52,20 @@ inputs: description: "Git commit message" default: "Update readme with latest winget version badge" required: false + confirm_and_push: + description: "Push commits to Github repo" + default: "true" + required: false + outputs: winget_ver: description: "Winget package version" + git_username: + description: "Committer username" + git_email: + description: "Committer email" + commit_message: + description: "Commit message" runs: using: "docker" diff --git a/examples/basic.yml b/examples/basic.yml index a3efdc1..6416b53 100644 --- a/examples/basic.yml +++ b/examples/basic.yml @@ -14,7 +14,7 @@ jobs: uses: actions/checkout@v4 - name: Winget Version Badge - uses: gnpaone/winget-version-badge@v1.0.2 + uses: gnpaone/winget-version-badge@v1.0.3 with: id: "Git.Git" marker_text: "TEST_PKG" diff --git a/examples/multi-ver.yml b/examples/multi-ver.yml index ffe8495..c413dd4 100644 --- a/examples/multi-ver.yml +++ b/examples/multi-ver.yml @@ -14,7 +14,7 @@ jobs: uses: actions/checkout@v4 - name: Winget Version Badge - uses: gnpaone/winget-version-badge@v1.0.2 + uses: gnpaone/winget-version-badge@v1.0.3 with: id: "GitHub.cli;Git.Git" marker_text: "TEST_PKG_1;TEST_PKG_2" diff --git a/examples/pr-only.yml b/examples/pr-only.yml new file mode 100644 index 0000000..b0f3a59 --- /dev/null +++ b/examples/pr-only.yml @@ -0,0 +1,68 @@ +name: Multiple badges update + +on: + release: + types: [published] + workflow_dispatch: + +jobs: + update-badge: + name: Update latest version + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Winget Version Badge + uses: gnpaone/winget-version-badge@v1 + with: + 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" + confirm_and_push: "false" + id: latestver + + - name: Create pull request + id: cpr + uses: peter-evans/create-pull-request@v6 + with: + token: ${{ secrets.GITHUB_TOKEN }} + commit-message: "${{ steps.latestver.outputs.commit_message }}" + committer: github-actions[bot] + author: github-actions[bot] + branch: github-actions/repo-update + delete-branch: true + title: Update README + + - name: Check outputs + if: ${{ steps.cpr.outputs.pull-request-number }} + run: | + echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}" + echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}" + + - name: Enable pull request automerge + if: steps.cpr.outputs.pull-request-operation == 'created' + uses: peter-evans/enable-pull-request-automerge@v3 + with: + token: ${{ secrets.PAT }} + pull-request-number: ${{ steps.cpr.outputs.pull-request-number }} + + - name: Auto approve + if: steps.cpr.outputs.pull-request-operation == 'created' + uses: juliangruber/approve-pull-request-action@v2 + with: + github-token: ${{ secrets.PAT }} + number: ${{ steps.cpr.outputs.pull-request-number }} + + - name: Version + run: | + version="${{ steps.latestver.outputs.winget_ver }}" + echo "ver=$version" >> $GITHUB_OUTPUT + IFS=';' read -r -a array <<< "$version" + for i in "${!array[@]}"; do + echo "ver$i=${array[$i]}" >> $GITHUB_OUTPUT + done + \ No newline at end of file diff --git a/winget.rb b/winget.rb index c99a3ac..2e72989 100644 --- a/winget.rb +++ b/winget.rb @@ -6,21 +6,22 @@ class Winget def run - id = ENV['INPUT_ID'] - style = ENV['INPUT_STYLE'] - label = ENV['INPUT_LABEL'] || 'Winget package' - labelColor = ENV['INPUT_LABEL_COLOR'] - color = ENV['INPUT_COLOR'] - 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, newline, html, readme_path, commit_message, git_username, git_email) + id = ENV['INPUT_ID'] + style = ENV['INPUT_STYLE'] + label = ENV['INPUT_LABEL'] || 'Winget package' + labelColor = ENV['INPUT_LABEL_COLOR'] + color = ENV['INPUT_COLOR'] + 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' + confirm_and_push = ENV['INPUT_CONFIRM_AND_PUSH'] + + fetch_winget(id, style, label, labelColor, color, marker_text, pkg_link, newline, html, readme_path, commit_message, git_username, git_email, confirm_and_push) rescue StandardError => e puts "Error: #{e.message}" exit 1 @@ -28,7 +29,7 @@ def run private - def fetch_winget(id, style, label, labelColor, color, marker_text, pkg_link, newline, html, readme_path, commit_message, git_username, git_email) + def fetch_winget(id, style, label, labelColor, color, marker_text, pkg_link, newline, html, readme_path, commit_message, git_username, git_email, confirm_and_push) id_array = id.split(';', -1) marker_text_array = marker_text.split(';', -1) @@ -38,7 +39,7 @@ def fetch_winget(id, style, label, labelColor, color, marker_text, pkg_link, new end def handle_param_array(param, id_length, param_name) - param_array = param.split(';', -1) + param_array = param.empty? ? [""] : param.split(';', -1) if param_array.length == 1 Array.new(id_length, param_array[0]) elsif param_array.length == id_length @@ -86,7 +87,7 @@ def handle_param_array(param, id_length, param_name) file.puts("winget_ver=#{winget_ver}") end - update_git_repo(readme_path, commit_message, git_username, git_email) + update_git_repo(readme_path, commit_message, git_username, git_email, confirm_and_push) end def update_readme_content(id, style, label, labelColor, color, marker_text, pkg_link, newline, html, readme_path, winget_ver) @@ -108,7 +109,7 @@ def update_readme_content(id, style, label, labelColor, color, marker_text, pkg_ File.write(readme_path, updated_readme_content) end - def update_git_repo(readme_path, commit_message, git_username, git_email) + def update_git_repo(readme_path, commit_message, git_username, git_email, confirm_and_push) `git config --global --add safe.directory /github/workspace` `git config user.name #{git_username}` `git config user.email #{git_email}` @@ -117,7 +118,15 @@ def update_git_repo(readme_path, commit_message, git_username, git_email) unless status.include?("nothing to commit") `git add #{readme_path}` `git commit -m "#{commit_message}"` - `git push` + if confirm_and_push == "true" + `git push` + elsif confirm_and_push == "false" + File.open(ENV['GITHUB_OUTPUT'], 'a') do |file| + file.puts("git_username=#{git_username}") + file.puts("git_email=#{git_email}") + file.puts("commit_message=#{commit_message}") + end + end end end end