-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdraft
More file actions
executable file
·159 lines (130 loc) · 4.08 KB
/
draft
File metadata and controls
executable file
·159 lines (130 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'json'
require 'open3'
require 'optparse'
require 'tempfile'
USAGE = %(
What does `draft` do?
Draft sets up a Github draft PR based on the supplied JIRA ticket.
Usage:
cd your-repo
draft SEC-1234
# Or, pass in a URL
draft https://yourcompany.atlassian.net/browse/SEC-4431
How does it work?
* creates branch named after jira ticket based on latest main
* swaps you to that branch
* creates an empty commit with the JIRA title in it
* pushes the branch to github
* opens a draft PR
* transitions the JIRA ticket to "In Progress", if possible
Feedback? Message David Trejo.
)
DEBUG = ENV['DEBUG']
JIRA_KEY_REGEX = /\b([A-Z]+-\d+)\b/i.freeze
def parse_options
OptionParser.new do |opts|
opts.banner = "Usage: #{$0} JIRA_KEY"
opts.on("-h", "--help", "Show this help") do
puts USAGE
exit
end
end.parse!
end
def main
if run('which jira').empty?
puts %(Please install go-jira:
brew install go-jira
# Follow instructions:
# https://github.com/go-jira/jira#quickstart-api-token-and-keychain
)
exit 1
end
if run('which gh').empty?
puts %(Please install gh:
brew install gh
)
exit 1
end
if run('which pandoc').empty?
puts %(Please install gh:
brew install pandoc
)
exit 1
end
parse_options
create_draft
end
def create_draft
key = ARGV.map { |arg| arg.match(JIRA_KEY_REGEX).to_s.upcase }.keep_if { |x| x }.first
abort("Could not find JIRA key in #{ARGV}") if !key
debug("key #{key}")
current_branch = run('git branch --show-current')
default_branch = run('gh repo view --json defaultBranchRef --jq .defaultBranchRef.name')
if current_branch != default_branch
stash = run("git stash push -m autosave-draft-#{Time.now.to_i}") if run('git status -s')
debug(stash)
checkout_default = run("git checkout #{default_branch}")
debug(checkout_default)
end
pull = run('git pull --rebase')
debug(pull)
issue = parse_issue(run("jira view -t json --expand=renderedFields #{key}"))
summary = issue.dig(:fields, :summary)
description_html = issue.dig(:renderedFields, :description).gsub("\n", ' ')
debug(summary, description_html)
description = nil
Tempfile.create('html') do |file|
file.write(description_html)
file.flush
description = run("pandoc --from=html --to=gfm-raw_html #{file.path}")
end
debug(summary, description)
description.gsub!(/([^\n ])\n([^\n ])/) { |_match| "#{Regexp.last_match(1)} #{Regexp.last_match(2)}" }
debug(summary, description)
# TODO: consider removing stopwords first
# https://github.com/brenes/stopwords-filter/blob/master/lib/stopwords/snowball/locales/en.csv
branch_name = "#{key}-#{summary.split(' ')[0..2].map { |w| w.gsub(/[^a-z0-9]/i, '') }.join('-')}"
create_branch = run("git branch #{branch_name}")
debug(create_branch)
checkout = run("git checkout #{branch_name}")
debug(checkout)
commit = run('git commit --allow-empty -m', "#{key} #{summary}\n\n#{key}\n#{description}")
debug(commit)
push = run("git push -u origin #{branch_name}")
debug(push)
[
'.github/pull_request_template.md', '.github/PULL_REQUEST_TEMPLATE.md'
].any? do |pr_template_path|
next unless File.exist?(pr_template_path)
Tempfile.create('github_description') do |file|
pull_request_template = File.read(pr_template_path)
file.write("#{description}\n\n---\n#{pull_request_template}")
file.flush
pr = run("gh pr create -a @me -d -f --body-file #{file.path}")
puts pr
end
true
end
view = run("gh pr view #{branch_name} --web")
debug(view)
sleep(0.1)
transition_to_in_progress = run("jira transition --noedit", 'In Progress', key)
debug(transition_to_in_progress)
end
def parse_issue(output)
JSON.parse(output, symbolize_names: true)
end
def run(command, *args, log_error: false)
program, simple_args = command.split(' ', 2)
output, status = Open3.capture2(program, *simple_args.split(' '), *args)
out = output.strip
puts(out) if log_error && status != 0
out
end
def debug(*args)
return unless DEBUG
puts(*args)
end
main