Module: BitbucketPr

Defined in:
lib/bitbucket_pr.rb,
lib/bitbucket_pr/system.rb,
lib/bitbucket_pr/version.rb

Constant Summary collapse

HOMES =
["HOME", "HOMEPATH"]
FILE_NAME =
".bitbucket_pr"
VERSION =
"0.1.3"

Class Method Summary collapse

Class Method Details

.create(source, destination, options) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bitbucket_pr.rb', line 8

def self.create(source, destination, options)
  auth_match = options.auth.match(":")
  conn = Faraday.new(url: "https://bitbucket.org")
  conn.basic_auth(auth_match.pre_match, auth_match.post_match)

  res = conn.post do |req|
    req.url "/api/2.0/repositories/#{options.repository}/pullrequests"
    req.headers["Content-Type"] = "application/json"
    req.body = {
        source: {
            branch: {
                name: source
            }
        },
        destination: {
            branch: {
                name: destination
            }
        },
        repository: {
            full_name: options.repository
        },
        title: options.title
    }

    req.body.merge!(close_source_branch: options.close) if options.close

    if options.reviewers
      reviewers = []
      options.reviewers.each { |r| reviewers << { username: r } }
      req.body.merge!(reviewers: reviewers)
    end

    req.body[:description] = options.description if options.description

    req.body = req.body.to_json
  end

  say res.reason_phrase

  if res.reason_phrase == "Created"
    notify_ok "PR Created"
  else
    notify_error "PR not created"
  end

  begin
    body = JSON.parse(res.body)
    color("Failed: " + body["error"]["message"], :red) if (body["type"] == "error")
  rescue
  end
end

.fileObject



11
# File 'lib/bitbucket_pr/system.rb', line 11

def file; File.join(home, FILE_NAME); end

.homeObject



9
# File 'lib/bitbucket_pr/system.rb', line 9

def home; ENV[HOMES.find { |h| ENV[h] != nil }]; end

.readObject



18
19
20
21
22
23
24
25
26
# File 'lib/bitbucket_pr/system.rb', line 18

def read
  begin
    credentials = YAML.safe_load(File.read(file), [Symbol])
    credentials[:password] = credentials[:password].unpack("u")[0]
    credentials
  rescue => e
    nil
  end
end

.write(credentials) ⇒ Object



13
14
15
16
# File 'lib/bitbucket_pr/system.rb', line 13

def write(credentials)
  credentials[:password] = [credentials[:password]].pack("u")
  File.open(file, "w") { |file| file.write(credentials.to_yaml) }
end