diff --git a/README.md b/README.md index e350370..3d23202 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,15 @@ You can specify this value by `GIT_PR_RELEASE_SSL_NO_VERIFY` to `1`. If not specified, verify SSL certificate always. +### `pr-release.draft` + +The pull request is draft no not. +Accepted values: `true` | `false` + +You can specify this value by `GIT_PR_RELEASE_DRAFT` environment variable. + +Default value: `false`. + ## Errors and exit statuses ### No pull requests to be released diff --git a/lib/git/pr/release/cli.rb b/lib/git/pr/release/cli.rb index d487c05..8f69b4c 100644 --- a/lib/git/pr/release/cli.rb +++ b/lib/git/pr/release/cli.rb @@ -6,7 +6,7 @@ module Pr module Release class CLI include Git::Pr::Release::Util - attr_reader :repository, :production_branch, :staging_branch, :template_path, :labels + attr_reader :repository, :production_branch, :staging_branch, :template_path, :labels, :draft def self.start result = self.new.start @@ -82,11 +82,18 @@ def configure _labels = ENV.fetch('GIT_PR_RELEASE_LABELS') { git_config('labels') } @labels = _labels && _labels.split(/\s*,\s*/) || [] + _draft = ENV.fetch('GIT_PR_RELEASE_DRAFT') { git_config('draft') } || false + @draft = to_boolean(_draft) + if draft.nil? + raise "#{_draft} is not boolean. set true or false." + end + say "Repository: #{repository}", :debug say "Production branch: #{production_branch}", :debug say "Staging branch: #{staging_branch}", :debug say "Template path: #{template_path}", :debug say "Labels #{labels}", :debug + say "Draft #{draft}", :debug end def fetch_merged_prs @@ -226,7 +233,8 @@ def detect_existing_release_pr def prepare_release_pr client.create_pull_request( - repository, production_branch, staging_branch, 'Preparing release pull request...', '' + repository, production_branch, staging_branch, 'Preparing release pull request...', '', + { draft: draft } ) end diff --git a/lib/git/pr/release/util.rb b/lib/git/pr/release/util.rb index 8ae61ae..0f2b7a1 100644 --- a/lib/git/pr/release/util.rb +++ b/lib/git/pr/release/util.rb @@ -199,6 +199,24 @@ def request_authorization(client, two_factor_code) auth end + + def to_boolean(flag) + case flag + when TrueClass, FalseClass + flag + when String + flag = flag.downcase + if flag == 'true' + true + elsif flag == 'false' + false + else + nil + end + else + nil + end + end end end end diff --git a/spec/git/pr/release/cli_spec.rb b/spec/git/pr/release/cli_spec.rb index 2630e03..b51a546 100644 --- a/spec/git/pr/release/cli_spec.rb +++ b/spec/git/pr/release/cli_spec.rb @@ -76,6 +76,7 @@ expect(@cli.staging_branch).to eq "staging" expect(@cli.template_path).to eq nil expect(@cli.labels).to eq [] + expect(@cli.draft).to eq false end end @@ -181,6 +182,58 @@ end end end + + describe "draft" do + context "When draft is not set" do + it "draft is false" do + subject + + expect(@cli.draft).to eq false + end + end + + context "When draft is set by ENV" do + around do |example| + original = ENV.to_hash + begin + ENV["GIT_PR_RELEASE_DRAFT"] = 'true' + example.run + ensure + ENV.replace(original) + end + end + + it "draft is configured" do + subject + + expect(@cli.draft).to eq true + end + end + + context "When draft is set by git_config" do + before { + allow(@cli).to receive(:git_config).with("draft") { true } + } + + it "draft is configured" do + subject + + expect(@cli.draft).to eq true + end + end + + context "When draft is not set true | false" do + let(:draft) { 'not_true' } + + before { + allow(@cli).to receive(:git_config).with("draft") { draft } + } + + it "draft is configured" do + expect { subject }.to raise_error("#{draft} is not boolean. set true or false.") + end + end + end end describe "#fetch_merged_prs" do @@ -368,6 +421,7 @@ "staging", "Preparing release pull request...", "", # empby body + { draft: @cli.draft } ) } end