Class: Workflow

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename:, content:) ⇒ Workflow

Returns a new instance of Workflow.



6
7
8
9
10
11
12
13
14
# File 'lib/workflow.rb', line 6

def initialize(filename:, content:)
    @filename = filename
    @raw = content
    @raw_lines = content.lines
    @data = YAML.safe_load(content, permitted_classes: [Symbol]) || {}
rescue YAML::SyntaxError => e
    @data = {}
    @parse_error = e.message
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/workflow.rb', line 4

def data
  @data
end

#filenameObject (readonly)

Returns the value of attribute filename.



4
5
6
# File 'lib/workflow.rb', line 4

def filename
  @filename
end

#rawObject (readonly)

Returns the value of attribute raw.



4
5
6
# File 'lib/workflow.rb', line 4

def raw
  @raw
end

#raw_linesObject (readonly)

Returns the value of attribute raw_lines.



4
5
6
# File 'lib/workflow.rb', line 4

def raw_lines
  @raw_lines
end

Instance Method Details

#env(scope: :workflow, step: nil) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/workflow.rb', line 42

def env(scope: :workflow, step: nil)
    case scope
    when :workflow
        @data["env"] || {}
    when :step
        step&.dig("env") || {}
    end
end

#jobsObject



22
23
24
# File 'lib/workflow.rb', line 22

def jobs
    @data["jobs"] || {}
end

#line_content(num) ⇒ Object



66
67
68
# File 'lib/workflow.rb', line 66

def line_content(num)
    raw_lines[num - 1]&.rstrip
end

#line_of(pattern) ⇒ Object



51
52
53
54
55
56
# File 'lib/workflow.rb', line 51

def line_of(pattern)
    raw_lines.each_with_index do |line, i|
        return i + 1 if line.match?(pattern)
    end
    nil
end

#lines_of(pattern) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/workflow.rb', line 58

def lines_of(pattern)
    results = []
    raw_lines.each_with_index do |line, i|
        results << (i + 1) if line.match?(pattern)
    end
    results
end

#parse_error?Boolean

Returns:

  • (Boolean)


16
# File 'lib/workflow.rb', line 16

def parse_error? = !@parse_error.nil?

#permissions(scope: :workflow, job: nil) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/workflow.rb', line 32

def permissions(scope: :workflow, job: nil)
    case scope
    when :workflow
        @data["permissions"]
    when :job
        j = job.is_a?(String) ? jobs[job] : job
        j&.dig("permissions")
    end
end

#run_blocksObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/workflow.rb', line 86

def run_blocks
    results = []
    all_run_lines = lines_of(/^\s+run:\s*[\|>]?\s*/)
    run_idx = 0
    jobs.each do |_job_id, job_hash|
        steps(job_hash).each do |step|
            next unless step["run"]
            line = all_run_lines[run_idx] || all_run_lines.last
            run_idx += 1
            results << { run: step["run"], step: step, env: step["env"] || {}, line: line }
        end
    end
    results
end

#steps(job) ⇒ Object



26
27
28
29
30
# File 'lib/workflow.rb', line 26

def steps(job)
    job_hash = job.is_a?(String) ? jobs[job] : job
    return [] unless job_hash.is_a?(Hash)
    job_hash["steps"] || []
end

#triggersObject



18
19
20
# File 'lib/workflow.rb', line 18

def triggers
    @data["on"] || @data[true] || {}
end

#uses_actionsObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/workflow.rb', line 70

def uses_actions
    results = []
    seen_lines = Hash.new(0)
    jobs.each do |_job_id, job_hash|
        steps(job_hash).each do |step|
            next unless step["uses"]
            all_lines = lines_of(/uses:\s*#{Regexp.escape(step["uses"])}/)
            idx = seen_lines[step["uses"]]
            line = all_lines[idx] || all_lines.last
            seen_lines[step["uses"]] += 1
            results << { uses: step["uses"], step: step, line: line }
        end
    end
    results
end