1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | 'use strict';
|
14 |
|
15 | const crypto = require('crypto');
|
16 | const { debug } = require('@adobe/helix-log');
|
17 | const { commitLog } = require('./git');
|
18 | const { resolveRepositoryPath } = require('./utils');
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | function createMiddleware(options) {
|
27 | |
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | return (req, res, next) => {
|
39 |
|
40 | const { owner } = req.params;
|
41 | const repoName = req.params.repo;
|
42 | const sha = req.query.sha || 'master';
|
43 | let fpath = req.query.path || '';
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | const repPath = resolveRepositoryPath(options, owner, repoName);
|
49 |
|
50 | if (typeof fpath !== 'string') {
|
51 | res.status(400).send('Bad request');
|
52 | return;
|
53 | }
|
54 |
|
55 | while (fpath.length && fpath[0] === '/') {
|
56 |
|
57 | fpath = fpath.substr(1);
|
58 | }
|
59 |
|
60 | const NOT_IMPL = 'not implemented';
|
61 |
|
62 | function email2avatarUrl(email) {
|
63 | const hash = crypto.createHash('md5').update(email).digest('hex');
|
64 | return `https://s.gravatar.com/avatar/${hash}`;
|
65 | }
|
66 |
|
67 | commitLog(repPath, sha, fpath)
|
68 | .then((commits) => {
|
69 | const host = req.mappedSubDomain ? `localhost:${options.listen[req.protocol].port}` : req.headers.host;
|
70 | const result = [];
|
71 | commits.forEach((commit) => {
|
72 | const parents = [];
|
73 | commit.parent.forEach((oid) => parents.push({
|
74 | sha: oid,
|
75 | url: `${req.protocol}://${host}/api/repos/${owner}/${repoName}/commits/${oid}`,
|
76 | html_url: `${req.protocol}://${host}/repos/${owner}/${repoName}/commit/${oid}`,
|
77 | }));
|
78 | result.push({
|
79 | sha: commit.oid,
|
80 | node_id: NOT_IMPL,
|
81 | commit: {
|
82 | author: {
|
83 | name: commit.author.name,
|
84 | email: commit.author.email,
|
85 | date: new Date(commit.author.timestamp * 1000).toISOString(),
|
86 | },
|
87 | committer: {
|
88 | name: commit.committer.name,
|
89 | email: commit.committer.email,
|
90 | date: new Date(commit.committer.timestamp * 1000).toISOString(),
|
91 | },
|
92 | message: commit.message,
|
93 | tree: {
|
94 | sha: commit.tree,
|
95 | url: `${req.protocol}://${host}/api/repos/${owner}/${repoName}/git/trees/${commit.tree}`,
|
96 | },
|
97 | url: `${req.protocol}://${host}/api/repos/${owner}/${repoName}/git/commits/${commit.oid}`,
|
98 | comment_count: 0,
|
99 | verification: {
|
100 | verified: false,
|
101 | reason: NOT_IMPL,
|
102 | signature: NOT_IMPL,
|
103 | payload: NOT_IMPL,
|
104 | },
|
105 | },
|
106 |
|
107 | url: `${req.protocol}://${host}/api/repos/${owner}/${repoName}/commits/${commit.oid}`,
|
108 | html_url: `${req.protocol}://${host}/repos/${owner}/${repoName}/commit/${commit.oid}`,
|
109 | comments_url: `${req.protocol}://${host}/api/repos/${owner}/${repoName}/commits/${commit.oid}/comments`,
|
110 | author: {
|
111 | avatar_url: email2avatarUrl(commit.author.email),
|
112 | gravatar_id: '',
|
113 |
|
114 | },
|
115 | committer: {
|
116 | avatar_url: email2avatarUrl(commit.committer.email),
|
117 | gravatar_id: '',
|
118 |
|
119 | },
|
120 | parents,
|
121 | });
|
122 | });
|
123 | res.json(result);
|
124 | })
|
125 | .catch((err) => {
|
126 | debug(`[commitHandler] code: ${err.code} message: ${err.message} stack: ${err.stack}`);
|
127 | next(err);
|
128 |
|
129 |
|
130 | });
|
131 | };
|
132 | }
|
133 | module.exports = createMiddleware;
|