-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
fetch-external-resources
executable file
·140 lines (122 loc) · 3.44 KB
/
fetch-external-resources
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env ruby
#
# This script is responsible for reaching out to the internet and downloading
# external content which needs to be built into the site.
require 'faraday'
require 'faraday_middleware'
require 'fileutils'
require 'json'
require 'uri'
require 'yaml'
require 'zip'
RESOURCES = [
[
'https://updates.jenkins.io/latestCore.txt',
'content/_tmp/latestCore.txt',
nil,
nil
],
[
'https://updates.jenkins.io/stable/latestCore.txt',
'content/_tmp/latestLTSCore.txt',
nil,
nil
],
[
'https://github.com/jenkinsci/pipeline-examples/archive/master.zip',
'content/_tmp/pipeline-examples-master.zip',
nil,
'content/_tmp/examples'
],
[
'https://ci.jenkins.io/job/Infra/job/pipeline-steps-doc-generator/job/master/lastSuccessfulBuild/artifact/allAscii.zip',
'content/_tmp/allAscii.zip',
nil,
'content/doc/pipeline/steps'
],
[
'https://repo.jenkins-ci.org/api/search/versions?g=org.jenkins-ci.main&a=jenkins-core&repos=releases&v=?.*.1',
'content/_data/_generated/lts_baselines.yml',
nil,
nil
],
[
'https://updates.jenkins.io/update-center.actual.json',
'content/_data/_generated/update_center.yml',
nil,
nil
],
[
'https://updates.jenkins.io/tiers.json',
'content/_data/_generated/core_tiers.yml',
nil,
nil
],
[
'https://ci.jenkins.io/job/Infra/job/backend-extension-indexer/job/master/lastSuccessfulBuild/artifact/*.adoc/*zip*/extension-indexer.zip',
'content/_tmp/extension-indexer.zip',
nil,
'content/doc/developer/extensions'
],
]
class Fetcher
# Fetch a single resource and return the +Faraday::Response+ for it
#
# This abstraction exists so more behaviors (caching, etc) can be injected
# into the fetching of specific resources themeslves
def fetch_resource(origin)
conn = Faraday.new(:ssl => {:verify => false}) do |f|
f.use FaradayMiddleware::FollowRedirects, limit: 5
f.adapter Faraday.default_adapter
end
return conn.get(origin)
end
# process a list of resources and generate files from them
def process(resources)
failures = []
puts "Fetching external resources:"
resources.each do |origin, destination, frontmatter, zip_dest|
response = fetch_resource(origin)
unless response.success?
failures << [response, origin]
next
end
FileUtils.mkdir_p File.dirname(destination)
File.open(destination, 'w+') do |f|
if frontmatter
f.write(frontmatter.to_yaml)
f.write("---\n")
end
puts "Writing #{destination}"
if destination.include? ".yml"
puts "Converting JSON to YAML"
f.write(YAML.dump(JSON.load(response.body)))
else
f.write(response.body)
end
end
#if downloading a zip file, extract it
if zip_dest
puts "Unzipping to #{zip_dest}"
Zip.on_exists_proc = true
FileUtils.mkdir_p zip_dest
Zip::File.open(destination) do |zip_file|
zip_file.each do |f|
fpath = File.join(zip_dest, f.name)
zip_file.extract(f, fpath)
end
end
end
end
# If we collected any failures, dump those out while we're here
unless failures.empty?
puts "Failed processing resources!"
puts
failures.each do |response, origin|
puts "#{origin} #{response.inspect}"
end
exit 1
end
end
end
Fetcher.new.process(RESOURCES)