Skip to content

Commit

Permalink
Merge pull request #1069 from travis-ci/sf-releases-release-notes
Browse files Browse the repository at this point in the history
rename --body to --release_notes, add --release_notes_file (see #155)
  • Loading branch information
svenfuchs authored Aug 21, 2019
2 parents 75d845d + d169c75 commit e26250b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
30 changes: 20 additions & 10 deletions lib/dpl/providers/releases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ class Releases < Provider
opt '--overwrite', 'Overwrite files with the same name'
opt '--prerelease', 'Identify the release as a prerelease'
opt '--release_number NUM', 'Release number (overide automatic release detection)'
opt '--release_notes STR', 'Content for the release notes', alias: :body
opt '--release_notes_file PATH', 'Path to a file containing the release notes', note: 'will be ignored if --release_notes is given'
opt '--draft', 'Identify the release as a draft'
opt '--tag_name TAG', 'Git tag from which to create the release'
opt '--target_commitish STR', 'Commitish value that determines where the Git tag is created from'
opt '--name NAME', 'Name for the release'
opt '--body BODY', 'Content for the release notes'
# should this have --github_url, like Pages does?

needs :git
Expand All @@ -42,7 +43,7 @@ class Releases < Provider
skip_existing: 'File %s already exists, skipping.',
set_tag_name: 'Setting tag_name to %s',
set_target_commitish: 'Setting target_commitish to %s',
missing_file: '%s does not exist.',
missing_file: 'File %s does not exist.',
not_a_file: '%s is not a file, skipping.'

cmds git_fetch_tags: 'git fetch --tags'
Expand Down Expand Up @@ -78,9 +79,7 @@ def login

def deploy
upload_files
opts = with_tag(self.opts.dup)
opts = with_target_commitish(opts)
api.update_release(url, filter(opts).merge(draft: draft?))
api.update_release(url, octokit_opts)
end

def upload_files
Expand All @@ -99,6 +98,13 @@ def delete(asset, file)
api.delete_release_asset(asset.url)
end

def octokit_opts
opts = with_tag(self.opts.dup)
opts = with_target_commitish(opts)
opts = opts.select { |key, _| OCTOKIT_OPTS.include?(key) }
compact(opts.merge(body: release_notes, draft: draft?))
end

def with_tag(opts)
return opts if tag_name? || draft?
info :set_tag_name, local_tag
Expand Down Expand Up @@ -162,6 +168,14 @@ def asset(path)
api.release_assets(url).detect { |asset| asset.name == path }
end

def release_notes
super || release_notes_file || nil
end

def release_notes_file
release_notes_file? && exists?(super) && read(super)
end

def user
@user ||= api.user
end
Expand All @@ -184,13 +198,9 @@ def files
files.select { |file| file?(file) }
end

def filter(opts)
opts.select { |key, _| OCTOKIT_OPTS.include?(key) }
end

def exists?(file)
return true if File.exists?(file)
warn :missing_file, file
error :missing_file, file
false
end

Expand Down
25 changes: 22 additions & 3 deletions spec/dpl/providers/releases_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,27 @@
before { stub_request(:patch, %r(/releases/1$)) }
before { stub_request(:post, %r(/releases/1/assets\?)) }
before { stub_request(:delete, %r(/releases/1/assets/1$)) }
before { subject.run }
before { |c| subject.run unless c.example_group.metadata[:run].is_a?(FalseClass) }

let(:repo) { 'travis-ci/dpl' }
let(:tag_name) { 'tag' }
let(:name_) { nil }
let(:body) { nil }
let(:draft) { false }
let(:prerelease) { nil }
let(:release_number) { nil }
let(:release_notes) { nil }
let(:target_commitish) { 'sha' }

matcher :release_json do
match do |body|
# p symbolize(JSON.parse(body))
expect(symbolize(JSON.parse(body))).to include(compact(
repo: repo,
name: name_,
tag_name: tag_name,
target_commitish: target_commitish,
release_number: release_number,
body: release_notes,
prerelease: prerelease,
draft: draft,
))
Expand Down Expand Up @@ -114,8 +116,25 @@
it { should have_requested(:patch, %r(/releases/1)).with(body: release_json) }
end

describe 'given --release_notes release_notes' do
let(:release_notes) { 'release_notes' }
it { should have_requested(:patch, %r(/releases/1)).with(body: release_json) }
end

describe 'given --release_notes_file ./release_notes', run: false do
let(:release_notes) { 'release_notes' }
file './release_notes', 'release_notes'
before { subject.run }
it { should have_requested(:patch, %r(/releases/1)).with(body: release_json) }
end

describe 'missing release notes file, given --release_notes_file ./release_notes', run: false do
let(:release_notes) { 'release_notes' }
it { expect { subject.run }.to raise_error('File ./release_notes does not exist.') }
end

describe 'given --body body' do
let(:body) { 'body' }
let(:release_notes) { 'body' }
it { should have_requested(:patch, %r(/releases/1)).with(body: release_json) }
end

Expand Down

0 comments on commit e26250b

Please sign in to comment.