This repository has been archived by the owner on Jul 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
132 lines (105 loc) · 3.66 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
122
123
124
125
126
127
128
129
130
131
132
const express = require('express');
const bodyParser = require('body-parser');
const triggerTravis = require('lyne-helper-trigger-travis');
const lint = require('@commitlint/lint').default;
const load = require('@commitlint/load').default;
require('dotenv')
.config();
const app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
const PORT = process.env.PORT || 5000;
const IS_TEST = process.env.TEST;
const getCommitlintParserOptions = (opts) => {
if (opts.parserPreset) {
return {
parserOpts: opts.parserPreset.parserOpts
};
}
return {};
};
const isValidSemanticCommit = (commit) => {
const config = {
extends: ['@commitlint/config-conventional']
};
return load(config)
.then((opts) => lint(commit, opts.rules, getCommitlintParserOptions(opts)))
.then((report) => report.valid)
.catch((error) => {
throw new Error(`Error in linting: ${error}`);
});
};
const triggerTravisJob = (commit) => {
console.log('FIGMA-LISTENER: -->> Will trigger travis job for icons');
const travisUrl = 'https://api.travis-ci.com/repo/lyne-design-system%2Flyne-icons/requests';
isValidSemanticCommit(commit)
.then((result) => {
if (result) {
triggerTravis({
branchName: 'master',
message: `${commit} (triggered from Figma)`,
travisToken: process.env.TRAVIS_TOKEN,
travisUrl
})
.then(() => {
console.log('FIGMA-LISTENER: -->> Successfully triggered Travis Job');
})
.catch(() => {
console.log('FIGMA-LISTENER: -->> Error in Triggering Travis');
});
} else {
console.log('FIGMA-LISTENER: -->> Is not a valid commit:');
console.log(commit);
}
})
.catch((error) => {
throw new Error(`FIGMA-LISTENER: -->> Error in triggering Travis: ${error}`);
});
};
/**
* If a figma library with thousands of assets is published, the request size
* for the webhook requests get too large. That's why figma sends several
* webhooks to make sure, request size meet the desired criteria. But we don't
* want to trigger our process pipeline for all these batch requests.
*/
let pendingTimeout = false;
let pendingCommitMessage = '';
const pendingDuration = 15 * 1000;
const triggerTravisJobDelayed = (commit, response) => {
if (pendingTimeout) {
console.log('FIGMA-LISTENER: -->> Previous received Figma webhook canceled, since I just received a new webhook request.');
clearTimeout(pendingTimeout);
}
response.sendStatus(200);
if (commit.length > 0) {
pendingCommitMessage = commit;
}
pendingTimeout = setTimeout(() => {
triggerTravisJob(pendingCommitMessage);
pendingTimeout = false;
pendingCommitMessage = '';
}, pendingDuration);
};
app.get('/', (req, res) => {
res.send('Lyne Design System. Simple express server to listen to webhooks send from Figma. Figma webhook will send POST requests to /figma-change');
});
app.post('/figma-change', (req, res) => {
const isFileIcons = req.body.file_key === process.env.FIGMA_FILE_ID_ICONS;
const isCorrectPasscode = req.body.passcode === process.env.FIGMA_PASSCODE;
const commit = req.body.description;
if (!isFileIcons || !isCorrectPasscode) {
console.log('FIGMA-LISTENER: -->> Either wrong Figma passcode or wrong file key');
res.sendStatus(400);
} else {
triggerTravisJobDelayed(commit, res);
}
});
const serverInstance = app.listen(PORT, () => {
console.log(`FIGMA-LISTENER: -->> Listening to http://localhost:${PORT}.`);
if (IS_TEST) {
serverInstance.close();
console.log('FIGMA-LISTENER: -->> Server closed again since this is only a test.');
}
});