diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 9ef440903..db7be5c72 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -107,6 +107,11 @@ class RepositoriesController < ApplicationController sha: params[:sha], page: params[:page], limit: params[:limit], token: current_user&.gitea_token).call end + def commits_slice + @hash_commit = Gitea::Repository::Commits::ListSliceService.call(@owner.login, @project.identifier, + sha: params[:sha], page: params[:page], limit: params[:limit], token: current_user&.gitea_token) + end + def commit @sha = params[:sha] @commit = Gitea::Repository::Commits::GetService.call(@owner.login, @repository.identifier, @sha, current_user&.gitea_token) diff --git a/app/services/gitea/repository/commits/list_slice_service.rb b/app/services/gitea/repository/commits/list_slice_service.rb new file mode 100644 index 000000000..04f45f55b --- /dev/null +++ b/app/services/gitea/repository/commits/list_slice_service.rb @@ -0,0 +1,42 @@ +# Get a list of all commits from a repository +class Gitea::Repository::Commits::ListSliceService < Gitea::ClientService + attr_reader :owner, :repo_name, :args + + # sha: SHA or branch to start listing commits from (usually 'master') + # ex: + # Gitea::Repository::Commits::ListService.new(@project.owner.login, @project.identifier, + # sha: params[:sha], page: params[:page], limit: params[:limit], token: current_user&.gitea_token).call + def initialize(owner, repo_name, **args) + @owner = owner + @repo_name = repo_name + @args = args + end + + def call + response = get(url, params) + render_result(response) + end + + private + def params + { sha: args[:sha] || 'master', page: args[:page] || PAGINATE_DEFAULT_PAGE, limit: args[:limit] || PAGINATE_DEFAULT_LIMIT, token: args[:token] || "" } + end + + def url + "/repos/#{owner}/#{repo_name}/commits_slice".freeze + end + + def render_result(response) + case response.status + when 200 + result = {} + headers = response.headers.to_hash + body = JSON.parse(response.body) + total_count = headers["x-total"] + result.merge(total_count: total_count.to_i, body: body) + else + nil + # {status: -1, message: "#{body['message']}"} + end + end +end diff --git a/app/views/repositories/commits_slice.json.jbuilder b/app/views/repositories/commits_slice.json.jbuilder new file mode 100644 index 000000000..e252cda69 --- /dev/null +++ b/app/views/repositories/commits_slice.json.jbuilder @@ -0,0 +1,38 @@ +if @hash_commit.blank? #如果有状态值,则表示报错了 + json.total_count 0 + json.commits [] +else + json.total_count @hash_commit[:total_count] + json.commit_dates do + json.array! @hash_commit[:body] do |commit_date| + json.commit_date commit_date["commit_date"] + json.commits do + json.array! commit_date["Commits"] do |commit| + commiter = commit['committer'] + + forge_user = + if commiter.present? + User.simple_select.find_by(gitea_uid: commiter['id']) + end + + json.sha commit['sha'] + json.message commit['commit']['message'] + json.timestamp render_unix_time(commit['commit']['author']['date']) + json.time_from_now time_from_now(commit['commit']['author']['date']) + if forge_user + json.partial! 'author', user: forge_user + else + json.author do + json.id nil + json.login commit['commit']['author']['name'] + json.type nil + json.name commit['commit']['author']['name'] + json.image_url User::Avatar.get_letter_avatar_url(commit['commit']['author']['name']) + end + end + end + end + end + end + +end diff --git a/config/routes.rb b/config/routes.rb index 1f029db3a..2a935b896 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -432,6 +432,7 @@ Rails.application.routes.draw do get :entries match :sub_entries, :via => [:get, :put] get :commits + get :commits_slice get :tags get :contributors post :create_file