github-grep 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE.txt +20 -0
  3. data/README.md +20 -0
  4. data/bin/github-grep +91 -0
  5. metadata +88 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c7dbab452ca85ef021446d8318c1293623ebbc17e9f2a6a3fd74b32aa2a5a0da
4
+ data.tar.gz: e5ccd15431cb9f46700aa7f336f68951e71f5b5269d0f97eb13e4a6904632316
5
+ SHA512:
6
+ metadata.gz: 994f71f2cd15ca77611c094fb62300fea5446e21939e83e059ff4d15a5cb59bfaec58ecc39c150686dafed72ea4c70225f858c27cad5f7e66e88f233c4b8e3f6
7
+ data.tar.gz: 39a96320037e21bcfd77b2399733c4a0f15ce40a7a045cd6c2c7153cced54bff5e4d8d52960a9a8077276fc014cd1275e35bea81579cda0433d5cbdc331a62d6
data/MIT-LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (C) 2016 Michael Grosser <michael@grosser.it>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,20 @@
1
+ makes github search grep and pipeable
2
+
3
+ - create a [application token](https://github.com/settings/applications) with read access
4
+ - clone repo
5
+ - follow script instructions
6
+
7
+ ```
8
+ bundle
9
+
10
+ export GITHUB_TOKEN=xxx
11
+ # or: git config github.token xxx
12
+
13
+ # search code:
14
+ bin/github-grep 'user:grosser unicorn' | grep 'narrow-it-down' | grep -v 'something good'
15
+
16
+ # search issues and PR comments:
17
+ bin/github-grep 'repo:kubernetes/kubernetes network error' --issues | grep 'narrow-it-down' | grep -v 'something good'
18
+ ```
19
+
20
+ NOTE: there are random 403 errors on the last page of a search (usually empty anyway), contacted github support about that :/
data/bin/github-grep ADDED
@@ -0,0 +1,91 @@
1
+ #!/usr/bin/env ruby
2
+ require 'cgi'
3
+ require 'json'
4
+ require 'shellwords'
5
+ require 'open3'
6
+
7
+ def usage
8
+ puts <<-TEXT.gsub(/^ /, "")
9
+ Setup
10
+ -----
11
+ # create a new token at https://github.com/settings/tokens/new with repo access
12
+ git config github.token NEW_TOKEN --local
13
+
14
+ Usage
15
+ -----
16
+ #{$0} 'something to search for'
17
+ TEXT
18
+ exit 1
19
+ end
20
+
21
+ def code_items_to_lines(items)
22
+ items.flat_map do |item|
23
+ file = item.fetch('repository').fetch('name') + ":" + item.fetch('path')
24
+ lines(item).map { |l| "#{file}: #{l}" }
25
+ end
26
+ end
27
+
28
+ def issue_items_to_lines(items)
29
+ items.flat_map do |item|
30
+ number = item.fetch("number")
31
+ lines(item).map { |l| "##{number}: #{l}" }
32
+ end
33
+ end
34
+
35
+ def lines(item)
36
+ item.fetch("text_matches").flat_map { |match| match.fetch('fragment').split("\n") }
37
+ end
38
+
39
+ def search(q, type)
40
+ per_page = 100
41
+ page = 1
42
+
43
+ loop do
44
+ response = page(q, type, page, per_page)
45
+ if page == 1
46
+ $stderr.puts "Found #{response.fetch("total_count")}"
47
+ else
48
+ $stderr.puts "Page #{page}"
49
+ end
50
+
51
+ items = response.fetch('items')
52
+ yield items
53
+
54
+ break if items.size < per_page
55
+ page += 1
56
+ end
57
+ end
58
+
59
+ def page(q, type, page, per_page)
60
+ github_token = ENV['GITHUB_TOKEN'] || `git config github.token`.strip # TODO: update docs
61
+ usage if github_token.empty?
62
+
63
+ # remove --fail and add -v to see response headers
64
+ # NOTE: github returns a 403 with a Retry-After: 60 on page 3+ ... talking with support atm but might have to handle it
65
+ url = "https://api.github.com/search/#{type}?per_page=#{per_page}&page=#{page}&q=#{CGI.escape(q)}"
66
+ command = ["curl", "-v", "-f", "-H", "Authorization: token #{github_token}", "-H", "Accept: application/vnd.github.v3.text-match+json", url]
67
+
68
+ out, err, status = Open3.capture3(*command)
69
+ if retry_after = err[/Retry-After: (\d+)/, 1] # 403 Abuse rate limit
70
+ warn "Sleeping #{retry_after} to avoid abuse rate-limit"
71
+ sleep Integer(retry_after)
72
+ out, err, status = Open3.capture3(*command)
73
+ end
74
+
75
+ raise "ERROR Request failed\n#{url}\n#{err}\n#{out}" unless status.success?
76
+
77
+ JSON.load(out)
78
+ end
79
+
80
+ type = (ARGV.delete('--issues') ? :issues : :code)
81
+
82
+ q = ARGV.shift
83
+ usage if ARGV.size != 0
84
+
85
+ search(q, type) do |items|
86
+ if type == :issues
87
+ puts issue_items_to_lines(items)
88
+ else
89
+ puts code_items_to_lines(items)
90
+ end
91
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: github-grep
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Grosser
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-10-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
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: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: bump
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email: michael@grosser.it
57
+ executables:
58
+ - github-grep
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE.txt
63
+ - README.md
64
+ - bin/github-grep
65
+ homepage: https://github.com/grosser/github-grep
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 2.0.0
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubygems_version: 3.2.16
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Makes github search grep and pipeable
88
+ test_files: []