UNPKG

2.31 kBJavaScriptView Raw
1const _ = require('lodash');
2
3const Github = require('../providers/github');
4const { comment, emptyLine } = require('../json-to-yaml');
5
6class AnalyzerContext {
7
8 constructor(context){
9 this.steps = {};
10 this.groups = {};
11 this.context = context;
12 this.headers = {
13 [comment()]: 'More examples of Codefresh YAML can be found at',
14 [comment()]: 'https://codefresh.io/docs/docs/yaml-examples/examples/',
15 [emptyLine()]: true,
16 version: '1.0',
17 [comment()]: 'Stages can help you organize your steps in stages'
18 };
19 const { repo: { repoOwner, repoName, branch } } = this.context;
20 this.github = new Github(context.credentials, repoOwner, repoName, branch);
21 }
22
23 async loadRepoInfo(){
24 this.repoInfo = await this.github.get();
25 //something go wrong
26 if(this.repoInfo.languageStatistics.message){
27 throw new Error(this.repoInfo.languageStatistics.message);
28 }
29 this.context.repoInfo = this.repoInfo;
30 }
31
32 getSteps(){
33 return this.steps;
34 }
35
36 getGroup(key) {
37 return this.groups[key];
38 }
39
40 getHeaders(){
41 return this.headers;
42 }
43
44
45 getContext(){
46 return this.context;
47 }
48
49 addStep(info){
50 this._checkForCommentsAndAdd(info.comments, this.steps);
51
52 if(_.isArray(info)){
53 info.forEach(i => {
54 this.steps[i.key] = i.step;
55 this.groups[i.key] = i.group;
56 })
57 }
58 else {
59 this.steps[info.key] = info.step;
60 this.groups[info.key] = info.group;
61 }
62 }
63
64 addHeader(info){
65 this._checkForCommentsAndAdd(info.comments, this.headers);
66
67 this.headers[info.key] = info.header;
68 }
69
70 getGithub(){
71 return this.github;
72 }
73
74 /**
75 * Check 'info' object for 'comments' array. If comments exist insert them before object.
76 * @param {*} comments - step/header comments
77 * @param {*} partition - 'headers' or 'steps' object
78 */
79 _checkForCommentsAndAdd(comments, partition) {
80 if (_.isArray(comments)) {
81 comments.forEach(c => {
82 partition[comment()] = c;
83 })
84 }
85 }
86
87}
88module.exports = AnalyzerContext;