/lib/api/repositories.rb

https://gitlab.com/vicvega/gitlab-ce · Ruby · 136 lines · 70 code · 22 blank · 44 comment · 4 complexity · 780dc71fa59340c2b92d4c2f45d24cc9 MD5 · raw file

  1. require 'mime/types'
  2. module API
  3. # Projects API
  4. class Repositories < Grape::API
  5. before { authenticate! }
  6. before { authorize! :download_code, user_project }
  7. resource :projects do
  8. helpers do
  9. def handle_project_member_errors(errors)
  10. if errors[:project_access].any?
  11. error!(errors[:project_access], 422)
  12. end
  13. not_found!
  14. end
  15. end
  16. # Get a project repository tree
  17. #
  18. # Parameters:
  19. # id (required) - The ID of a project
  20. # ref_name (optional) - The name of a repository branch or tag, if not given the default branch is used
  21. # Example Request:
  22. # GET /projects/:id/repository/tree
  23. get ':id/repository/tree' do
  24. ref = params[:ref_name] || user_project.try(:default_branch) || 'master'
  25. path = params[:path] || nil
  26. commit = user_project.commit(ref)
  27. not_found!('Tree') unless commit
  28. tree = user_project.repository.tree(commit.id, path)
  29. present tree.sorted_entries, with: Entities::RepoTreeObject
  30. end
  31. # Get a raw file contents
  32. #
  33. # Parameters:
  34. # id (required) - The ID of a project
  35. # sha (required) - The commit or branch name
  36. # filepath (required) - The path to the file to display
  37. # Example Request:
  38. # GET /projects/:id/repository/blobs/:sha
  39. get [ ":id/repository/blobs/:sha", ":id/repository/commits/:sha/blob" ] do
  40. required_attributes! [:filepath]
  41. ref = params[:sha]
  42. repo = user_project.repository
  43. commit = repo.commit(ref)
  44. not_found! "Commit" unless commit
  45. blob = Gitlab::Git::Blob.find(repo, commit.id, params[:filepath])
  46. not_found! "File" unless blob
  47. send_git_blob repo, blob
  48. end
  49. # Get a raw blob contents by blob sha
  50. #
  51. # Parameters:
  52. # id (required) - The ID of a project
  53. # sha (required) - The blob's sha
  54. # Example Request:
  55. # GET /projects/:id/repository/raw_blobs/:sha
  56. get ':id/repository/raw_blobs/:sha' do
  57. ref = params[:sha]
  58. repo = user_project.repository
  59. begin
  60. blob = Gitlab::Git::Blob.raw(repo, ref)
  61. rescue
  62. not_found! 'Blob'
  63. end
  64. not_found! 'Blob' unless blob
  65. send_git_blob repo, blob
  66. end
  67. # Get a an archive of the repository
  68. #
  69. # Parameters:
  70. # id (required) - The ID of a project
  71. # sha (optional) - the commit sha to download defaults to the tip of the default branch
  72. # Example Request:
  73. # GET /projects/:id/repository/archive
  74. get ':id/repository/archive',
  75. requirements: { format: Gitlab::Regex.archive_formats_regex } do
  76. authorize! :download_code, user_project
  77. begin
  78. send_git_archive user_project.repository, ref: params[:sha], format: params[:format]
  79. rescue
  80. not_found!('File')
  81. end
  82. end
  83. # Compare two branches, tags or commits
  84. #
  85. # Parameters:
  86. # id (required) - The ID of a project
  87. # from (required) - the commit sha or branch name
  88. # to (required) - the commit sha or branch name
  89. # Example Request:
  90. # GET /projects/:id/repository/compare?from=master&to=feature
  91. get ':id/repository/compare' do
  92. authorize! :download_code, user_project
  93. required_attributes! [:from, :to]
  94. compare = Gitlab::Git::Compare.new(user_project.repository.raw_repository, params[:from], params[:to])
  95. present compare, with: Entities::Compare
  96. end
  97. # Get repository contributors
  98. #
  99. # Parameters:
  100. # id (required) - The ID of a project
  101. # Example Request:
  102. # GET /projects/:id/repository/contributors
  103. get ':id/repository/contributors' do
  104. authorize! :download_code, user_project
  105. begin
  106. present user_project.repository.contributors,
  107. with: Entities::Contributor
  108. rescue
  109. not_found!
  110. end
  111. end
  112. end
  113. end
  114. end