Class: Atlassian::Stash::CreatePullRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/atlassian/stash/pull_request.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ CreatePullRequest

Returns a new instance of CreatePullRequest.


54
55
56
# File 'lib/atlassian/stash/pull_request.rb', line 54

def initialize(config)
  @config = config
end

Instance Method Details

#create_pull_request(source, target, reviewers, options) ⇒ Object


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
120
121
122
123
124
125
126
127
128
129
# File 'lib/atlassian/stash/pull_request.rb', line 58

def create_pull_request(source, target, reviewers, options)
  Process.exit if not target or not source

  @source = source
  @target = target

  @source_ref = source
  if options.src_remote and not options.src_remote.empty?
    @source_ref = options.src_remote + "/" + @source_ref
  end

  @target_ref = target
  if options.target_remote and not options.target_remote.empty?
    @target_ref = options.target_remote + "/" + @target_ref
  end

  srcRepoInfo = RepoInfo.create(@config, options.src_remote)
  targetRepoInfo = RepoInfo.create(@config, options.target_remote)

  title, description = title_and_description(options)

  resource = CreatePullRequestResource.new(srcRepoInfo, targetRepoInfo, title, description, reviewers, @source, @target).resource

  username = @config["username"]
  password = @config["password"]
  proxy_addr, proxy_port = parse_proxy(@config["proxy"])

  username = ask("Username: ") unless @config["username"]
  password = ask("Password: ") { |q| q.echo = '*' } unless @config["password"]

  uri = URI.parse(@config["stash_url"])
  prPath = targetRepoInfo.repoPath + '/pull-requests'

  req = Net::HTTP::Post.new(uri.query.nil? ? "#{prPath}" : "#{prPath}?#{uri.query}", {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
  req.basic_auth username, password
  req.body = resource.to_json
  http = Net::HTTP.new(uri.host, uri.port, proxy_addr, proxy_port)
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE if @config["ssl_no_verify"]
  http.use_ssl = uri.scheme.eql?("https")

  response = http.start {|conn| conn.request(req) }

  if not response.is_a? Net::HTTPCreated
    responseBody = JSON.parse(response.body)
    if responseBody['errors']
      responseBody['errors'].collect { |error|
        puts error['message']
        if error['reviewerErrors']
          error['reviewerErrors'].collect { |revError|
            puts revError['message']
          }
        end
      }
    elsif responseBody['message']
      puts responseBody['message']
    else
      puts 'An unknown error occurred.'
      puts response.code
      puts response.body
    end
  else
    responseBody = JSON.parse(response.body)
    prUri = uri.clone
    prUri.path = prPath + '/' + responseBody['id'].to_s
    prUri.query = uri.query
    puts prUri.to_s

    if @config["open"] || options.open
      Launchy.open prUri.to_s
    end
  end
end