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
Extract milestones
  • Loading branch information
fabianhauser committed Mar 25, 2019
commit 93bcd756deeea55cb595de7c5b7b772de7c2f1ae
106 changes: 88 additions & 18 deletions src/output/sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const TABLE_MERGE_REQUESTS = 'merge_requests';
const TABLE_TIMES = 'times';
const TABLE_LABELS = 'labels';
const TABLE_LABEL_GROUP = 'label_group';
const TABLE_MILESTONES = 'milestones';

const COLUMNS = {
[TABLE_ISSUES]: 'issueColumns',
Expand Down Expand Up @@ -55,6 +56,10 @@ function getGeneralColumnType(columnName) {
return `REFERENCES ${TABLE_LABELS}(id)`;
}

if(columnName === 'milestone') {
return `REFERENCES ${TABLE_MILESTONES}(id)`;
}

// default:
return 'TEXT';
}
Expand Down Expand Up @@ -185,23 +190,49 @@ 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();

this.initSpecialTables();
}

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'],
[]
));
/**
* Initializes special tables like labels or milestones, which are extracted from the output tables.
*/
initSpecialTables() {
const issueColumns = this.config.get(COLUMNS[TABLE_ISSUES]);
const mergeRequestColumns = this.config.get(COLUMNS[TABLE_MERGE_REQUESTS]);
if(issueColumns.includes('labels') || mergeRequestColumns.includes('labels')) {
/**
* @type {Map<string, number>} Maps Labels to label group ids.
*/
this.labels = new Map();
this.labelGroupSerial = 0;
this.labelsSerial = 0;

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'],
[]
));
}

if(issueColumns.includes('milestone') || mergeRequestColumns.includes('milestone')) {
/**
* @type {Map<string, number>} Maps Milestones to ids
*/
this.milestones = new Map();
this.milestonesSerial = 0;

this.tables.set(TABLE_MILESTONES, new Table(
TABLE_MILESTONES,
['id', 'name'],
[]
));
}
}

makeStats() {
Expand All @@ -227,6 +258,13 @@ class Sqlite extends Base {
const table = this.makeTable(TABLE_ISSUES, this.report.issues);
const columns = this.config.get(COLUMNS[TABLE_ISSUES]);

if(columns.includes('milestone')) {
table.records.forEach(record => {
const columnIndex = columns.indexOf('milestone');
record[columnIndex] = this.parseMilestone(record[columnIndex]);
});
}

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

Expand Down Expand Up @@ -262,24 +300,38 @@ class Sqlite extends Base {
return table;
}

/**
* Creates a normalized table for milestones.
*
* @param milestone {string | number} Name of the milestone (or 0 if empty)
* @returns {number | null} ID of the milestone record.
*/
parseMilestone(milestone) {
if(milestone === 0) {
return null;
}

return this.getOrCreateMilestoneId(milestone);
}


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

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

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

return serial;
}
Expand All @@ -302,6 +354,24 @@ class Sqlite extends Base {
}


/**
* Creates a Milestone (or fetches the existing one) and returns the ID.
*
* @param milestone {string}
* @returns {number}
*/
getOrCreateMilestoneId(milestone) {
if(this.milestones.has(milestone)) {
return this.milestones.get(milestone);
}

const serial = this.milestonesSerial++;
this.tables.get('milestones').records.push([serial, milestone]);
this.milestones.set(milestone, serial);
return serial;
}


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