spm_version_updates 1.2.0 → 3.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.
@@ -16,21 +16,166 @@ require_relative "version_tags_persistent_cache"
16
16
  require_relative "xcode_parser"
17
17
 
18
18
  # Core SPM version checking logic (migrated from Danger plugin)
19
+ # rubocop:disable-next Metrics/ClassLength
19
20
  class SpmChecker
20
- VERSION_TAG_WORKER_COUNT = 8
21
+ DEFAULT_VERSION_LOOKUP_WORKERS = 4
21
22
 
22
23
  # Raised when allow-hosts blocks a repository before git is contacted.
23
24
  class DisallowedRepositoryHost < SpmVersionUpdates::PolicyError; end
24
25
 
25
- # Structured facts about each warning, used by the GitHub Action comment
26
- # renderer. `check_for_updates` and `check_manifests` still return the legacy
27
- # string warnings for compatibility with existing plugin-style callers.
28
- attr_reader :warning_details
26
+ # Filters configured Package.resolved paths and reports missing files.
27
+ class ResolvedPathList
28
+ def initialize(paths, missing_handler)
29
+ @paths = paths
30
+ @missing_handler = missing_handler
31
+ end
32
+
33
+ def existing
34
+ return @paths if missing.empty?
35
+
36
+ raise(ManifestParser::CouldNotFindResolvedFile, missing.join(", ")) unless @missing_handler
37
+
38
+ @missing_handler.call(missing)
39
+ @paths - missing
40
+ end
41
+
42
+ private
43
+
44
+ def missing
45
+ @missing ||= @paths.reject { |path| File.exist?(path) }
46
+ end
47
+ end
48
+ private_constant :ResolvedPathList
49
+
50
+ # Converts resolved pins into the package and version maps consumed by checks.
51
+ class ResolvedPackageEntries
52
+ def initialize(path, malformed_handler)
53
+ @path = path
54
+ @malformed_handler = malformed_handler
55
+ end
56
+
57
+ def packages_and_versions
58
+ pins.each_with_object([{}, {}]) { |pin, (packages, versions)|
59
+ entry = ResolvedPinEntry.new(pin)
60
+ entry.add_to(packages, versions)
61
+ }
62
+ end
63
+
64
+ private
65
+
66
+ def pins
67
+ PackageResolved.pins_from(@path)
68
+ rescue PackageResolved::MalformedFileError => error
69
+ raise unless @malformed_handler
70
+
71
+ @malformed_handler.call(@path, error)
72
+ []
73
+ end
74
+ end
75
+ private_constant :ResolvedPackageEntries
76
+
77
+ # Normalized data derived from one Package.resolved pin.
78
+ class ResolvedPinEntry
79
+ def initialize(pin)
80
+ @pin = pin
81
+ end
82
+
83
+ def normalized_url
84
+ @pin["normalized_url"]
85
+ end
86
+
87
+ def package
88
+ {
89
+ "repository_url" => @pin["repository_url"],
90
+ "requirement" => requirement
91
+ }
92
+ end
93
+
94
+ def resolved_version
95
+ @pin["version"] || @pin["revision"]
96
+ end
97
+
98
+ def add_to(packages, versions)
99
+ packages[normalized_url] = package
100
+ versions[normalized_url] = resolved_version
101
+ end
102
+
103
+ private
104
+
105
+ def requirement
106
+ version = @pin["version"]
107
+ return { "kind" => "resolvedPin", "version" => version } if version
29
108
 
30
- # ParseWarning records for `.package(...)` declarations the manifest parser
31
- # had to skip. Kept separate from update warnings so reported update counts
32
- # and fail-on thresholds are unaffected.
33
- attr_reader :parse_warnings
109
+ { "kind" => "revision", "revision" => @pin["revision"] }
110
+ end
111
+ end
112
+ private_constant :ResolvedPinEntry
113
+
114
+ # Builds warning detail records while preserving explicit nil requirements.
115
+ class WarningDetailRecord
116
+ def initialize(message, package, detail)
117
+ @message = message
118
+ @package = package
119
+ @detail = detail
120
+ end
121
+
122
+ def to_h
123
+ with_optional_requirement(compacted_record)
124
+ end
125
+
126
+ private
127
+
128
+ def record
129
+ @detail.merge(message: @message, source: @package.source)
130
+ end
131
+
132
+ def compacted_record
133
+ record.compact
134
+ end
135
+
136
+ def with_optional_requirement(compacted)
137
+ return compacted unless record.key?(:suggested_requirement)
138
+
139
+ compacted.merge(suggested_requirement: record[:suggested_requirement])
140
+ end
141
+ end
142
+ private_constant :WarningDetailRecord
143
+
144
+ # Parses a package's resolved version for semantic comparisons.
145
+ class PackageSemver
146
+ def initialize(package)
147
+ @package = package
148
+ end
149
+
150
+ def value
151
+ resolved_version = @package.resolved_version
152
+ SpmVersionUpdates::Semver.new(resolved_version)
153
+ rescue ArgumentError => error
154
+ puts("Unable to extract semver from #{resolved_version} for #{@package.name} (#{error})")
155
+ nil
156
+ end
157
+ end
158
+ private_constant :PackageSemver
159
+
160
+ # Structured result from one checker run. Parse warnings stay separate from
161
+ # update records so counts and fail-on thresholds only consider real updates.
162
+ class Result
163
+ attr_reader :updates, :parse_warnings
164
+
165
+ def initialize(updates:, parse_warnings:)
166
+ @updates = normalize_records(updates)
167
+ @parse_warnings = normalize_records(parse_warnings)
168
+ end
169
+
170
+ private
171
+
172
+ def normalize_records(records)
173
+ Array(records).map { |record| record.to_h.transform_keys(&:to_s).compact.freeze }
174
+ .freeze
175
+ end
176
+ end
177
+
178
+ attr_reader :version_lookup_workers
34
179
 
35
180
  attr_accessor :allow_hosts,
36
181
  :check_branches,
@@ -54,6 +199,12 @@ class SpmChecker
54
199
  # default), PackageResolved::MalformedFileError is raised.
55
200
  attr_accessor :malformed_resolved_handler
56
201
 
202
+ # Optional callable `(missing_paths)` invoked instead of raising when expected
203
+ # Package.resolved files are missing. Missing paths are dropped and packages
204
+ # without a resolved version keep the existing "Unable to locate..." behavior:
205
+ # no warning record is created and no version tags are fetched for them.
206
+ attr_accessor :missing_resolved_handler
207
+
57
208
  def self.redact_credentials(value)
58
209
  CredentialRedactor.redact(value)
59
210
  end
@@ -61,12 +212,12 @@ class SpmChecker
61
212
  def initialize
62
213
  @check_when_exact = @check_revisions = @report_above_maximum = @report_pre_releases = false
63
214
  @check_branches = true
64
- @lookup_failure_handler = @malformed_resolved_handler = nil
215
+ @lookup_failure_handler = @malformed_resolved_handler = @missing_resolved_handler = nil
65
216
  @ignore_repos = []
66
217
  @repository_update_rules = RepositoryUpdateRules.empty
67
218
  @allow_hosts = []
68
- @warnings = []
69
- @warning_details = []
219
+ @version_lookup_workers = DEFAULT_VERSION_LOOKUP_WORKERS
220
+ @updates = []
70
221
  @parse_warnings = []
71
222
  @version_tags_cache = {}
72
223
  @version_tag_lookup_errors = {}
@@ -75,15 +226,19 @@ class SpmChecker
75
226
  @version_tags_cache_ttl_seconds = VersionTagsPersistentCache::DEFAULT_TTL_SECONDS
76
227
  end
77
228
 
229
+ def version_lookup_workers=(value)
230
+ workers = Integer(value.to_s, 10, exception: false)
231
+ raise(SpmVersionUpdates::ConfigurationError, "version_lookup_workers must be a positive integer") unless workers && workers >= 1
232
+
233
+ @version_lookup_workers = workers
234
+ end
235
+
78
236
  # Check for SPM updates using an Xcode project as the source of dependencies.
79
237
  #
80
238
  # @param [String] xcodeproj_path The path to your Xcode project
81
- # @return [Array<String>] Array of warning messages
239
+ # @return [Result] Structured update records and parse warnings
82
240
  def check_for_updates(xcodeproj_path)
83
- clear_warnings
84
- reset_version_tags_cache
85
- normalize_ignore_repos
86
- normalize_allow_hosts
241
+ prepare_run
87
242
 
88
243
  remote_packages = XcodeParser.get_packages(xcodeproj_path)
89
244
  resolved_versions = XcodeParser.get_resolved_versions(xcodeproj_path, &@malformed_resolved_handler)
@@ -91,7 +246,7 @@ class SpmChecker
91
246
  warn_for_empty_xcode_project(remote_packages, resolved_versions, xcodeproj_path)
92
247
 
93
248
  check_packages(remote_packages, resolved_versions)
94
- @warnings
249
+ result
95
250
  end
96
251
 
97
252
  # Check for SPM updates using one or more `Package.swift` manifests as the
@@ -107,12 +262,9 @@ class SpmChecker
107
262
  # `Package.resolved` paths. When omitted, a `Package.resolved` next to
108
263
  # each manifest is used.
109
264
  # @raise [ManifestParser::CouldNotFindResolvedFile] if no resolved file exists
110
- # @return [Array<String>] Array of warning messages
265
+ # @return [Result] Structured update records and parse warnings
111
266
  def check_manifests(manifest_paths, resolved_paths = nil)
112
- clear_warnings
113
- reset_version_tags_cache
114
- normalize_ignore_repos
115
- normalize_allow_hosts
267
+ prepare_run
116
268
 
117
269
  resolved_versions = merged_resolved_versions(manifest_paths, resolved_paths)
118
270
  puts("Found resolved versions for #{resolved_versions.size} packages")
@@ -120,11 +272,34 @@ class SpmChecker
120
272
  manifest_paths.each { |manifest_path|
121
273
  check_packages(manifest_packages(manifest_path), resolved_versions, manifest_path)
122
274
  }
123
- @warnings
275
+ result
276
+ end
277
+
278
+ # Check for SPM updates using one or more `Package.resolved` files as the
279
+ # source of dependencies. Version pins are compared directly with available
280
+ # tags; revision-only pins are reported only when check_revisions is enabled.
281
+ #
282
+ # @param [Array<String>] resolved_paths Paths to one or more Package.resolved files
283
+ # @raise [SpmVersionUpdates::ConfigurationError] if no paths are supplied
284
+ # @return [Result] Structured update records and parse warnings
285
+ def check_resolved(resolved_paths)
286
+ prepare_run
287
+ paths = normalized_resolved_paths(resolved_paths)
288
+ raise(SpmVersionUpdates::ConfigurationError, "package-resolved-paths must be set") if paths.empty?
289
+
290
+ check_existing_resolved_paths(paths)
291
+ result
124
292
  end
125
293
 
126
294
  private
127
295
 
296
+ def prepare_run
297
+ clear_updates
298
+ reset_version_tags_cache
299
+ normalize_ignore_repos
300
+ normalize_allow_hosts
301
+ end
302
+
128
303
  def manifest_packages(manifest_path)
129
304
  ManifestParser.get_packages(manifest_path) { |skip| record_parse_warning(skip, manifest_path) }
130
305
  end
@@ -149,9 +324,12 @@ class SpmChecker
149
324
  raw_allow_hosts.any? && @allow_hosts.empty?
150
325
  end
151
326
 
152
- def clear_warnings
153
- @warnings.clear
154
- @warning_details.clear
327
+ def result
328
+ Result.new(updates: @updates, parse_warnings: @parse_warnings)
329
+ end
330
+
331
+ def clear_updates
332
+ @updates.clear
155
333
  @parse_warnings.clear
156
334
  end
157
335
 
@@ -185,16 +363,23 @@ class SpmChecker
185
363
  #
186
364
  # @return [Hash<String, String>]
187
365
  def merged_resolved_versions(manifest_paths, resolved_paths)
188
- paths = Array(resolved_paths).map(&:to_s).reject(&:empty?)
366
+ paths = normalized_resolved_paths(resolved_paths)
189
367
  paths = manifest_paths.map { |manifest| ManifestParser.default_resolved_path(manifest) } if paths.empty?
190
368
 
191
- missing = paths.reject { |path| File.exist?(path) }
192
- raise(ManifestParser::CouldNotFindResolvedFile, missing.join(", ")) unless missing.empty?
369
+ paths = existing_paths(paths)
193
370
 
194
371
  puts("Reading resolved packages from: #{paths}")
195
372
  paths.each_with_object({}) { |path, pins| pins.merge!(resolved_versions_from(path)) }
196
373
  end
197
374
 
375
+ def normalized_resolved_paths(paths)
376
+ Array(paths).map(&:to_s).reject(&:empty?)
377
+ end
378
+
379
+ def existing_paths(paths)
380
+ ResolvedPathList.new(paths, @missing_resolved_handler).existing
381
+ end
382
+
198
383
  def resolved_versions_from(path)
199
384
  PackageResolved.versions_from(path)
200
385
  rescue PackageResolved::MalformedFileError => error
@@ -204,6 +389,17 @@ class SpmChecker
204
389
  {}
205
390
  end
206
391
 
392
+ def packages_from_resolved(path)
393
+ ResolvedPackageEntries.new(path, @malformed_resolved_handler).packages_and_versions
394
+ end
395
+
396
+ def check_existing_resolved_paths(paths)
397
+ existing_paths(paths).each { |path|
398
+ remote_packages, resolved_versions = packages_from_resolved(path)
399
+ check_packages(remote_packages, resolved_versions, path)
400
+ }
401
+ end
402
+
207
403
  # Compare a set of declared dependencies against the resolved pins.
208
404
  #
209
405
  # Packages are keyed by their normalized repository URL, which is what we match
@@ -312,7 +508,7 @@ class SpmChecker
312
508
  persistent_cache = VersionTagsPersistentCache.new(directory: @version_tags_cache_dir, ttl_seconds: @version_tags_cache_ttl_seconds)
313
509
  results, errors = VersionTagFetcher.call(
314
510
  pending,
315
- worker_limit: VERSION_TAG_WORKER_COUNT,
511
+ worker_limit: @version_lookup_workers,
316
512
  persistent_cache:,
317
513
  raise_on_error: !@lookup_failure_handler
318
514
  )
@@ -386,6 +582,8 @@ class SpmChecker
386
582
  warn_for_new_versions(package, available_versions, :minor)
387
583
  when "versionRange"
388
584
  warn_for_new_versions_range(package, available_versions)
585
+ when "resolvedPin"
586
+ warn_for_new_versions_pin(package, available_versions)
389
587
  else
390
588
  puts("Not processing dependency rule '#{kind}' for #{package.name} (#{self.class.redact_credentials(repository_url)})")
391
589
  end
@@ -399,13 +597,12 @@ class SpmChecker
399
597
  end
400
598
 
401
599
  def record_warning(message, package, record)
402
- @warnings << [message, package.source_line].compact.join("\n")
403
- @warning_details << record
600
+ @updates << record
404
601
  puts("WARNING: #{message}#{package.source_suffix}")
405
602
  end
406
603
 
407
604
  def warning_detail_record(message, package, detail)
408
- detail.merge(message:, source: package.source).compact
605
+ WarningDetailRecord.new(message, package, detail).to_h
409
606
  end
410
607
 
411
608
  def warning_detail(type, package, available_version, note = nil)
@@ -506,6 +703,20 @@ class SpmChecker
506
703
  end
507
704
  end
508
705
 
706
+ def warn_for_new_versions_pin(package, available_versions)
707
+ resolved_version = PackageSemver.new(package).value
708
+ return unless resolved_version
709
+
710
+ newest_version = newest_reportable_version(available_versions)
711
+ return unless newest_version && newest_version > resolved_version
712
+
713
+ add_warning(
714
+ "Newer version of #{package.name}: #{newest_version}",
715
+ package,
716
+ warning_detail(:version, package, newest_version, "resolved pin")
717
+ )
718
+ end
719
+
509
720
  def warn_for_new_versions(package, available_versions, major_or_minor)
510
721
  name = package.name
511
722
  resolved_version_string = package.resolved_version
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SpmVersionUpdates
4
- VERSION = "1.2.0"
4
+ VERSION = "3.0.0"
5
5
  end
@@ -7,6 +7,7 @@ require_relative "spm_version_updates/fail_on_threshold"
7
7
  require_relative "spm_version_updates/git_host_normalizer"
8
8
  require_relative "spm_version_updates/git_operations"
9
9
  require_relative "spm_version_updates/manifest_parser"
10
+ require_relative "spm_version_updates/manifest_updater"
10
11
  require_relative "spm_version_updates/package_resolved"
11
12
  require_relative "spm_version_updates/parse_warning"
12
13
  require_relative "spm_version_updates/repository_link"
@@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
14
14
  spec.summary = "Core library for checking Swift Package Manager dependency updates."
15
15
  spec.homepage = "https://github.com/hbmartin/github-action-spm_version_updates"
16
16
  spec.license = "MIT"
17
- spec.required_ruby_version = ">= 3.2"
17
+ spec.required_ruby_version = ">= 3.3"
18
18
 
19
19
  release_paths = [
20
20
  "LICENSE.txt",
@@ -40,7 +40,7 @@ Gem::Specification.new do |spec|
40
40
  spec.require_paths = ["lib"]
41
41
  spec.metadata["rubygems_mfa_required"] = "true"
42
42
 
43
- spec.add_runtime_dependency("semverify", "~> 0.3")
43
+ spec.add_dependency("semverify", "~> 0.3")
44
44
  # Xcode-project mode additionally requires the "xcodeproj" gem (loaded
45
45
  # lazily); manifest mode works without it.
46
46
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spm_version_updates
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harold Martin
@@ -41,6 +41,7 @@ files:
41
41
  - lib/spm_version_updates/git_host_normalizer.rb
42
42
  - lib/spm_version_updates/git_operations.rb
43
43
  - lib/spm_version_updates/manifest_parser.rb
44
+ - lib/spm_version_updates/manifest_updater.rb
44
45
  - lib/spm_version_updates/package_resolved.rb
45
46
  - lib/spm_version_updates/parse_warning.rb
46
47
  - lib/spm_version_updates/repository_link.rb
@@ -68,14 +69,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
68
69
  requirements:
69
70
  - - ">="
70
71
  - !ruby/object:Gem::Version
71
- version: '3.2'
72
+ version: '3.3'
72
73
  required_rubygems_version: !ruby/object:Gem::Requirement
73
74
  requirements:
74
75
  - - ">="
75
76
  - !ruby/object:Gem::Version
76
77
  version: '0'
77
78
  requirements: []
78
- rubygems_version: 4.0.10
79
+ rubygems_version: 4.0.20
79
80
  specification_version: 4
80
81
  summary: Core library for checking Swift Package Manager dependency updates.
81
82
  test_files: []