# encoding: utf-8moduleGithubclassClient::GitData::References<APIVALID_REF_PARAM_NAMES=%w[ ref sha force ].freezeREQUIRED_REF_PARAMS=%w[ ref sha ].freezeVALID_REF_PARAM_VALUES={'ref'=>%r{^refs\/\w+(\/\w+)*}# test fully qualified reference}# Get all references## This will return an array of all the references on the system,# including things like notes and stashes if they exist on the server.# Anything in the namespace, not just <tt>heads</tt> and <tt>tags</tt>,# though that would be the most common.## @example# github = Github.new# github.git_data.references.list 'user-name', 'repo-name'## @example# github.git_data.references.list 'user-name', 'repo-name', ref:'tags'## @api publicdeflist(*args)arguments(args,required: [:user,:repo])params=arguments.paramsuser=arguments.userrepo=arguments.reporesponse=if(ref=params.delete('ref'))validate_referencerefget_request("/repos/#{user}/#{repo}/git/refs/#{ref}",params)elseget_request("/repos/#{user}/#{repo}/git/refs",params)endreturnresponseunlessblock_given?response.each{|el|yieldel}endalias:all:list# Get a reference## The ref in the URL must be formatted as <tt>heads/branch</tt>,# not just branch. For example, the call to get the data for a# branch named sc/featureA would be formatted as heads/sc/featureA## @example# github = Github.new# github.git_data.references.get 'user-name', 'repo-name', 'heads/branch'## @api publicdefget(*args)arguments(args,required: [:user,:repo,:ref])validate_referencearguments.refparams=arguments.paramsget_request("/repos/#{arguments.user}/#{arguments.repo}/git/refs/#{arguments.ref}",params)endalias:find:get# Create a reference## @param [Hash] params# @input params [String] :ref# The name of the fully qualified reference (ie: refs/heads/master).# If it doesn’t start with ‘refs’ and have at least two slashes,# it will be rejected.# @input params [String] :sha# The SHA1 value to set this reference to## @example# github = Github.new# github.git_data.references.create 'user-name', 'repo-name',# ref: "refs/heads/master",# sha: "827efc6d56897b048c772eb4087f854f46256132"## @api publicdefcreate(*args)arguments(args,required: [:user,:repo])dopermitVALID_REF_PARAM_NAMESassert_requiredREQUIRED_REF_PARAMSendparams=arguments.paramsvalidate_referenceparams['ref']post_request("/repos/#{arguments.user}/#{arguments.repo}/git/refs",params)end# Update a reference## @param [Hash] params# @input params [String] :sha# The SHA1 value to set this reference to# @input params [Boolean] :force# Indicates whether to force the update or to make sure the update# is a fast-forward update. Leaving this out or setting it to false# will make sure you’re not overwriting work. Default: false## @example# github = Github.new# github.git_data.references.update 'user-name', 'repo-name', 'heads/master',# sha: "827efc6d56897b048c772eb4087f854f46256132",# force: true## @api publicdefupdate(*args)arguments(args,required: [:user,:repo,:ref])dopermitVALID_REF_PARAM_NAMESassert_required%w[ sha ]endpatch_request("/repos/#{arguments.user}/#{arguments.repo}/git/refs/#{arguments.ref}",arguments.params)end# Delete a reference## @example# github = Github.new# github.git_data.references.delete 'user-name', 'repo-name',# ref: "refs/heads/master"## @api publicdefdelete(*args)arguments(args,required: [:user,:repo,:ref])params=arguments.paramsdelete_request("/repos/#{arguments.user}/#{arguments.repo}/git/refs/#{arguments.ref}",params)endalias:remove:deleteprivatedefvalidate_reference(ref)refs=(ref=~(/^(\/)?refs.*/)?ref:"refs/#{ref}").gsub(/(\/)+/,'/')refs.gsub!(/^\//,'')unlessVALID_REF_PARAM_VALUES['ref']=~refsraiseArgumentError,"Provided 'reference' is invalid"endendend# GitData::Referencesend# Github