-
Notifications
You must be signed in to change notification settings - Fork 1
/
with-title-email-escalations.rb
executable file
·130 lines (119 loc) · 4.21 KB
/
with-title-email-escalations.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
#!/usr/bin/env ruby
require 'json'
require 'rubygems'
require "google/apis/gmail_v1"
require "googleauth"
require "googleauth/stores/file_token_store"
require "fileutils"
require 'mime'
require 'mail'
require 'awesome_print'
require 'parseconfig'
require 'logger'
require 'csv'
logger = Logger.new(STDERR)
logger.level = Logger::DEBUG
OOB_URI = "urn:ietf:wg:oauth:2.0:oob".freeze
APPLICATION_NAME = "Gmail API Ruby Quickstart".freeze
CREDENTIALS_PATH = "credentials.json".freeze
# The file token.yaml stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
TOKEN_PATH = "token.yaml".freeze
SCOPE = Google::Apis::GmailV1::AUTH_GMAIL_SEND #
# https://www.googleapis.com/auth/gmail.modify #Google::Apis::GmailV1::AUTH_GMAIL_READONLY
##
# Ensure valid credentials, either by restoring from the saved credentials
# files or intitiating an OAuth2 authorization. If authorization is required,
# the user's default browser will be launched to approve the request.
#
# @return [Google::Auth::UserRefreshCredentials] OAuth2 credentials
def authorize
client_id = Google::Auth::ClientId.from_file CREDENTIALS_PATH
token_store = Google::Auth::Stores::FileTokenStore.new file: TOKEN_PATH
authorizer = Google::Auth::UserAuthorizer.new client_id, SCOPE, token_store
user_id = "default"
credentials = authorizer.get_credentials user_id
if credentials.nil?
url = authorizer.get_authorization_url base_url: OOB_URI
puts "Open the following URL in the browser and enter the " \
"resulting code after authorization:\n" + url
code = gets
credentials = authorizer.get_and_store_credentials_from_code(
user_id: user_id, code: code, base_url: OOB_URI
)
end
credentials
end
# Initialize the API
service = Google::Apis::GmailV1::GmailService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize
email_config = ParseConfig.new('email.conf').params
to = email_config['to_address']
previous_escalations_file_exists = File.exists? 'previous-escalations.txt'
pp previous_escalations_file_exists
previous_ids = []
if previous_escalations_file_exists
File.readlines('previous-escalations.txt').each do |line|
previous_ids.push(line.to_i)
end
end
logger.ap previous_ids
escalations_for_subject = ""
no_changes = true
new_ids_to_escalate = []
ARGF.each_line do |csv_line|
parsed_csv = CSV.parse(csv_line)
logger.ap parsed_csv[0]
escalate_id = parsed_csv[0][0]
title = parsed_csv[0][3]
logger.debug "escalate id:" + escalate_id
logger.debug "title:" + title
escalate_id = escalate_id.to_i
if !previous_ids.include?(escalate_id)
no_changes = false
logger.debug "new id to escalate:" + escalate_id.to_s
escalations_for_subject += " " + escalate_id.to_s.chomp + ':' + title[0..39] + ","
new_ids_to_escalate.push("<li> " + "<a href =\"https://support.mozilla.org/questions/"+ escalate_id.to_s + "\">" + escalate_id.to_s + "</a>:" + title + "</li>")
previous_ids.push(escalate_id)
end
end
logger.debug "new ids to escalate:"
logger.ap new_ids_to_escalate
logger.debug "previous ids with new ids to escalate:"
logger.debug previous_ids
body = "<ol>"
new_ids_to_escalate.each do |text|
body += text
end
body += "</ol>"
logger.debug "escalations for subject" + escalations_for_subject
logger.debug "body" + body
time = Time.now
body = "Time:" + time.to_s + "<br /><br/>" + body
if no_changes
logger.debug "Exiting because there were no new escalations"
exit
end
user_id = "me"
message = Mail.new
message.date = time
message.subject = 'FF Desktop escalations:' + time.to_s
message.body = body
message.content_type = 'text/html'
message.from = user_id
message.to = to
msg = message.encoded
message_object = Google::Apis::GmailV1::Message.new(raw:message.to_s)
response = service.send_user_message("me", message_object)
ap response
# cleanup
# rename old file if it exists and if there were changes
FileUtils.mv("previous-escalations.txt", "previous-escalations.txt~") if previous_escalations_file_exists
# write out new file
File.open('previous-escalations.txt', 'w') do |file|
previous_ids.each do |id|
file.print id.to_s + "\n"
end
end