Commit 7c5f8b07 authored by wu-lee's avatar wu-lee

serve.js etc. - implement (optional) posting issues on gitlab

parent 9ac300e5
......@@ -44,6 +44,16 @@ module.exports = {
// Also, request under 'request'
},
},
gitlab: {
server: {
issuesUrl: 'https://example.com/api/v4/projects/9/issues',
token: 'sekret',
},
registration: {
title: 'Registration for {{oc-user}}',
description: './src/templates/issue.hbs',
},
},
webhook: {
path: '/example-path',
secret: 'secret!',
......
'use strict';
const axios = require('axios');
const submitlog = require('./logging.js').loggers.submit;
const config = require('./config.js').gitlab;
const templates = require('./templates.js');
if (!config) {
// We can't do anything
module.exports = null;
return;
}
const {server, registration} = config;
if (!server.issuesUrl)
throw new Error("there must be a gitlab.server.issuesUrl value configured");
if (!server.token)
throw new Error("there must be a gitlab.server.token value configured");
module.exports = {
newRegistrationIssue: function(context) {
const {request, config} = context;
const body = request.body;
function onsuccess(response) {
const id = response.data.iid;
submitlog.info(`added a new ticket ${id} for registration by ${body['oc-user']}`);
}
function onfailure(error) {
submitlog.info(`failed to add a new ticket for registration by ${body['oc-user']}. `, error);
}
return axios.post(
server.issuesUrl,
{
title: `Registration for ${body['oc-user']}`,
confidential: true,
description: templates.issue(context),
},
{ headers: {'PRIVATE-TOKEN': server.token} },
).then(onsuccess, onfailure);
},
};
......@@ -42,6 +42,24 @@ module.exports = {
return new Handlebars.SafeString(text);
},
/** Markdown quote text */
mdquote: function() {
var [opts, text] = getopts(arguments);
text = (text || '').replace(/\r/mg, '').replace(/^/mg, '> ');
return new Handlebars.SafeString(text);
},
/** Json output */
json: function() {
var [opts, data] = getopts(arguments);
console.log("hash", opts.hash);
data = JSON.stringify(
data, null,
opts.hash.pretty? Number(opts.hash.indent || 2) : null
);
return new Handlebars.SafeString(data);
},
// Ghost emulations
asset: function(path) {
return '/'+path;
......
......@@ -42,11 +42,14 @@ function defineTemplateLoader(file, name, defaultTemplate) {
// one required field in submitted data is an 'email' field with a
// recipient address. All fields, plus parts of the config, are
// supplied to the templates.
var notificationTemplate, acknowledgementTemplate;
var notificationTemplate, acknowledgementTemplate, issueTemplate;
if (config.email && config.email.templates) {
notificationTemplate = config.email.templates.notification;
acknowledgementTemplate = config.email.templates.acknowledgement;
}
if (config.gitlab && config.gitlab.registration && config.gitlab.registration) {
issueTemplate = config.gitlab.registration.description;
}
module.exports = {
notification: defineTemplateLoader(
......@@ -60,4 +63,10 @@ module.exports = {
'email acknowledgement template',
(context) => 'Thanks! We shall be in touch.'
),
issue: defineTemplateLoader(
issueTemplate,
'registration issue template',
(context) =>
'Form Data:\n\n```'+JSON.stringify(context.request.body, null, 2)+'```'
),
};
......@@ -134,7 +134,7 @@ module.exports = () => {
.ignore([
'**/*~', // Emacs backup files
'.*', // dotfiles
'email-templates', // email templates directory
'templates', // templates directory
])
.clean(true) // clean destination before building
.use(metalsmithDebug())
......
......@@ -4,6 +4,7 @@ const fs = require('fs');
const templates = require('./lib/templates.js');
const metalsmith = require('./metalsmith.js');
const email = require('./lib/email.js');
const gitlab = require('./lib/gitlab.js');
const git = require('./lib/git.js')({});
const loggers = require('./lib/logging.js').loggers;
const config = require('./lib/config.js');
......@@ -115,18 +116,28 @@ else{
});
}
app.post('/registration', (req, res) => {
const templateContext = {
config: {theme: config.theme},
request: req,
};
function ok() {}
function error(err) {
submitlog.info("error emailing registration notification, ",
err);
function error() {
return (err) => submitlog.info(`$message: `, err);
}
if (gitlab) {
try {
gitlab.newRegistrationIssue(templateContext)
.then(ok, error("error posting new gitlab issue"));
}
catch(e) {
submitlog.info('error posting new gitlab issue: ', e);
}
}
// Send notification of registration to admins
const sendConfig = config.email.registration || {};
const templateContext = {
config: {theme: config.theme},
request: req
};
submitlog.info(`emailing registration notification to ${sendConfig.to}`, req.body);
const notificationConfig = {
from: sendConfig.from,
......@@ -138,7 +149,7 @@ app.post('/registration', (req, res) => {
send(notificationConfig,
html,
req.body)
.then(ok, error);
.then(ok, error("error emailing registration notification"));
}
catch(e) {
maillog.error(`failed to send notification of registration to ${notificationConfig.to}: `, e);
......@@ -156,7 +167,7 @@ app.post('/registration', (req, res) => {
submitlog.info(`emailing registration acknowledgement to ${acknowledgementConfig.to}: `);
send(acknowledgementConfig,
html)
.then(ok, error);
.then(ok, error("error emailing registration acknowledgement"));
}
catch(e) {
maillog.error(`failed to send registration acknowledgement to ${acknowledgementConfig.to} `, e);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment