Skip to content
This repository was archived by the owner on May 22, 2025. It is now read-only.
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implement labels extraction
  • Loading branch information
fabianhauser committed Mar 22, 2019
commit 6709be224c6758ab1f9524e1b0bac41486da0872
94 changes: 90 additions & 4 deletions src/output/sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const Cli = require('./../include/cli');
const TABLE_ISSUES = 'issues';
const TABLE_MERGE_REQUESTS = 'merge_requests';
const TABLE_TIMES = 'times';
const TABLE_LABELS = 'labels';
const TABLE_LABEL_GROUP = 'label_group';

const COLUMNS = {
[TABLE_ISSUES]: 'issueColumns',
Expand All @@ -33,8 +35,8 @@ function getGeneralColumnType(columnName) {
return 'INTEGER PRIMARY KEY';
}

if(['iid', 'project_id'].includes(columnName)) {
return 'INTEGER';
if(['iid', 'project_id', 'gid'].includes(columnName)) {
return 'INTEGER NOT NULL';
}

if(['spent', 'total_spent', 'total_estimate', 'time'].includes(columnName)) {
Expand All @@ -45,6 +47,14 @@ function getGeneralColumnType(columnName) {
return 'DATE';
}

if(columnName === 'label_group') {
return `REFERENCES ${TABLE_LABEL_GROUP}(gid)`;
}

if(columnName === 'label_id') {
return `REFERENCES ${TABLE_LABELS}(id)`;
}

// default:
return 'TEXT';
}
Expand Down Expand Up @@ -175,6 +185,23 @@ class Sqlite extends Base {
super(config, report);
this.tables = new Map();
this.stats = new Map();
this.labels = new Map();
this.labelGroupSerial = 0;
this.labelsSerial = 0;
this.makeDefaultTables();
}

makeDefaultTables() {
this.tables.set(TABLE_LABELS, new Table(
TABLE_LABELS,
['id', 'name'],
[]
));
this.tables.set(TABLE_LABEL_GROUP, new Table(
TABLE_LABEL_GROUP,
['gid', 'label_id'],
[]
));
}

makeStats() {
Expand All @@ -197,11 +224,29 @@ class Sqlite extends Base {
}

makeIssues() {
this.makeTable(TABLE_ISSUES, this.report.issues);
const table = this.makeTable(TABLE_ISSUES, this.report.issues);
const columns = this.config.get(COLUMNS[TABLE_ISSUES]);

if(columns.includes('labels')) {
columns.push('label_group');

table.records.forEach(record => {
record[columns.indexOf('label_group')] = this.parseLabelList(record[columns.indexOf('labels')]);
});
}
}

makeMergeRequests() {
this.makeTable(TABLE_MERGE_REQUESTS, this.report.mergeRequests);
const table = this.makeTable(TABLE_MERGE_REQUESTS, this.report.mergeRequests);
const columns = this.config.get(COLUMNS[TABLE_MERGE_REQUESTS]);

if(columns.includes('labels')) {
columns.push('label_group');

table.records.forEach(record => {
record[columns.indexOf('label_group')] = this.parseLabelList(record[columns.indexOf('labels')]);
});
}
}

makeRecords() {
Expand All @@ -214,8 +259,49 @@ class Sqlite extends Base {

const table = new Table(name, columns, preparedData);
this.tables.set(name,table);
return table;
}


/**
* Creates a normalized labels structure from a labels comma list.
*
* @param labels {Array<string>}
* @returns {number | 'NULL'} id of the new label group
*/
parseLabelList(labels) {
const serial = this.labelGroupSerial++;

if(!labels) {
return 'NULL';
}

const records = labels
.map(label => this.getOrCreateLabelId(label))
.map(labelId => [serial, labelId]);
this.tables.get('label_group').records.push(...records);

return serial;
}

/**
* Gets the id of a label (and create it, if it not yet exists)
*
* @param name
* @returns {number} id of the label
*/
getOrCreateLabelId(name) {
if(this.labels.has(name)) {
return this.labels.get(name);
}

const serial = this.labelsSerial++;
this.tables.get('labels').records.push([serial, name]);
this.labels.set(name, serial);
return serial;
}


toFile(file, resolve) {
this.db = new SqliteDatabaseAbstraction(file);
this.provisionDatabase()
Expand Down