Skip to content

A logic to generate an empty plugin placeholder. #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions generate_plugin_placeholder.rb
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions git_helper.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 7 additions & 10 deletions versioned_plugins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down