diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/api/api.rb | 1 | ||||
| -rw-r--r-- | lib/api/commits.rb | 64 | ||||
| -rw-r--r-- | lib/api/repositories.rb | 44 | 
3 files changed, 65 insertions, 44 deletions
diff --git a/lib/api/api.rb b/lib/api/api.rb index 283f7642f67..4157397b5e2 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -40,6 +40,7 @@ module API      mount ProjectHooks      mount Services      mount Files +    mount Commits      mount Namespaces    end  end diff --git a/lib/api/commits.rb b/lib/api/commits.rb new file mode 100644 index 00000000000..33b8b3d2244 --- /dev/null +++ b/lib/api/commits.rb @@ -0,0 +1,64 @@ +require 'mime/types' + +module API +  # Projects API +  class Commits < Grape::API +    before { authenticate! } +    before { authorize! :download_code, user_project } + +    resource :projects do +      helpers do +        def handle_project_member_errors(errors) +          if errors[:project_access].any? +            error!(errors[:project_access], 422) +          end +          not_found! +        end +      end + +      # Get a project repository commits +      # +      # Parameters: +      #   id (required) - The ID of a project +      #   ref_name (optional) - The name of a repository branch or tag, if not given the default branch is used +      # Example Request: +      #   GET /projects/:id/repository/commits +      get ":id/repository/commits" do +        page = (params[:page] || 0).to_i +        per_page = (params[:per_page] || 20).to_i +        ref = params[:ref_name] || user_project.try(:default_branch) || 'master' + +        commits = user_project.repository.commits(ref, nil, per_page, page * per_page) +        present commits, with: Entities::RepoCommit +      end + +      # Get a specific commit of a project +      # +      # Parameters: +      #   id (required) - The ID of a project +      #   sha (required) - The commit hash or name of a repository branch or tag +      # Example Request: +      #   GET /projects/:id/repository/commits/:sha +      get ":id/repository/commits/:sha" do +        sha = params[:sha] +        commit = user_project.repository.commit(sha) +        not_found! "Commit" unless commit +        present commit, with: Entities::RepoCommitDetail +      end + +      # Get the diff for a specific commit of a project +      # +      # Parameters: +      #   id (required) - The ID of a project +      #   sha (required) - The commit or branch name +      # Example Request: +      #   GET /projects/:id/repository/commits/:sha/diff +      get ":id/repository/commits/:sha/diff" do +        sha = params[:sha] +        commit = user_project.repository.commit(sha) +        not_found! "Commit" unless commit +        commit.diffs +      end +    end +  end +end diff --git a/lib/api/repositories.rb b/lib/api/repositories.rb index cad64760abb..00a0c919b4d 100644 --- a/lib/api/repositories.rb +++ b/lib/api/repositories.rb @@ -85,50 +85,6 @@ module API          present user_project.repo.tags.sort_by(&:name).reverse, with: Entities::RepoObject, project: user_project        end -      # Get a project repository commits -      # -      # Parameters: -      #   id (required) - The ID of a project -      #   ref_name (optional) - The name of a repository branch or tag, if not given the default branch is used -      # Example Request: -      #   GET /projects/:id/repository/commits -      get ":id/repository/commits" do -        page = (params[:page] || 0).to_i -        per_page = (params[:per_page] || 20).to_i -        ref = params[:ref_name] || user_project.try(:default_branch) || 'master' - -        commits = user_project.repository.commits(ref, nil, per_page, page * per_page) -        present commits, with: Entities::RepoCommit -      end - -      # Get a specific commit of a project -      # -      # Parameters: -      #   id (required) - The ID of a project -      #   sha (required) - The commit hash or name of a repository branch or tag -      # Example Request: -      #   GET /projects/:id/repository/commits/:sha -      get ":id/repository/commits/:sha" do -        sha = params[:sha] -        commit = user_project.repository.commit(sha) -        not_found! "Commit" unless commit -        present commit, with: Entities::RepoCommitDetail -      end - -      # Get the diff for a specific commit of a project -      # -      # Parameters: -      #   id (required) - The ID of a project -      #   sha (required) - The commit or branch name -      # Example Request: -      #   GET /projects/:id/repository/commits/:sha/diff -      get ":id/repository/commits/:sha/diff" do -        sha = params[:sha] -        commit = user_project.repository.commit(sha) -        not_found! "Commit" unless commit -        commit.diffs -      end -        # Get a project repository tree        #        # Parameters:  | 
