Merge pull request #19839 from Homebrew/tap-new-for-private-repos

dev-cmd/tap-new: support private repos
This commit is contained in:
Mike McQuaid 2025-04-29 07:33:55 +00:00 committed by GitHub
commit 3332d3331b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,6 +2,7 @@
# frozen_string_literal: true # frozen_string_literal: true
require "abstract_command" require "abstract_command"
require "erb"
require "fileutils" require "fileutils"
require "tap" require "tap"
require "utils/uid" require "utils/uid"
@ -72,25 +73,35 @@ module Homebrew
# <!-- vale on --> # <!-- vale on -->
write_path(tap, "README.md", readme) write_path(tap, "README.md", readme)
actions_main = <<~YAML tests_yml = <<~ERB
name: brew test-bot name: brew test-bot
on: on:
push: push:
branches: branches:
- #{branch} - <%= branch %>
pull_request: pull_request:
jobs: jobs:
test-bot: test-bot:
strategy: strategy:
matrix: matrix:
os: [ubuntu-22.04, macos-13, macos-15] os: [ ubuntu-22.04, macos-13, macos-15 ]
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
permissions:
actions: read
checks: read
contents: read
<% if args.github_packages? -%>
packages: read
<% end -%>
pull-requests: read
steps: steps:
- name: Set up Homebrew - name: Set up Homebrew
id: set-up-homebrew id: set-up-homebrew
uses: Homebrew/actions/setup-homebrew@master uses: Homebrew/actions/setup-homebrew@master
with:
token: ${{ github.token }}
- name: Cache Homebrew Bundler RubyGems - name: Cache Homebrew Bundler RubyGems
uses: actions/cache@v4 uses: actions/cache@v4
@ -104,9 +115,23 @@ module Homebrew
- run: brew test-bot --only-setup - run: brew test-bot --only-setup
- run: brew test-bot --only-tap-syntax - run: brew test-bot --only-tap-syntax
<% if args.github_packages? -%>
- run: brew test-bot --only-formulae#{" --root-url='#{root_url}'" if root_url} - name: Base64-encode GITHUB_TOKEN for HOMEBREW_DOCKER_REGISTRY_TOKEN
id: base64-encode
if: github.event_name == 'pull_request' if: github.event_name == 'pull_request'
env:
TOKEN: ${{ github.token }}
run: |
base64_token=$(echo -n "${TOKEN}" | base64 | tr -d "\\n")
echo "::add-mask::${base64_token}"
echo "token=${base64_token}" >> "${GITHUB_OUTPUT}"
<% 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 - name: Upload bottles as artifact
if: always() && github.event_name == 'pull_request' if: always() && github.event_name == 'pull_request'
@ -114,21 +139,9 @@ module Homebrew
with: with:
name: bottles_${{ matrix.os }} name: bottles_${{ matrix.os }}
path: '*.bottle.*' path: '*.bottle.*'
YAML ERB
pr_pull_permissions = { publish_yml = <<~ERB
"contents" => "write",
"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
name: brew pr-pull name: brew pr-pull
on: on:
@ -138,39 +151,51 @@ module Homebrew
jobs: jobs:
pr-pull: 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 runs-on: ubuntu-22.04
permissions: 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: steps:
- name: Set up Homebrew - name: Set up Homebrew
uses: Homebrew/actions/setup-homebrew@master uses: Homebrew/actions/setup-homebrew@master
with:
token: ${{ github.token }}
- name: Set up git - name: Set up git
uses: Homebrew/actions/git-user-config@master uses: Homebrew/actions/git-user-config@master
- name: Pull bottles - name: Pull bottles
env: 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 }} PULL_REQUEST: ${{ github.event.pull_request.number }}
run: brew pr-pull --debug --tap="$GITHUB_REPOSITORY" "$PULL_REQUEST" run: brew pr-pull --debug --tap="$GITHUB_REPOSITORY" "$PULL_REQUEST"
- name: Push commits - name: Push commits
uses: Homebrew/actions/git-try-push@master uses: Homebrew/actions/git-try-push@master
with: with:
token: ${{ github.token }} branch: <%= branch %>
branch: #{branch}
- name: Delete branch - name: Delete branch
if: github.event.pull_request.head.repo.fork == false if: github.event.pull_request.head.repo.fork == false
env: env:
BRANCH: ${{ github.event.pull_request.head.ref }} BRANCH: ${{ github.event.pull_request.head.ref }}
run: git push --delete origin "$BRANCH" run: git push --delete origin "$BRANCH"
YAML ERB
(tap.path/".github/workflows").mkpath (tap.path/".github/workflows").mkpath
write_path(tap, ".github/workflows/tests.yml", actions_main) write_path(tap, ".github/workflows/tests.yml", ERB.new(tests_yml, trim_mode: "-").result(binding))
write_path(tap, ".github/workflows/publish.yml", actions_publish) write_path(tap, ".github/workflows/publish.yml", ERB.new(publish_yml, trim_mode: "-").result(binding))
unless args.no_git? unless args.no_git?
cd tap.path do |path| cd tap.path do |path|