-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into stagesetup
- Loading branch information
Showing
29 changed files
with
1,914 additions
and
14 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
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,61 @@ | ||
'use strict'; | ||
|
||
const boom = require('@hapi/boom'); | ||
const schema = require('screwdriver-data-schema'); | ||
const validator = require('screwdriver-template-validator').parsePipelineTemplate; | ||
const templateSchema = schema.api.templateValidator; | ||
|
||
module.exports = () => ({ | ||
method: 'POST', | ||
path: '/pipeline/template', | ||
options: { | ||
description: 'Create a new pipeline template', | ||
notes: 'Create a specific pipeline template', | ||
tags: ['api', 'pipelineTemplate'], | ||
auth: { | ||
strategies: ['token'], | ||
scope: ['build'] | ||
}, | ||
|
||
handler: async (request, h) => { | ||
const { pipelineTemplateVersionFactory, pipelineTemplateFactory } = request.server.app; | ||
|
||
const config = await validator(request.payload.yaml); | ||
|
||
if (config.errors.length > 0) { | ||
throw boom.badRequest(`Template has invalid format: ${config.errors.length} error(s).`, config.errors); | ||
} | ||
|
||
const pipelineTemplate = await pipelineTemplateFactory.get({ | ||
name: config.template.name, | ||
namespace: config.template.namespace | ||
}); | ||
|
||
const { isPR, pipelineId } = request.auth.credentials; | ||
|
||
// If template name exists, but this build's pipelineId is not the same as template's pipelineId | ||
// Then this build does not have permission to publish | ||
if (isPR || (pipelineTemplate && pipelineId !== pipelineTemplate.pipelineId)) { | ||
throw boom.forbidden('Not allowed to publish this template'); | ||
} | ||
|
||
const template = await pipelineTemplateVersionFactory.create( | ||
{ | ||
...config.template, | ||
pipelineId | ||
}, | ||
pipelineTemplateFactory | ||
); | ||
|
||
const location = new URL( | ||
`${request.path}/${template.id}`, | ||
`${request.server.info.protocol}://${request.headers.host}` | ||
).toString(); | ||
|
||
return h.response(template.toJson()).header('Location', location).code(201); | ||
}, | ||
validate: { | ||
payload: templateSchema.input | ||
} | ||
} | ||
}); |
Oops, something went wrong.