dev-cmd/tap-new: use ERB templates instead of string interpolation

This commit is contained in:
Alexander Bayandin 2025-04-28 23:57:44 +01:00 committed by Alexander Bayandin
parent 875702d51a
commit d60e72a055

View File

@ -2,6 +2,7 @@
# frozen_string_literal: true
require "abstract_command"
require "erb"
require "fileutils"
require "tap"
require "utils/uid"
@ -72,30 +73,29 @@ module Homebrew
# <!-- vale on -->
write_path(tap, "README.md", readme)
tests_permissions = {
"actions" => "read",
"checks" => "read",
"contents" => "read",
"pull-requests" => "read",
}
tests_permissions["packages"] = "read" if args.github_packages?
actions_main = <<~YAML
tests_yml = <<~ERB
name: brew test-bot
on:
push:
branches:
- #{branch}
- <%= branch %>
pull_request:
jobs:
test-bot:
strategy:
matrix:
os: [ubuntu-22.04, macos-13, macos-15]
os: [ ubuntu-22.04, macos-13, macos-15 ]
runs-on: ${{ matrix.os }}
permissions:
#{tests_permissions.sort.map { |k, v| " #{k}: #{v}" }.join("\n")}
actions: read
checks: read
contents: read
<% if args.github_packages? -%>
packages: read
<% end -%>
pull-requests: read
steps:
- name: Set up Homebrew
id: set-up-homebrew
@ -115,8 +115,8 @@ module Homebrew
- run: brew test-bot --only-setup
- run: brew test-bot --only-tap-syntax
- name: Base64-encode GITHUB_TOKEN
<% if args.github_packages? -%>
- name: Base64-encode GITHUB_TOKEN for HOMEBREW_DOCKER_REGISTRY_TOKEN
id: base64-encode
if: github.event_name == 'pull_request'
env:
@ -125,11 +125,13 @@ module Homebrew
base64_token=$(echo -n "${TOKEN}" | base64 | tr -d "\\n")
echo "::add-mask::${base64_token}"
echo "token=${base64_token}" >> "${GITHUB_OUTPUT}"
- run: brew test-bot --only-formulae#{" --root-url='#{root_url}'" if root_url}
<% end -%>
- run: brew test-bot --only-formulae<% if root_url %> --root-url='<%= root_url %>'<% end %>
if: github.event_name == 'pull_request'
<% if args.github_packages? -%>
env:
HOMEBREW_DOCKER_REGISTRY_TOKEN: ${{ steps.base64-encode.outputs.token }}
<% end -%>
- name: Upload bottles as artifact
if: always() && github.event_name == 'pull_request'
@ -137,24 +139,9 @@ module Homebrew
with:
name: bottles_${{ matrix.os }}
path: '*.bottle.*'
YAML
ERB
pr_pull_permissions = {
"actions" => "read",
"checks" => "read",
"contents" => "write",
"issues" => "read",
"pull-requests" => "write",
}
pr_pull_env = {
"HOMEBREW_GITHUB_API_TOKEN" => "${{ github.token }}",
}
if args.github_packages?
pr_pull_permissions["packages"] = "write"
pr_pull_env["HOMEBREW_GITHUB_PACKAGES_TOKEN"] = "${{ github.token }}"
pr_pull_env["HOMEBREW_GITHUB_PACKAGES_USER"] = "${{ github.repository_owner }}"
end
actions_publish = <<~YAML
publish_yml = <<~ERB
name: brew pr-pull
on:
@ -164,10 +151,17 @@ module Homebrew
jobs:
pr-pull:
if: contains(github.event.pull_request.labels.*.name, '#{label}')
if: contains(github.event.pull_request.labels.*.name, '<%= label %>')
runs-on: ubuntu-22.04
permissions:
#{pr_pull_permissions.sort.map { |k, v| " #{k}: #{v}" }.join("\n")}
actions: read
checks: read
contents: write
issues: read
<% if args.github_packages? -%>
packages: write
<% end -%>
pull-requests: write
steps:
- name: Set up Homebrew
uses: Homebrew/actions/setup-homebrew@master
@ -179,25 +173,29 @@ module Homebrew
- name: Pull bottles
env:
#{pr_pull_env.sort.map { |k, v| " #{k}: #{v}" }.join("\n")}
HOMEBREW_GITHUB_API_TOKEN: ${{ github.token }}
<% if args.github_packages? -%>
HOMEBREW_GITHUB_PACKAGES_TOKEN: ${{ github.token }}
HOMEBREW_GITHUB_PACKAGES_USER: ${{ github.repository_owner }}
<% end -%>
PULL_REQUEST: ${{ github.event.pull_request.number }}
run: brew pr-pull --debug --tap="$GITHUB_REPOSITORY" "$PULL_REQUEST"
- name: Push commits
uses: Homebrew/actions/git-try-push@master
with:
branch: #{branch}
branch: <%= branch %>
- name: Delete branch
if: github.event.pull_request.head.repo.fork == false
env:
BRANCH: ${{ github.event.pull_request.head.ref }}
run: git push --delete origin "$BRANCH"
YAML
ERB
(tap.path/".github/workflows").mkpath
write_path(tap, ".github/workflows/tests.yml", actions_main)
write_path(tap, ".github/workflows/publish.yml", actions_publish)
write_path(tap, ".github/workflows/tests.yml", ERB.new(tests_yml, trim_mode: "-").result(binding))
write_path(tap, ".github/workflows/publish.yml", ERB.new(publish_yml, trim_mode: "-").result(binding))
unless args.no_git?
cd tap.path do |path|