forked from bepronetwork/microservice-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron.js
48 lines (43 loc) · 1.27 KB
/
cron.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
const CronJob = require('cron').CronJob;
const models = require('./models');
const { Op } = require('sequelize');
const GithubService = require('./services/github.service');
const dayjs = require('dayjs');
const {subDays} = require('date-fns')
async function changeDraftedIssues() {
console.log('##### Checking draft issues to move to open');
const issues = await models.issue.findAll(
{
where: {
createdAt: {
[Op.lt]: subDays(+new Date(), 3).toISOString(),
},
state: 'draft',
},
});
for (const issue of issues) {
try {
const repo = await models.repositories.findOne({where: {id: issue.repository_id}})
await GithubService.removeDraftLabelFromIssue(issue.githubId, repo?.githubPath);
} catch (error) {
// label not exists, ignoring
}
issue.state = 'open';
await issue.save();
}
console.log('##### FINISH Checking draft issues to move to open');
}
try {
console.log('Starting CronJob...');
const stamp = +new Date();
new CronJob({
cronTime: '00 8 * * *',
onTick: () => changeDraftedIssues(),
start: true,
timeZone: 'Europe/Lisbon'
});
console.log(`Started!`, +new Date() - stamp, `ms`);
changeDraftedIssues();
} catch (ex) {
console.log('cron pattern not valid');
}