Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create builds for branches and commit hashes #26

Open
wants to merge 5 commits into
base: feature/node-12
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 4 additions & 2 deletions defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ codeHostingHandlers:
gitlab: 'http://localhost:3013'
bitbucket: 'http://localhost:3012'

perBranchBuildLimit: 1
perBranchBuildLimit: 3

reaperCriteria:
pullRequest:
open:
max: 1
max: 2
maxAge: ''
closed:
max: 0
branch:
max: 2

limitRuleExclutions: []
# Allow any build where build.project.id == 1234 to escape normal restrictions.
Expand Down
12 changes: 0 additions & 12 deletions lib/ContainerManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,6 @@ class ContainerManager {
return new Promise(resolve => {

buildStream.pipe(through2.obj(function(build, enc, callback) {
if (build.container) {
this.push(_.pick(build, [
'id',
'ref',
'branch',
'pullRequest',
'project',
'container',
'createdAt',
'pinned',
]));
}

builds.push(build);

Expand Down
2 changes: 1 addition & 1 deletion lib/ReaperCLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ReaperCLI extends Reaper {
console.log(`DRY RUN: container ${build.id} NOT being removed`);
}
else {
return this._deleteBuild(build);
this._deleteBuild(build);
}
}
}
Expand Down
92 changes: 81 additions & 11 deletions lib/Transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ class Transform {
* id: 2,
* builds: [...],
* }
* ],
* branches: [
* {
* branch: 'master',
* builds: [...],
* }
* ]
* }
* }
Expand All @@ -56,31 +62,67 @@ class Transform {

// Adds the pull request object to the project.
project.pullRequests = {};

// Adds the branches object to the project (builds for active branches).
project.branches = {};
}

const branch = typeof build.branch == 'object' ? build.branch.name : build.branch + '';
if (!project.pullRequests[branch]) {
project.pullRequests[branch] = {
branch: branch,
state: 'open',
id: build.pullRequest.number,
diskUsage: 0,
builds: [],
};
}

project.pullRequests[branch].diskUsage += build.diskSpace.realBytes;
// Builds before https://github.com/ProboCI/probo-reaper/pull/26 did not
// have a type, so they were pull_request builds.
if (!build.type || build.type === 'pull_request') {

if (!project.pullRequests[branch]) {
project.pullRequests[branch] = {
branch: branch,
state: 'open',
id: build.pullRequest.number,
diskUsage: 0,
builds: [],
};
}

project.pullRequests[branch].builds.push(build);
project.pullRequests[branch].diskUsage += build.diskSpace.realBytes;

project.pullRequests[branch].builds.push(build);
}
// Else it's a branch build.
else {
if (!project.branches[branch]) {
project.branches[branch] = {
branch: branch,
diskUsage: 0,
builds: [],
};
}

project.branches[branch].diskUsage += build.diskSpace.realBytes;

project.branches[branch].builds.push(build);
}
}

return this.processProjects(projects);
}

/**
* Transforms project.branches and project.pullRequest objects into arrays
* and set the state of the pull requests (open or closed). This also sorts
* the builds for each branch/pull request by creation date.
*/
async processProjects(projects) {
for (let project in projects) {
if (projects.hasOwnProperty(project)) {

let pullRequests = projects[project].pullRequests;
let branches = projects[project].branches;

// Sorts builds, sets state, and transform PRs object to array.
projects[project].pullRequests = await this.processPullRequests(projects[project], pullRequests);

// Sorts builds, and transform branches object to array.
projects[project].branches = await this.processBranches(branches);
}
}

Expand Down Expand Up @@ -118,6 +160,34 @@ class Transform {
return prArray;
}

/**
* Transforms the branches object into an array and sorts the builds for each
* branch by creation date.
*
* @param {Object.<string, any>} branches - The object of pull branches
* including builds for each branch.
*/
processBranches(branches) {
let branchesArray = [];

for (let branchName in branches) {

if (branches.hasOwnProperty(branchName)) {

let branch = branches[branchName];

// Sorts the builds by descending start date.
branch.builds.sort(utils.sortBuildsByDateDesc);


branchesArray.push(branch);
}

}

return branchesArray;
}

}

module.exports = Transform;
24 changes: 24 additions & 0 deletions lib/criteria.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ function applyCriteria(project, criteria) {
throw new TypeError('project and criteria are required.');
}

if (criteria.branch) {
project.branches.forEach(function(branch) {
result = applyBranchCriteria(project, branch, criteria, result);
});
}

if (criteria.pullRequest) {
PR_STATES.forEach(function(state) {
result = applyPullRequestCriteria(project, state, criteria, result);
Expand All @@ -51,6 +57,24 @@ function applyCriteria(project, criteria) {
return result;
}

function applyBranchCriteria(project, branch, criteria, result) {
let max;
try {
max = criteria.branch.max;
}
catch (e) {
return result;
}

if (!Number.isNaN(max)) {
console.log('Applying Branch max:', max);
console.log('Branch:', branch.branch);
result = merge(result, applyMax(branch.builds, max, reapedReasons.REAPED_REASON_BRANCH_BUILDS));
}

return result;
}

function applyPullRequestCriteria(project, state, criteria, result) {
let stateCriteria = criteria.pullRequest[state];
let max = Number(stateCriteria.max);
Expand Down
3 changes: 2 additions & 1 deletion lib/reaper.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ class Reaper {
this.printer.printProject(project);
this.printer.printPRs(project.pullRequests);

// Returns {remove, keep} arrays of container IDs to remove and keep, respectively
// Returns {remove, keep} arrays of container IDs to remove and keep,
// respectively
let containerActions = criteria.apply(project, this.criteria);

projectActions.push(containerActions);
Expand Down
Loading