Skip to content

Commit

Permalink
Add commits endpoint to repository routes
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew committed Feb 7, 2024
1 parent 0e909b6 commit 89c97e9
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 1 deletion.
22 changes: 22 additions & 0 deletions app/controllers/api/v1/commits_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Api::V1::CommitsController < Api::V1::ApplicationController
def index
@host = Host.find_by_name!(params[:host_id])
@repository = @host.repositories.find_by!('lower(full_name) = ?', params[:repository_id].downcase)

scope = @repository.commits.order('timestamp DESC')

scope = scope.since(params[:since]) if params[:since].present?
scope = scope.until(params[:until]) if params[:until].present?

if params[:sort].present? || params[:order].present?
sort = params[:sort] || 'timestamp'
order = params[:order] || 'desc'
sort_options = sort.split(',').zip(order.split(',')).to_h
scope = scope.order(sort_options)
else
scope = scope.order('timestamp DESC')
end

@pagy, @commits = pagy_countless(@repository.commits.order('timestamp DESC'))
end
end
3 changes: 3 additions & 0 deletions app/models/commit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ class Commit < ApplicationRecord
has_one :host, through: :repository

validates :sha, presence: true, uniqueness: { scope: :repository_id }

scope :since, ->(date) { where('timestamp > ?', date) }
scope :until, ->(date) { where('timestamp < ?', date) }
end
1 change: 1 addition & 0 deletions app/views/api/v1/commits/_commit.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.extract! commit, :sha, :message, :author, :committer, :timestamp, :merge
1 change: 1 addition & 0 deletions app/views/api/v1/commits/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @commits, partial: 'api/v1/commits/commit', as: :commit
1 change: 1 addition & 0 deletions app/views/api/v1/repositories/_repository.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
json.extract! repository, :full_name, :default_branch, :committers, :total_commits, :total_committers, :total_bot_commits, :total_bot_committers, :mean_commits, :dds, :past_year_committers, :past_year_total_commits, :past_year_total_committers, :past_year_total_bot_commits, :past_year_total_bot_committers, :past_year_mean_commits, :past_year_dds, :last_synced_at, :last_synced_commit, :created_at, :updated_at
json.commits_url api_v1_host_repository_commits_url(repository.host, repository)
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
member do
get 'ping', to: 'repositories#ping'
end
resources :commits, only: [:index]
end
end
end
Expand Down
84 changes: 83 additions & 1 deletion openapi/api/v1/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,70 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Repository'
/hosts/{hostName}/repositories/{repoName}/commits:
get:
summary: get a list of commits from a repository
operationId: getRepositoryCommits
parameters:
- in: path
name: hostName
schema:
type: string
required: true
description: name of host
- in: path
name: repoName
schema:
type: string
required: true
description: name of repository
- name: page
in: query
description: pagination page number
required: false
schema:
type: integer
- name: per_page
in: query
description: Number of records to return
required: false
schema:
type: integer
- name: since
in: query
description: filter by commits since given time
required: false
schema:
type: string
format: date-time
- name: until
in: query
description: filter by commits until given time
required: false
schema:
type: string
format: date-time
- name: sort
in: query
description: field to order results by
required: false
schema:
type: string
- name: order
in: query
description: direction to order results by
required: false
schema:
type: string
responses:
200:
description: OK
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Commit'
components:
schemas:
Host:
Expand Down Expand Up @@ -239,4 +303,22 @@ components:
format: date-time
created_at:
type: string
format: date-time
format: date-time
commits_url:
type: string
Commit:
type: object
properties:
sha:
type: string
message:
type: string
author:
type: string
committer:
type: string
timestamp:
type: string
format: date-time
merge:
type: boolean
19 changes: 19 additions & 0 deletions test/controllers/api/v1/commits_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'test_helper'

class ApiV1CommitsControllerTest < ActionDispatch::IntegrationTest
setup do
@host = Host.create(name: 'GitHub', url: 'https://github.com', kind: 'github')
@repository = @host.repositories.create(full_name: 'ecosyste-ms/repos', last_synced_at: Time.now, total_commits: 100, total_committers: 10)
@commit = @repository.commits.create(sha: '1234567890', timestamp: Time.now, author: 'author', message: 'message')
end

test 'list commits for a repository' do
get api_v1_host_repository_commits_path(host_id: @host.name, repository_id: @repository.full_name)
assert_response :success
assert_template 'commits/index', file: 'commits/index.json.jbuilder'

actual_response = JSON.parse(@response.body)

assert_equal actual_response.length, 1
end
end

0 comments on commit 89c97e9

Please sign in to comment.