UNPKG

4.96 kBJavaScriptView Raw
1/*
2 * Copyright 2018 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12
13'use strict';
14
15const crypto = require('crypto');
16const { debug } = require('@adobe/helix-log');
17const { commitLog } = require('./git');
18const { resolveRepositoryPath } = require('./utils');
19
20/**
21 * Export the api handler (express middleware) through a parameterizable function
22 *
23 * @param {object} options configuration hash
24 * @returns {function(*, *, *)} handler function
25 */
26function createMiddleware(options) {
27 /**
28 * Express middleware handling Git API Commits requests
29 *
30 * Only a small subset will be implemented
31 *
32 * @param {Request} req Request object
33 * @param {Response} res Response object
34 * @param {callback} next next middleware in chain
35 *
36 * @see https://developer.github.com/v3/repos/commits/#list-commits-on-a-repository
37 */
38 return (req, res, next) => {
39 // GET /repos/:owner/:repo/commits/?path=:path&sha=:sha
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 // TODO: support filtering (author, since, until)
46 // const { author, since, until } = req.query;
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 // issue #247: lenient handling of redundant leading slashes in path
55 while (fpath.length && fpath[0] === '/') {
56 // trim leading slash
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 // TODO
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 // TODO
114 },
115 committer: {
116 avatar_url: email2avatarUrl(commit.committer.email),
117 gravatar_id: '',
118 // TODO
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 // github seems to swallow errors and just return an empty array...
129 // res.json([]);
130 });
131 };
132}
133module.exports = createMiddleware;