-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.js
182 lines (153 loc) · 4.65 KB
/
handler.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
'use strict';
const jobsFile = process.env.JOBS_FILE || 'jobs.yml';
module.exports.crawlerRemoteJobs = (event, context, callback) => {
const S3Lib = require('./lib/S3')
, stackoverflow = require('./stackoverflow-jobs')
, YAML = require('yamljs')
, bucketName = process.env.BUCKET_NAME
, region = process.env.REGION || process.env.AWS_REGION
;
S3Lib.setConfig(bucketName, region);
stackoverflow.run(function(error, results) {
let output = [];
results.forEach(function(result) {
output = output.concat(result);
});
let yamlString = YAML.stringify(output, 4);
S3Lib.putObject(yamlString, jobsFile, 'public-read', function(err, data) {
if (err) console.log('error', err, err.stack); // an error occurred
else console.log('success', data); // successful response
});
console.log('LogScheduledEvent');
});
const response = {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*', // Required for CORS support to work
},
body: JSON.stringify({
message: 'crawlerRemoteJobs: It works!'
}),
};
callback(null, response);
};
module.exports.sendJobsFileToRepository = (event, context, callback) => {
const GitHubApi = require('github');
const S3Lib = require('./lib/S3');
const async = require('async');
const moment = require('moment');
const user = process.env.GITHUB_OWNER;
const repo = process.env.GITHUB_REPO;
const filename = process.env.GITHUB_FILE;
const token = process.env.GITHUB_TOKEN;
const path = process.env.DATA_FILE;
const bucketName = process.env.BUCKET_NAME;
const region = process.env.REGION || process.env.AWS_REGION;
const commitMessage = 'Code commited from AWS Lambda at ' + moment().format();
S3Lib.setConfig(bucketName, region);
var code, referenceCommitSha, newTreeSha, newCommitSha;
var github = new GitHubApi();
// user token
github.authenticate({
type: 'token',
token: token,
});
async.waterfall([
// get the object from s3 which is the actual code
// that needs to be pushed to github
function(callback) {
console.log('Getting Jobs from S3...');
S3Lib.getObject(filename, function(err, data) {
if (err) console.log(err, err.stack);
if (! err) {
// code from s3 to commit to github
code = data.Body.toString('utf8');
callback(null);
}
});
},
// get a reference to the master branch of the repo
function(callback){
console.log('Getting reference...');
github.gitdata.getReference({
owner: user,
repo: repo,
ref: 'heads/master'
}, function(err, data){
if (err) console.log(err);
if (!err) {
referenceCommitSha = data.data.object.sha;
callback(null);
}
});
},
// create a new tree with our code
function(callback){
console.log('Creating tree...');
var files = [];
files.push({
path: path,
mode: '100644',
type: 'blob',
content: code
});
github.gitdata.createTree({
owner: user,
repo: repo,
tree: files,
base_tree: referenceCommitSha
}, function(err, data){
if (err) console.log(err);
if (!err) {
newTreeSha = data.data.sha;
callback(null);
}
});
},
// create the commit with our new code
function(callback){
console.log('Creating commit...');
github.gitdata.createCommit({
owner: user,
repo: repo,
message: commitMessage,
tree: newTreeSha,
parents: [referenceCommitSha]
}, function(err, data) {
if (err) console.log(err);
if (!err) {
newCommitSha = data.data.sha;
callback(null);
}
});
},
// update the reference to point to the new commit
function(callback){
console.log('Updating reference...');
github.gitdata.updateReference({
owner: user,
repo: repo,
ref: 'heads/master',
sha: newCommitSha,
force: true
}, function(err, data) {
if (err) console.log(err);
if (!err) callback(null, 'done');
});
}
], function (err, result) {
if (err) context.done(err, "Drat!!");
if (!err) context.done(null, "Code successfully pushed to github.");
});
const response = {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*', // Required for CORS support to work
},
body: JSON.stringify({
message: 'sendJobsFileToRepository: It works!'
}),
};
console.log(context);
callback(null, response);
};