PageRenderTime 65ms CodeModel.GetById 25ms RepoModel.GetById 15ms app.codeStats 0ms

/lib/github_api/client/git_data/references.rb

http://github.com/peter-murach/github
Ruby | 150 lines | 63 code | 18 blank | 69 comment | 1 complexity | 49adac6bba44253a45b6f376bb171a85 MD5 | raw file
  1. # encoding: utf-8
  2. require_relative '../../api'
  3. module Github
  4. class Client::GitData::References < API
  5. VALID_REF_PARAM_NAMES = %w[ ref sha force ].freeze
  6. REQUIRED_REF_PARAMS = %w[ ref sha ].freeze
  7. VALID_REF_PARAM_VALUES = {
  8. 'ref' => %r{^refs\/\w+(\/\w+)*} # test fully qualified reference
  9. }
  10. # Get all references
  11. #
  12. # This will return an array of all the references on the system,
  13. # including things like notes and stashes if they exist on the server.
  14. # Anything in the namespace, not just <tt>heads</tt> and <tt>tags</tt>,
  15. # though that would be the most common.
  16. #
  17. # @example
  18. # github = Github.new
  19. # github.git_data.references.list 'user-name', 'repo-name'
  20. #
  21. # @example
  22. # github.git_data.references.list 'user-name', 'repo-name', ref:'tags'
  23. #
  24. # @api public
  25. def list(*args)
  26. arguments(args, required: [:user, :repo])
  27. params = arguments.params
  28. user = arguments.user
  29. repo = arguments.repo
  30. response = if (ref = params.delete('ref'))
  31. formatted_ref = validate_reference ref
  32. get_request("/repos/#{user}/#{repo}/git/#{formatted_ref}", params)
  33. else
  34. get_request("/repos/#{user}/#{repo}/git/refs", params)
  35. end
  36. return response unless block_given?
  37. response.each { |el| yield el }
  38. end
  39. alias :all :list
  40. # Get a reference
  41. #
  42. # The ref in the URL must be formatted as <tt>heads/branch</tt>,
  43. # not just branch. For example, the call to get the data for a
  44. # branch named sc/featureA would be formatted as heads/sc/featureA
  45. #
  46. # @example
  47. # github = Github.new
  48. # github.git_data.references.get 'user-name', 'repo-name', 'heads/branch'
  49. #
  50. # @api public
  51. def get(*args)
  52. arguments(args, required: [:user, :repo, :ref])
  53. validate_reference arguments.ref
  54. params = arguments.params
  55. get_request("/repos/#{arguments.user}/#{arguments.repo}/git/refs/#{arguments.ref}", params)
  56. end
  57. alias :find :get
  58. # Create a reference
  59. #
  60. # @param [Hash] params
  61. # @input params [String] :ref
  62. # The name of the fully qualified reference (ie: refs/heads/master).
  63. # If it doesnt start with refs and have at least two slashes,
  64. # it will be rejected.
  65. # @input params [String] :sha
  66. # The SHA1 value to set this reference to
  67. #
  68. # @example
  69. # github = Github.new
  70. # github.git_data.references.create 'user-name', 'repo-name',
  71. # ref: "refs/heads/master",
  72. # sha: "827efc6d56897b048c772eb4087f854f46256132"
  73. #
  74. # @api public
  75. def create(*args)
  76. arguments(args, required: [:user, :repo]) do
  77. permit VALID_REF_PARAM_NAMES
  78. assert_required REQUIRED_REF_PARAMS
  79. end
  80. params = arguments.params
  81. validate_reference params['ref']
  82. post_request("/repos/#{arguments.user}/#{arguments.repo}/git/refs", params)
  83. end
  84. # Update a reference
  85. #
  86. # @param [Hash] params
  87. # @input params [String] :sha
  88. # The SHA1 value to set this reference to
  89. # @input params [Boolean] :force
  90. # Indicates whether to force the update or to make sure the update
  91. # is a fast-forward update. Leaving this out or setting it to false
  92. # will make sure youre not overwriting work. Default: false
  93. #
  94. # @example
  95. # github = Github.new
  96. # github.git_data.references.update 'user-name', 'repo-name', 'heads/master',
  97. # sha: "827efc6d56897b048c772eb4087f854f46256132",
  98. # force: true
  99. #
  100. # @api public
  101. def update(*args)
  102. arguments(args, required: [:user, :repo, :ref]) do
  103. permit VALID_REF_PARAM_NAMES
  104. assert_required %w[ sha ]
  105. end
  106. patch_request("/repos/#{arguments.user}/#{arguments.repo}/git/refs/#{arguments.ref}", arguments.params)
  107. end
  108. # Delete a reference
  109. #
  110. # @example
  111. # github = Github.new
  112. # github.git_data.references.delete 'user-name', 'repo-name',
  113. # "heads/master"
  114. #
  115. # @api public
  116. def delete(*args)
  117. arguments(args, required: [:user, :repo, :ref])
  118. params = arguments.params
  119. delete_request("/repos/#{arguments.user}/#{arguments.repo}/git/refs/#{arguments.ref}", params)
  120. end
  121. alias :remove :delete
  122. private
  123. def validate_reference(ref)
  124. refs = (ref =~ (/^(\/)?refs.*/) ? ref : "refs/#{ref}").gsub(/(\/)+/, '/')
  125. refs.gsub!(/^\//, '')
  126. unless VALID_REF_PARAM_VALUES['ref'] =~ refs
  127. raise ArgumentError, "Provided 'reference' is invalid"
  128. end
  129. refs
  130. end
  131. end # GitData::References
  132. end # Github