brew-github-private-download-strategy 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '0206035838f1fcded049f82a24ee2446cbc221b0f826bd34a5a6a909127c02bb'
4
+ data.tar.gz: e6f7ff8280981e03c8191f19a3dbfc2e33cf2653b8ec45a4964874dd98b92198
5
+ SHA512:
6
+ metadata.gz: c6e89caf74e17ba151cf64a7032dfe4b648802e07d9fc197ef2aa1dd8dc6221d9d9b68800a1c5c6f9ede22e519aafc21b11ca8752dd43f201c3d4c39e16c2dc8
7
+ data.tar.gz: bfb354bc84ef87a20004c0b91e73a664089c3194dd2324ec7d8b06e9f2d4256da487e05a967bf4c31b20d1d1826ca37e067d4118acbbea03b8fd757ca3517c30
@@ -0,0 +1,113 @@
1
+ # Copyright (c) 2017-present SIGHUP s.r.l All rights reserved.
2
+ # Use of this source code is governed by a proprietary
3
+ # license that can be found in the LICENSE file.
4
+
5
+ # SPDX-License-Identifier: MIT
6
+ # SPDX-FileType: SOURCE
7
+
8
+ # Credits: https://raw.githubusercontent.com/MikeMcQuaid/brew/599ecc9b5ad7951b8ddc51490ebe93a976d43b29/Library/Homebrew/compat/download_strategy.rb
9
+
10
+ require "download_strategy"
11
+
12
+ # GitHubPrivateRepositoryDownloadStrategy downloads contents from GitHub
13
+ # Private Repository. To use it, add
14
+ # `:using => :github_private_repo` to the URL section of
15
+ # your formula. This download strategy uses GitHub access tokens (in the
16
+ # environment variables `HOMEBREW_GITHUB_API_TOKEN`) to sign the request. This
17
+ # strategy is suitable for corporate use just like S3DownloadStrategy, because
18
+ # it lets you use a private GitHub repository for internal distribution. It
19
+ # works with public one, but in that case simply use CurlDownloadStrategy.
20
+ class GitHubPrivateRepositoryDownloadStrategy < CurlDownloadStrategy
21
+ def initialize(url, name, version, **meta)
22
+ super
23
+ parse_url_pattern
24
+ set_github_token
25
+ end
26
+
27
+ def parse_url_pattern
28
+ unless match = url.match(%r{https://github.com/([^/]+)/([^/]+)/(\S+)})
29
+ raise CurlDownloadStrategyError, "Invalid url pattern for GitHub Repository."
30
+ end
31
+
32
+ _, @owner, @repo, @filepath = *match
33
+ end
34
+
35
+ def download_url
36
+ "https://#{@github_token}@github.com/#{@owner}/#{@repo}/#{@filepath}"
37
+ end
38
+
39
+ private
40
+
41
+ def _fetch(url:, resolved_url:, timeout:)
42
+ curl_download download_url, to: temporary_path
43
+ end
44
+
45
+ def set_github_token
46
+ @github_token = ENV["HOMEBREW_GITHUB_API_TOKEN"]
47
+ unless @github_token
48
+ raise CurlDownloadStrategyError, "Environmental variable HOMEBREW_GITHUB_API_TOKEN is required."
49
+ end
50
+
51
+ validate_github_repository_access!
52
+ end
53
+
54
+ def validate_github_repository_access!
55
+ # Test access to the repository
56
+ GitHub.repository(@owner, @repo)
57
+ rescue GitHub::HTTPNotFoundError
58
+ # We only handle HTTPNotFoundError here,
59
+ # becase AuthenticationFailedError is handled within util/github.
60
+ message = <<~EOS
61
+ HOMEBREW_GITHUB_API_TOKEN can not access the repository: #{@owner}/#{@repo}
62
+ This token may not have permission to access the repository or the url of formula may be incorrect.
63
+ EOS
64
+ raise CurlDownloadStrategyError, message
65
+ end
66
+ end
67
+
68
+ # GitHubPrivateRepositoryReleaseDownloadStrategy downloads tarballs from GitHub
69
+ # Release assets. To use it, add `:using => :github_private_release` to the URL section
70
+ # of your formula. This download strategy uses GitHub access tokens (in the
71
+ # environment variables HOMEBREW_GITHUB_API_TOKEN) to sign the request.
72
+ class GitHubPrivateRepositoryReleaseDownloadStrategy < GitHubPrivateRepositoryDownloadStrategy
73
+ def initialize(url, name, version, **meta)
74
+ super
75
+ end
76
+
77
+ def parse_url_pattern
78
+ url_pattern = %r{https://github.com/([^/]+)/([^/]+)/releases/download/([^/]+)/(\S+)}
79
+ unless @url =~ url_pattern
80
+ raise CurlDownloadStrategyError, "Invalid url pattern for GitHub Release."
81
+ end
82
+
83
+ _, @owner, @repo, @tag, @filename = *@url.match(url_pattern)
84
+ end
85
+
86
+ def download_url
87
+ "https://#{@github_token}@api.github.com/repos/#{@owner}/#{@repo}/releases/assets/#{asset_id}"
88
+ end
89
+
90
+ private
91
+
92
+ def _fetch(url:, resolved_url:, timeout:)
93
+ # HTTP request header `Accept: application/octet-stream` is required.
94
+ # Without this, the GitHub API will respond with metadata, not binary.
95
+ curl_download download_url, "--header", "Accept: application/octet-stream", to: temporary_path
96
+ end
97
+
98
+ def asset_id
99
+ @asset_id ||= resolve_asset_id
100
+ end
101
+
102
+ def resolve_asset_id
103
+ release_metadata = fetch_release_metadata
104
+ assets = release_metadata["assets"].select { |a| a["name"] == @filename }
105
+ raise CurlDownloadStrategyError, "Asset file not found." if assets.empty?
106
+
107
+ assets.first["id"]
108
+ end
109
+
110
+ def fetch_release_metadata
111
+ GitHub.get_release(@owner, @repo, @tag)
112
+ end
113
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: brew-github-private-download-strategy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Claudio Beatrice
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-12-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |-
14
+ Whenever you want to create a homebrew package for a private repository, you have to create a custom download strategy as Homebrew does not support downloading releases from private repositories.
15
+ Thanks to this Gem and a bit of configuration in your homebrew formula, you will be able to make your private repository available to your team via `brew install`.
16
+ email: engineering@sighup.io
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/brew-github-private-download-strategy.rb
22
+ homepage: https://rubygems.org/gems/brew-github-private-download-strategy
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubygems_version: 3.0.3.1
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Create homebrew packages that allow to download private releases from Github.
45
+ test_files: []