bitbucket_pr_comment 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f3542feda13acf5e83519315649aacfdf69e6a32
4
+ data.tar.gz: baf3160c49767a9d113f0fa8d9e2d4db59b836e9
5
+ SHA512:
6
+ metadata.gz: 4c6395faec302f2a1966f7a16a9bd77fa43c1bdc68995929894ff81245fff23d01f0ee7e05fdaec05eda5d3a77befd3ca26880696876c945eb181e7932810d88
7
+ data.tar.gz: e3f4badab4242954fdd32607524ada9558389c9220d1c69ea7ae2f1ecb766b318e6ca50238cb76503b5b6b9436bf20316e8b8d3e4ceafce6fc5e6e593baba1b6
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/bundle/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bitbucket_pr_comment.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 ABCanG
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ bitbucket_pr_comment
2
+ ===
3
+
4
+ [![Gem Version](https://badge.fury.io/rb/bitbucket_pr_comment.svg)](https://badge.fury.io/rb/bitbucket_pr_comment)
5
+
6
+ post comment to pull requiest on bitbucket
7
+
8
+ ## Require
9
+ * git
10
+
11
+ ## Install
12
+
13
+ ```bash
14
+ $ gem install bitbucket_pr_comment
15
+ ```
16
+
17
+ ## Setup
18
+ Get the bitbucket consumer key.
19
+
20
+ [OAuth on Bitbucket Cloud](https://confluence.atlassian.com/bitbucket/oauth-on-bitbucket-cloud-238027431.html)
21
+
22
+ * Callback URL: `urn:ietf:wg:oauth:2.0:oob`
23
+ * Check `This is a private consumer`
24
+
25
+ Pleasse make sure the consumer key and secret.
26
+
27
+ ## Usage
28
+ Set the client_id and client_secret to arguments or environment variables.
29
+ And, please specify a comment by the filename or pipe.
30
+
31
+ Exaple:
32
+ ```
33
+ $ cat comment_file
34
+ Pull request comment test!
35
+ $ bitbucket_pr_comment comment_file --client_id xxxx --client_secret xxxx
36
+ ```
37
+
38
+ OR
39
+
40
+ ```
41
+ $ export CLIENT_ID=xxxx
42
+ $ export CLIENT_SECRET=xxxx
43
+ $ cat comment_file
44
+ Pull request comment test!
45
+ $ cat comment_file | bitbucket_pr_comment
46
+ ```
47
+
48
+ ## License
49
+ MIT
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'bitbucket_pr_comment'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bitbucket_pr_comment/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'bitbucket_pr_comment'
8
+ spec.version = BitbucketPrComment::VERSION
9
+ spec.authors = ['ABCanG']
10
+ spec.email = ['abcang1015@gmail.com']
11
+
12
+ spec.summary = 'post comment to pull requiest on bitbucket.'
13
+ spec.description = 'post comment to pull requiest on bitbucket.'
14
+ spec.homepage = 'https://github.com/ABCanG/bitbucket_pr_comment'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.13'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.0'
27
+ end
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'open3'
5
+ require 'bitbucket_pr_comment'
6
+
7
+ begin
8
+ option = ARGV.getopts('', 'client_id', 'client_secret')
9
+
10
+ unless (`git` rescue nil)
11
+ STDERR.puts 'git command not found'
12
+ exit 1
13
+ end
14
+
15
+ match = `git remote -v`.match(%r{bitbucket\.org[:/]([^/]+)/([^\.]+)\.git})
16
+ unless match
17
+ STDERR.puts 'remote repository not found'
18
+ exit 1
19
+ end
20
+
21
+ repo_username = match[1]
22
+ repo_slug = match[2]
23
+ branch = `git rev-parse --abbrev-ref HEAD`.chomp
24
+
25
+ if branch == 'HEAD'
26
+ branches = `git branch -a --contains`.scan(%r{.*/(.+?)$}).flatten
27
+ if branches.length == 1
28
+ branch = branches.first
29
+ else
30
+ STDERR.puts 'failed to specify branch'
31
+ exit 1
32
+ end
33
+ end
34
+
35
+ if ARGF.path == '-' && STDIN.isatty
36
+ STDERR.puts 'please select comment file'
37
+ exit 1
38
+ end
39
+ content = ARGF.read
40
+
41
+ BitbucketPrComment.exec(
42
+ client_id: option['client_id'] || ENV['CLIENT_ID'],
43
+ client_secret: option['client_secret'] || ENV['CLIENT_SECRET'],
44
+ content: content,
45
+ repo_username: repo_username,
46
+ repo_slug: repo_slug,
47
+ branch: branch
48
+ )
49
+ rescue BitbucketPrComment::AuthorizedError,
50
+ BitbucketPrComment::NotFoundError,
51
+ Net::HTTPExceptions,
52
+ Errno::ENOENT => e
53
+
54
+ STDERR.puts e.message
55
+ exit 1
56
+ end
@@ -0,0 +1,19 @@
1
+ require 'bitbucket_pr_comment/version'
2
+ require 'bitbucket_pr_comment/error'
3
+ require 'bitbucket_pr_comment/bitbucket'
4
+
5
+ module BitbucketPrComment
6
+ def self.exec(client_id:, client_secret:, content:, repo_username:, repo_slug:, branch:)
7
+ bitbucket = Bitbucket.new(client_id, client_secret, repo_username, repo_slug)
8
+ pullreq_list = bitbucket.get_pullreq_list
9
+
10
+ pullreq = pullreq_list.find{|data| data[:source][:branch][:name] == branch}
11
+ raise NotFoundError, 'pull request not found' unless pullreq
12
+
13
+ user = bitbucket.get_user
14
+ comment_list = bitbucket.get_pullreq_comment_list(pullreq[:id])
15
+ comment = comment_list.find {|data| data[:user][:username] == user[:username]}
16
+
17
+ bitbucket.send_pullreq_comment(pullreq[:id], content, comment)
18
+ end
19
+ end
@@ -0,0 +1,111 @@
1
+ require 'net/https'
2
+ require 'json'
3
+ require 'base64'
4
+ require 'cgi'
5
+
6
+ module BitbucketPrComment
7
+ class Bitbucket
8
+ def initialize(key, secret, repo_username, repo_slug)
9
+ bearer_token = "#{key}:#{secret}"
10
+ encoded_bearer_token = Base64.strict_encode64(bearer_token)
11
+ @auth = "Basic #{encoded_bearer_token}"
12
+ @repo_username = repo_username
13
+ @repo_slug = repo_slug
14
+ authorize
15
+ end
16
+
17
+ def get_user
18
+ request_bitbucket('get', '/user', '2.0')
19
+ end
20
+
21
+ def get_pullreq_list
22
+ list = []
23
+ page = 1
24
+ loop do
25
+ json = request_bitbucket('get', pullreq_path, '2.0', state: 'OPEN', pagelen: 50, page: page)
26
+ list.concat(json[:values])
27
+ page += 1
28
+ break if json[:next].nil?
29
+ end
30
+ list
31
+ end
32
+
33
+ def get_pullreq_comment_list(pullreq_id)
34
+ list = []
35
+ page = 1
36
+ loop do
37
+ json = request_bitbucket('get', pullreq_path + "/#{pullreq_id}/comments", '2.0', pagelen: 50, page: page)
38
+ list.concat(json[:values])
39
+ page += 1
40
+ break if json[:next].nil?
41
+ end
42
+ list
43
+ end
44
+
45
+ def send_pullreq_comment(pullreq_id, content, comment = nil)
46
+ method = comment ? 'put' : 'post'
47
+ path = pullreq_path + "/#{pullreq_id}/comments"
48
+ path += "/#{comment[:id]}" if comment
49
+ request_bitbucket(method, path, '1.0', content: content)
50
+ end
51
+
52
+ private
53
+
54
+ def base_uri(version)
55
+ "https://api.bitbucket.org/#{version}"
56
+ end
57
+
58
+ def pullreq_path
59
+ "/repositories/#{@repo_username}/#{@repo_slug}/pullrequests"
60
+ end
61
+
62
+ def http_request(method, uri, query_hash = {})
63
+ uri = URI.parse(uri) if uri.is_a? String
64
+ method = method.to_s.strip.downcase
65
+ query_string = (query_hash || {}).map{|k, v|
66
+ CGI.escape(k.to_s) + '=' + CGI.escape(v.to_s)
67
+ }.join('&')
68
+ header = { 'Authorization' => @auth }
69
+
70
+ args =
71
+ if method == 'post'
72
+ [Net::HTTP::Post.new(uri.path, header), query_string]
73
+ elsif method == 'put'
74
+ [Net::HTTP::Put.new(uri.path, header), query_string]
75
+ else
76
+ [Net::HTTP::Get.new(uri.path + (query_string.empty? ? '' : "?#{query_string}"), header)]
77
+ end
78
+
79
+ https = Net::HTTP.new(uri.host, uri.port)
80
+ https.use_ssl = true
81
+ https.verify_mode = OpenSSL::SSL::VERIFY_NONE
82
+
83
+ https.start do |http|
84
+ http.request(*args)
85
+ end
86
+ end
87
+
88
+ def request_json(method, path, params = {})
89
+ res = http_request(method, path, params)
90
+ case res
91
+ when Net::HTTPSuccess
92
+ JSON.parse(res.body, symbolize_names: true)
93
+ else
94
+ res.value
95
+ end
96
+ end
97
+
98
+ def request_bitbucket(method, path, version, params = {})
99
+ request_json(method, base_uri(version) + path, params)
100
+ end
101
+
102
+ def authorize
103
+ path = 'https://bitbucket.org/site/oauth2/access_token'
104
+ json = request_json('post', path, grant_type: 'client_credentials')
105
+ @auth = "Bearer #{json[:access_token]}"
106
+
107
+ rescue Net::HTTPExceptions
108
+ raise AuthorizedError, 'authorization failed'
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,4 @@
1
+ module BitbucketPrComment
2
+ class AuthorizedError < StandardError; end
3
+ class NotFoundError < StandardError; end
4
+ end
@@ -0,0 +1,3 @@
1
+ module BitbucketPrComment
2
+ VERSION = '0.1.1'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bitbucket_pr_comment
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - ABCanG
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-12-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: post comment to pull requiest on bitbucket.
56
+ email:
57
+ - abcang1015@gmail.com
58
+ executables:
59
+ - bitbucket_pr_comment
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - bitbucket_pr_comment.gemspec
73
+ - exe/bitbucket_pr_comment
74
+ - lib/bitbucket_pr_comment.rb
75
+ - lib/bitbucket_pr_comment/bitbucket.rb
76
+ - lib/bitbucket_pr_comment/error.rb
77
+ - lib/bitbucket_pr_comment/version.rb
78
+ homepage: https://github.com/ABCanG/bitbucket_pr_comment
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.5.1
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: post comment to pull requiest on bitbucket.
102
+ test_files: []