gurney_client 1.0.1 → 2.0.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 +4 -4
- data/CHANGELOG.md +9 -3
- data/README.md +59 -33
- data/exe/gurney +2 -2
- data/lib/gurney_client/api.rb +52 -0
- data/lib/gurney_client/cli.rb +106 -0
- data/lib/gurney_client/config.rb +52 -0
- data/lib/gurney_client/report.rb +53 -0
- data/lib/gurney_client/repository.rb +67 -0
- data/lib/gurney_client/version.rb +3 -0
- data/lib/gurney_client.rb +10 -0
- metadata +16 -137
- data/.gitignore +0 -4
- data/.rspec +0 -1
- data/.ruby-version +0 -1
- data/Gemfile +0 -7
- data/Gemfile.lock +0 -59
- data/Rakefile +0 -6
- data/git_hook_sample +0 -3
- data/gurney.gemspec +0 -29
- data/gurney.yml.sample +0 -6
- data/lib/gurney/api.rb +0 -46
- data/lib/gurney/cli/option_parser.rb +0 -60
- data/lib/gurney/cli.rb +0 -111
- data/lib/gurney/config.rb +0 -22
- data/lib/gurney/dependency.rb +0 -28
- data/lib/gurney/dependency_collector.rb +0 -64
- data/lib/gurney/git_file_reader.rb +0 -25
- data/lib/gurney/source/base.rb +0 -15
- data/lib/gurney/source/bundler.rb +0 -35
- data/lib/gurney/source/npm.rb +0 -58
- data/lib/gurney/source/pnpm.rb +0 -54
- data/lib/gurney/source/ruby_version.rb +0 -27
- data/lib/gurney/source/yarn.rb +0 -28
- data/lib/gurney/version.rb +0 -3
- data/lib/gurney.rb +0 -14
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: cac596635ba6531a6934e59df1b124a3baae1dc666eedfc57e10d006b8f0d9ba
|
|
4
|
+
data.tar.gz: 81854187483da6786a207a2d98adc37ec246a79203c59a4bf900c9880edcea8d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6437b10f7eec9ebd23e11d9387f96270f9c83d7901afab6c2260edb806dab4746a0f56d20a39efcc5c9cb4a6cb87980256f0d137728f21feefa53ab9faa73f2b
|
|
7
|
+
data.tar.gz: 20caa7870ffa260212fb2819437f2989402a3c4d943ca2f8e9851550f6cf5973d3e4572cedc659635719bf201c184febb8857ed1d87b8c359c2b2c737b6841b1
|
data/CHANGELOG.md
CHANGED
|
@@ -3,11 +3,17 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
|
5
5
|
|
|
6
|
-
##
|
|
7
|
-
|
|
8
|
-
### Compatible changes
|
|
6
|
+
## 2.0.0 (2026-12-01)
|
|
9
7
|
|
|
10
8
|
### Breaking changes
|
|
9
|
+
* Gurney no longer parses dependency files. It sends them verbatim to Ambulance's gurney API v2, which parses them. It needs an Ambulance that has the v2 API.
|
|
10
|
+
* `api_url` is replaced by `api_host` (the address of Ambulance, `--api-host`). There is no default.
|
|
11
|
+
* Removed the client-side pre-push hook mode (`--client-hook`).
|
|
12
|
+
* No more gem dependencies; gurney needs only Ruby and `git`.
|
|
13
|
+
|
|
14
|
+
### Compatible changes
|
|
15
|
+
* `directories` in `gurney.yml` names several directories with dependency files (monorepos). `prefix` still works for one.
|
|
16
|
+
* The hook reports branches with any name, not only those of word characters.
|
|
11
17
|
|
|
12
18
|
|
|
13
19
|
## 1.0.1 2026-05-21
|
data/README.md
CHANGED
|
@@ -1,48 +1,74 @@
|
|
|
1
1
|
# Gurney
|
|
2
2
|
|
|
3
|
-
Gurney
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
Usually, we configure the latter on our Git server to automatically run on each
|
|
9
|
-
push, with the API url passed as command line option.
|
|
10
|
-
|
|
11
|
-
When run as a post-receive hook, Gurney will make a bare copy of the project and
|
|
12
|
-
look for a gurney.yml file. If present, Gurney looks at the configured branches
|
|
13
|
-
and collects their dependencies. These are reported to the web API.
|
|
14
|
-
|
|
15
|
-
## Usage
|
|
16
|
-
```
|
|
17
|
-
Usage: gurney [options]
|
|
18
|
-
--api-url [API URL] Url for web API call, can have parameters for <project_id> and <branch>
|
|
19
|
-
Example: --api-url "https://example.com/project/<project_id>/branch/<branch>"
|
|
20
|
-
--api-token [API TOKEN] Token to be sent to the API in the X-AuthToken header
|
|
21
|
-
-c, --config [CONFIG FILE] Config file to use
|
|
22
|
-
-h, --hook Run as a Git post-receive hook
|
|
23
|
-
--client-hook Run as a Git pre-push hook
|
|
24
|
-
-p, --project-id [PROJECT ID] Specify project id for API
|
|
25
|
-
--prefix [PATH] Specify the prefix of dependency paths
|
|
26
|
-
--help Print this help
|
|
27
|
-
```
|
|
3
|
+
Gurney reports the dependency files of a project to Ambulance, which parses them and tracks the dependencies' security advisories. It is published to RubyGems as `gurney_client` and has no dependencies besides Ruby (3.2 or later) and `git`.
|
|
4
|
+
|
|
5
|
+
## Configuration
|
|
6
|
+
|
|
7
|
+
A `gurney.yml` at the root of the repository opts the project in. Ambulance shows the right one on the project's page.
|
|
28
8
|
|
|
29
|
-
## Sample Config
|
|
30
9
|
```yaml
|
|
31
10
|
project_id: 1
|
|
32
11
|
branches:
|
|
33
|
-
-
|
|
12
|
+
- main
|
|
34
13
|
- production
|
|
35
|
-
|
|
14
|
+
api_host: https://ambulance.example.com
|
|
36
15
|
api_token: 1234567890
|
|
16
|
+
# Optional: where the dependency files are, if not at the root (default: .)
|
|
17
|
+
directories:
|
|
18
|
+
- .
|
|
19
|
+
- frontend
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Command line options take precedence over the file:
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
Usage: gurney [options]
|
|
26
|
+
--api-host HOST Address of Ambulance, like https://ambulance.example.com
|
|
27
|
+
--api-token TOKEN The project's ingest token, sent in the X-AuthToken header
|
|
28
|
+
-c, --config FILE Config file to use (default: gurney.yml)
|
|
29
|
+
-h, --hook Run as a Git post-receive hook
|
|
30
|
+
-p, --project-id ID Project ID in Ambulance
|
|
31
|
+
--prefix PATH Directory of the dependency files, instead of the directories in the config
|
|
32
|
+
--help Print this help
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Modes
|
|
36
|
+
|
|
37
|
+
- **In a working copy** (`gurney`): reports the current branch, which must be listed under `branches`, with the files as they are on disk.
|
|
38
|
+
- **As a post-receive hook** on the Git server (`gurney --hook`, usually with `--api-host`): reads `gurney.yml` from `master` or `main` and reports each pushed branch listed there, with the files of the pushed commit. Repositories without a `gurney.yml` are skipped. To run it for every repository on GitLab, install it as a [global server hook](https://docs.gitlab.com/ee/administration/server_hooks.html#create-global-server-hooks-for-all-repositories):
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
#!/bin/bash
|
|
42
|
+
gurney --hook --api-host https://ambulance.example.com
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## What gurney sends
|
|
46
|
+
|
|
47
|
+
`POST <api_host>/api/gurney/v2/projects/<project_id>/reports`, gzipped JSON, with the token in `X-AuthToken`:
|
|
48
|
+
|
|
49
|
+
```json
|
|
50
|
+
{
|
|
51
|
+
"environment": "main",
|
|
52
|
+
"source": "hook",
|
|
53
|
+
"client": "gurney_client 2.0.0",
|
|
54
|
+
"revision": "<commit SHA>",
|
|
55
|
+
"repository_path": "/var/opt/gitlab/git-data/repositories/…",
|
|
56
|
+
"config": "<gurney.yml, verbatim>",
|
|
57
|
+
"files": { "Gemfile.lock": "…", "frontend/package.json": "…" }
|
|
58
|
+
}
|
|
37
59
|
```
|
|
38
60
|
|
|
39
|
-
|
|
40
|
-
|
|
61
|
+
Gurney sends only the files named in `GurneyClient::Report::FILES`, from each directory in the config, and only those that exist. Ambulance never decides what is read, so a project can see in this gem what leaves its repository. Clients may also send `observed`, a list of `{ ecosystem, name, version, scope }` seen on the running system (like the Ruby actually installed). An observed entry replaces the one parsed from the files with the same ecosystem and name.
|
|
62
|
+
|
|
63
|
+
Ambulance answers with `{ "status": "ok" | "error", "message": "…" }`; gurney prints the message.
|
|
41
64
|
|
|
42
65
|
## Development
|
|
43
|
-
|
|
66
|
+
|
|
67
|
+
Gurney lives in the Ambulance repository, whose test suite covers it (`test/gurney/`), against the real API. To try it in a project:
|
|
44
68
|
|
|
45
69
|
```bash
|
|
46
|
-
cd some/
|
|
47
|
-
ruby
|
|
70
|
+
cd some/project
|
|
71
|
+
ruby path/to/ambulance/gurney/exe/gurney --api-host http://localhost:3000
|
|
48
72
|
```
|
|
73
|
+
|
|
74
|
+
Release: bump `lib/gurney_client/version.rb`, date its entry in `CHANGELOG.md`, merge to main, then run `bin/rails gurney:release` in the Ambulance repository. It builds the gem into `pkg/`, pushes it to rubygems.org, and tags the commit `gurney-v<version>`. `bin/rails gurney:build` only builds it.
|
data/exe/gurney
CHANGED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
require 'net/http'
|
|
3
|
+
require 'uri'
|
|
4
|
+
require 'zlib'
|
|
5
|
+
|
|
6
|
+
module GurneyClient
|
|
7
|
+
class ApiError < Error; end
|
|
8
|
+
|
|
9
|
+
# Posts reports to Ambulance's gurney API (v2). `transport` replaces the HTTP request in tests.
|
|
10
|
+
class Api
|
|
11
|
+
|
|
12
|
+
def initialize(host:, token:, transport: nil)
|
|
13
|
+
@base = host.match?(%r(\Ahttps?://)) ? host : "https://#{host}"
|
|
14
|
+
@base = @base.chomp('/')
|
|
15
|
+
@token = token
|
|
16
|
+
@transport = transport || method(:post_http)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def post_report(project_id:, report:)
|
|
20
|
+
uri = URI("#{@base}/api/gurney/v2/projects/#{URI.encode_www_form_component(project_id)}/reports")
|
|
21
|
+
headers = {
|
|
22
|
+
'Content-Type' => 'application/json',
|
|
23
|
+
'Content-Encoding' => 'gzip',
|
|
24
|
+
'X-AuthToken' => @token,
|
|
25
|
+
'User-Agent' => "gurney_client/#{VERSION}",
|
|
26
|
+
}
|
|
27
|
+
status, body = @transport.call(uri, Zlib.gzip(JSON.generate(report)), headers)
|
|
28
|
+
message = parse_message(body)
|
|
29
|
+
raise ApiError, "#{status} #{message || 'Is api_host the address of Ambulance?'}" unless (200..299).cover?(status)
|
|
30
|
+
|
|
31
|
+
message
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def parse_message(body)
|
|
37
|
+
JSON.parse(body.to_s)['message']
|
|
38
|
+
rescue JSON::ParserError
|
|
39
|
+
nil
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def post_http(uri, body, headers)
|
|
43
|
+
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https', open_timeout: 10, read_timeout: 60) do |http|
|
|
44
|
+
http.post(uri.request_uri, body, headers)
|
|
45
|
+
end
|
|
46
|
+
[response.code.to_i, response.body]
|
|
47
|
+
rescue SocketError, SystemCallError, Timeout::Error, OpenSSL::SSL::SSLError => e
|
|
48
|
+
raise ApiError, "Could not reach #{uri.host}: #{e.message}"
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
require 'optparse'
|
|
2
|
+
|
|
3
|
+
module GurneyClient
|
|
4
|
+
# `gurney` reports the current branch of the working copy it runs in, or, with --hook, the pushed branches that
|
|
5
|
+
# gurney.yml lists, as a post-receive hook on the Git server.
|
|
6
|
+
class CLI
|
|
7
|
+
|
|
8
|
+
HOOK_LINE = %r{\A(?<old>\h{40}|\h{64}) (?<new>\h{40}|\h{64}) refs/heads/(?<branch>\S+)\z}
|
|
9
|
+
DELETED = /\A0+\z/
|
|
10
|
+
MAIN_BRANCHES = %w[master main].freeze
|
|
11
|
+
|
|
12
|
+
def initialize(argv, stdin: $stdin, stdout: $stdout, stderr: $stderr, env: ENV, pwd: Dir.pwd, transport: nil)
|
|
13
|
+
@argv = argv.dup
|
|
14
|
+
@stdin = stdin
|
|
15
|
+
@stdout = stdout
|
|
16
|
+
@stderr = stderr
|
|
17
|
+
@env = env
|
|
18
|
+
@pwd = pwd
|
|
19
|
+
@transport = transport
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Returns the exit status. A hook always exits with 0: the push has happened anyway.
|
|
23
|
+
def run
|
|
24
|
+
hook = @argv.intersect?(%w[-h --hook])
|
|
25
|
+
@options = parse_options
|
|
26
|
+
return 0 if @options[:help]
|
|
27
|
+
|
|
28
|
+
hook ? run_hook : run_working_copy
|
|
29
|
+
0
|
|
30
|
+
rescue OptionParser::ParseError, Error => e
|
|
31
|
+
@stderr.puts "Gurney error: #{e.message}"
|
|
32
|
+
hook ? 0 : 1
|
|
33
|
+
rescue StandardError => e
|
|
34
|
+
raise unless hook
|
|
35
|
+
|
|
36
|
+
@stderr.puts "Gurney: unexpected error (#{e.class}: #{e.message})"
|
|
37
|
+
0
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def run_working_copy
|
|
43
|
+
repository = Repository::WorkingCopy.new(@pwd)
|
|
44
|
+
config = load_config(repository.read(@options[:config_file]))
|
|
45
|
+
branch = repository.current_branch
|
|
46
|
+
raise Error, "The current branch (#{branch}) is not listed in the config." unless config.branches.include?(branch)
|
|
47
|
+
|
|
48
|
+
report(repository, config, branch:, revision: repository.revision, source: 'cli')
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def run_hook
|
|
52
|
+
repository = Repository::Bare.new(@env['GIT_DIR'] || @pwd)
|
|
53
|
+
raw = MAIN_BRANCHES.lazy.filter_map { |branch| repository.read(@options[:config_file], branch) }.first
|
|
54
|
+
return unless raw # Projects opt in with a gurney.yml.
|
|
55
|
+
|
|
56
|
+
config = load_config(raw)
|
|
57
|
+
pushed_branches.each do |branch, revision|
|
|
58
|
+
next unless config.branches.include?(branch)
|
|
59
|
+
|
|
60
|
+
begin
|
|
61
|
+
report(repository, config, branch:, revision:, source: 'hook')
|
|
62
|
+
rescue ApiError => e
|
|
63
|
+
@stderr.puts "Gurney API error for #{branch}: #{e.message}"
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def pushed_branches
|
|
69
|
+
@stdin.each_line.filter_map do |line|
|
|
70
|
+
match = line.dup.force_encoding(Encoding::UTF_8).scrub.strip.match(HOOK_LINE)
|
|
71
|
+
[match[:branch], match[:new]] if match && !match[:new].match?(DELETED)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def load_config(raw)
|
|
76
|
+
Config.new(raw, @options).tap(&:validate!)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def report(repository, config, branch:, revision:, source:)
|
|
80
|
+
payload = Report.new(repository:, config:, environment: branch, revision:, source:)
|
|
81
|
+
api = Api.new(host: config.api_host, token: config.api_token, transport: @transport)
|
|
82
|
+
message = api.post_report(project_id: config.project_id, report: payload.to_h)
|
|
83
|
+
@stdout.puts "Gurney: #{message || "reported #{payload.files.size} files for #{branch}"}"
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def parse_options
|
|
87
|
+
options = { config_file: 'gurney.yml', hook: false }
|
|
88
|
+
parser = OptionParser.new do |opts|
|
|
89
|
+
opts.banner = 'Usage: gurney [options]'
|
|
90
|
+
opts.on('--api-host HOST', 'Address of Ambulance, like https://ambulance.example.com') { |value| options[:api_host] = value }
|
|
91
|
+
opts.on('--api-token TOKEN', "The project's ingest token, sent in the X-AuthToken header") { |value| options[:api_token] = value }
|
|
92
|
+
opts.on('-c', '--config FILE', 'Config file to use (default: gurney.yml)') { |value| options[:config_file] = value }
|
|
93
|
+
opts.on('-h', '--hook', 'Run as a Git post-receive hook') { options[:hook] = true }
|
|
94
|
+
opts.on('-p', '--project-id ID', 'Project ID in Ambulance') { |value| options[:project_id] = value }
|
|
95
|
+
opts.on('--prefix PATH', 'Directory of the dependency files, instead of the directories in the config') { |value| options[:prefix] = value }
|
|
96
|
+
opts.on_tail('--help', 'Print this help') do
|
|
97
|
+
@stdout.puts opts
|
|
98
|
+
options[:help] = true
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
parser.parse!(@argv)
|
|
102
|
+
options
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
end
|
|
106
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
require 'yaml'
|
|
2
|
+
|
|
3
|
+
module GurneyClient
|
|
4
|
+
# The settings gurney needs itself, from gurney.yml and the command line (which wins). Ambulance gets the whole
|
|
5
|
+
# gurney.yml besides and reads what else it needs from it.
|
|
6
|
+
class Config
|
|
7
|
+
|
|
8
|
+
REQUIRED = %i[project_id branches api_host api_token].freeze
|
|
9
|
+
|
|
10
|
+
attr_reader :raw, :project_id, :branches, :api_host, :api_token, :directories
|
|
11
|
+
|
|
12
|
+
def initialize(raw, overrides = {})
|
|
13
|
+
@raw = raw
|
|
14
|
+
values = parse(raw)
|
|
15
|
+
@project_id = (overrides[:project_id] || values['project_id'])&.to_s
|
|
16
|
+
@branches = Array(values['branches']).map(&:to_s)
|
|
17
|
+
@api_host = (overrides[:api_host] || values['api_host'])&.to_s
|
|
18
|
+
@api_token = (overrides[:api_token] || values['api_token'])&.to_s
|
|
19
|
+
@directories = directories_from(overrides[:prefix] ? [overrides[:prefix]] : values['directories'] || [values['prefix'] || '.'])
|
|
20
|
+
@legacy_api_url = values.key?('api_url')
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def validate!
|
|
24
|
+
missing = REQUIRED.select { |name| public_send(name).nil? || public_send(name).empty? }
|
|
25
|
+
return if missing.empty?
|
|
26
|
+
|
|
27
|
+
message = "Incomplete config - missing #{missing.map(&:inspect).join(', ')}."
|
|
28
|
+
if missing.include?(:api_host) && @legacy_api_url
|
|
29
|
+
message += ' Gurney 2 does not use api_url; set api_host to the address of Ambulance (https://…) instead.'
|
|
30
|
+
end
|
|
31
|
+
raise Error, message
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def parse(raw)
|
|
37
|
+
return {} if raw.nil? || raw.strip.empty?
|
|
38
|
+
|
|
39
|
+
values = YAML.safe_load(raw)
|
|
40
|
+
raise Error, 'gurney.yml must be a YAML mapping.' unless values.nil? || values.is_a?(Hash)
|
|
41
|
+
|
|
42
|
+
(values || {}).transform_keys(&:to_s)
|
|
43
|
+
rescue Psych::Exception => e
|
|
44
|
+
raise Error, "gurney.yml is not valid YAML: #{e.message}"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def directories_from(list)
|
|
48
|
+
Array(list).map { |directory| directory.to_s.sub(%r(\A\./), '').chomp('/') }.map { |directory| directory.empty? ? '.' : directory }.uniq
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
module GurneyClient
|
|
2
|
+
# One report to Ambulance: the dependency files of one environment, verbatim. Ambulance parses them.
|
|
3
|
+
#
|
|
4
|
+
# Gurney sends only these files, so projects can see what leaves the repository without trusting Ambulance. Add a
|
|
5
|
+
# file here before Ambulance needs it, so the next parser change needs no gurney release.
|
|
6
|
+
class Report
|
|
7
|
+
|
|
8
|
+
FILES = %w[
|
|
9
|
+
Gemfile
|
|
10
|
+
Gemfile.lock
|
|
11
|
+
.ruby-version
|
|
12
|
+
.tool-versions
|
|
13
|
+
package.json
|
|
14
|
+
package-lock.json
|
|
15
|
+
yarn.lock
|
|
16
|
+
pnpm-lock.yaml
|
|
17
|
+
pnpm-workspace.yaml
|
|
18
|
+
.nvmrc
|
|
19
|
+
.node-version
|
|
20
|
+
].freeze
|
|
21
|
+
|
|
22
|
+
def initialize(repository:, config:, environment:, revision:, source:)
|
|
23
|
+
@repository = repository
|
|
24
|
+
@config = config
|
|
25
|
+
@environment = environment
|
|
26
|
+
@revision = revision
|
|
27
|
+
@source = source
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def files
|
|
31
|
+
@files ||= @config.directories.flat_map do |directory|
|
|
32
|
+
FILES.filter_map do |name|
|
|
33
|
+
path = (directory == '.') ? name : File.join(directory, name)
|
|
34
|
+
content = @repository.read(path, @revision)
|
|
35
|
+
[path, content] if content
|
|
36
|
+
end
|
|
37
|
+
end.to_h
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def to_h
|
|
41
|
+
{
|
|
42
|
+
environment: @environment,
|
|
43
|
+
source: @source,
|
|
44
|
+
client: "gurney_client #{VERSION}",
|
|
45
|
+
revision: @revision,
|
|
46
|
+
repository_path: @repository.path,
|
|
47
|
+
config: @config.raw,
|
|
48
|
+
files:,
|
|
49
|
+
}.compact
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
require 'open3'
|
|
2
|
+
|
|
3
|
+
module GurneyClient
|
|
4
|
+
# Where gurney reads files from: a working copy (files as they are on disk) or, as a post-receive hook on the Git
|
|
5
|
+
# server, a bare repository (files of a pushed commit).
|
|
6
|
+
#
|
|
7
|
+
# `path` identifies the repository to Ambulance, which rejects reports from another one than the first, to catch
|
|
8
|
+
# forks that kept the original's gurney.yml. Gurney 1 sent the same paths; keep them as they are.
|
|
9
|
+
module Repository
|
|
10
|
+
|
|
11
|
+
# Read as binary: Git hooks often run without a UTF-8 locale, which would make Ruby read files as US-ASCII.
|
|
12
|
+
def self.as_unicode(content)
|
|
13
|
+
content&.force_encoding(Encoding::UTF_8)&.scrub
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
class WorkingCopy
|
|
17
|
+
|
|
18
|
+
attr_reader :dir
|
|
19
|
+
|
|
20
|
+
def initialize(dir)
|
|
21
|
+
@dir = File.expand_path(dir)
|
|
22
|
+
raise Error, 'Must be run within a git repository' unless File.exist?(File.join(@dir, '.git'))
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def path
|
|
26
|
+
git('rev-parse', '--absolute-git-dir')
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def current_branch
|
|
30
|
+
git('rev-parse', '--abbrev-ref', 'HEAD')
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def revision
|
|
34
|
+
git('rev-parse', 'HEAD')
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def read(file, _revision = nil)
|
|
38
|
+
full_path = File.join(@dir, file)
|
|
39
|
+
Repository.as_unicode(File.binread(full_path)) if File.file?(full_path)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
def git(*args)
|
|
45
|
+
out, status = Open3.capture2('git', *args, chdir: @dir)
|
|
46
|
+
out.strip if status.success?
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
class Bare
|
|
52
|
+
|
|
53
|
+
attr_reader :path
|
|
54
|
+
|
|
55
|
+
def initialize(git_dir)
|
|
56
|
+
@path = File.expand_path(git_dir)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def read(file, revision)
|
|
60
|
+
out, _err, status = Open3.capture3('git', '--git-dir', @path, 'show', "#{revision}:#{file}", binmode: true)
|
|
61
|
+
Repository.as_unicode(out) if status.success?
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
module GurneyClient
|
|
2
|
+
class Error < StandardError; end
|
|
3
|
+
end
|
|
4
|
+
|
|
5
|
+
require_relative 'gurney_client/version'
|
|
6
|
+
require_relative 'gurney_client/config'
|
|
7
|
+
require_relative 'gurney_client/repository'
|
|
8
|
+
require_relative 'gurney_client/report'
|
|
9
|
+
require_relative 'gurney_client/api'
|
|
10
|
+
require_relative 'gurney_client/cli'
|