From 6a7e07f427fab8aafa1dffc9b5a20f29ee93de88 Mon Sep 17 00:00:00 2001 From: Dmitry Rybakov Date: Sat, 29 Jul 2023 12:47:54 +0200 Subject: [PATCH 1/6] 3209 --- .gitignore | 1 + upload_api_docs.rb | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 upload_api_docs.rb diff --git a/.gitignore b/.gitignore index b127af9a1c..a60fb2cf76 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,4 @@ data/* gemfiles/*.gemfile.lock .env.private* .env +build diff --git a/upload_api_docs.rb b/upload_api_docs.rb new file mode 100644 index 0000000000..89ff1e17e3 --- /dev/null +++ b/upload_api_docs.rb @@ -0,0 +1,54 @@ +require 'bundler/inline' + +gemfile true do + source 'https://rubygems.org' + gem 'nokogiri' + gem 'aws-sdk-s3' + gem 'mimemagic' +end + +require 'aws-sdk-s3' +require 'mimemagic' +require_relative 'lib/mongo/version' + +def upload_files(local_folder_path, s3, bucket_name, s3_folder_path) + Dir.glob("#{local_folder_path}/**/*").each do |file| + next if File.directory?(file) + + key = File.join(s3_folder_path, file.gsub("#{local_folder_path}/",'')) + + mime_type = MimeMagic.by_path(file) + + puts "Mime type for #{file} is #{mime_type}" + + if mime_type.nil? + puts "Unable to determine mime type for #{file}" + s3.put_object(bucket: bucket_name, key: key, body: File.read(file)) + else + s3.put_object(bucket: bucket_name, key: key, body: File.read(file), content_type: mime_type.type) + end + + print '.' + $stdout.flush + end +end + +ACCESS_KEY = ENV['DOCS_AWS_ACCESS_KEY_ID'] +SECRET_KEY = ENV['DOCS_AWS_SECRET_ACCESS_KEY'] +S3_BUCKET = ENV['DOCS_AWS_BUCKET'] +S3_PREFIX = "docs/ruby-driver/#{Mongo::VERSION}/api" + + +Aws.config.update({ + region: 'us-east-2', + credentials: Aws::Credentials.new(ACCESS_KEY, SECRET_KEY) +}) +Aws.use_bundled_cert! + +s3 = Aws::S3::Client.new + +MimeMagic.add('text/html', extensions: ['html']) + +upload_files('build/public/master/api', s3, S3_BUCKET, S3_PREFIX) +puts "\nDone!" + From 70029bafcab0dd6c8d1cb4bd0262092b03b5d699 Mon Sep 17 00:00:00 2001 From: Dmitry Rybakov Date: Sun, 30 Jul 2023 14:13:18 +0200 Subject: [PATCH 2/6] 3209 --- upload-api-docs | 119 +++++++++++++++++++++++++++++++++++++++++++++ upload_api_docs.rb | 54 -------------------- 2 files changed, 119 insertions(+), 54 deletions(-) create mode 100755 upload-api-docs delete mode 100644 upload_api_docs.rb diff --git a/upload-api-docs b/upload-api-docs new file mode 100755 index 0000000000..6d2604f8fc --- /dev/null +++ b/upload-api-docs @@ -0,0 +1,119 @@ +#!/usr/bin/env ruby + +require 'bundler/inline' + +gemfile true do + source 'https://rubygems.org' + gem 'nokogiri' + gem 'aws-sdk-s3' + gem 'mimemagic' + gem 'yard' +end + +require 'aws-sdk-s3' +require 'mimemagic' +require 'optparse' +require 'yard' + +class FileUploader + def initialize(options) + Aws.config.update({ + region: options[:region], + credentials: Aws::Credentials.new(options[:access_key], options[:secret_key]) + }) + Aws.use_bundled_cert! + @s3 = Aws::S3::Client.new + @bucket = options[:bucket] + @prefix = options[:prefix] + @docs_path = options[:docs_path] + MimeMagic.add('text/html', extensions: ['html']) + end + + def upload_docs + Dir.glob("#{@docs_path}/**/*").each do |file| + next if File.directory?(file) + + upload_file(file, key(file)) + print '.' + $stdout.flush + end + puts "\nDone!" + end + + private + + def key(file, local_folder_path) + File.join(@prefix, file.gsub("#{@docs_path}/",'')) + end + + def upload_file(file, key) + mime_type = MimeMagic.by_path(file) + if mime_type.nil? + puts "Unable to determine mime type for #{file}" + @s3.put_object(bucket: @bucket, key: key, body: File.read(file)) + else + @s3.put_object(bucket: @bucket, key: key, body: File.read(file), content_type: mime_type.type) + end + end +end + +class Options + def initialize + @options = {} + parse_cli_options! + parse_env_options! + @options[:prefix] = "docs/ruby-driver/#{@options[:version]}/api" + @options[:docs_path] = "build/public/#{@options[:version]}/api" + end + + def [](key) + @options[key] + end + + private + + def parse_cli_options! + OptionParser.new do |opts| + opts.banner = "Usage: upload-api-docs [options]" + + opts.on("-v VERSION", "--version=VERSION", "Driver version") do |v| + @options[:version] = v + end + opts.on('-b BUCKET', '--bucket=BUCKET', 'S3 Bucket to upload') do |b| + @options[:bucket] = b + end + opts.on('-r REGION', '--region=REGION', 'AWS region') do |r| + @options[:region] = r + end + end.parse! + [:version, :bucket, :region].each do |opt| + raise OptionParser::MissingArgument, "Option --#{opt} is required" unless @options[opt] + end + end + + def parse_env_options! + @options[:access_key]= ENV['DOCS_AWS_ACCESS_KEY_ID'] || raise ArgumentError, 'Please provide aws access key via DOCS_AWS_ACCESS_KEY_ID env variable' + @options[:secret_key] = ENV['DOCS_AWS_SECRET_ACCESS_KEY'] || raise ArgumentError, 'Please provide aws secret key via DOCS_AWS_SECRET_ACCESS_KEY env variable' + end + +end + +def generate_docs(options) + YARD::CLI::Yardoc.run( + '.', + '--exclude', './.evergreen', + '--exclude', './.mod', + '--exclude', './examples', + '--exclude', './profile', + '--exclude', './release', + '--exclude', './spec', + '--readme', './README.md', + '-o', options[:docs_path] + ) +end + + +options = Options.new +generate_docs(options) +FileUploader.new(options).upload_docs + diff --git a/upload_api_docs.rb b/upload_api_docs.rb deleted file mode 100644 index 89ff1e17e3..0000000000 --- a/upload_api_docs.rb +++ /dev/null @@ -1,54 +0,0 @@ -require 'bundler/inline' - -gemfile true do - source 'https://rubygems.org' - gem 'nokogiri' - gem 'aws-sdk-s3' - gem 'mimemagic' -end - -require 'aws-sdk-s3' -require 'mimemagic' -require_relative 'lib/mongo/version' - -def upload_files(local_folder_path, s3, bucket_name, s3_folder_path) - Dir.glob("#{local_folder_path}/**/*").each do |file| - next if File.directory?(file) - - key = File.join(s3_folder_path, file.gsub("#{local_folder_path}/",'')) - - mime_type = MimeMagic.by_path(file) - - puts "Mime type for #{file} is #{mime_type}" - - if mime_type.nil? - puts "Unable to determine mime type for #{file}" - s3.put_object(bucket: bucket_name, key: key, body: File.read(file)) - else - s3.put_object(bucket: bucket_name, key: key, body: File.read(file), content_type: mime_type.type) - end - - print '.' - $stdout.flush - end -end - -ACCESS_KEY = ENV['DOCS_AWS_ACCESS_KEY_ID'] -SECRET_KEY = ENV['DOCS_AWS_SECRET_ACCESS_KEY'] -S3_BUCKET = ENV['DOCS_AWS_BUCKET'] -S3_PREFIX = "docs/ruby-driver/#{Mongo::VERSION}/api" - - -Aws.config.update({ - region: 'us-east-2', - credentials: Aws::Credentials.new(ACCESS_KEY, SECRET_KEY) -}) -Aws.use_bundled_cert! - -s3 = Aws::S3::Client.new - -MimeMagic.add('text/html', extensions: ['html']) - -upload_files('build/public/master/api', s3, S3_BUCKET, S3_PREFIX) -puts "\nDone!" - From 8e4dc15512f5b7196f339457f0a68dfc1f5f1236 Mon Sep 17 00:00:00 2001 From: Dmitry Rybakov Date: Sun, 30 Jul 2023 14:36:04 +0200 Subject: [PATCH 3/6] 3209 --- upload-api-docs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/upload-api-docs b/upload-api-docs index 6d2604f8fc..8279e598f1 100755 --- a/upload-api-docs +++ b/upload-api-docs @@ -43,7 +43,7 @@ class FileUploader private def key(file, local_folder_path) - File.join(@prefix, file.gsub("#{@docs_path}/",'')) + File.join(@prefix, file.gsub("#{@docs_path}/", '')) end def upload_file(file, key) @@ -62,8 +62,9 @@ class Options @options = {} parse_cli_options! parse_env_options! - @options[:prefix] = "docs/ruby-driver/#{@options[:version]}/api" - @options[:docs_path] = "build/public/#{@options[:version]}/api" + @options[:branch] = `git rev-parse --abbrev-ref HEAD`.strip + @options[:prefix] = "docs/ruby-driver/#{@options[:branch]}/api" + @options[:docs_path] = "build/public/#{@options[:branch]}/api" end def [](key) @@ -76,9 +77,6 @@ class Options OptionParser.new do |opts| opts.banner = "Usage: upload-api-docs [options]" - opts.on("-v VERSION", "--version=VERSION", "Driver version") do |v| - @options[:version] = v - end opts.on('-b BUCKET', '--bucket=BUCKET', 'S3 Bucket to upload') do |b| @options[:bucket] = b end @@ -86,16 +84,15 @@ class Options @options[:region] = r end end.parse! - [:version, :bucket, :region].each do |opt| + [:bucket, :region].each do |opt| raise OptionParser::MissingArgument, "Option --#{opt} is required" unless @options[opt] end end def parse_env_options! - @options[:access_key]= ENV['DOCS_AWS_ACCESS_KEY_ID'] || raise ArgumentError, 'Please provide aws access key via DOCS_AWS_ACCESS_KEY_ID env variable' - @options[:secret_key] = ENV['DOCS_AWS_SECRET_ACCESS_KEY'] || raise ArgumentError, 'Please provide aws secret key via DOCS_AWS_SECRET_ACCESS_KEY env variable' + @options[:access_key] = ENV.fetch('DOCS_AWS_ACCESS_KEY_ID') { raise ArgumentError, 'Please provide aws access key via DOCS_AWS_ACCESS_KEY_ID env variable' } + @options[:secret_key] = ENV.fetch('DOCS_AWS_SECRET_ACCESS_KEY') { raise ArgumentError, 'Please provide aws secret key via DOCS_AWS_SECRET_ACCESS_KEY env variable' } end - end def generate_docs(options) From 14e1d2046c1ad135571a7abe8717f93ccbea6005 Mon Sep 17 00:00:00 2001 From: Dmitry Rybakov Date: Sun, 30 Jul 2023 14:45:02 +0200 Subject: [PATCH 4/6] Get back to version cli param --- upload-api-docs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/upload-api-docs b/upload-api-docs index 8279e598f1..f369a33f5c 100755 --- a/upload-api-docs +++ b/upload-api-docs @@ -62,9 +62,8 @@ class Options @options = {} parse_cli_options! parse_env_options! - @options[:branch] = `git rev-parse --abbrev-ref HEAD`.strip - @options[:prefix] = "docs/ruby-driver/#{@options[:branch]}/api" - @options[:docs_path] = "build/public/#{@options[:branch]}/api" + @options[:prefix] = "docs/ruby-driver/#{@options[:version]}/api" + @options[:docs_path] = "build/public/#{@options[:version]}/api" end def [](key) @@ -83,15 +82,22 @@ class Options opts.on('-r REGION', '--region=REGION', 'AWS region') do |r| @options[:region] = r end + opt.on('-v VERSION', '--version=VERSION', 'Ruby driver version') do |v| + @options[:version] = v + end end.parse! - [:bucket, :region].each do |opt| + [:bucket, :region, :version].each do |opt| raise OptionParser::MissingArgument, "Option --#{opt} is required" unless @options[opt] end end def parse_env_options! - @options[:access_key] = ENV.fetch('DOCS_AWS_ACCESS_KEY_ID') { raise ArgumentError, 'Please provide aws access key via DOCS_AWS_ACCESS_KEY_ID env variable' } - @options[:secret_key] = ENV.fetch('DOCS_AWS_SECRET_ACCESS_KEY') { raise ArgumentError, 'Please provide aws secret key via DOCS_AWS_SECRET_ACCESS_KEY env variable' } + @options[:access_key] = ENV.fetch('DOCS_AWS_ACCESS_KEY_ID') do + raise ArgumentError, 'Please provide aws access key via DOCS_AWS_ACCESS_KEY_ID env variable' + end + @options[:secret_key] = ENV.fetch('DOCS_AWS_SECRET_ACCESS_KEY') do + raise ArgumentError, 'Please provide aws secret key via DOCS_AWS_SECRET_ACCESS_KEY env variable' + end end end @@ -113,4 +119,4 @@ end options = Options.new generate_docs(options) FileUploader.new(options).upload_docs - +return 0 From d98da81e6617e43a8121e3a46c268a2713d08e53 Mon Sep 17 00:00:00 2001 From: Dmitry Rybakov Date: Tue, 1 Aug 2023 10:15:59 +0200 Subject: [PATCH 5/6] Always use current --- upload-api-docs | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/upload-api-docs b/upload-api-docs index f369a33f5c..b68a560cde 100755 --- a/upload-api-docs +++ b/upload-api-docs @@ -6,12 +6,10 @@ gemfile true do source 'https://rubygems.org' gem 'nokogiri' gem 'aws-sdk-s3' - gem 'mimemagic' gem 'yard' end require 'aws-sdk-s3' -require 'mimemagic' require 'optparse' require 'yard' @@ -26,7 +24,6 @@ class FileUploader @bucket = options[:bucket] @prefix = options[:prefix] @docs_path = options[:docs_path] - MimeMagic.add('text/html', extensions: ['html']) end def upload_docs @@ -42,18 +39,21 @@ class FileUploader private - def key(file, local_folder_path) + def key(file) File.join(@prefix, file.gsub("#{@docs_path}/", '')) end def upload_file(file, key) - mime_type = MimeMagic.by_path(file) - if mime_type.nil? - puts "Unable to determine mime type for #{file}" - @s3.put_object(bucket: @bucket, key: key, body: File.read(file)) - else - @s3.put_object(bucket: @bucket, key: key, body: File.read(file), content_type: mime_type.type) - end + mime_type = mime_type(file) + @s3.put_object(bucket: @bucket, key: key, body: File.read(file), content_type: mime_type) + end + + def mime_type(file) + { + '.html' => 'text/html', + '.css' => 'text/css', + '.js' => 'application/javascript', + }.fetch(File.extname(file)) end end @@ -62,8 +62,8 @@ class Options @options = {} parse_cli_options! parse_env_options! - @options[:prefix] = "docs/ruby-driver/#{@options[:version]}/api" - @options[:docs_path] = "build/public/#{@options[:version]}/api" + @options[:prefix] = "docs/ruby-driver/current/api" + @options[:docs_path] = "build/public/current/api" end def [](key) @@ -82,11 +82,8 @@ class Options opts.on('-r REGION', '--region=REGION', 'AWS region') do |r| @options[:region] = r end - opt.on('-v VERSION', '--version=VERSION', 'Ruby driver version') do |v| - @options[:version] = v - end end.parse! - [:bucket, :region, :version].each do |opt| + [:bucket, :region].each do |opt| raise OptionParser::MissingArgument, "Option --#{opt} is required" unless @options[opt] end end From 18247523cac96d6430057c052728b0aa5bf95980 Mon Sep 17 00:00:00 2001 From: Dmitry Rybakov Date: Thu, 3 Aug 2023 21:24:16 +0200 Subject: [PATCH 6/6] Fix rubocop complaints --- upload-api-docs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/upload-api-docs b/upload-api-docs index b68a560cde..1915efabb0 100755 --- a/upload-api-docs +++ b/upload-api-docs @@ -1,4 +1,5 @@ #!/usr/bin/env ruby +# frozen_string_literal: true require 'bundler/inline' @@ -13,12 +14,13 @@ require 'aws-sdk-s3' require 'optparse' require 'yard' +# This class contains logic for uploading API docs to S3. class FileUploader def initialize(options) Aws.config.update({ - region: options[:region], - credentials: Aws::Credentials.new(options[:access_key], options[:secret_key]) - }) + region: options[:region], + credentials: Aws::Credentials.new(options[:access_key], options[:secret_key]) + }) Aws.use_bundled_cert! @s3 = Aws::S3::Client.new @bucket = options[:bucket] @@ -57,13 +59,14 @@ class FileUploader end end +# This class contains logic for parsing CLI and ENV options. class Options def initialize @options = {} parse_cli_options! parse_env_options! - @options[:prefix] = "docs/ruby-driver/current/api" - @options[:docs_path] = "build/public/current/api" + @options[:prefix] = 'docs/ruby-driver/current/api' + @options[:docs_path] = 'build/public/current/api' end def [](key) @@ -74,7 +77,7 @@ class Options def parse_cli_options! OptionParser.new do |opts| - opts.banner = "Usage: upload-api-docs [options]" + opts.banner = 'Usage: upload-api-docs [options]' opts.on('-b BUCKET', '--bucket=BUCKET', 'S3 Bucket to upload') do |b| @options[:bucket] = b @@ -83,7 +86,7 @@ class Options @options[:region] = r end end.parse! - [:bucket, :region].each do |opt| + %i[bucket region].each do |opt| raise OptionParser::MissingArgument, "Option --#{opt} is required" unless @options[opt] end end @@ -112,8 +115,7 @@ def generate_docs(options) ) end - options = Options.new generate_docs(options) FileUploader.new(options).upload_docs -return 0 +return