Add MediaWiki integration basics

This commit is contained in:
Basti 2018-04-19 12:32:03 +02:00
parent 74429feb9d
commit c0c2f97dae
4 changed files with 91 additions and 79 deletions

View File

@ -147,4 +147,8 @@ module.exports = async function(robot) {
require('./integrations/github')(robot, kredits);
if (typeof process.env.KREDITS_MEDIAWIKI_URL !== 'undefined') {
require('./integrations/mediawiki')(robot, kredits);
}
};

View File

@ -1,5 +1,7 @@
module.exports = async function(robot, kredits) {
robot.logger.debug('[hubot-kredits] Loading GitHub integration...')
const Contributor = kredits.Contributor;
const Operator = kredits.Operator;

85
integrations/mediawiki.js Normal file
View File

@ -0,0 +1,85 @@
const util = require('util');
const fetch = require('node-fetch');
const groupArray = require('group-array');
module.exports = async function(robot, kredits) {
robot.logger.debug('[hubot-kredits] Loading MediaWiki integration...')
const Contributor = kredits.Contributor;
const Operator = kredits.Operator;
const apiURL = process.env.KREDITS_MEDIAWIKI_URL + 'api.php';
const robot = {
data: {},
brain: {
set(key, value) {
this.data[key] = value;
},
get(key) {
return this.data[key];
}
}
};
function fetchChanges () {
const params = [
'action=query',
'format=json',
'list=recentchanges',
'rctype=edit|new',
'rcshow=!minor|!bot|!anon|!redirect',
'rclimit=max',
'rcprop=ids|title|timestamp|user|sizes|comment|flags'
];
const url = `${apiURL}?${params.join('&')}`;
return fetch(url).then(res => {
if (res.status === 200) {
return res.json();
} else {
robot.logger.warn(`Fetching ${url} returned HTTP status ${res.status}:`);
robot.logger.warn(res.body);
throw Error('Unexpected response from '+url);
}
}).then(res => {
return res.query.recentchanges;
});
}
function groupChangesByUser (changes) {
return groupArray(changes, 'user');
}
function analyzeUserChanges (user, changes) {
robot.logger.info(`Analyzing ${changes.length} edits from ${user} ...`);
const results = {};
results.pagesCreated = changes.filter(c => c.type === 'new');
results.pagesChanged = changes.filter(c => c.type === 'edit');
results.linesAdded = changes
.map(c => { return (c.oldlen < c.newlen) ? (c.newlen - c.oldlen) : 0; })
.reduce((a, b) => a + b);
robot.logger.info(`Created ${results.pagesCreated.length} pages`);
robot.logger.info(`Edited ${results.pagesChanged.length} pages`);
robot.logger.info(`Added ${results.linesAdded} lines of text\n`);
return results;
}
function createProposalForUserChanges (user, changes) {
const details = analyzeUserChanges(user, changes);
// robot.logger.info(util.inspect(details));
}
fetchChanges()
.then(res => groupChangesByUser(res))
.then(res => {
Object.keys(res).forEach(user => createProposalForUserChanges(user, res[user]));
});
};

View File

@ -1,79 +0,0 @@
#!/usr/bin/env node
const util = require('util');
const fetch = require('node-fetch');
const groupArray = require('group-array');
if (typeof process.env.KREDITS_MEDIAWIKI_URL === 'undefined') { return false; }
const apiURL = process.env.KREDITS_MEDIAWIKI_URL + 'api.php';
const robot = {
data: {},
brain: {
set(key, value) {
this.data[key] = value;
},
get(key) {
return this.data[key];
}
}
};
function fetchChanges () {
const params = [
'action=query',
'format=json',
'list=recentchanges',
'rctype=edit|new',
'rcshow=!minor|!bot|!anon|!redirect',
'rclimit=max',
'rcprop=ids|title|timestamp|user|sizes|comment|flags'
];
const url = `${apiURL}?${params.join('&')}`;
return fetch(url).then(res => {
if (res.status === 200) {
return res.json();
} else {
console.log(`Fetching ${url} returned HTTP status ${res.status}:`);
console.log(res.body);
throw Error('Unexpected response from '+url);
}
}).then(res => {
return res.query.recentchanges;
});
}
function groupChangesByUser (changes) {
return groupArray(changes, 'user');
}
function analyzeUserChanges (user, changes) {
console.log(`Analyzing ${changes.length} edits from ${user} ...`);
const results = {};
results.pagesCreated = changes.filter(c => c.type === 'new');
results.pagesChanged = changes.filter(c => c.type === 'edit');
results.linesAdded = changes
.map(c => { return (c.oldlen < c.newlen) ? (c.newlen - c.oldlen) : 0; })
.reduce((a, b) => a + b);
console.log(`Created ${results.pagesCreated.length} pages`);
console.log(`Edited ${results.pagesChanged.length} pages`);
console.log(`Added ${results.linesAdded} lines of text\n`);
return results;
}
function createProposalForUserChanges (user, changes) {
const details = analyzeUserChanges(user, changes);
// console.log(util.inspect(details));
}
fetchChanges()
.then(res => groupChangesByUser(res))
.then(res => {
Object.keys(res).forEach(user => createProposalForUserChanges(user, res[user]));
});