Skip to content

Commit

Permalink
Merge pull request #3 from dawidd6/rewrite-in-ruby
Browse files Browse the repository at this point in the history
Rewrite in Ruby
  • Loading branch information
dawidd6 authored Apr 1, 2020
2 parents 37893b6 + 0fde40f commit b5daa9a
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 200 deletions.
28 changes: 15 additions & 13 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
name: Test Action

on:
push:
branches:
- master
on: push

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
formula:
- dawidd6/test/test-formula-url
- dawidd6/test/test-formula-git-revision
include:
- formula: dawidd6/test/test-formula-url
tag: v0.1.12
- formula: dawidd6/test/test-formula-git-revision
tag: v0.3.3
revision: c43abd765cf51c06bbcaa5479dc49aab1396989f
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Test
uses: ./
with:
token: ${{secrets.TOKEN}}
formula: dawidd6/homebrew-test/test-formula-url
url: https://github.com/dawidd6/actions-updater/archive/v0.1.12.tar.gz
- name: Test
uses: ./
with:
token: ${{secrets.TOKEN}}
formula: dawidd6/homebrew-test/test-formula-git-revision
tag: v0.3.3
revision: c43abd765cf51c06bbcaa5479dc49aab1396989f
formula: ${{matrix.formula}}
tag: ${{matrix.tag}}
revision: ${{matrix.revision}}
9 changes: 7 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
FROM homebrew/brew

COPY *.sh /
ENV HOMEBREW_NO_ENV_FILTERING=1
ENV HOMEBREW_NO_AUTO_UPDATE=1
ENV HOMEBREW_NO_ANALYTICS=1
ENV HOMEBREW_COLOR=1

ENTRYPOINT ["/main.sh"]
COPY main.rb /

ENTRYPOINT ["brew", "ruby", "/main.rb"]
75 changes: 13 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ An action that wraps `brew bump-formula-pr` to ease the process of updating the

## Usage

One should use the [Personal Access Token](https://github.com/settings/tokens/new?scopes=public_repo) for `token` input to this Action, not the default `GITHUB_TOKEN`, because `brew bump-formula-pr` creates a fork of the formula's tap repository and creates a PR.
One should use the [Personal Access Token](https://github.com/settings/tokens/new?scopes=public_repo) for `token` input to this Action, not the default `GITHUB_TOKEN`, because `brew bump-formula-pr` creates a fork of the formula's tap repository (if needed) and then creates a pull request.

It is best to use this Action when a new tag is pushed:

Expand All @@ -15,71 +15,22 @@ on:
- '*'
```
Example of bumping any formula in any user tap:
because then, the script will extract all needed informations by itself, you just need to specify the following step in your workflow:
```yaml
- name: Get tag
id: tag
uses: dawidd6/action-get-tag@v1

- name: Update Homebrew formula
uses: dawidd6/action-homebrew-bump-formula@v1
uses: dawidd6/action-homebrew-bump-formula@v2
with:
token: ${{secrets.GITHUB_PAT}}
token: ${{secrets.TOKEN}}
# For example:
# dawidd6/tap/actions-updater
# or if the formula is in the core tap:
# lazygit
formula: USER/REPO/FORMULA
url: "https://github.com/USER/REPO/archive/${{steps.tag.outputs.tag}}.tar.gz"
```
**note:** `USER/REPO/FORMULA` can be in the form that `brew install` would accept it. That is, for formula "baz" in foo's tap repo "homebrew-bar", one could use `foo/bar/baz` instead of `foo/homebrew-bar/baz`. This is because homebrew expects tap names to have the `homebrew-` prefix.

Example of bumping [`lazygit`](https://github.com/jesseduffield/lazygit) formula in [`Homebrew/homebrew-core`](https://github.com/Homebrew/homebrew-core) tap:

```yaml
- name: Get tag
id: tag
uses: dawidd6/action-get-tag@v1
- name: Update Homebrew formula
uses: dawidd6/action-homebrew-bump-formula@v1
with:
token: ${{secrets.GITHUB_PAT}}
formula: lazygit
url: "https://github.com/jesseduffield/lazygit/archive/${{steps.tag.outputs.tag}}.tar.gz"
```

... using `url` input because the formula already specifies it:

```ruby
class Lazygit < Formula
desc "Simple terminal UI for git commands"
homepage "https://github.com/jesseduffield/lazygit/"
url "https://github.com/jesseduffield/lazygit/archive/v0.16.2.tar.gz"
sha256 "76c043e59afc403d7353cdb188ac6850ce4c4125412e291240c787b0187e71c6"
```

Example of bumping [`lazydocker`](https://github.com/jesseduffield/lazdockert) formula in [`Homebrew/homebrew-core`](https://github.com/Homebrew/homebrew-core) tap:

```yaml
- name: Get tag
id: tag
uses: dawidd6/action-get-tag@v1
- name: Update Homebrew formula
uses: dawidd6/action-homebrew-bump-formula@v1
with:
token: ${{secrets.GITHUB_PAT}}
formula: lazydocker
tag: ${{steps.tag.outputs.tag}}
# Optional, will be determined automatically
tag: ${{github.ref}}
# Optional, will be determined automatically
revision: ${{github.sha}}
```

... using `tag` and `revision` inputs because the formula already specifies them:

```ruby
class Lazydocker < Formula
desc "The lazier way to manage everything docker"
homepage "https://github.com/jesseduffield/lazydocker"
url "https://github.com/jesseduffield/lazydocker.git",
:tag => "v0.8",
:revision => "cea67bc570daaa757a886813ff3c2763189efef6"
# Optional, if don't want to check for already open PRs
force: true
```
34 changes: 21 additions & 13 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,33 @@ inputs:
description: Github token (not the default one)
required: true
formula:
description: Formula name (for example "unzip" or "user/tap/formula")
description: |
Formula name
Optionally in format USER/REPO/FORMULA
Example: lazygit
Example: dawidd6/tap/actions-updater
required: true
url:
description: URL to download source archive
required: false
mirror:
description: URL to download source archive (mirror)
required: false
sha256:
description: Source archive SHA256 checksum
required: false
tag:
description: Git tag
description: |
Git tag
Example: v1.0.0
Example: refs/tags/v1.0.0
required: false
default: ${{github.ref}}
revision:
description: Git revision
description: |
Git revision
Only required for formulae that use git to download the source
Example: 130d3a3af72f66780ae4e24cd143ae7a4d757f9d
required: false
default: ${{github.sha}}
force:
description: Check open PRs or not
description: Check open PRs or not (will fail if detected)
required: false
runs:
using: docker
Expand Down
78 changes: 78 additions & 0 deletions main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# frozen_string_literal: true

module Homebrew
module_function

def brew(*args)
puts "[command]brew #{args.join(' ')}"
return if ENV['DEBUG']

safe_system('brew', *args)
end

def git(*args)
puts "[command]git #{args.join(' ')}"
return if ENV['DEBUG']

safe_system('git', *args)
end

# Get inputs
token = ENV['INPUT_TOKEN']
formula = ENV['INPUT_FORMULA']
tag = ENV['INPUT_TAG']
revision = ENV['INPUT_REVISION']
force = ENV['INPUT_FORCE']

# Die if required inputs are not provided
odie 'TOKEN is required' if token.blank?
odie 'FORMULA is required' if formula.blank?
odie 'TAG is required' if tag.blank?

# Set needed HOMEBREW environment variables
ENV['HOMEBREW_GITHUB_API_TOKEN'] = token

# Get user details
actor = ENV['GITHUB_ACTOR']
user = GitHub.open_api "#{GitHub::API_URL}/users/#{actor}"
user_name = user['name'] || actor
user_email = user['email'] || (
# https://help.github.com/en/github/setting-up-and-managing-your-github-user-account/setting-your-commit-email-address
user_created_at = Date.parse user['created_at']
plus_after_date = Date.parse '2017-07-18'
need_plus_email = (user_created_at - plus_after_date).positive?
user_email = "#{actor}@users.noreply.github.com"
user_email = "#{user['id']}+#{user_email}" if need_plus_email
user_email
)

# Tell git who you are
git 'config', '--global', 'user.name', user_name
git 'config', '--global', 'user.email', user_email

# Update Homebrew
brew 'update-reset'

# Tap if desired
tap = formula[%r{^(.+/.+)/.+$}, 1]
brew 'tap', tap if tap

# Get info about formula
stable = Formula[formula].stable
is_git = stable.downloader.is_a? GitDownloadStrategy

# Prepare tag and url
tag = tag.delete_prefix 'refs/tags/'
url = stable.url.gsub stable.version, Version.parse(tag)

# Finally bump the formula
brew 'bump-formula-pr',
'--no-audit',
'--no-browse',
'--message=[`action-homebrew-bump-formula`](https://github.com/dawidd6/action-homebrew-bump-formula)',
*("--url=#{url}" unless is_git),
*("--tag=#{tag}" if is_git),
*("--revision=#{revision}" if is_git),
*('--force' unless force.blank?),
formula
end
110 changes: 0 additions & 110 deletions main.sh

This file was deleted.

0 comments on commit b5daa9a

Please sign in to comment.