Skip to content

Commit 9dbe15b

Browse files
authored
Merge pull request #97 from mashhurs/add-empty-placeholder
A logic to generate an empty plugin placeholder.
2 parents 62dde10 + fd0ca9e commit 9dbe15b

File tree

3 files changed

+105
-10
lines changed

3 files changed

+105
-10
lines changed

generate_plugin_placeholder.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
require "clamp"
2+
require "erb"
3+
require "octokit"
4+
require_relative "git_helper"
5+
6+
class GeneratePluginPlaceholderDoc < Clamp::Command
7+
option "--output-path", "OUTPUT", "Path to a directory where logstash-docs repository is cloned and changes going to be written to", required: true
8+
option "--plugin-type", "STRING", "Type (ex: integration, input, etc) of a new plugin.", required: true
9+
option "--plugin-name", "STRING", "Name of the plugin.", required: true
10+
11+
SUPPORTED_TYPES = %w(integration)
12+
13+
def execute
14+
generate_placeholder(plugin_type, plugin_name)
15+
submit_pr
16+
end
17+
18+
# adds an empty static page under the VPR
19+
# this helps us to eliminate broken link issues in the docs
20+
def generate_placeholder(type, name)
21+
if type.nil? || name.nil?
22+
$stderr.puts("Plugin type and name are required.")
23+
return
24+
end
25+
26+
unless SUPPORTED_TYPES.include?(type)
27+
$stderr.puts("#{type} is not supported. Supported types are #{SUPPORTED_TYPES.inspect}")
28+
return
29+
end
30+
31+
placeholder_asciidoc = "#{logstash_docs_path}/docs/versioned-plugins/#{type}s/#{name}-index.asciidoc"
32+
# changing template will fail to re-index the doc, do not change or keep consistent with VPR templates
33+
template = ERB.new(IO.read("logstash/templates/docs/versioned-plugins/plugin-index.asciidoc.erb"))
34+
content = template.result_with_hash(name: name, type: type, versions: [])
35+
File.write(placeholder_asciidoc, content)
36+
end
37+
38+
def logstash_docs_path
39+
path = File.join(output_path, "logstash-docs")
40+
fail("#{path} doesn't exist. Please provide the path for `--output-path` where `logstash-docs` repo is located.") unless Dir.exist?(path)
41+
path
42+
end
43+
44+
def submit_pr
45+
branch_name = "new_plugin_placeholder"
46+
git_helper = GitHelper.new("elastic/logstash-docs")
47+
if git_helper.branch_exists?(branch_name)
48+
puts "WARNING: Branch \"#{branch_name}\" already exists. Aborting creation of PR. Please merge the existing PR or delete the PR and the branch."
49+
return
50+
end
51+
52+
pr_title = "A placeholder for new plugin"
53+
git_helper.commit(logstash_docs_path, branch_name, "create an empty placeholder for new plugin")
54+
git_helper.create_pull_request(branch_name, "versioned_plugin_docs", pr_title, "", { draft: true })
55+
end
56+
end
57+
58+
if __FILE__ == $0
59+
GeneratePluginPlaceholderDoc.run
60+
end

git_helper.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require "octokit"
2+
3+
class GitHelper
4+
5+
attr_reader :git_client, :repo_name
6+
7+
def initialize(repo_name)
8+
print "not " if ENV.fetch("GITHUB_TOKEN", "").empty?
9+
puts "using a github token"
10+
11+
@git_client = Octokit::Client.new(:access_token => ENV["GITHUB_TOKEN"])
12+
13+
fail("Repo name cannot be null or empty") if repo_name.nil? || repo_name.empty?
14+
@repo_name = repo_name
15+
end
16+
17+
def commit(repo_path, branch_name, commit_msg)
18+
puts "Committing changes..."
19+
Dir.chdir(repo_path) do |path|
20+
`git checkout -b #{branch_name}`
21+
`git add .`
22+
`git commit -m "#{commit_msg}" -a`
23+
`git push origin #{branch_name}`
24+
end
25+
end
26+
27+
def create_pull_request(branch_name, against_branch, title, description, options = {})
28+
puts "Creating a PR..."
29+
@git_client.create_pull_request(@repo_name, against_branch, branch_name, title, description, options)
30+
end
31+
32+
def branch_exists?(branch_name)
33+
@git_client.branch(@repo_name, branch_name)
34+
true
35+
rescue Octokit::NotFound
36+
false
37+
end
38+
end

versioned_plugins.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
require_relative 'lib/core_ext/erb_result_with_hash'
1111
require_relative 'lib/logstash-docket'
12+
require_relative 'git_helper'
1213

1314
class VersionedPluginDocs < Clamp::Command
1415
option "--output-path", "OUTPUT", "Path to a directory where logstash-docs repository will be cloned and written to", required: true
@@ -234,19 +235,15 @@ def new_versions?
234235

235236
def submit_pr
236237
branch_name = "versioned_docs_new_content"
237-
octo = Octokit::Client.new(:access_token => ENV["GITHUB_TOKEN"])
238-
if branch_exists?(octo, branch_name)
238+
git_helper = GitHelper.new("elastic/logstash-docs")
239+
if git_helper.branch_exists?(branch_name)
239240
puts "WARNING: Branch \"#{branch_name}\" already exists. Not creating a new PR. Please merge the existing PR or delete the PR and the branch."
240241
return
241242
end
242-
Dir.chdir(logstash_docs_path) do |path|
243-
`git checkout -b #{branch_name}`
244-
`git add .`
245-
`git commit -m "updated versioned plugin docs" -a`
246-
`git push origin #{branch_name}`
247-
end
248-
octo.create_pull_request("elastic/logstash-docs", "versioned_plugin_docs", branch_name,
249-
"auto generated update of versioned plugin documentation", "")
243+
244+
pr_title = "auto generated update of versioned plugin documentation"
245+
git_helper.commit(logstash_docs_path, branch_name, "updated versioned plugin docs")
246+
git_helper.create_pull_request(branch_name, "versioned_plugin_docs", pr_title, "")
250247
end
251248

252249
def branch_exists?(client, branch_name)

0 commit comments

Comments
 (0)