-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2094 from flowforge/2075-application-pipelines
Application Pipelines
- Loading branch information
Showing
45 changed files
with
2,925 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* Add the ProjectStacks table | ||
*/ | ||
const { DataTypes } = require('sequelize') | ||
|
||
module.exports = { | ||
up: async (context) => { | ||
await context.createTable('Pipelines', { | ||
id: { | ||
type: DataTypes.INTEGER, | ||
primaryKey: true, | ||
autoIncrement: true | ||
}, | ||
name: { type: DataTypes.STRING, allowNull: false }, | ||
|
||
createdAt: { type: DataTypes.DATE }, | ||
updatedAt: { type: DataTypes.DATE }, | ||
|
||
ApplicationId: { | ||
type: DataTypes.INTEGER, | ||
references: { model: 'Applications', key: 'id' }, | ||
onDelete: 'cascade', | ||
onUpdate: 'cascade' | ||
} | ||
}) | ||
|
||
await context.createTable('PipelineStages', { | ||
id: { | ||
type: DataTypes.INTEGER, | ||
primaryKey: true, | ||
autoIncrement: true | ||
}, | ||
name: { type: DataTypes.STRING, allowNull: false }, | ||
|
||
NextStageId: { | ||
type: DataTypes.INTEGER, | ||
allowNull: true, | ||
references: { model: 'PipelineStages', key: 'id' }, | ||
onDelete: 'SET NULL ', | ||
onUpdate: 'SET NULL ' | ||
}, | ||
|
||
PipelineId: { | ||
type: DataTypes.INTEGER, | ||
allowNull: false, | ||
references: { model: 'Pipelines', key: 'id' }, | ||
onDelete: 'cascade', | ||
onUpdate: 'cascade' | ||
}, | ||
|
||
createdAt: { type: DataTypes.DATE }, | ||
updatedAt: { type: DataTypes.DATE } | ||
}) | ||
|
||
await context.createTable('PipelineStageInstances', { | ||
InstanceId: { | ||
type: DataTypes.UUID, | ||
allowNull: false, | ||
references: { model: 'Projects', key: 'id' }, | ||
onDelete: 'cascade', | ||
onUpdate: 'cascade' | ||
}, | ||
PipelineStageId: { | ||
type: DataTypes.INTEGER, | ||
allowNull: false, | ||
references: { model: 'PipelineStages', key: 'id' }, | ||
onDelete: 'cascade', | ||
onUpdate: 'cascade' | ||
} | ||
}) | ||
}, | ||
down: async (context) => { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
module.exports = { | ||
addPipelineStage: async function (app, pipeline, options) { | ||
if (!options.instanceId) { | ||
throw new Error('Param instanceId is required when creating a new pipeline stage') | ||
} | ||
|
||
let source | ||
options.PipelineId = pipeline.id | ||
if (options.source) { | ||
// this gives us the input stage to this new stage. | ||
// we store "targets", so need to update the source to point to this new stage | ||
source = options.source | ||
delete options.source | ||
} | ||
const stage = await app.db.models.PipelineStage.create(options) | ||
await stage.addInstanceId(options.instanceId) | ||
|
||
if (source) { | ||
const sourceStage = await app.db.models.PipelineStage.byId(source) | ||
sourceStage.NextStageId = stage.id | ||
await sourceStage.save() | ||
} | ||
|
||
return stage | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const { | ||
DataTypes | ||
} = require('sequelize') | ||
|
||
module.exports = { | ||
name: 'Pipeline', | ||
schema: { | ||
name: { | ||
type: DataTypes.STRING, | ||
allowNull: false | ||
} | ||
}, | ||
associations: function (M) { | ||
this.belongsTo(M.Application) | ||
this.hasMany(M.PipelineStage) | ||
}, | ||
finders: function (M) { | ||
const self = this | ||
return { | ||
instance: { | ||
stages: async function () { | ||
return await M.PipelineStage.byPipeline(this.id) | ||
} | ||
}, | ||
static: { | ||
byId: async function (idOrHash) { | ||
let id = idOrHash | ||
if (typeof idOrHash === 'string') { | ||
id = M.Pipeline.decodeHashid(idOrHash) | ||
} | ||
return this.findOne({ | ||
where: { id } | ||
}) | ||
}, | ||
byApplicationId: async function (applicationId) { | ||
if (typeof applicationId === 'string') { | ||
applicationId = M.Application.decodeHashid(applicationId) | ||
} | ||
return self.findAll({ | ||
where: { | ||
ApplicationId: applicationId | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
const { | ||
DataTypes | ||
} = require('sequelize') | ||
|
||
module.exports = { | ||
name: 'PipelineStage', | ||
schema: { | ||
name: { | ||
type: DataTypes.STRING, | ||
allowNull: false | ||
}, | ||
|
||
NextStageId: { | ||
type: DataTypes.INTEGER, | ||
allowNull: true | ||
} | ||
}, | ||
options: { | ||
validate: { | ||
async instancesHaveSameApplication () { | ||
const instancesPromise = this.getInstances() | ||
const pipelinePromise = this.getPipeline() | ||
|
||
const instances = await instancesPromise | ||
const pipeline = await pipelinePromise | ||
|
||
instances.forEach((instance) => { | ||
if (instance.ApplicationId !== pipeline.ApplicationId) { | ||
throw new Error(`All instances on a pipeline stage, must be a member of the same application as the pipeline. ${instance.name} is not a member of application ${pipeline.ApplicationId}.`) | ||
} | ||
}) | ||
} | ||
} | ||
}, | ||
associations: function (M) { | ||
this.belongsTo(M.Pipeline) | ||
this.belongsToMany(M.Project, { through: M.PipelineStageInstance, as: 'Instances', otherKey: 'InstanceId' }) | ||
this.hasOne(M.PipelineStage, { as: 'NextStage', foreignKey: 'NextStageId', allowNull: true }) | ||
}, | ||
finders: function (M) { | ||
const self = this | ||
return { | ||
instance: { | ||
async addInstanceId (instanceId) { | ||
const instance = await M.Project.byId(instanceId) | ||
if (!instance) { | ||
throw new Error('instanceId not found') | ||
} | ||
|
||
await this.addInstance(instance) | ||
} | ||
}, | ||
static: { | ||
byId: async function (idOrHash) { | ||
let id = idOrHash | ||
if (typeof idOrHash === 'string') { | ||
id = M.PipelineStage.decodeHashid(idOrHash) | ||
} | ||
return this.findOne({ | ||
where: { id }, | ||
include: [ | ||
{ | ||
association: 'Instances', | ||
attributes: ['hashid', 'id', 'name', 'url', 'updatedAt'] | ||
} | ||
] | ||
}) | ||
}, | ||
byPipeline: async function (pipelineId) { | ||
if (typeof pipelineId === 'string') { | ||
pipelineId = M.Pipeline.decodeHashid(pipelineId) | ||
} | ||
return await self.findAll({ | ||
where: { | ||
PipelineId: pipelineId | ||
}, | ||
include: [ | ||
{ | ||
association: 'Instances', | ||
attributes: ['hashid', 'id', 'name', 'url', 'updatedAt'] | ||
} | ||
] | ||
}) | ||
}, | ||
byNextStage: async function (idOrHash) { | ||
let id = idOrHash | ||
if (typeof idOrHash === 'string') { | ||
id = M.PipelineStage.decodeHashid(idOrHash) | ||
} | ||
return this.findOne({ | ||
where: { NextStageId: id } | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* This is the many:1 association model between a Project and a PipelineStage | ||
* @namespace forge.db.models.PipelineStageInstance | ||
*/ | ||
|
||
module.exports = { | ||
name: 'PipelineStageInstance', | ||
options: { | ||
timestamps: false | ||
}, | ||
associations: function (M) { | ||
this.belongsTo(M.PipelineStage) | ||
this.belongsTo(M.Project, { as: 'Instance' }) // @TODO: need to guard that the instance is part of the same application that owns the stage | ||
}, | ||
meta: { | ||
slug: false, | ||
hashid: false, | ||
links: false | ||
} | ||
} |
Oops, something went wrong.