forked from ainformatico/git-new
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-new
More file actions
executable file
·47 lines (33 loc) · 1009 Bytes
/
git-new
File metadata and controls
executable file
·47 lines (33 loc) · 1009 Bytes
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
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'net/http'
require 'json'
issue = ARGV[0]
unless issue
puts 'Please provide an issue number'
exit 1
end
jira_server = ENV['JIRA_SERVER']
jira_user = ENV['JIRA_USER']
jira_token = ENV['JIRA_TOKEN']
unless jira_server && jira_user && jira_token
puts 'please set JIRA_SERVER, JIRA_USER and JIRA_TOKEN'
exit 1
end
issue = issue.downcase
uri = URI("#{jira_server}/rest/api/2/issue/#{issue}")
req = Net::HTTP::Get.new(uri.request_uri)
req.basic_auth jira_user, jira_token
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
http.request(req)
end
body = JSON.parse(res.body)
summary = body['fields']['summary']
.downcase
.strip # trim spaces
.gsub(/\W+/, '-') # non alphabetical chars to -
.gsub(/-+/, '-') # remove duplicated -
.gsub(/-$/, '') # remove trailing -
# remove project prefix
issue_id = issue.gsub(/^\w+-/, '')
`git checkout -b #{issue_id}-#{summary}`