Skip to content

Commit

Permalink
changelog endpoint ecosyste-ms/roadmap#14
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew committed Nov 8, 2023
1 parent d2bfa7c commit 5f6844a
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 1 deletion.
11 changes: 11 additions & 0 deletions app/controllers/api/v1/archives_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,15 @@ def readme
render json: readme
end
end

def changelog
expires_in(60.days, public: true, "s-maxage" => 60.days) # TODO this needs to be more dynamic to take into account headers from where the file was loaded
@archive = Archive.new(params[:url])
changelog = @archive.changelog
if changelog.nil?
render json: {:error => "path not found"}, :status => 404
else
render json: changelog
end
end
end
29 changes: 29 additions & 0 deletions app/models/archive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,33 @@ def readme
}
end
end

def changelog
Dir.mktmpdir do |dir|
download_file(dir)
base_path = extract(dir)

return nil if base_path.nil?
all_files = Dir.glob("**/*", File::FNM_DOTMATCH, base: base_path).tap{|a| a.delete(".")}

changelog_files = all_files.select{|path| path.match(/^changelog/i) }

return nil if changelog_files.empty?

changelog_file = changelog_files.first

raw = File.read(File.join(base_path, changelog_file))
html = GitHub::Markup.render(changelog_file, raw.force_encoding("UTF-8"))
language = GitHub::Markup.language(changelog_file, raw.force_encoding("UTF-8")).try(:name)

return {
name: changelog_file,
raw: raw,
html: html,
plain: Nokogiri::HTML(html).try(:text),
extension: File.extname(changelog_file),
language: language
}
end
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
get :list
get :contents
get :readme
get :changelog
end
end
end
Expand Down
35 changes: 34 additions & 1 deletion openapi/api/v1/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Readme'
/archives/changelog:
get:
summary: get changelog from a package archive
operationId: changelog
parameters:
- name: url
in: query
description: url to package archive
required: true
schema:
type: string
responses:
200:
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Changelog'
components:
schemas:
Content:
Expand Down Expand Up @@ -107,4 +125,19 @@ components:
other_readme_files:
type: array
items:
type: string
type: string
Changelog:
type: object
properties:
name:
type: string
raw:
type: string
html:
type: string
plain:
type: string
extension:
type: string
language:
type: string
17 changes: 17 additions & 0 deletions test/controllers/api/v1/archives_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,21 @@ class ApiV1ArchivesControllerTest < ActionDispatch::IntegrationTest
assert_equal actual_response['language'], "Markdown"
assert_equal actual_response['other_readme_files'], []
end

test 'changelog' do
stub_request(:get, "https://github.com/splitrb/split/archive/refs/heads/main.zip")
.to_return({ status: 200, body: File.open(File.join(Rails.root, 'test', 'fixtures', 'files','main.zip')).read })

get changelog_api_v1_archives_path(url: 'https://github.com/splitrb/split/archive/refs/heads/main.zip')
assert_response :success
actual_response = JSON.parse(@response.body)

assert_equal actual_response['name'], 'CHANGELOG.md'
assert_equal actual_response['raw'][0..20], "# 4.0.2 (December 2nd"
assert_equal actual_response['html'][0..30], "<h1>4.0.2 (December 2nd, 2022)<"
assert_equal actual_response['plain'][0..8], "4.0.2 (De"

assert_equal actual_response['extension'], '.md'
assert_equal actual_response['language'], "Markdown"
end
end
Binary file added test/fixtures/files/main.zip
Binary file not shown.

0 comments on commit 5f6844a

Please sign in to comment.