Class: Action

Inherits:
Object
  • Object
show all
Defined in:
lib/action.rb

Constant Summary collapse

USES_REGEX =
%r{^(?<user>[^/]+)[/](?<repo>[^/]+)[/]?(?<dir>.+)?[@](?<ref>.+)$}.freeze
TAGS_REGEX =
%r{refs/tags/(.*)$}.freeze
DOMAIN =
'https://github.com'
TAG_REJECT_REASONS =
%w[
  alpha
  beta
  rc
  ^{}
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uses) ⇒ Action

Returns a new instance of Action.



30
31
32
33
34
35
36
# File 'lib/action.rb', line 30

def initialize(uses)
  captures = uses.match(USES_REGEX).named_captures
  @user = captures['user']
  @repo = captures['repo']
  @dir = captures['dir']
  @ref = captures['ref']
end

Instance Attribute Details

#dirObject

Returns the value of attribute dir.



6
7
8
# File 'lib/action.rb', line 6

def dir
  @dir
end

#refObject

Returns the value of attribute ref.



6
7
8
# File 'lib/action.rb', line 6

def ref
  @ref
end

#repoObject

Returns the value of attribute repo.



6
7
8
# File 'lib/action.rb', line 6

def repo
  @repo
end

#userObject

Returns the value of attribute user.



6
7
8
# File 'lib/action.rb', line 6

def user
  @user
end

Class Method Details

.array_from_yaml(yaml) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/action.rb', line 18

def self.array_from_yaml(yaml)
  hash = YAML.safe_load(yaml)
  useses = hash['jobs'].values.map do |job|
    job['steps'].map do |step|
      step['uses']
    end
  end
  useses.flatten.compact.uniq.map do |uses|
    new(uses)
  end
end

Instance Method Details

#==(other) ⇒ Object



50
51
52
# File 'lib/action.rb', line 50

def ==(other)
  to_s == other.to_s
end

#latest_tagObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/action.rb', line 54

def latest_tag
  url = "#{DOMAIN}/#{@user}/#{@repo}"
  tags = `git ls-remote -t #{url}`.scan(TAGS_REGEX).flatten
  tags.reject! do |tag|
    TAG_REJECT_REASONS.any? do |reason|
      tag.include?(reason)
    end
  end
  tags.last || @ref
end


42
43
44
# File 'lib/action.rb', line 42

def print
  "#{@user}/#{@repo}#{@dir ? '/' : ''}#{@dir}#{@ref ? '@' : ''}#{@ref}"
end


46
47
48
# File 'lib/action.rb', line 46

def print_without_ref
  "#{@user}/#{@repo}#{@dir ? '/' : ''}#{@dir}"
end

#to_sObject



38
39
40
# File 'lib/action.rb', line 38

def to_s
  print
end