-
Notifications
You must be signed in to change notification settings - Fork 57
/
helpx_html.pre.js
112 lines (100 loc) · 3.45 KB
/
helpx_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
/*
* 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.
*/
/* eslint-disable no-param-reassign */
const _ = require('lodash/fp');
const { fetch } = require('@adobe/helix-fetch');
function collectMetadata(req, logger) {
const options = {
uri: `https://api.github.com/repos/${req.params.owner}/${
req.params.repo
}/commits?path=${req.params.path}&sha=${req.params.ref}`,
headers: {
'User-Agent': 'Request-Promise',
},
};
logger.debug(`Fetching Git Metadata from ${options.uri}`);
return fetch(options.uri, options)
.then(async (response) => {
if (!response.ok) {
const e = new Error(`${response.status} - "${await response.text()}"`);
e.statusCode = response.status;
throw e;
}
const metadata = await response.json();
logger.debug('Got git metadata');
return metadata;
})
.catch((error) => {
logger.error('Failed to get metadata', error);
return {};
});
}
/**
* Extracts some committers data from the list of commits and appends the list to the content
* @param {RequestContext} ctx Context
*/
function extractCommittersFromMetadata(meta) {
const res = Object.values(meta)
.filter((commit) => !!commit.author)
.map((commit) => ({
avatar_url: commit.author.avatar_url,
display: `${commit.commit.author.name} | ${commit.commit.author.email}`,
}));
const uniq = _.uniqBy(JSON.stringify, res);
return uniq;
}
function extractLastModifiedFromMetadata(meta = [], logger) {
logger.debug('Getting last modified date from Git');
const lastMod = meta.length > 0
&& meta[0].commit
&& meta[0].commit.author ? meta[0].commit.author.date : null;
logger.debug(lastMod);
return {
raw: lastMod,
display: lastMod ? new Date(lastMod) : 'Unknown',
};
}
// the most compact way to write a pre.js:
//
// module.exports.pre is a function (taking next as an argument)
// that returns a function (with context, secrets, logger as arguments)
// that calls next (after modifying the context a bit)
module.exports.pre = (context, config) => {
const { logger } = config;
logger.debug('setting context path');
context.content.contextPath = 'myinjectedcontextpath';
logger.debug('collecting metadata');
try {
return collectMetadata(config.request, logger)
.then((gitmeta) => {
logger.debug('Metadata has arrived');
context.content.gitmetadata = gitmeta;
return gitmeta;
})
.then((gitmeta) => {
const committers = extractCommittersFromMetadata(gitmeta, logger);
context.content.committers = committers;
return gitmeta;
})
.then((gitmeta) => {
const lastMod = extractLastModifiedFromMetadata(gitmeta, logger);
context.content.lastModified = lastMod;
})
.catch((e) => {
logger.error(e);
return { error: e };
});
} catch (e) {
logger.error(e);
return { error: e };
}
};