Class: Sah::API

Inherits:
Object
  • Object
show all
Defined in:
lib/sah/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ API

Returns a new instance of API.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/sah/api.rb', line 9

def initialize(config)
  @config = config

  base_url = (config.url).to_s.sub(/#{config.url.path}$/, '')
  @conn = Faraday.new(url: base_url) do |faraday|
    faraday.response :json
    faraday.response :logger if config.verbose
    faraday.adapter Faraday.default_adapter
    faraday.basic_auth config.user, config.password
  end
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/sah/api.rb', line 7

def config
  @config
end

#connObject

Returns the value of attribute conn.



7
8
9
# File 'lib/sah/api.rb', line 7

def conn
  @conn
end

Instance Method Details

#create_pull_request(source, target, title = "", description = "", reviewers = []) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/sah/api.rb', line 85

def create_pull_request(source, target, title="", description="", reviewers=[])
  @conn.post do |req|
    req.url @config.url.path +
      "/rest/api/1.0/projects/#{target[:project]}/repos/#{target[:repository]}/pull-requests"
    req.headers['Content-Type'] = 'application/json'
    req.body = {
      title: title,
      description: description,
      state: "OPEN",
      closed: false,
      fromRef: {
        id: "refs/heads/#{source[:branch]}",
        repository: {
          slug: source[:repository],
          name: nil,
          project: {
            key: source[:project]
          }
        }
      },
      toRef: {
        id: "refs/heads/#{target[:branch]}",
        repository: {
          slug: target[:repository],
          name: nil,
          project: {
            key: target[:project]
          }
        }
      },
      locked: false,
      reviewers: reviewers.map{ |r| { user: { name: r } } }
    }.to_json
  end
end

#create_repository(project, repo) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/sah/api.rb', line 38

def create_repository(project, repo)
  @conn.post do |req|
    req.url @config.url.path + "/rest/api/1.0/projects/#{project}/repos"
    req.headers['Content-Type'] = 'application/json'
    req.body = {name: repo, scmId: "git", forkable: true}.to_json
  end
end

#fork_repository(project, repo, name = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/sah/api.rb', line 27

def fork_repository(project, repo, name=nil)
  body = {slug: repo}
  body = body.merge(name: name) if name

  @conn.post do |req|
    req.url @config.url.path + "/rest/api/1.0/projects/#{project}/repos/#{repo}"
    req.headers['Content-Type'] = 'application/json'
    req.body = body.to_json
  end
end

#list_projectObject



46
47
48
49
50
51
# File 'lib/sah/api.rb', line 46

def list_project
  @conn.get do |req|
    req.url @config.url.path + "/rest/api/1.0/projects"
    req.params['limit'] = 1000
  end
end

#list_repository(project) ⇒ Object



72
73
74
75
76
77
# File 'lib/sah/api.rb', line 72

def list_repository(project)
  @conn.get do |req|
    req.url @config.url.path + "/rest/api/1.0/projects/#{project}/repos"
    req.params['limit'] = 1000
  end
end

#list_userObject



59
60
61
62
63
64
# File 'lib/sah/api.rb', line 59

def list_user
  @conn.get do |req|
    req.url @config.url.path + "/rest/api/1.0/users"
    req.params['limit'] = 1000
  end
end

#show_branches(project, repository) ⇒ Object



21
22
23
24
25
# File 'lib/sah/api.rb', line 21

def show_branches(project, repository)
  @conn.get do |req|
    req.url @config.url.path + "/rest/api/1.0/projects/#{project}/repos/#{repository}/branches"
  end
end

#show_project(project) ⇒ Object



53
54
55
56
57
# File 'lib/sah/api.rb', line 53

def show_project(project)
  @conn.get do |req|
    req.url @config.url.path + "/rest/api/1.0/projects/#{project}"
  end
end

#show_repository(project, repository) ⇒ Object



79
80
81
82
83
# File 'lib/sah/api.rb', line 79

def show_repository(project, repository)
  @conn.get do |req|
    req.url @config.url.path + "/rest/api/1.0/projects/#{project}/repos/#{repository}"
  end
end

#show_user(user) ⇒ Object



66
67
68
69
70
# File 'lib/sah/api.rb', line 66

def show_user(user)
   @conn.get do |req|
    req.url @config.url.path + "/rest/api/1.0/users/#{user}"
  end
end