Skip to content

Commit

Permalink
squash! Clear out gitea access in the frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdulrhmnGhanem committed Feb 16, 2023
1 parent 7958a66 commit c086ab3
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 99 deletions.
33 changes: 5 additions & 28 deletions frontend/src/components/SharedProjectPage/elements.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ import Readme from '@components/Board/Readme'
const PageElements = ({
assetPath,
repoName,
hasUploadPermission,
hasIBOM,
kitspaceYAML,
bomInfo,
zipUrl,
readme,
boardSpecs,
previewOnly,
description,
projectName,
projectFullname,
Expand All @@ -28,18 +26,6 @@ const PageElements = ({
readmeExists,
boardShowcaseAssetsExist,
}) => {
const AssetPlaceholderWithUploadPermissions = ({ asset }) => (
<AssetPlaceholder
asset={asset}
hasUploadPermission={hasUploadPermission}
previewOnly={previewOnly}
/>
)

AssetPlaceholderWithUploadPermissions.propTypes = {
asset: string.isRequired,
}

return (
<>
<InfoBar
Expand All @@ -59,7 +45,7 @@ const PageElements = ({
/>
</>
) : (
<AssetPlaceholderWithUploadPermissions asset="board" />
<AssetPlaceholder asset="board" />
)}
</div>
<div>
Expand All @@ -70,7 +56,7 @@ const PageElements = ({
zipUrl={zipUrl}
/>
) : (
<AssetPlaceholderWithUploadPermissions asset="gerber files" />
<AssetPlaceholder asset="gerber files" />
)}
{bomInfoExists ? (
<BuyParts
Expand All @@ -79,29 +65,23 @@ const PageElements = ({
projectFullName={projectFullname}
/>
) : (
<AssetPlaceholderWithUploadPermissions asset="bill of materials" />
<AssetPlaceholder asset="bill of materials" />
)}
</div>
<div>
{readmeExists ? (
<Readme renderedReadme={readme} />
) : (
<AssetPlaceholderWithUploadPermissions asset="README" />
<AssetPlaceholder asset="README" />
)}
</div>
</>
)
}

const AssetPlaceholder = ({ asset, hasUploadPermission, previewOnly }) => {
const AssetPlaceholder = ({ asset }) => {
let message = `No ${asset} files were found`

if (hasUploadPermission && previewOnly) {
message += ', commit files to the original repo and it will be synced'
} else if (hasUploadPermission && !previewOnly) {
message += ', upload some'
}

return (
<div
style={{
Expand All @@ -122,13 +102,10 @@ const AssetPlaceholder = ({ asset, hasUploadPermission, previewOnly }) => {

AssetPlaceholder.propTypes = {
asset: string.isRequired,
hasUploadPermission: bool.isRequired,
previewOnly: bool.isRequired,
}

PageElements.propTypes = {
assetPath: string.isRequired,
hasUploadPermission: bool.isRequired,
hasIBOM: bool.isRequired,
kitspaceYAML: shape({
summary: string,
Expand Down
13 changes: 1 addition & 12 deletions frontend/src/components/SharedProjectPage/index.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useState } from 'react'
import { useRouter } from 'next/router'
import { Loader, Message } from 'semantic-ui-react'
import { Loader } from 'semantic-ui-react'

import Page from '@components/Page'
import { useMigrationStatus } from '@hooks/Gitea'
Expand Down Expand Up @@ -73,16 +73,6 @@ const SharedProjectPage = props => {

return (
<Page title={title}>
{props.isSynced && props.hasUploadPermission ? (
<Message color="yellow" data-cy="sync-msg">
<Message.Header>A synced repository!</Message.Header>
<Message.Content>
<p>Files uploading isn&apos;t supported for synced repositories.</p>
Please commit files to the original git repository and it will be synced
automatically.
</Message.Content>
</Message>
) : null}
<PageElements
{...props}
assetPath={`${props.rootAssetPath}/${props.projectName}`}
Expand All @@ -104,7 +94,6 @@ SharedProjectPage.propTypes = {
isEmpty: bool.isRequired,
finishedProcessing: bool.isRequired,
isSynced: bool.isRequired,
hasUploadPermission: bool.isRequired,
}

export default SharedProjectPage
9 changes: 2 additions & 7 deletions frontend/src/pages/[user]/[repo]/[project]/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import { bool } from 'prop-types'
import getConfig from 'next/config'

import { canCommit, getRepo, repoExists } from '@utils/giteaApi'
import { getRepo, repoExists } from '@utils/giteaApi'
import {
getBoardBomInfo,
getBoardGerberInfo,
Expand All @@ -19,14 +19,13 @@ const assetUrl = getConfig().publicRuntimeConfig.KITSPACE_ASSET_URL
const MultiProjectPage = props =>
props.notFound ? <Custom404 /> : <SharedProjectPage {...props} />

MultiProjectPage.getInitialProps = async ({ query, req = null, res = null }) => {
MultiProjectPage.getInitialProps = async ({ query, res = null }) => {
const { user: username, repo: repoName, project: projectName } = query

const repoFullName = `${username}/${repoName}`

const rootAssetPath = `${assetUrl}/files/${repoFullName}/HEAD`
const assetPath = `${rootAssetPath}/${projectName}`
const session = req?.session ?? JSON.parse(sessionStorage.getItem('session'))

const exists = await repoExists(repoFullName)
if (exists) {
Expand All @@ -38,7 +37,6 @@ MultiProjectPage.getInitialProps = async ({ query, req = null, res = null }) =>
[kitspaceYAMLExists, kitspaceYamlArray],
finishedProcessing,
hasIBOM,
hasUploadPermission,
] = await Promise.all([
getRepo(repoFullName),
getReadme(assetPath),
Expand All @@ -47,8 +45,6 @@ MultiProjectPage.getInitialProps = async ({ query, req = null, res = null }) =>
getKitspaceYamlArray(rootAssetPath),
getIsProcessingDone(rootAssetPath),
hasInteractiveBom(assetPath),
// The repo owner and collaborators can upload files.
canCommit(repoFullName, session.user?.username, session.apiToken),
])

const kitspaceYAML = kitspaceYamlArray.find(p => p.name === projectName)
Expand All @@ -68,7 +64,6 @@ MultiProjectPage.getInitialProps = async ({ query, req = null, res = null }) =>
rootAssetPath,
repo,
projectFullname: repoFullName,
hasUploadPermission,
hasIBOM,
kitspaceYAML,
zipUrl,
Expand Down
11 changes: 3 additions & 8 deletions frontend/src/pages/_app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import 'semantic-ui-css/components/dimmer.min.css'
import 'semantic-ui-css/components/table.min.css'
import 'semantic-ui-css/components/progress.min.css'

import AuthProvider from '@contexts/AuthContext'
import './_app.scss'

if (typeof window !== 'undefined') {
Expand All @@ -42,7 +41,7 @@ if (typeof window !== 'undefined') {
}
}

function KitspaceApp({ Component, pageProps, session, isStaticFallback }) {
function KitspaceApp({ Component, pageProps, isStaticFallback }) {
const setStaticFallback = isStaticFallback ? (
<script
// using dangerouslySetInnerHTML to avoid browser parsing errors
Expand All @@ -57,25 +56,21 @@ function KitspaceApp({ Component, pageProps, session, isStaticFallback }) {
isStaticFallback = isStaticFallback || window.isStaticFallback
}
return (
<AuthProvider initialSession={session}>
<>
<Head>{setStaticFallback}</Head>
<Component {...pageProps} />
{isStaticFallback ? <ErrorMessage /> : null}
</AuthProvider>
</>
)
}

KitspaceApp.getInitialProps = async appContext => {
const appProps = await App.getInitialProps(appContext)

const { isStaticFallback } = appContext.ctx.query
const session =
appContext.ctx?.req?.session ??
JSON.parse(window?.sessionStorage.getItem('session'))

return {
...appProps,
session,
isStaticFallback,
}
}
Expand Down
44 changes: 0 additions & 44 deletions frontend/src/utils/giteaApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,47 +48,3 @@ export const userExists = async username => {

return res.ok
}

/**
* Check if a user is a collaborator in a Gitea repo.
* @param {string} repo
* @param {string} username
* @param apiToken {string}
* @returns {Promise<boolean>}
*/
const isCollaborator = async (repo, username, apiToken) => {
if (username == null) {
return false
}

const endpoint = `${giteaApiUrl}/repos/${repo}/collaborators`

const res = await fetch(endpoint, {
method: 'GET',
mode,
credentials,
headers: {
...headers,
Authorization: `auth ${apiToken}`,
},
})

if (res.ok) {
const collaborators = await res.json()
return collaborators.find(x => x.login === username) != null
}

return false
}

/**
* Check if a user can commit to a Gitea repo.
* @param {string} repo
* @param {string} username
* @param apiToken {string}
* @returns
*/
export const canCommit = async (repo, username, apiToken) => {
const repoOwner = repo.split('/')[0]
return repoOwner === username || isCollaborator(repo, username, apiToken)
}

0 comments on commit c086ab3

Please sign in to comment.