-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
121 lines (109 loc) · 4.37 KB
/
index.js
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
const { Toolkit } = require('actions-toolkit')
const Geocoder = require('node-geocoder');
const dotenv = require('dotenv');
dotenv.config();
const axios = require('axios').default;
// Create variables for future values
var user = '';
var person = '';
var person_info = '';
var user_location = '';
var body = '';
var issue_number = '';
var date_time = '';
var date_string = '';
Toolkit.run(async tools => {
// Assign repo data to variables
const owner = tools.context.payload.repository.owner.login;
const repo = tools.context.payload.repository.name;
const actor = tools.context.actor;
// Events the action is looking for
const expected_events= ['opened', 'edited', 'reopened', 'created', 'submitted'];
if (expected_events.includes(tools.context.payload.action) && tools.context.payload.issue) {
// Issue details
const action = tools.context.payload.issue.action
user = tools.context.payload.issue.user
body = tools.context.payload.issue.body
issue_number = tools.context.payload.issue.number
} else if (expected_events.includes(tools.context.payload.action) && tools.context.payload.pull_request) {
// Pull Request details
const action = tools.context.payload.pull_request.action
user = tools.context.payload.pull_request.user
body = tools.context.payload.pull_request.body
};
// Check for string that triggers time check
body = body.toLowerCase();
if (body.includes('is') && body.includes('awake?')) {
// If it does, get user info
var question = body.substring(
body.lastIndexOf('is'),
body.lastIndexOf('awake?')
);
var question_arr = question.split(' ');
person = question_arr[1].replace(/@/g, '');
person_info = (await tools.github.users.getByUsername({
username: person
})).data;
// Get the location specified in their profile
user_location = person_info.location;
// Check if location is defined
// If it is then gather the time information for it
if (person_info.location != '' || person_info.location.length != 0) {
// Get the time in that location, first get lat and long then get the time for those coordinates
// Set options for the Geocoder
var options = {
provider: 'google',
httpAdapter: 'https',
apiKey: process.env.GOOGLE_API_KEY,
formatter: null
};
// Initialize the Geocoder with the options and get the data
var geocoder = Geocoder(options);
var geocode_data = (await geocoder.geocode(`${user_location}`));
// Timestamp of current time in UTC
var timestamp = Math.floor((new Date()).getTime() / 1000);
// Get the time zone data from the Google Time Zone API
const getTimezoneData = () => {
return axios ({
method: 'get',
url: `https://maps.googleapis.com/maps/api/timezone/json?location=${geocode_data[0]['latitude']},${geocode_data[0]['longitude']}×tamp=${timestamp}&key=${process.env.GOOGLE_API_KEY}`
})
}
timeZoneData = (await getTimezoneData()).data;
// Assign the date and time in the user's location to the date_time variable
local_timestamp = timestamp + + timeZoneData['dstOffset'] + timeZoneData['rawOffset'];
date_time = new Date(local_timestamp * 1000);
date_string = date_time.toDateString() + ' - ' + date_time.getHours() + ':' + date_time.getMinutes();
const responseMsg = `
Hi there, ${actor}! 👋
\n
You asked if ${person} was awake yet.\n
I can't tell you about their personal sleeping habits, sadly.\n
I can tell you though that the date and time for ${person} is currently:\n
${date_string}\n
I hope that helps clarify the matter for you!
`;
console.log(responseMsg)
await tools.github.issues.createComment({
owner: owner,
repo: repo,
issue_number: issue_number,
body: responseMsg
});
} else {
// If it is not, formulate a response that lets the questioner know that
const responseMsg = `
Sorry, but ${person} did not specify a location in their profile!\n
I can't look up the time in an undefined location.
`;
console.log(responseMsg)
await tools.github.issues.createComment({
owner: owner,
repo: repo,
issue_number: issue_number,
body: responseMsg
});
};
};
tools.exit.success('Completed successfully!')
});