dod 0.1.0

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: 97cf06b562ec86180ce8328730c1045d88cffa92
4
+ data.tar.gz: bcef025e73123b0d1c62873d636355b236a44f0e
5
+ SHA512:
6
+ metadata.gz: 18c7771ea4aed1071a0f306cff91be9116bf001bd80b1c22782f0f3949df1c4137b285ac93379981967faf78653a588ac5ad5694ad453b3d60906b3aa693b16c
7
+ data.tar.gz: 2ca9fbdbb000bc8c1ef018f44dc4e8c29addc6e9e27af3517e6b03ead38a6dbd30dad336115f6b3870cd5399901d637742543a2e082520ab27226d0d434a94e6
data/bin/dod ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.push File.expand_path("../../lib", __FILE__)
3
+
4
+ require "dod"
5
+ Dod::Runner.run ARGV
@@ -0,0 +1,78 @@
1
+ # coding: utf-8
2
+
3
+ module Dod
4
+ class BitbucketServerAPI
5
+ attr_accessor :endpoint, :pull_request_id
6
+
7
+ def initialize(environment, project, repo, branch)
8
+ @username = environment["BITBUCKET_USERNAME"]
9
+ @password = environment["BITBUCKET_PASSWORD"]
10
+
11
+ self.endpoint = "https://stash.allegrogroup.com/rest/api/1.0/projects/#{project}/repos/#{repo}"
12
+ self.pull_request_id = get_pull_request_id(branch)
13
+ end
14
+
15
+ def create_definition_of_done(tasks)
16
+ unless credentials_given?
17
+ puts "Bitbucket credentials not given. Use BITBUCKET_USERNAME and BITBUCKET_PASSWORD environment variables".red
18
+ return
19
+ end
20
+ unless definition_of_done_not_created?
21
+ puts "Definition of Done already created.".red
22
+ return
23
+ end
24
+ comment_id = post_comment
25
+ create_tasks_for_comment(comment_id, tasks)
26
+ puts "Definition of Done created.".green
27
+ end
28
+
29
+ def credentials_given?
30
+ @username && !@username.empty? && @password && !@password.empty?
31
+ end
32
+
33
+ def create_tasks_for_comment(comment_id, tasks)
34
+ uri = URI("https://stash.allegrogroup.com/rest/api/1.0/tasks")
35
+ tasks.each do |task|
36
+ body_hash = { anchor: { id: comment_id, type: "COMMENT" }, text: task }
37
+ body = body_hash.to_json
38
+ post(uri, body)
39
+ end
40
+ end
41
+
42
+ def post_comment
43
+ uri = URI("#{endpoint}/pull-requests/#{pull_request_id}/comments")
44
+ body = { text: "Definition of Done." }.to_json
45
+ post(uri, body)[:id]
46
+ end
47
+
48
+ def definition_of_done_not_created?
49
+ uri = URI("#{endpoint}/pull-requests/#{pull_request_id}/tasks/count")
50
+ tasks_count = fetch_json(uri)
51
+ tasks_count[:open] == 0 && tasks_count[:resolved] == 0
52
+ end
53
+
54
+ def get_pull_request_id(branch)
55
+ uri = URI("#{endpoint}/pull-requests")
56
+ fetch_json(uri)[:values].select { |v| v[:fromRef][:displayId] == branch }.map { |v| v[:id] }.first
57
+ end
58
+
59
+ def fetch_json(uri)
60
+ req = Net::HTTP::Get.new(uri.request_uri, { "Content-Type" => "application/json" })
61
+ req.basic_auth @username, @password
62
+ res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
63
+ http.request(req)
64
+ end
65
+ JSON.parse(res.body, symbolize_names: true)
66
+ end
67
+
68
+ def post(uri, body)
69
+ req = Net::HTTP::Post.new(uri.request_uri, { "Content-Type" => "application/json" })
70
+ req.basic_auth @username, @password
71
+ req.body = body
72
+ res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
73
+ http.request(req)
74
+ end
75
+ JSON.parse(res.body, symbolize_names: true)
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,41 @@
1
+ require "claide"
2
+ require "colored"
3
+
4
+ module Dod
5
+ class Runner < CLAide::Command
6
+
7
+ self.summary = "Creates Definition of Done task list for pull request linked to current branch and remote."
8
+ self.command = "dod"
9
+ self.version = Dod::VERSION
10
+
11
+ def initialize(argv)
12
+ donefile = argv.option("donefile", "Donefile")
13
+ @donefile_path = donefile if File.exist?(donefile)
14
+ @git = GitHelper.new
15
+ @donefile = Donefile.new
16
+ super
17
+ end
18
+
19
+ def validate!
20
+ super
21
+ if self.class == Runner && !@donefile_path
22
+ help!("Could not find a Donefile.")
23
+ end
24
+ if not File.directory?(".git")
25
+ help!("Not a git repository.")
26
+ end
27
+ end
28
+
29
+ def self.options
30
+ [
31
+ ["--donefile=<path/to/donefile>", "Path to the Donefile where all tasks for Definition of Done are defined."]
32
+ ]
33
+ end
34
+
35
+ def run
36
+ tasks = @donefile.parse_tasks(@donefile_path)
37
+ bitbucket = BitbucketServerAPI.new(ENV, @git.project_name, @git.repo_name, @git.current_branch)
38
+ bitbucket.create_definition_of_done(tasks)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,19 @@
1
+ require 'pathname'
2
+
3
+ module Dod
4
+ class Donefile
5
+
6
+ def parse_tasks(donefile_path)
7
+ contents = File.open(Pathname.new(donefile_path), "r:utf-8", &:read)
8
+
9
+ if contents.tr!("“”‘’‛", %(""'''))
10
+ puts "Your #{path.basename} has had smart quotes sanitised. " \
11
+ "To avoid issues in the future, you should not use " \
12
+ "TextEdit for editing it. If you are not using TextEdit, " \
13
+ "you should turn off smart quotes in your editor of choice.".red
14
+ end
15
+
16
+ contents.each_line.map { |line| line }
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ require 'git'
2
+
3
+ module Dod
4
+ class GitHelper
5
+
6
+ attr_accessor :g
7
+
8
+ def initialize
9
+ @g = Git.open(".")
10
+ end
11
+
12
+ def current_branch
13
+ g.current_branch
14
+ end
15
+
16
+ def repo_name
17
+ url_components = URI.parse(g.remote('origin').url)
18
+ url_components.path.split('/')[3].split('.').first
19
+ end
20
+
21
+ def project_name
22
+ url_components = URI.parse(g.remote('origin').url)
23
+ url_components.path.split('/')[2].upcase
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Dod
2
+ VERSION = "0.1.0"
3
+ end
data/lib/dod.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "dod/version"
2
+ require "dod/commands/runner"
3
+ require "dod/dod_core/donefile"
4
+ require "dod/helpers/git_helper"
5
+ require "dod/client/bitbucket_server_api"
6
+
7
+ module Dod
8
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dod
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aleksander Grzyb
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: claide
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.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: '1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: colored
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.13'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.13'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ description: Creates Definition of Done for PR.
84
+ email:
85
+ - aleksander.grzyb@allegrogroup.com
86
+ executables:
87
+ - dod
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - bin/dod
92
+ - lib/dod.rb
93
+ - lib/dod/client/bitbucket_server_api.rb
94
+ - lib/dod/commands/runner.rb
95
+ - lib/dod/dod_core/donefile.rb
96
+ - lib/dod/helpers/git_helper.rb
97
+ - lib/dod/version.rb
98
+ homepage:
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: 2.0.0
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.6.6
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Creates Definition of Done for PR.
122
+ test_files: []