neetob 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0efeb267893c2c4b205174f5c8e98f995b96ecb9c897802a7a6ebb69b35c948b
4
- data.tar.gz: b67600bfdfbb52cc703ca194db5d68e2353dc0d3533f038b2a2a3085a4ab37b6
3
+ metadata.gz: 1a0657b62734699934769a7e533576814055ea5221ef854e3addd5c76915635c
4
+ data.tar.gz: 230107c6ff888b7d8c666aea78243637bd7747ba2ab741e59527ad1528b9a7c3
5
5
  SHA512:
6
- metadata.gz: 37aaf0fc506ab02bf04450e54de083cefe1b953620f8ec73ad08e0dbb35dc245420af2b98fdd1cc4acb3c4ee5b58ddf377dd5c7ae7f4af9a43bb812eb12edcfe
7
- data.tar.gz: e09bebaa6f439bcbb217de61a9822df674bfaab52f1a22680b4eccb45a0bd2d759122c257a487bacb582c4a8ae5d815c7ed0359e53a2dbe6630c0d7ca84e3b83
6
+ metadata.gz: ebdb1be1339eb0ac7ae8929f8da953038c00f07aece1d39095a6cac0b7af7a0d42da21a7140fe4b5c18ec96c18269f8d6667005f0396bc6f64031b364cbf153c
7
+ data.tar.gz: eae36243252b3cd4fa34cada869589339b799a6f59a1b0e0ca463094bc4ede4769daf535c0b7fb8fba0721cadd7090acc971fc9e1e3711dc4a0d3c71a9c2d378
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ### [0.4.2](https://www.github.com/bigbinary/neetob/compare/v0.4.1...v0.4.2) (2023-04-14)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * Allow regex values for path and keyword in github search command. ([#233](https://www.github.com/bigbinary/neetob/issues/233)) ([f6866db](https://www.github.com/bigbinary/neetob/commit/f6866dbe3decf897c483a5b77aaa8989a21192d4))
9
+
3
10
  ### [0.4.1](https://www.github.com/bigbinary/neetob/compare/v0.4.0...v0.4.1) (2023-04-05)
4
11
 
5
12
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- neetob (0.4.1)
4
+ neetob (0.4.2)
5
5
  brakeman (~> 5.0)
6
6
  chronic
7
7
  dotenv (~> 2.8.1)
@@ -148,9 +148,9 @@ GEM
148
148
  net-smtp (0.3.3)
149
149
  net-protocol
150
150
  nio4r (2.5.8)
151
- nokogiri (1.13.10-x86_64-darwin)
151
+ nokogiri (1.14.3-x86_64-darwin)
152
152
  racc (~> 1.4)
153
- nokogiri (1.13.10-x86_64-linux)
153
+ nokogiri (1.14.3-x86_64-linux)
154
154
  racc (~> 1.4)
155
155
  octokit (4.25.1)
156
156
  faraday (>= 1, < 3)
@@ -166,7 +166,7 @@ GEM
166
166
  coderay (~> 1.1)
167
167
  method_source (~> 1.0)
168
168
  public_suffix (5.0.0)
169
- racc (1.6.1)
169
+ racc (1.6.2)
170
170
  rack (2.2.6.4)
171
171
  rack-test (2.0.2)
172
172
  rack (>= 1.3)
data/README.md CHANGED
@@ -164,6 +164,16 @@ Searches for keywords across multiple neeto projects within specified files by u
164
164
  neetob github search --keyword neeto --path README.md --repos "neeto-*-staging"
165
165
  ```
166
166
 
167
+ We can also use regular-expressions as the value for both the keyword and the path of the file.
168
+
169
+ ```sh
170
+ neetob github search --keyword "ne*to" --path ".*\.md$" --repos "neeto-*-staging"
171
+ ```
172
+
173
+ The above command searches for all occurrences of any word matching the given reg-expression in all files with the extension ".md".
174
+
175
+ We can pass the `--replace` option to replace all found instances of the keyword with another word.
176
+
167
177
  ### Protect branch
168
178
 
169
179
  Updates branch protection rules in neeto repos by using the `protect_branch` command.
@@ -31,8 +31,9 @@ module Neetob
31
31
  option :keyword, type: :string, aliases: "-k", desc: "Keyword which needs to be searched", required: true
32
32
  option :path, type: :string, aliases: "-p",
33
33
  desc: "Path of the file in which you want to search the given keyword.", required: true
34
+ option :replace, type: :string, aliases: "-s", desc: "Word with which you want to replace the found instances of the keyword"
34
35
  def search
35
- Search.new(options[:repos], options[:keyword], options[:path], options[:sandbox]).run
36
+ Search.new(options[:repos], options[:keyword], options[:path], options[:replace], options[:sandbox]).run
36
37
  end
37
38
 
38
39
  desc "login", "Update the Github access token by authenticating via browser"
@@ -36,6 +36,10 @@ module Neetob
36
36
  `git clone --quiet --depth 1 git@github.com:#{repo}.git /tmp/neetob/#{repo_name_without_org_suffix(repo)}`
37
37
  end
38
38
 
39
+ def clone_repo_in_tmp_dir(repo)
40
+ `git clone --quiet git@github.com:#{repo}.git /tmp/neetob/#{repo_name_without_org_suffix(repo)}`
41
+ end
42
+
39
43
  def add_commmit_and_push_changes(repo)
40
44
  `#{cd_to_repo_in_tmp_dir(repo)} && git add . && \\
41
45
  git commit -m "#{pr_title}" && git push -u origin #{branch_name} --force`
@@ -1,37 +1,99 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "base"
3
+ require_relative "./make_pr/base"
4
4
 
5
5
  module Neetob
6
6
  class CLI
7
7
  module Github
8
- class Search < Base
9
- attr_accessor :repos, :key_to_search, :search_file_path, :sandbox
8
+ class Search < MakePr::Base
9
+ attr_accessor :repos, :key_to_search, :search_file_path, :replacement_term, :sandbox
10
10
 
11
- def initialize(repos, key_to_search, search_file_path, sandbox = false)
12
- super()
13
- @repos = repos
11
+ def initialize(repos, key_to_search, search_file_path, replacement_term, sandbox = false)
14
12
  @key_to_search = key_to_search
13
+ @replacement_term = replacement_term
14
+ branch_name = build_branch_name
15
+ pr_title = build_pr_title
16
+ super(pr_title, branch_name)
15
17
  @search_file_path = search_file_path
18
+ @repos = repos
16
19
  @sandbox = sandbox
17
20
  end
18
21
 
19
22
  def run
20
23
  matching_repos = find_all_matching_apps_or_repos(repos, :github, sandbox)
21
24
  matching_repos.each do |repo|
25
+ ui.info("\nSearching in #{repo}")
26
+ clone_repo_in_tmp_dir(repo)
22
27
  begin
23
- ui.info("\n Searching in \"#{repo}/#{search_file_path}\" for \"#{key_to_search}\"\n")
24
- content = Base64.decode64(client.contents(repo, path: search_file_path).content)
25
- ui.error("Keyword not found") and next unless content.include? key_to_search
28
+ if replacement_term.nil?
29
+ find_and_print_matching_lines(repo)
30
+ else
31
+ check_and_delete_remote_branch(repo)
32
+ find_and_replace_keyword(repo)
33
+ ui.say("No changes were made to #{repo}, check if keyword exists.") and next if !are_changes_made?(repo)
26
34
 
27
- content.each_line do |line|
28
- ui.success(line) if line.include?(key_to_search)
35
+ ui.info(add_commmit_and_push_changes(repo))
36
+ delete_local_feature_branch(repo)
37
+ pull_request = client.create_pull_request(repo, "main", branch_name, pr_title)
38
+ ui.success("Successfully replaced \"#{key_to_search}\" with \"#{replacement_term}\" in #{repo}")
39
+ ui.success("PR Link:- #{pull_request.html_url}")
29
40
  end
30
41
  rescue StandardError => e
31
42
  ExceptionHandler.new(e).process
32
43
  end
33
44
  end
45
+ `rm -rf /tmp/neetob`
34
46
  end
47
+
48
+ private
49
+
50
+ def search_for_keyword(repo)
51
+ `#{cd_to_repo_in_tmp_dir(repo)} && \
52
+ find . -type f -iregex "#{search_file_path}" | xargs grep -E "#{key_to_search}"`
53
+ end
54
+
55
+ def find_and_print_matching_lines(repo)
56
+ files_with_matching_lines = find_matching_files(repo)
57
+ files_with_matching_lines.empty? ? ui.error("Keyword not found")
58
+ : print_matching_lines(files_with_matching_lines)
59
+ end
60
+
61
+ def find_matching_files(repo)
62
+ result = search_for_keyword(repo)
63
+ result.each_line.reduce({}) do |lines_by_file, line|
64
+ filename, content = line.split(":", 2)
65
+ lines_by_file[filename] ||= []
66
+ lines_by_file[filename] << content.strip
67
+ lines_by_file
68
+ end
69
+ end
70
+
71
+ def print_matching_lines(files_with_matching_lines)
72
+ files_with_matching_lines.each do |file, list_of_lines|
73
+ ui.info("#{file}")
74
+ list_of_lines.each { |line| ui.success(" ↳#{line}") }
75
+ end
76
+ end
77
+
78
+ def are_changes_made?(repo)
79
+ changes_made = `#{cd_to_repo_in_tmp_dir(repo)} && git checkout #{branch_name} && git status`
80
+ !changes_made.include?("nothing to commit")
81
+ end
82
+
83
+ def find_and_replace_keyword(repo)
84
+ `#{cd_to_repo_in_tmp_dir(repo)} && \
85
+ find . -type f -regex #{search_file_path} -print0 | \
86
+ xargs -0 sed -i '' 's/#{key_to_search}/#{replacement_term}/g'`
87
+ end
88
+
89
+ def build_pr_title
90
+ "Replaced \"#{key_to_search}\" with \"#{replacement_term}\""
91
+ end
92
+
93
+ def build_branch_name
94
+ branch_name = "search-#{key_to_search}-and-replace-with-#{replacement_term}"
95
+ branch_name.gsub(/\W/, "_")
96
+ end
35
97
  end
36
98
  end
37
99
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Neetob
4
- VERSION = "0.4.1"
4
+ VERSION = "0.4.2"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: neetob
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Udai Gupta
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-05 00:00:00.000000000 Z
11
+ date: 2023-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor