client.rb 1.5 KB
Newer Older
1 2 3 4 5 6 7 8
module BitbucketServer
  class Client
    attr_reader :connection

    def initialize(options = {})
      @connection = Connection.new(options)
    end

S
Stan Hu 已提交
9 10
    def pull_requests(project_key, repo)
      path = "/projects/#{project_key}/repos/#{repo}/pull-requests?state=ALL"
11 12 13
      get_collection(path, :pull_request)
    end

14 15
    def activities(project_key, repo, pull_request)
      path = "/projects/#{project_key}/repos/#{repo}/pull-requests/#{pull_request}/activities"
S
Stan Hu 已提交
16
      get_collection(path, :activity)
17 18 19 20 21 22 23 24 25 26 27 28
    end

    def repo(project, repo_name)
      parsed_response = connection.get("/projects/#{project}/repos/#{repo_name}")
      BitbucketServer::Representation::Repo.new(parsed_response)
    end

    def repos
      path = "/repos"
      get_collection(path, :repo)
    end

29 30 31 32 33 34 35 36 37 38
    def create_branch(project_key, repo, branch_name, sha)
      payload = {
        name: branch_name,
        startPoint: sha,
        message: 'GitLab temporary branch for import'
      }

      connection.post("/projects/#{project_key}/repos/#{repo}/branches", payload.to_json)
    end

39 40 41 42 43 44 45 46 47
    def delete_branch(project_key, repo, branch_name, sha)
      payload = {
        name: Gitlab::Git::BRANCH_REF_PREFIX + branch_name,
        dryRun: false
      }

      connection.delete(:branches, "/projects/#{project_key}/repos/#{repo}/branches", payload.to_json)
    end

48 49 50 51 52 53 54 55
    private

    def get_collection(path, type)
      paginator = BitbucketServer::Paginator.new(connection, path, type)
      BitbucketServer::Collection.new(paginator)
    end
  end
end