Skip to content

Commit

Permalink
Fixes for project import/update
Browse files Browse the repository at this point in the history
Made checking for project permissions only apply to external calls
  • Loading branch information
barankyle committed Oct 13, 2023
1 parent ede2524 commit 9786e89
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Ethereal Engine. All Rights Reserved.

import { BadRequest, Forbidden } from '@feathersjs/errors'
import { HookContext, Paginated } from '@feathersjs/feathers'
import { Application } from '../../declarations'

import { GITHUB_URL_REGEX } from '@etherealengine/common/src/constants/GitHubConstants'

Expand All @@ -41,7 +42,7 @@ import { UserType } from '@etherealengine/engine/src/schemas/user/user.schema'
import { checkUserRepoWriteStatus } from '../projects/project/github-helper'

export default (writeAccess) => {
return async (context: HookContext): Promise<HookContext> => {
return async (context: HookContext<Application>) => {
const { params, app } = context
if (context.params.isInternal) return context
const loggedInUser = params.user as UserType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ const ensureInviteCode = async (context: HookContext<ProjectPermissionService>)
const data: ProjectPermissionData[] = Array.isArray(context.data) ? context.data : [context.data]

if (data[0].inviteCode && USER_ID_REGEX.test(data[0].inviteCode)) {
context.data[0].userId = data[0].inviteCode as UserID
delete context.data[0].inviteCode
data[0].userId = data[0].inviteCode as UserID
delete data[0].inviteCode
}
if (data[0].userId && INVITE_CODE_REGEX.test(data[0].userId)) {
context.data[0].inviteCode = data[0].userId
delete context.data[0].userId
data[0].inviteCode = data[0].userId
delete data[0].userId
}
context.data = data[0]
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/server-core/src/projects/project/github-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ const uploadToRepo = async (
//Create the new commit with all of the file changes
const newCommit = await createNewCommit(octo, org, repo, commitMessage, newTree.sha, currentCommit.commitSha)

await app.service(projectPath)._patch(project.id, { commitSHA: newCommit.sha, commitDate: toDateTimeSql(new Date()) })
await app.service(projectPath).patch(project.id, { commitSHA: newCommit.sha, commitDate: toDateTimeSql(new Date()) })

try {
//This pushes the commit to the main branch in GitHub
Expand Down
48 changes: 24 additions & 24 deletions packages/server-core/src/projects/project/project-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import { ProjectCheckSourceDestinationMatchType } from '@etherealengine/engine/s
import { ProjectCheckUnfetchedCommitType } from '@etherealengine/engine/src/schemas/projects/project-check-unfetched-commit.schema'
import { ProjectCommitType } from '@etherealengine/engine/src/schemas/projects/project-commits.schema'
import { ProjectDestinationCheckType } from '@etherealengine/engine/src/schemas/projects/project-destination-check.schema'
import { projectPermissionPath } from '@etherealengine/engine/src/schemas/projects/project-permission.schema'
import {
projectPath,
ProjectSettingType,
Expand Down Expand Up @@ -1509,7 +1508,7 @@ export const updateProject = async (

const returned = !existingProject
? // Add to DB
await app.service(projectPath)._create(
await app.service(projectPath).create(
{
id: v4(),
name: projectName,
Expand All @@ -1527,25 +1526,22 @@ export const updateProject = async (
},
params || {}
)
: await app.service(projectPath).patch(existingProject.id, {
commitSHA,
commitDate: toDateTimeSql(commitDate),
sourceRepo: data.sourceURL,
sourceBranch: data.sourceBranch,
updateType: data.updateType,
updateSchedule: data.updateSchedule,
updateUserId: userId
})
: await app.service(projectPath).patch(
existingProject.id,
{
commitSHA,
commitDate: toDateTimeSql(commitDate),
sourceRepo: data.sourceURL,
sourceBranch: data.sourceBranch,
updateType: data.updateType,
updateSchedule: data.updateSchedule,
updateUserId: userId
},
params
)

returned.needsRebuild = typeof data.needsRebuild === 'boolean' ? data.needsRebuild : true

if (!existingProject) {
await app.service(projectPermissionPath).create({
projectId: returned.id,
userId
})
}

if (returned.name !== projectName)
await app.service(projectPath).patch(existingProject!.id, {
name: projectName
Expand All @@ -1558,10 +1554,14 @@ export const updateProject = async (
await git.raw(['lfs', 'fetch', '--all'])
await git.push('destination', branchName, ['-f', '--tags'])
const { commitSHA, commitDate } = await getCommitSHADate(projectName)
await app.service(projectPath).patch(returned.id, {
commitSHA,
commitDate: toDateTimeSql(commitDate)
})
await app.service(projectPath).patch(
returned.id,
{
commitSHA,
commitDate: toDateTimeSql(commitDate)
},
params
)
}
// run project install script
if (projectConfig.onEvent) {
Expand All @@ -1570,9 +1570,9 @@ export const updateProject = async (

const k8BatchClient = getState(ServerState).k8BatchClient

if (k8BatchClient && (data.updateType === 'tag' || data.updateType === 'commit')) {
if (k8BatchClient && (data.updateType === 'tag' || data.updateType === 'commit'))
await createOrUpdateProjectUpdateJob(app, projectName)
} else if (k8BatchClient && (data.updateType === 'none' || data.updateType == null))
else if (k8BatchClient && (data.updateType === 'none' || data.updateType == null))
await removeProjectUpdateJob(app, projectName)

if (params?.jobId) {
Expand Down
47 changes: 22 additions & 25 deletions packages/server-core/src/projects/project/project.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,24 +266,26 @@ const checkIfNameIsValid = async (context: HookContext<ProjectService>) => {
*/
const uploadLocalProject = async (context: HookContext<ProjectService>) => {
const projectLocalDirectory = path.resolve(projectsRootFolder, context.projectName)
copyFolderRecursiveSync(templateFolderDirectory, projectsRootFolder)
fs.renameSync(path.resolve(projectsRootFolder, 'template-project'), projectLocalDirectory)
if (!fs.existsSync(projectLocalDirectory)) {
copyFolderRecursiveSync(templateFolderDirectory, projectsRootFolder)
fs.renameSync(path.resolve(projectsRootFolder, 'template-project'), projectLocalDirectory)

fs.mkdirSync(path.resolve(projectLocalDirectory, '.git'), { recursive: true })
fs.mkdirSync(path.resolve(projectLocalDirectory, '.git'), { recursive: true })

const git = useGit(path.resolve(projectLocalDirectory, '.git'))
try {
await git.init(true)
} catch (e) {
logger.warn(e)
}
const git = useGit(path.resolve(projectLocalDirectory, '.git'))
try {
await git.init(true)
} catch (e) {
logger.warn(e)
}

const packageData = Object.assign({}, templateProjectJson) as any
packageData.name = context.projectName
packageData.etherealEngine.version = getEnginePackageJson().version
fs.writeFileSync(path.resolve(projectLocalDirectory, 'package.json'), JSON.stringify(packageData, null, 2))
const packageData = Object.assign({}, templateProjectJson) as any
packageData.name = context.projectName
packageData.etherealEngine.version = getEnginePackageJson().version
fs.writeFileSync(path.resolve(projectLocalDirectory, 'package.json'), JSON.stringify(packageData, null, 2))

await uploadLocalProjectToProvider(context.app, context.projectName, false)
await uploadLocalProjectToProvider(context.app, context.projectName, false)
}
}

/**
Expand Down Expand Up @@ -328,7 +330,7 @@ const linkGithubToProject = async (context: HookContext) => {
if (githubIdentityProvider.data.length === 0)
throw new Error('Must be logged in with GitHub to link a project to a GitHub repo')
const split = githubPathRegexExec[2].split('/')
const org = split
const org = split[0]
const repo = split[1].replace('.git', '')
const appOrgAccess = await checkAppOrgStatus(org, githubIdentityProvider.data[0].oauthToken)
if (!appOrgAccess)
Expand Down Expand Up @@ -485,9 +487,7 @@ const removeProjectUpdate = async (context: HookContext<ProjectService>) => {
* 1. Clones the repo to the local FS
* 2. If in production mode, uploads it to the storage provider
* 3. Creates a database entry
* @param data
* @param placeholder This is where data normally goes, but we've put data as the first parameter
* @param params
* @param context Hook context
* @returns
*/
const updateProjectJob = async (context: HookContext) => {
Expand All @@ -497,7 +497,7 @@ const updateProjectJob = async (context: HookContext) => {

const data: ProjectBuildUpdateItemType = context.data as ProjectBuildUpdateItemType
if (!config.kubernetes.enabled || context.params?.isJob)
context.result = updateProject(context.app, context.data, context.params)
context.result = await updateProject(context.app, context.data, context.params)
else {
const urlParts = data.sourceURL.split('/')
let projectName = data.name || urlParts.pop()
Expand Down Expand Up @@ -559,21 +559,18 @@ export default {
updateCreateData
],
update: [
iff(isProvider('external'), verifyScope('editor', 'write')),
projectPermissionAuthenticate(false),
iff(isProvider('external'), verifyScope('editor', 'write'), projectPermissionAuthenticate(false)),
() => schemaHooks.validateData(projectPatchValidator),
updateProjectJob
],
patch: [
iff(isProvider('external'), verifyScope('editor', 'write')),
projectPermissionAuthenticate(false),
iff(isProvider('external'), verifyScope('editor', 'write'), projectPermissionAuthenticate(false)),
() => schemaHooks.validateData(projectPatchValidator),
schemaHooks.resolveData(projectPatchResolver),
iff(isProvider('external'), linkGithubToProject)
],
remove: [
iff(isProvider('external'), verifyScope('editor', 'write')),
projectPermissionAuthenticate(false),
iff(isProvider('external'), verifyScope('editor', 'write'), projectPermissionAuthenticate(false)),
getProjectName,
runProjectUninstallScript,
removeProjectFiles,
Expand Down
13 changes: 6 additions & 7 deletions packages/server-core/src/projects/project/project.resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,12 @@ export const projectDbToSchema = (rawData: ProjectDatabaseType): ProjectType =>
export const projectResolver = resolve<ProjectType, HookContext>(
{
projectPermissions: virtual(async (project, context) => {
if (context.params?.query?.allowed)
return (await context.app.service(projectPermissionPath).find({
query: {
projectId: project.id
},
paginate: false
})) as any as ProjectPermissionType[]
return (await context.app.service(projectPermissionPath).find({
query: {
projectId: project.id
},
paginate: false
})) as any as ProjectPermissionType[]
}),

commitDate: virtual(async (project) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/server-core/src/projects/project/project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const cleanup = async (app: Application) => {
const projectDir = path.resolve(appRootPath.path, `packages/projects/projects/${newProjectName}/`)
deleteFolderRecursive(projectDir)
try {
await app.service(projectPath)._remove(null, { query: { name: newProjectName } })
await app.service(projectPath).remove(null, { query: { name: newProjectName } })
} catch (e) {
//
}
Expand Down

0 comments on commit 9786e89

Please sign in to comment.