Sha256: 171b596e81eb173126c8ce97deae38db6f123d73797b106dbcbf9a7d88576042

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

require 'yaml'

class Action
  attr_accessor :user, :repo, :dir, :ref
  attr_reader :url

  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

  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

  def initialize(uses)
    captures = uses.match(USES_REGEX).named_captures
    @user = captures['user']
    @repo = captures['repo']
    @dir = captures['dir']
    @ref = captures['ref']
    @url = "#{DOMAIN}/#{@user}/#{@repo}"
  end

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

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

  def latest_tag
    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
actions-updater-0.1.4 lib/action.rb