11const { Op } = require ( 'sequelize' ) ;
22const { toInt } = require ( '../utils' ) ;
3+ const { buildEnum } = require ( '../utils' ) ;
4+ const { buildSourceCodeUrl } = require ( '../utils/site' ) ;
35const {
46 isEmptyOrBranch,
57 isEmptyOrUrl,
68 isValidSubdomain,
79} = require ( '../utils/validators' ) ;
810
11+ const SOURCE_CODE_PLATFORM_GITHUB = 'github' ;
12+ const SOURCE_CODE_PLATFORM_WORKSHOP = 'workshop' ;
13+
14+ const Platforms = buildEnum ( [ SOURCE_CODE_PLATFORM_GITHUB , SOURCE_CODE_PLATFORM_WORKSHOP ] ) ;
15+ const DEFAULT_SOURCE_CODE_PLATFORM = Platforms . Github ;
16+
917const afterValidate = ( site ) => {
1018 if ( site . defaultBranch === site . demoBranch ) {
1119 const error = new Error ( 'Default branch and demo branch cannot be the same' ) ;
@@ -27,6 +35,33 @@ const validationFailed = (site, options, validationError) => {
2735 throw error ;
2836} ;
2937
38+ function defaultSourceCodePlatform ( site ) {
39+ if ( ! site . sourceCodePlatform ) {
40+ site . sourceCodePlatform = DEFAULT_SOURCE_CODE_PLATFORM ;
41+ }
42+ }
43+
44+ function defaultSourceCodeUrl ( site ) {
45+ if ( ! site . sourceCodeUrl ) {
46+ site . sourceCodeUrl = buildSourceCodeUrl (
47+ site . owner ,
48+ site . repository ,
49+ site . sourceCodePlatform ,
50+ Platforms . GitHub ,
51+ ) ;
52+ }
53+ }
54+
55+ const beforeCreate = ( site ) => {
56+ defaultSourceCodePlatform ( site ) ;
57+ defaultSourceCodeUrl ( site ) ;
58+ } ;
59+
60+ const beforeUpdate = ( site ) => {
61+ defaultSourceCodePlatform ( site ) ;
62+ defaultSourceCodeUrl ( site ) ;
63+ } ;
64+
3065const associate = ( {
3166 Build,
3267 Domain,
@@ -133,6 +168,9 @@ const beforeValidate = (site) => {
133168 if ( site . owner ) {
134169 site . owner = site . owner . toLowerCase ( ) ;
135170 }
171+
172+ defaultSourceCodePlatform ( site ) ;
173+ defaultSourceCodeUrl ( site ) ;
136174} ;
137175
138176async function getOrgUsers ( ) {
@@ -267,13 +305,28 @@ module.exports = (sequelize, DataTypes) => {
267305 webhookId : {
268306 type : DataTypes . INTEGER ,
269307 } ,
308+ sourceCodePlatform : {
309+ type : DataTypes . ENUM ,
310+ values : Platforms . values ,
311+ defaultValue : Platforms . Github ,
312+ allowNull : false ,
313+ validate : {
314+ isIn : [ Platforms . values ] ,
315+ } ,
316+ } ,
317+ sourceCodeUrl : {
318+ type : DataTypes . STRING ,
319+ allowNull : false ,
320+ } ,
270321 } ,
271322 {
272323 tableName : 'site' ,
273324 hooks : {
274325 beforeValidate,
275326 afterValidate,
276327 validationFailed,
328+ beforeCreate,
329+ beforeUpdate,
277330 } ,
278331 paranoid : true ,
279332 } ,
@@ -296,5 +349,6 @@ module.exports = (sequelize, DataTypes) => {
296349 Site . branchFromContext = ( context ) =>
297350 context === 'site' ? 'defaultBranch' : 'demoBranch' ;
298351 Site . prototype . getOrgUsers = getOrgUsers ;
352+ Site . Platforms = Platforms ;
299353 return Site ;
300354} ;
0 commit comments