Class: Bk::Commands::Artifacts

Inherits:
Base
  • Object
show all
Defined in:
lib/bk/commands/artifacts.rb

Constant Summary collapse

BuildArtifactsQuery =
Client.parse "    query($slug: ID!, $jobs_after: String) {\n      build(slug: $slug) {\n        number\n        message\n        uuid\n\n        branch\n        pipeline {\n          slug\n        }\n\n        url\n        pullRequest {\n          id\n        }\n\n        state\n        scheduledAt\n        startedAt\n        finishedAt\n        canceledAt\n\n        jobs(first: 500, after: $jobs_after) {\n          pageInfo {\n            endCursor\n            hasNextPage\n          }\n          edges {\n            node {\n              __typename\n              ... on JobTypeWait {\n                uuid\n                label\n              }\n              ... on JobTypeTrigger {\n                uuid\n                label\n              }\n\n              ... on JobTypeCommand {\n                uuid\n                label\n\n                url\n                exitStatus\n\n                parallelGroupIndex\n                parallelGroupTotal\n\n                artifacts(first: 500) {\n                  edges {\n                    node {\n                      state\n                      path\n                      downloadURL\n                    }\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    }\n\n"

Constants included from Format

Format::HORIZONTAL_PIPE, Format::VERTICAL_PIPE

Instance Attribute Summary

Attributes inherited from Base

#spinner

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from Format

#annotation_colors, #build_colors, #build_header, #is_tty?, #job_colors, #pastel, #vertical_pipe

Methods included from Bk::Color

#colorize, #create_color_hash, #default_color, #error_color, #info_color, #success_color, #warning_color

Constructor Details

This class inherits a constructor from Bk::Commands::Base

Instance Method Details

#call(args: {}, url_or_slug: nil, **options) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/bk/commands/artifacts.rb', line 78

def call(args: {}, url_or_slug: nil, **options)
  slug = determine_slug(url_or_slug)
  unless slug
    raise ArgumentError, "Unable to figure out slug to use"
  end

  glob = options[:glob]
  download = options[:download]

  jobs_after = nil
  has_next_page = true

  while has_next_page
    result = query(BuildArtifactsQuery, variables: {slug: slug, jobs_after: jobs_after})

    build = result.data.build
    # only show the first time
    if jobs_after.nil?
      puts build_header(build)
      puts ""
    end

    jobs_after = build.jobs.page_info.end_cursor
    has_next_page = build.jobs.page_info.has_next_page

    jobs = build.jobs.edges.map(&:node)
    jobs.each do |job|
      next unless job.respond_to?(:exit_status)

      artifacts = job.artifacts.edges.map(&:node).select { |artifact| glob_matches?(glob, artifact.path) }
      next unless artifacts.any?

      color = job_colors[job.exit_status]
      header = color.call(job.label)
      if job.parallel_group_index && job.parallel_group_total
        header = "#{header} (#{job.parallel_group_index + 1}/#{job.parallel_group_total})"
      end

      puts header

      artifacts.each do |artifact|
        if download
          puts "  - #{artifact.path} (downloading to tmp/bk/[filename])"
          download_artifact(artifact)
        else
          puts "  - #{artifact.path}"
        end
      end
    end
  end
end

#download_artifact(artifact) ⇒ Object



138
139
140
141
142
143
144
145
# File 'lib/bk/commands/artifacts.rb', line 138

def download_artifact(artifact)
  download_url = artifact.to_h["downloadURL"]
  redirected_response_from_aws = Net::HTTP.get_response(URI(download_url))
  artifact_response = Net::HTTP.get_response(URI(redirected_response_from_aws["location"]))
  path = Pathname.new("tmp/bk/#{artifact.path}")
  FileUtils.mkdir_p(path.dirname)
  path.write(artifact_response.body)
end

#glob_matches?(glob, path) ⇒ Boolean



130
131
132
133
134
135
136
# File 'lib/bk/commands/artifacts.rb', line 130

def glob_matches?(glob, path)
  if glob
    File.fnmatch?(glob, path, File::FNM_PATHNAME)
  else
    true
  end
end