tukune 0.0.1 → 0.0.3

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
  SHA1:
3
- metadata.gz: da8eeb756b92ae9e7bc02d2d46628172de826a24
4
- data.tar.gz: 1f382801f54a7311e21091ea41baede08179b4d3
3
+ metadata.gz: 73d08b6076a382f4cf209647056bfcce7a43f133
4
+ data.tar.gz: 99146e70bef2a37a55df7cc8b72f83087a4d7485
5
5
  SHA512:
6
- metadata.gz: 8b6b1afb41a8ad6e67255f9fdf59cc03c76847e054124589fec0f2011a8dd2dad439e4234ee3eda4adc6f08a34a870966bcc0af7ee2c24ab9f6f8f825a0fd920
7
- data.tar.gz: 34ae658de6d2e7eee6d7d48e8810be478fb8d2b0ec04c03bd17e7c6c72114a603b7f2101717fc81b4c98c7e7084506d6136c5ee78b7c9e8d94862b7e956cd11a
6
+ metadata.gz: c1e7cddf73594634ccb4cddb30f5653b0f2be8f54ccbff0084a9821933aca170dc91d19926e13c645ff85e6a129e5a475072f447563a013df17989a0d431ca84
7
+ data.tar.gz: 5f0cc1005ff747072627982110e0066fdc7910bc27cf7845f47fbe0dda7103d83a1c0bd052333e9763fa339840992d346ca154a68c6fe2d8289bab005cf7878d
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
- # [WIP]Tukune
1
+ # Tukune
2
+ [![Gem Version](https://badge.fury.io/rb/tukune.svg)](https://badge.fury.io/rb/tukune)
2
3
  [![Build Status](https://travis-ci.org/gin0606/tukune.svg?branch=master)](https://travis-ci.org/gin0606/tukune)
3
4
  [![Coverage Status](https://coveralls.io/repos/github/gin0606/tukune/badge.svg?branch=master)](https://coveralls.io/github/gin0606/tukune?branch=master)
4
5
 
@@ -43,6 +44,11 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
43
44
 
44
45
  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).
45
46
 
47
+
48
+ ### TODO
49
+ - [ ] testing
50
+ - [ ] refactoring
51
+
46
52
  ## Contributing
47
53
  1. Fork it ( https://github.com/gin0606/tukune/fork )
48
54
  2. Create your feature branch (`git checkout -b my-new-feature`)
data/bin/tukune CHANGED
@@ -18,6 +18,10 @@ opt_parser = OptionParser.new do |opt|
18
18
  options[:body] = body
19
19
  end
20
20
 
21
+ opt.on('--enable-all') do |enable_all|
22
+ options[:enable_all] = enable_all
23
+ end
24
+
21
25
  opt.on('-h', '--help', 'help') do
22
26
  puts opt_parser
23
27
  end
data/lib/github.rb ADDED
@@ -0,0 +1,137 @@
1
+ require 'octokit'
2
+ require 'mem'
3
+
4
+ class Github
5
+ include Mem
6
+
7
+ def initialize(repo, current_branch = 'master')
8
+ @repo = repo
9
+ @current_branch_name = current_branch
10
+ @branch = BranchCache.new(repo)
11
+ end
12
+
13
+ def branch(name)
14
+ current_branch = @branch.read(@current_branch_name)
15
+ @branch.create(name, current_branch[:object][:sha])
16
+ end
17
+
18
+ def checkout(branch)
19
+ @current_branch_name = branch
20
+ @branch.read(@current_branch_name)
21
+ end
22
+
23
+ def add(path)
24
+ added_files << path
25
+ end
26
+
27
+ def commit(message)
28
+ current_branch = @branch.read(@current_branch_name)
29
+
30
+ tree = create_tree(current_branch[:object][:sha])
31
+ new_commit = client.create_commit(@repo, message, tree[:sha], current_branch[:object][:sha])
32
+
33
+ @branch.update(@current_branch_name, new_commit[:sha])
34
+ added_files.clear
35
+
36
+ new_commit
37
+ end
38
+
39
+ def pull_request(base, title, body)
40
+ client.create_pull_request(
41
+ @repo,
42
+ base,
43
+ @current_branch_name,
44
+ title,
45
+ body
46
+ )
47
+ end
48
+
49
+ private
50
+
51
+ def create_tree(branch_sha)
52
+ tree_helper = TreeHelper.new(@repo, branch_sha)
53
+ added_files.each { |path| tree_helper.add(path) }
54
+ tree_helper.create_tree
55
+ end
56
+
57
+ def added_files
58
+ []
59
+ end
60
+ memoize :added_files
61
+
62
+ def client
63
+ ::Octokit::Client.new
64
+ end
65
+ memoize :client
66
+
67
+ class BranchCache
68
+ include Mem
69
+
70
+ def initialize(repo)
71
+ @repo = repo
72
+ @cache = {}
73
+ end
74
+
75
+ def create(name, sha)
76
+ @cache[name] = client.create_ref(@repo, "heads/#{name}", sha)
77
+ end
78
+
79
+ def read(name)
80
+ @cache[name] = client.ref(@repo, "heads/#{name}")
81
+ end
82
+
83
+ def update(name, sha)
84
+ @cache[name] = client.update_branch(@repo, name, sha)
85
+ end
86
+
87
+ def delete(name)
88
+ ref = client.delete_ref(@repo, "heads/#{name}")
89
+ @cache.delete(name)
90
+ ref
91
+ end
92
+
93
+ private
94
+
95
+ def client
96
+ ::Octokit::Client.new
97
+ end
98
+ memoize :client
99
+ end
100
+
101
+ class TreeHelper
102
+ include Mem
103
+
104
+ def initialize(repo, branch_sha)
105
+ @repo = repo
106
+ @branch_sha = branch_sha
107
+ end
108
+
109
+ def add(path)
110
+ content = Base64.encode64(File.read(path))
111
+ sha = client.create_blob(@repo, content, 'base64')
112
+ tree << { path: path, mode: '100644', type: 'blob', sha: sha }
113
+ end
114
+
115
+ def create_tree
116
+ client.create_tree(@repo, tree, base_tree: current_tree[:sha])
117
+ end
118
+
119
+ private
120
+
121
+ def current_tree
122
+ commit = client.commit(@repo, @branch_sha)
123
+ client.tree(@repo, commit[:commit][:tree][:sha], recursive: true)
124
+ end
125
+ memoize :current_tree
126
+
127
+ def tree
128
+ []
129
+ end
130
+ memoize :tree
131
+
132
+ def client
133
+ ::Octokit::Client.new
134
+ end
135
+ memoize :client
136
+ end
137
+ end
data/lib/tukune.rb CHANGED
@@ -1,7 +1,7 @@
1
+ require 'github'
1
2
  require 'tukune/cli'
2
3
  require 'tukune/configuration'
3
4
  require 'tukune/git/diff'
4
- require 'tukune/git/commit'
5
5
  require 'tukune/version'
6
6
 
7
7
  module Tukune
data/lib/tukune/cli.rb CHANGED
@@ -2,33 +2,37 @@ module Tukune
2
2
  class CLI
3
3
  class << self
4
4
  def start(options)
5
- if Tukune.configuration.tukune_branch?
5
+ config = Tukune.configuration
6
+ diff = Tukune::Git::Diff.name_status
7
+ if config.tukune_branch?
6
8
  puts 'this branch is tukune'
7
9
  return
8
10
  end
9
- unless Tukune.configuration.pull_request?
11
+ unless options[:enable_all] || config.pull_request?
10
12
  puts 'This build is not part of pull request.'
11
13
  puts 'If you want to exec tukune, try to use `--enable-all` option.'
12
14
  return
13
15
  end
14
- diff = Tukune::Git::Diff.name_status
15
16
  if diff.nothing_to_commit?
16
17
  puts 'nothing to commit, working directory clean'
17
18
  return
18
19
  end
19
- c = Tukune::Git::Commit.new(Tukune.configuration)
20
+
21
+ github = Github.new(config.repository_name, config.current_branch)
22
+ github.branch(config.feature_branch)
23
+ github.checkout(config.feature_branch)
24
+
20
25
  diff.modified_files.each do |f|
21
- c.add(f)
22
- puts "Create #{f} blob."
26
+ github.add(f)
27
+ puts "Create #{f}."
23
28
  end
24
29
  diff.added_files.each do |f|
25
- c.add(f)
26
- puts "Create #{f} blob."
30
+ github.add(f)
31
+ puts "Create #{f}."
27
32
  end
28
- # diff.deleted_files.each {|f| c.delete(f) }
29
- c.commit("#{options[:title]}\n\n#{options[:body]}")
33
+ p github.commit("#{options[:title]}\n\n#{options[:body]}")
30
34
  puts 'Create commit'
31
- c.pull_request(options[:title], options[:body])
35
+ github.pull_request(config.current_branch, options[:title], options[:body])
32
36
  puts 'Create pull request'
33
37
  end
34
38
  end
@@ -1,3 +1,3 @@
1
1
  module Tukune
2
- VERSION = '0.0.1'.freeze
2
+ VERSION = '0.0.3'.freeze
3
3
  end
data/tukune.gemspec CHANGED
@@ -15,8 +15,8 @@ Gem::Specification.new do |spec|
15
15
  spec.license = 'MIT'
16
16
 
17
17
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
- spec.bindir = 'exe'
19
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.bindir = 'bin'
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
20
  spec.require_paths = ['lib']
21
21
 
22
22
  spec.add_dependency 'octokit'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tukune
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - gin0606
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-30 00:00:00.000000000 Z
11
+ date: 2016-07-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: octokit
@@ -111,7 +111,10 @@ dependencies:
111
111
  description: Create a pull request if there is a difference via Github API
112
112
  email:
113
113
  - kkgn06@gmail.com
114
- executables: []
114
+ executables:
115
+ - console
116
+ - setup
117
+ - tukune
115
118
  extensions: []
116
119
  extra_rdoc_files: []
117
120
  files:
@@ -127,13 +130,13 @@ files:
127
130
  - bin/console
128
131
  - bin/setup
129
132
  - bin/tukune
133
+ - lib/github.rb
130
134
  - lib/tukune.rb
131
135
  - lib/tukune/cli.rb
132
136
  - lib/tukune/configuration.rb
133
137
  - lib/tukune/configuration/circle_ci.rb
134
138
  - lib/tukune/configuration/default.rb
135
139
  - lib/tukune/configuration/travis_ci.rb
136
- - lib/tukune/git/commit.rb
137
140
  - lib/tukune/git/diff.rb
138
141
  - lib/tukune/version.rb
139
142
  - tukune.gemspec
@@ -1,113 +0,0 @@
1
- require 'octokit'
2
- require 'mem'
3
-
4
- module Tukune
5
- module Git
6
- class Commit
7
- include Mem
8
-
9
- def initialize(configuration)
10
- @repository_name = configuration.repository_name
11
- @current_branch = configuration.current_branch
12
- @feature_branch = configuration.feature_branch
13
- end
14
-
15
- def add(file_path)
16
- content = Base64.encode64(File.read(file_path))
17
- sha = client.create_blob(@repository_name, content, 'base64')
18
- blobs[file_path] = sha
19
- end
20
-
21
- def delete(file_path)
22
- delete_files << file_path
23
- end
24
-
25
- def commit(message)
26
- tree = if delete_files.empty?
27
- create_tree(changed_blobs)
28
- else
29
- create_tree_with_delete_files(changed_blobs)
30
- end
31
-
32
- commits << client.create_commit(@repository_name, message, tree[:sha], current_branch[:object][:sha])
33
- end
34
-
35
- def pull_request(title, body)
36
- create_feature_branch(@feature_branch, commits.last[:sha])
37
- client.create_pull_request(
38
- @repository_name,
39
- @current_branch,
40
- @feature_branch,
41
- title,
42
- body
43
- )
44
- end
45
-
46
- private
47
-
48
- def create_tree(changed_blobs)
49
- client.create_tree(@repository_name, changed_blobs, base_tree: current_tree[:sha])
50
- end
51
-
52
- # FIXME: can not commit correctly if exist delete_files
53
- def create_tree_with_delete_files(changed_blobs)
54
- changed_files = blobs.map { |path, _| path }
55
-
56
- # trees = current_tree[:tree].map(&:to_h)
57
- # trees = current_tree[:tree].map(&:to_h).delete_if { |blob| delete_files.include?(blob[:path]) }
58
- trees = current_tree[:tree].map(&:to_h)
59
- .delete_if { |blob| delete_files.include?(blob[:path]) }
60
- .delete_if { |blob| changed_files.include?(blob[:path]) }
61
- trees += changed_blobs
62
-
63
- client.create_tree(@repository_name, trees)
64
- end
65
-
66
- def current_tree
67
- commit = client.commit(@repository_name, current_branch[:object][:sha])
68
- client.tree(@repository_name, commit[:commit][:tree][:sha], recursive: true)
69
- end
70
- memoize :current_tree
71
-
72
- def current_branch
73
- client.ref(@repository_name, "heads/#{@current_branch}")
74
- end
75
- memoize :current_branch
76
-
77
- def create_feature_branch(name, sha)
78
- client.create_ref(@repository_name, "heads/#{name}", sha)
79
- end
80
-
81
- def commits
82
- []
83
- end
84
- memoize :commits
85
-
86
- def blobs
87
- {}
88
- end
89
- memoize :blobs
90
-
91
- def changed_blobs
92
- blobs.map do |file_path, sha|
93
- {
94
- path: file_path,
95
- mode: '100644',
96
- type: 'blob',
97
- sha: sha
98
- }
99
- end
100
- end
101
-
102
- def delete_files
103
- []
104
- end
105
- memoize :delete_files
106
-
107
- def client
108
- ::Octokit::Client.new
109
- end
110
- memoize :client
111
- end
112
- end
113
- end