diff --git a/generate_plugin_placeholder.rb b/generate_plugin_placeholder.rb new file mode 100644 index 0000000..0bfcbdd --- /dev/null +++ b/generate_plugin_placeholder.rb @@ -0,0 +1,60 @@ +require "clamp" +require "erb" +require "octokit" +require_relative "git_helper" + +class GeneratePluginPlaceholderDoc < Clamp::Command + option "--output-path", "OUTPUT", "Path to a directory where logstash-docs repository is cloned and changes going to be written to", required: true + option "--plugin-type", "STRING", "Type (ex: integration, input, etc) of a new plugin.", required: true + option "--plugin-name", "STRING", "Name of the plugin.", required: true + + SUPPORTED_TYPES = %w(integration) + + def execute + generate_placeholder(plugin_type, plugin_name) + submit_pr + end + + # adds an empty static page under the VPR + # this helps us to eliminate broken link issues in the docs + def generate_placeholder(type, name) + if type.nil? || name.nil? + $stderr.puts("Plugin type and name are required.") + return + end + + unless SUPPORTED_TYPES.include?(type) + $stderr.puts("#{type} is not supported. Supported types are #{SUPPORTED_TYPES.inspect}") + return + end + + placeholder_asciidoc = "#{logstash_docs_path}/docs/versioned-plugins/#{type}s/#{name}-index.asciidoc" + # changing template will fail to re-index the doc, do not change or keep consistent with VPR templates + template = ERB.new(IO.read("logstash/templates/docs/versioned-plugins/plugin-index.asciidoc.erb")) + content = template.result_with_hash(name: name, type: type, versions: []) + File.write(placeholder_asciidoc, content) + end + + def logstash_docs_path + path = File.join(output_path, "logstash-docs") + fail("#{path} doesn't exist. Please provide the path for `--output-path` where `logstash-docs` repo is located.") unless Dir.exist?(path) + path + end + + def submit_pr + branch_name = "new_plugin_placeholder" + git_helper = GitHelper.new("elastic/logstash-docs") + if git_helper.branch_exists?(branch_name) + puts "WARNING: Branch \"#{branch_name}\" already exists. Aborting creation of PR. Please merge the existing PR or delete the PR and the branch." + return + end + + pr_title = "A placeholder for new plugin" + git_helper.commit(logstash_docs_path, branch_name, "create an empty placeholder for new plugin") + git_helper.create_pull_request(branch_name, "versioned_plugin_docs", pr_title, "", { draft: true }) + end +end + +if __FILE__ == $0 + GeneratePluginPlaceholderDoc.run +end \ No newline at end of file diff --git a/git_helper.rb b/git_helper.rb new file mode 100644 index 0000000..0964e13 --- /dev/null +++ b/git_helper.rb @@ -0,0 +1,38 @@ +require "octokit" + +class GitHelper + + attr_reader :git_client, :repo_name + + def initialize(repo_name) + print "not " if ENV.fetch("GITHUB_TOKEN", "").empty? + puts "using a github token" + + @git_client = Octokit::Client.new(:access_token => ENV["GITHUB_TOKEN"]) + + fail("Repo name cannot be null or empty") if repo_name.nil? || repo_name.empty? + @repo_name = repo_name + end + + def commit(repo_path, branch_name, commit_msg) + puts "Committing changes..." + Dir.chdir(repo_path) do |path| + `git checkout -b #{branch_name}` + `git add .` + `git commit -m "#{commit_msg}" -a` + `git push origin #{branch_name}` + end + end + + def create_pull_request(branch_name, against_branch, title, description, options = {}) + puts "Creating a PR..." + @git_client.create_pull_request(@repo_name, against_branch, branch_name, title, description, options) + end + + def branch_exists?(branch_name) + @git_client.branch(@repo_name, branch_name) + true + rescue Octokit::NotFound + false + end +end \ No newline at end of file diff --git a/versioned_plugins.rb b/versioned_plugins.rb index b2ee16f..0bbecfd 100644 --- a/versioned_plugins.rb +++ b/versioned_plugins.rb @@ -9,6 +9,7 @@ require_relative 'lib/core_ext/erb_result_with_hash' require_relative 'lib/logstash-docket' +require_relative 'git_helper' class VersionedPluginDocs < Clamp::Command option "--output-path", "OUTPUT", "Path to a directory where logstash-docs repository will be cloned and written to", required: true @@ -233,19 +234,15 @@ def new_versions? def submit_pr branch_name = "versioned_docs_new_content" - octo = Octokit::Client.new(:access_token => ENV["GITHUB_TOKEN"]) - if branch_exists?(octo, branch_name) + git_helper = GitHelper.new("elastic/logstash-docs") + if git_helper.branch_exists?(branch_name) puts "WARNING: Branch \"#{branch_name}\" already exists. Not creating a new PR. Please merge the existing PR or delete the PR and the branch." return end - Dir.chdir(logstash_docs_path) do |path| - `git checkout -b #{branch_name}` - `git add .` - `git commit -m "updated versioned plugin docs" -a` - `git push origin #{branch_name}` - end - octo.create_pull_request("elastic/logstash-docs", "versioned_plugin_docs", branch_name, - "auto generated update of versioned plugin documentation", "") + + pr_title = "auto generated update of versioned plugin documentation" + git_helper.commit(logstash_docs_path, branch_name, "updated versioned plugin docs") + git_helper.create_pull_request(branch_name, "versioned_plugin_docs", pr_title, "") end def branch_exists?(client, branch_name)