1 | const fs = require('fs');
|
2 | const Promise = require('bluebird');
|
3 | const path = require('path');
|
4 |
|
5 | const Analyzer = require('./analyzer');
|
6 |
|
7 | const clone = require('./analyzer/steps/clone');
|
8 | const build = require('./analyzer/steps/build');
|
9 | const push = require('./analyzer/steps/push');
|
10 | const packageJson = require('./analyzer/steps/package.json');
|
11 | const sendToSlack = require('./analyzer/steps/slack.notifier');
|
12 |
|
13 |
|
14 | const stages = require('./analyzer/header/stages');
|
15 | const stagesEnricher = require('./analyzer/enricher/stages');
|
16 | const codefreshYaml = require('./analyzer/transform/codefresh.yaml');
|
17 |
|
18 | function isValidContext(context){
|
19 | const { credentials, repo: { repoOwner, repoName }} = context;
|
20 | return credentials && repoOwner && repoName;
|
21 | }
|
22 |
|
23 | async function _defaultYaml(){
|
24 | const defaultYaml = await Promise.fromCallback((cb) => fs.readFile(path.resolve(__dirname, 'default.yaml'), cb));
|
25 | return defaultYaml.toString();
|
26 | }
|
27 |
|
28 | module.exports = {
|
29 |
|
30 | 'onboarding': async (context) => {
|
31 |
|
32 | const analyzer = new Analyzer();
|
33 |
|
34 |
|
35 | if(!isValidContext(context)){
|
36 | return _defaultYaml();
|
37 | }
|
38 | try{
|
39 | await analyzer.init(context);
|
40 | }
|
41 | catch (e) {
|
42 | console.error(e);
|
43 | return _defaultYaml();
|
44 | }
|
45 | return analyzer
|
46 | .step(clone)
|
47 | .step(build)
|
48 | .step(push)
|
49 | .header(stages)
|
50 | .enrich(stagesEnricher)
|
51 | .resolve();
|
52 | },
|
53 |
|
54 | 'api': async (context) => {
|
55 | const analyzer = new Analyzer();
|
56 |
|
57 | await analyzer.init(context);
|
58 | return analyzer
|
59 | .step(clone)
|
60 | .step(build)
|
61 | .step(push)
|
62 | .step(packageJson)
|
63 | .step(sendToSlack)
|
64 | .header(stages)
|
65 | .enrich(stagesEnricher)
|
66 | .transform(codefreshYaml)
|
67 | .resolve();
|
68 | }
|
69 |
|
70 | };
|