github_commenter 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8881b422640642f18b254be0de4128bd861375ee
4
+ data.tar.gz: 86a3c02b88f7f7037375fb37d298acc2c1b4e05b
5
+ SHA512:
6
+ metadata.gz: d19812132ce7a5f4a82b2976af0f41299ac681669711fcc2ecfc551bbeaf656b35ba17da2f99ea90991eb06e702a04791034e92e9175a296183f646fdd0ed765
7
+ data.tar.gz: 440454140f7e42cbf40aecf746bc7aab3f87ba50107c6ccd3e24a6498d02de475d71d78874fba8500160f9fa8e25485e79e891f20efb177198c65faa681ea574
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at okita.kunio@dena.jp. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in github_commenter.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 okita-kunio
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.
@@ -0,0 +1,41 @@
1
+ # GithubCommenter
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/github_commenter`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'github_commenter'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install github_commenter
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/github_commenter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "github_commenter"
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
@@ -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,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "github_commenter/cli"
4
+
5
+ ::GithubCommenter::CLI.start
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ Gem::Specification.new do |spec|
3
+ spec.name = "github_commenter"
4
+ spec.version = File.read(File.expand_path("../VERSION", __FILE__)).chomp
5
+ spec.authors = ["okitan"]
6
+ spec.email = ["okitakunio@gmail.com"]
7
+
8
+ spec.summary = "CLI to comment github"
9
+ spec.description = "CLI to comment github PR/commit"
10
+ spec.homepage = "https://github.com/okitan/github_commenter"
11
+ spec.license = "MIT"
12
+
13
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
14
+ spec.bindir = "exe"
15
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
16
+ spec.require_paths = ["lib"]
17
+
18
+ spec.add_dependency "addressable"
19
+ spec.add_dependency "git"
20
+ spec.add_dependency "git_diff_parser"
21
+ spec.add_dependency "ltsv"
22
+ spec.add_dependency "octokit"
23
+ spec.add_dependency "thor"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.12"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
@@ -0,0 +1,3 @@
1
+ module GithubCommenter
2
+ # Your code goes here...
3
+ end
@@ -0,0 +1,178 @@
1
+ require "github_commenter"
2
+
3
+ require "thor"
4
+
5
+ require "addressable/template"
6
+ require "git"
7
+ require "git_diff_parser"
8
+ require "octokit"
9
+
10
+ module GithubCommenter
11
+ class CLI < ::Thor
12
+ class << self
13
+ def github_pr_options
14
+ option :from_env, enum: %w[ circleci ], description: "complete things from ENV variables of CI. override github,repo,pr,after,until"
15
+
16
+ # github things
17
+ option :github, description: "github API entry point (default: https://api.github.com/v3)"
18
+ option :repo, description: "respoitory of github e.g. okitan/github_commenter"
19
+ option :pr, type: :numeric, description: "number of PR"
20
+
21
+ option :github_access_token, required: true, default: ENV["GITHUB_ACCESS_TOKEN"]
22
+
23
+ # comments
24
+ option :after, description: "post comment when commits of inserting this line after this hash exist"
25
+ option :until, description: "post comment when commits of inserting this line exists"
26
+ end
27
+ end
28
+
29
+ desc "pr", "comment to pr"
30
+ github_pr_options
31
+
32
+ option :input_format, default: "ltsv", enum: %w[ ltsv ], description: "when stdin comes, it will be parsed according to this format"
33
+
34
+ option :message, description: "when no stdin comes, we use this option as pr comment message"
35
+ option :file, description: "when no stdin comes, we use this option as target file of pr comment"
36
+ option :line, type: :numeric, description: "when no stdin comes, we use this option as line number in target file of pr comment (Note: this is not line in diff of pr)"
37
+
38
+ option :debug, type: :boolean, descriptin: "if true, do not post pr comment"
39
+ def pr
40
+ @options, comments = parse_options
41
+
42
+ post_pr_comments(filter_comments(comments, after: @options["after"], _until: @options["before"]))
43
+ end
44
+
45
+ protected
46
+ def post_pr_comments(comments)
47
+ base = pr_info.base.sha
48
+ head = ::Git::Log.new(::Git.open("."), 1).last.sha
49
+
50
+ diff = GitDiffParser.parse(`git diff #{base}`)
51
+
52
+ comments.each do |comment|
53
+ patch_position = find_patch_position_of_comment(diff, comment)
54
+
55
+ if @options["debug"]
56
+ warn "PR comment to #{@options["repo"]}/pulls/#{@options["pr"]}@#{head}##{comment[:file]}:#{patch_position} => #{comment[:message]}"
57
+ else
58
+ github_client.create_pull_request_comment(@options["repo"], @options["pr"], comment[:message], head, comment[:file], patch_position)
59
+ end
60
+ end
61
+ end
62
+
63
+ def github_client
64
+ @github_client ||= begin
65
+ Octokit.configure {|config| config.api_endpoint = @options["github"] }
66
+ Octokit::Client.new(access_token: @options["github_access_token"])
67
+ end
68
+ end
69
+
70
+ def pr_info
71
+ @pr_info ||= begin
72
+ github_client.pull_request(@options["repo"], @options["pr"])
73
+ end
74
+ end
75
+
76
+ def filter_comments(comments, after: nil, _until: nil)
77
+ after ||= pr_info.base.sha
78
+
79
+ # XXX: commandline injection risk
80
+ diff = GitDiffParser.parse(::Git::Diff.new(::Git.open("."), after, _until).patch)
81
+
82
+ comments.select do |comment|
83
+ if comment[:file]
84
+ find_patch_position_of_comment(diff, comment)
85
+ else # pr comment
86
+ false # TODO: post to pr
87
+ end
88
+ end
89
+ end
90
+
91
+ def find_patch_position_of_comment(diff, comment)
92
+ diff_of_file = diff.find {|f| f.file == comment[:file] }
93
+
94
+ if diff_of_file
95
+ line = diff_of_file.changed_lines.find {|line| line.number == comment[:line].to_i }
96
+
97
+ return line.patch_position if line
98
+ end
99
+
100
+ nil
101
+ end
102
+
103
+ def parse_options
104
+ comments = if stdin?
105
+ parse_comments($stdin.read)
106
+ else
107
+ if options[:message] && options[:file] && options[:line]
108
+ [ { message: options[:message], file: options[:file], line: options[:line] } ]
109
+ else
110
+ raise ::Thor::RequiredArgumentMissingError, "no comments found. gives stdin or use --message option"
111
+ end
112
+ end
113
+
114
+ options = complete_options(self.options)
115
+
116
+ return [ options, comments ]
117
+ end
118
+
119
+ def parse_comments(string)
120
+ case options[:input_format]
121
+ when "ltsv"
122
+ require "ltsv"
123
+
124
+ LTSV.parse(string).map do |line|
125
+ line[:line] = line[:line].to_i if line[:line]
126
+ line
127
+ end
128
+ else
129
+ raise "unknown input format: #{options[:input_format]}"
130
+ end
131
+ end
132
+
133
+ def complete_options(options)
134
+ complete = case options[:from_env]
135
+ when "circleci"
136
+ completed_params_of_circleci(options)
137
+ else
138
+ {}
139
+ end
140
+
141
+ options.merge(complete)
142
+ end
143
+
144
+ def completed_params_of_circleci(options)
145
+ if pr = ENV["CI_PULL_REQUEST"]
146
+ pr_num = pr.split("/").last.to_i
147
+
148
+ template = Addressable::Template.new("https://{host}/{organization}/{repository}/compare/{after}...{until}")
149
+
150
+ if params = template.extract(ENV["CIRCLE_COMPARE_URL"])
151
+ github = if params["host"] == "github.com"
152
+ "https://api.github.com/v3"
153
+ else
154
+ "https://#{params["host"]}/api/v3" # I don't know this is correct
155
+ end
156
+
157
+ return {
158
+ "github" => github,
159
+ "repo" => [ params["organization"], params["repository"] ].join("/"),
160
+ "pr" => pr_num,
161
+ "after" => params["after"],
162
+ "until" => params["until"]
163
+ }
164
+ else
165
+ warn `env`
166
+ raise "really?"
167
+ end
168
+ end
169
+
170
+ {}
171
+ end
172
+
173
+ def stdin?
174
+ # http://www.ownway.info/Ruby/idiom/judge_stdin
175
+ File.pipe?($stdin) || File.select([$stdin], [], [], 0)
176
+ end
177
+ end
178
+ end
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: github_commenter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - okitan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: addressable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: git
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: git_diff_parser
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: ltsv
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: octokit
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: thor
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: bundler
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.12'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.12'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '10.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '10.0'
125
+ description: CLI to comment github PR/commit
126
+ email:
127
+ - okitakunio@gmail.com
128
+ executables:
129
+ - github_commenter
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - CODE_OF_CONDUCT.md
135
+ - Gemfile
136
+ - LICENSE.txt
137
+ - README.md
138
+ - Rakefile
139
+ - VERSION
140
+ - bin/console
141
+ - bin/setup
142
+ - exe/github_commenter
143
+ - github_commenter.gemspec
144
+ - lib/github_commenter.rb
145
+ - lib/github_commenter/cli.rb
146
+ homepage: https://github.com/okitan/github_commenter
147
+ licenses:
148
+ - MIT
149
+ metadata: {}
150
+ post_install_message:
151
+ rdoc_options: []
152
+ require_paths:
153
+ - lib
154
+ required_ruby_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ required_rubygems_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ requirements: []
165
+ rubyforge_project:
166
+ rubygems_version: 2.5.1
167
+ signing_key:
168
+ specification_version: 4
169
+ summary: CLI to comment github
170
+ test_files: []