Class: StashCLI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/stash_cli/client.rb

Constant Summary collapse

BASE_API_URL =
'rest/api/1.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, auth_token) ⇒ Client



14
15
16
17
18
19
20
21
22
23
# File 'lib/stash_cli/client.rb', line 14

def initialize(server, auth_token)
  @server = URI(server)
  @resource = RestClient::Resource.new(
    File.join(@server.to_s, BASE_API_URL),
    headers: {
      authorization: "Basic #{auth_token}",
      accept: :json,
      content_type: :json
    })
end

Instance Attribute Details

#resourceObject (readonly)

Returns the value of attribute resource.



12
13
14
# File 'lib/stash_cli/client.rb', line 12

def resource
  @resource
end

#serverObject (readonly)

Returns the value of attribute server.



12
13
14
# File 'lib/stash_cli/client.rb', line 12

def server
  @server
end

Instance Method Details

#branches(project, slug) ⇒ Object



35
36
37
38
# File 'lib/stash_cli/client.rb', line 35

def branches(project, slug)
  response = resource["projects/#{project}/repos/#{slug}/branches?limit=1000"].get
  JSON.parse(response.body)
end

#pull_request(options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/stash_cli/client.rb', line 40

def pull_request(options={})
  params = {
    title: options[:title],
    fromRef: {
      id: "refs/heads/#{options[:from_branch]}",
      repository: {
        slug: options[:from_slug],
        project: {
          key: options[:project]
        }
      }
    },
    toRef: {
      id: "refs/heads/#{options[:target_branch]}",
      repository: {
        slug: options[:target_slug],
        project: {
          key: options[:project]
        }
      }
    },
    reviewers: []
  }

  if options[:reviewers].any?
    params[:reviewers] = options[:reviewers].map do |name|
      {
        user: {
          name: name
        }
      }
    end
  end

  params[:description] = options[:description] if options[:description]

  path = "projects/#{options[:project]}/repos/#{options[:target_slug]}/pull-requests"

  response = resource[path].post(params.to_json)

  PullRequest.from_response(JSON.parse(response.body), server.to_s)
end

#repositories(project) ⇒ Object



30
31
32
33
# File 'lib/stash_cli/client.rb', line 30

def repositories(project)
  response = resource["projects/#{project}/repos?limit=1000"].get
  JSON.parse(response.body)
end

#usersObject



25
26
27
28
# File 'lib/stash_cli/client.rb', line 25

def users
  response = resource['users?limit=1000'].get
  JSON.parse(response.body)['values']
end