-
Notifications
You must be signed in to change notification settings - Fork 0
/
jiradir.rb
138 lines (110 loc) · 5.45 KB
/
jiradir.rb
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
#!/usr/bin/env ruby
require 'time'
require './routeddir'
require './jira'
def to_json(object)
"#{JSON.pretty_generate(object)}\n\n" # TODO: figure out why 2 characters are cut off the end
end
def to_timestamp(time_string)
Time.parse(time_string).to_i
end
class JiraDir < RoutedDir
def initialize
@jira = Jira.new
end
def list_projects(params)
@jira.get('/project', max_age: 1800).map { |entry| entry['key'] }
end
def list_project_issues(params)
@jira.get("/search?fields=key&jql=project%3D#{params[:project]}", max_age: 30)['issues'].map { |entry| entry['key'] }
end
def list_issue_comments(params)
comments = @jira.get("/issue/#{params[:issue]}/comment")['comments'].map { |entry| entry['id'] }
return comments.map { |id| id+'.txt' } + comments.map { |id| id+'.json' } + [ 'new' ]
end
def read_issue_comment(params)
@jira.get("/issue/#{params[:issue]}/comment/#{params[:comment]}")
end
def read_issue_comment_json(params)
to_json(read_issue_comment(params))
end
def read_issue_comment_body(params)
read_issue_comment(params)['body']
end
def get_issue_comment_times(params)
comment = read_issue_comment(params)
ctime = to_timestamp(comment['created'])
mtime = to_timestamp(comment['updated'])
atime = mtime
return [ atime, mtime, ctime ]
end
def add_comment(params, contents)
return if contents.empty?
@jira.post("/issue/#{params[:issue]}/comment", body: { 'body' => contents }.to_json)
end
def read_issue(params)
@jira.get("/issue/#{params[:issue]}?fields=attachment,description,subtasks")
end
def list_issue_attachments(params)
attachments = read_issue(params)['fields']['attachment'].map { |entry| entry['id'] }
return attachments + attachments.map { |id| id+'.json' }
end
def read_attachment(params)
@jira.get("/attachment/#{params[:attachment]}")
end
def read_attachment_json(params)
to_json(read_attachment(params))
end
def list_attachment_body(params)
[ read_attachment(params)['filename'] ]
end
def read_attachment_body(params)
@jira.raw_get(read_attachment(params)['content'])
end
def get_issue_attachment_times(params)
attachment = read_attachment(params)
ctime = to_timestamp(attachment['created'])
mtime = ctime
atime = mtime
return [ atime, mtime, ctime ]
end
def get_attachment_body_size(params)
read_attachment(params)['size']
end
def read_issue_title(params)
read_issue(params)['fields']['description']
end
def list_issue_subtasks(params)
read_issue(params)['fields']['subtasks'].map { |subtask|
subtask['key']
#"#{subtask['outwardIssue']['key']}.#{subtask['type']['outward']}"
}
end
def read_issue_subtask(params)
"../../#{params[:subtask].split('.')[0]}"
end
route_add :list, '/', to: [ 'projects' ]
route_add :list, '/projects', to: :list_projects
route_add :list, '/projects/:project', to: [ 'issues' ]
route_add :list, '/projects/:project/issues', to: :list_project_issues
route_add :list, '/projects/:project/issues/:issue', to: [ 'comments', 'attachments', 'metadata', 'subtasks' ]
route_add :list, '/projects/:project/issues/:issue/comments', to: :list_issue_comments
route_add :read, '/projects/:project/issues/:issue/comments/:comment.txt', to: :read_issue_comment_body
route_add :times, '/projects/:project/issues/:issue/comments/:comment.txt', to: :get_issue_comment_times
route_add :read, '/projects/:project/issues/:issue/comments/:comment.json', to: :read_issue_comment_json
route_add :times, '/projects/:project/issues/:issue/comments/:comment.json', to: :get_issue_comment_times
route_add :read, '/projects/:project/issues/:issue/comments/new', to: ""
route_add :write, '/projects/:project/issues/:issue/comments/new', to: :add_comment
route_add :list, '/projects/:project/issues/:issue/attachments', to: :list_issue_attachments
route_add :read, '/projects/:project/issues/:issue/attachments/:attachment.json', to: :read_attachment_json
route_add :times, '/projects/:project/issues/:issue/attachments/:attachment.json', to: :get_issue_attachment_times
route_add :list, '/projects/:project/issues/:issue/attachments/:attachment', to: :list_attachment_body, constraints: { attachment: /[^\/.]+/ }
route_add :times, '/projects/:project/issues/:issue/attachments/:attachment', to: :get_issue_attachment_times, constraints: { attachment: /[^\/.]+/ }
route_add :read, '/projects/:project/issues/:issue/attachments/:attachment/:filename', to: :read_attachment_body
route_add :size, '/projects/:project/issues/:issue/attachments/:attachment/:filename', to: :get_attachment_body_size
route_add :times, '/projects/:project/issues/:issue/attachments/:attachment/:filename', to: :get_issue_attachment_times
route_add :list, '/projects/:project/issues/:issue/metadata', to: [ 'title' ]
route_add :read, '/projects/:project/issues/:issue/metadata/title', to: :read_issue_title
route_add :list, '/projects/:project/issues/:issue/subtasks', to: :list_issue_subtasks
route_add :read, '/projects/:project/issues/:issue/subtasks/:subtask', to: :read_issue_subtask
end