Skip to content

Commit b5daa9a

Browse files
authored
Merge pull request #3 from dawidd6/rewrite-in-ruby
Rewrite in Ruby
2 parents 37893b6 + 0fde40f commit b5daa9a

File tree

6 files changed

+134
-200
lines changed

6 files changed

+134
-200
lines changed

.github/workflows/test.yml

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
name: Test Action
22

3-
on:
4-
push:
5-
branches:
6-
- master
3+
on: push
74

85
jobs:
96
test:
107
runs-on: ubuntu-latest
8+
strategy:
9+
matrix:
10+
formula:
11+
- dawidd6/test/test-formula-url
12+
- dawidd6/test/test-formula-git-revision
13+
include:
14+
- formula: dawidd6/test/test-formula-url
15+
tag: v0.1.12
16+
- formula: dawidd6/test/test-formula-git-revision
17+
tag: v0.3.3
18+
revision: c43abd765cf51c06bbcaa5479dc49aab1396989f
1119
steps:
1220
- name: Checkout
1321
uses: actions/checkout@v2
1422
- name: Test
1523
uses: ./
1624
with:
1725
token: ${{secrets.TOKEN}}
18-
formula: dawidd6/homebrew-test/test-formula-url
19-
url: https://github.com/dawidd6/actions-updater/archive/v0.1.12.tar.gz
20-
- name: Test
21-
uses: ./
22-
with:
23-
token: ${{secrets.TOKEN}}
24-
formula: dawidd6/homebrew-test/test-formula-git-revision
25-
tag: v0.3.3
26-
revision: c43abd765cf51c06bbcaa5479dc49aab1396989f
26+
formula: ${{matrix.formula}}
27+
tag: ${{matrix.tag}}
28+
revision: ${{matrix.revision}}

Dockerfile

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
FROM homebrew/brew
22

3-
COPY *.sh /
3+
ENV HOMEBREW_NO_ENV_FILTERING=1
4+
ENV HOMEBREW_NO_AUTO_UPDATE=1
5+
ENV HOMEBREW_NO_ANALYTICS=1
6+
ENV HOMEBREW_COLOR=1
47

5-
ENTRYPOINT ["/main.sh"]
8+
COPY main.rb /
9+
10+
ENTRYPOINT ["brew", "ruby", "/main.rb"]

README.md

+13-62
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ An action that wraps `brew bump-formula-pr` to ease the process of updating the
44

55
## Usage
66

7-
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.
7+
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.
88

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

@@ -15,71 +15,22 @@ on:
1515
- '*'
1616
```
1717
18-
Example of bumping any formula in any user tap:
18+
because then, the script will extract all needed informations by itself, you just need to specify the following step in your workflow:
1919
2020
```yaml
21-
- name: Get tag
22-
id: tag
23-
uses: dawidd6/action-get-tag@v1
24-
2521
- name: Update Homebrew formula
26-
uses: dawidd6/action-homebrew-bump-formula@v1
22+
uses: dawidd6/action-homebrew-bump-formula@v2
2723
with:
28-
token: ${{secrets.GITHUB_PAT}}
24+
token: ${{secrets.TOKEN}}
25+
# For example:
26+
# dawidd6/tap/actions-updater
27+
# or if the formula is in the core tap:
28+
# lazygit
2929
formula: USER/REPO/FORMULA
30-
url: "https://github.com/USER/REPO/archive/${{steps.tag.outputs.tag}}.tar.gz"
31-
```
32-
33-
**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.
34-
35-
Example of bumping [`lazygit`](https://github.com/jesseduffield/lazygit) formula in [`Homebrew/homebrew-core`](https://github.com/Homebrew/homebrew-core) tap:
36-
37-
```yaml
38-
- name: Get tag
39-
id: tag
40-
uses: dawidd6/action-get-tag@v1
41-
42-
- name: Update Homebrew formula
43-
uses: dawidd6/action-homebrew-bump-formula@v1
44-
with:
45-
token: ${{secrets.GITHUB_PAT}}
46-
formula: lazygit
47-
url: "https://github.com/jesseduffield/lazygit/archive/${{steps.tag.outputs.tag}}.tar.gz"
48-
```
49-
50-
... using `url` input because the formula already specifies it:
51-
52-
```ruby
53-
class Lazygit < Formula
54-
desc "Simple terminal UI for git commands"
55-
homepage "https://github.com/jesseduffield/lazygit/"
56-
url "https://github.com/jesseduffield/lazygit/archive/v0.16.2.tar.gz"
57-
sha256 "76c043e59afc403d7353cdb188ac6850ce4c4125412e291240c787b0187e71c6"
58-
```
59-
60-
Example of bumping [`lazydocker`](https://github.com/jesseduffield/lazdockert) formula in [`Homebrew/homebrew-core`](https://github.com/Homebrew/homebrew-core) tap:
61-
62-
```yaml
63-
- name: Get tag
64-
id: tag
65-
uses: dawidd6/action-get-tag@v1
66-
67-
- name: Update Homebrew formula
68-
uses: dawidd6/action-homebrew-bump-formula@v1
69-
with:
70-
token: ${{secrets.GITHUB_PAT}}
71-
formula: lazydocker
72-
tag: ${{steps.tag.outputs.tag}}
30+
# Optional, will be determined automatically
31+
tag: ${{github.ref}}
32+
# Optional, will be determined automatically
7333
revision: ${{github.sha}}
74-
```
75-
76-
... using `tag` and `revision` inputs because the formula already specifies them:
77-
78-
```ruby
79-
class Lazydocker < Formula
80-
desc "The lazier way to manage everything docker"
81-
homepage "https://github.com/jesseduffield/lazydocker"
82-
url "https://github.com/jesseduffield/lazydocker.git",
83-
:tag => "v0.8",
84-
:revision => "cea67bc570daaa757a886813ff3c2763189efef6"
34+
# Optional, if don't want to check for already open PRs
35+
force: true
8536
```

action.yml

+21-13
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,33 @@ inputs:
99
description: Github token (not the default one)
1010
required: true
1111
formula:
12-
description: Formula name (for example "unzip" or "user/tap/formula")
12+
description: |
13+
Formula name
14+
15+
Optionally in format USER/REPO/FORMULA
16+
17+
Example: lazygit
18+
Example: dawidd6/tap/actions-updater
1319
required: true
14-
url:
15-
description: URL to download source archive
16-
required: false
17-
mirror:
18-
description: URL to download source archive (mirror)
19-
required: false
20-
sha256:
21-
description: Source archive SHA256 checksum
22-
required: false
2320
tag:
24-
description: Git tag
21+
description: |
22+
Git tag
23+
24+
Example: v1.0.0
25+
Example: refs/tags/v1.0.0
2526
required: false
27+
default: ${{github.ref}}
2628
revision:
27-
description: Git revision
29+
description: |
30+
Git revision
31+
32+
Only required for formulae that use git to download the source
33+
34+
Example: 130d3a3af72f66780ae4e24cd143ae7a4d757f9d
2835
required: false
36+
default: ${{github.sha}}
2937
force:
30-
description: Check open PRs or not
38+
description: Check open PRs or not (will fail if detected)
3139
required: false
3240
runs:
3341
using: docker

main.rb

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# frozen_string_literal: true
2+
3+
module Homebrew
4+
module_function
5+
6+
def brew(*args)
7+
puts "[command]brew #{args.join(' ')}"
8+
return if ENV['DEBUG']
9+
10+
safe_system('brew', *args)
11+
end
12+
13+
def git(*args)
14+
puts "[command]git #{args.join(' ')}"
15+
return if ENV['DEBUG']
16+
17+
safe_system('git', *args)
18+
end
19+
20+
# Get inputs
21+
token = ENV['INPUT_TOKEN']
22+
formula = ENV['INPUT_FORMULA']
23+
tag = ENV['INPUT_TAG']
24+
revision = ENV['INPUT_REVISION']
25+
force = ENV['INPUT_FORCE']
26+
27+
# Die if required inputs are not provided
28+
odie 'TOKEN is required' if token.blank?
29+
odie 'FORMULA is required' if formula.blank?
30+
odie 'TAG is required' if tag.blank?
31+
32+
# Set needed HOMEBREW environment variables
33+
ENV['HOMEBREW_GITHUB_API_TOKEN'] = token
34+
35+
# Get user details
36+
actor = ENV['GITHUB_ACTOR']
37+
user = GitHub.open_api "#{GitHub::API_URL}/users/#{actor}"
38+
user_name = user['name'] || actor
39+
user_email = user['email'] || (
40+
# https://help.github.com/en/github/setting-up-and-managing-your-github-user-account/setting-your-commit-email-address
41+
user_created_at = Date.parse user['created_at']
42+
plus_after_date = Date.parse '2017-07-18'
43+
need_plus_email = (user_created_at - plus_after_date).positive?
44+
user_email = "#{actor}@users.noreply.github.com"
45+
user_email = "#{user['id']}+#{user_email}" if need_plus_email
46+
user_email
47+
)
48+
49+
# Tell git who you are
50+
git 'config', '--global', 'user.name', user_name
51+
git 'config', '--global', 'user.email', user_email
52+
53+
# Update Homebrew
54+
brew 'update-reset'
55+
56+
# Tap if desired
57+
tap = formula[%r{^(.+/.+)/.+$}, 1]
58+
brew 'tap', tap if tap
59+
60+
# Get info about formula
61+
stable = Formula[formula].stable
62+
is_git = stable.downloader.is_a? GitDownloadStrategy
63+
64+
# Prepare tag and url
65+
tag = tag.delete_prefix 'refs/tags/'
66+
url = stable.url.gsub stable.version, Version.parse(tag)
67+
68+
# Finally bump the formula
69+
brew 'bump-formula-pr',
70+
'--no-audit',
71+
'--no-browse',
72+
'--message=[`action-homebrew-bump-formula`](https://github.com/dawidd6/action-homebrew-bump-formula)',
73+
*("--url=#{url}" unless is_git),
74+
*("--tag=#{tag}" if is_git),
75+
*("--revision=#{revision}" if is_git),
76+
*('--force' unless force.blank?),
77+
formula
78+
end

main.sh

-110
This file was deleted.

0 commit comments

Comments
 (0)