This repository was archived by the owner on Aug 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhtml.pre.js
172 lines (152 loc) · 5.13 KB
/
html.pre.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
/*
* Copyright 2018 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
const request = require('request-promise');
/**
* Removes the first title from the content children
* @param Array children Children
* @param {Object} logger Logger
*/
function removeFirstTitle(children, logger) {
logger.debug('html-pre.js - Removing first title');
let ret = children;
if (ret && ret.length > 0) {
ret = ret.slice(1);
}
return ret;
}
/**
* Fetches the commits history
* @param String apiRoot API root url
* @param String owner Owner
* @param String repo Repo
* @param String ref Ref
* @param String path Path to the content
* @param {Object} logger Logger
*/
async function fetchCommitsHistory(apiRoot, owner, repo, ref, path, logger) {
logger.debug('html-pre.js - Fetching the commits history');
const options = {
uri:
`${apiRoot}` +
'repos/' +
`${owner}` +
'/' +
`${repo}` +
'/commits?path=' +
`${path}` +
'&sha=' +
`${ref}`,
headers: {
'User-Agent': 'Request-Promise',
},
json: true,
};
logger.debug(`html-pre.js - Fetching... ${options.uri}`);
return request(options);
}
/**
* Extracts some committers data from the list of commits
* and returns the list of committers
* @param Array commits Commits
* @param {Object} logger Logger
*/
function extractCommittersFromCommitsHistory(commits, logger) {
logger.debug('html-pre.js - Extracting committers from metadata');
if (commits) {
const committers = [];
commits.forEach((entry) => {
if (entry.author
&& entry.commit.author
&& committers.map(item => item.avatar_url).indexOf(entry.author.avatar_url) < 0) {
committers.push({
avatar_url: entry.author.avatar_url,
display: `${entry.commit.author.name} | ${entry.commit.author.email}`,
});
}
});
logger.debug(`html-pre.js - Number of committers extracted: ${committers.length}`);
return committers;
}
logger.debug('html-pre.js - No committers found!');
return [];
}
/**
* Extracts the last modified date of commits and
* returns an object containing the date details
* @param Array commits Commits
* @param {Object} logger Logger
*/
function extractLastModifiedFromCommitsHistory(commits, logger) {
logger.debug('html-pre.js - Extracting last modified from metadata');
if (commits) {
const lastMod = commits.length > 0
&& commits[0].commit
&& commits[0].commit.author ? commits[0].commit.author.date : null;
const display = new Date(lastMod);
const lastModified = {
raw: lastMod,
display: lastMod ? display : 'Unknown',
};
logger.debug(`html-pre.js - Managed to fetch a last modified: ${display}`);
return lastModified;
}
logger.debug('html-pre.js - No last modified found!');
return {
raw: 'Unknown',
display: 'Unknown',
};
}
// module.exports.pre is a function (taking next as an argument)
// that returns a function (with payload, config, logger as arguments)
// that calls next (after modifying the payload a bit)
async function pre(payload, action) {
const { logger, secrets, request: actionReq } = action;
try {
if (!payload.content) {
logger.debug('html-pre.js - Payload has no content, nothing we can do');
return payload;
}
const p = payload;
// clean up the content
// TODO: re-implement removeFirstTitle without p.content.children
// p.content.children = removeFirstTitle(p.content.children, logger);
// extract committers info and last modified based on commits history
if (secrets.REPO_API_ROOT) {
p.content.commits =
await fetchCommitsHistory(
secrets.REPO_API_ROOT,
actionReq.params.owner,
actionReq.params.repo,
actionReq.params.ref,
actionReq.params.path,
logger,
);
p.content.committers = extractCommittersFromCommitsHistory(p.content.commits, logger);
p.content.lastModified = extractLastModifiedFromCommitsHistory(p.content.commits, logger);
p.content.nav = '/SUMMARY';
} else {
logger.debug('html-pre.js - No REPO_API_ROOT provided');
}
return p;
} catch (e) {
logger.error(`Error while executing html.pre.js: ${e.stack || e}`);
return {
error: e,
};
}
}
module.exports.pre = pre;
// required only for testing
module.exports.removeFirstTitle = removeFirstTitle;
module.exports.fetchCommitsHistory = fetchCommitsHistory;
module.exports.extractCommittersFromCommitsHistory = extractCommittersFromCommitsHistory;
module.exports.extractLastModifiedFromCommitsHistory = extractLastModifiedFromCommitsHistory;