From 352b13518c1ba37d475bf27b9b67d099a8f7c900 Mon Sep 17 00:00:00 2001 From: Sumit Jain Date: Sun, 26 May 2024 17:53:27 +0530 Subject: [PATCH 1/5] fix:get all whitelist API , remove all commented whitelist API functions --- commit/commit/code_analysis/apis.py | 107 ++++++++++++++++++++++++---- 1 file changed, 94 insertions(+), 13 deletions(-) diff --git a/commit/commit/code_analysis/apis.py b/commit/commit/code_analysis/apis.py index 483995b..c26ee1a 100644 --- a/commit/commit/code_analysis/apis.py +++ b/commit/commit/code_analysis/apis.py @@ -31,7 +31,8 @@ def find_all_occurrences_of_whitelist(path: str, app_name: str): ## Comment out later # if file.endswith('party.py'): - indexes,line_nos = find_indexes_of_whitelist(file_content, no_of_occurrences) + indexes,line_nos,no_of_occurrences = find_indexes_of_whitelist(file_content, no_of_occurrences) + api_count += no_of_occurrences apis = get_api_details(file, file_content, indexes,line_nos, path) api_details.extend(apis) @@ -43,17 +44,70 @@ def find_all_occurrences_of_whitelist(path: str, app_name: str): def find_indexes_of_whitelist(file_content: str, count: int): ''' - Find indexes of @frappe.whitelist in the file content + Find indexes of @frappe.whitelist in the file content, + ensuring it's not commented out or inside a string. ''' + def is_in_string_or_comment(file_content, index): + # State variables + in_single_quote = False + in_double_quote = False + in_comment = False + in_triple_single_quote = False + in_triple_double_quote = False + + i = 0 + while i < index: + char = file_content[i] + + # Handle triple single-quoted strings + if file_content[i:i+3] == "'''" and not in_double_quote: + if in_triple_single_quote: + in_triple_single_quote = False + i += 2 + else: + in_triple_single_quote = True + i += 2 + # Handle triple double-quoted strings + elif file_content[i:i+3] == '"""' and not in_single_quote: + if in_triple_double_quote: + in_triple_double_quote = False + i += 2 + else: + in_triple_double_quote = True + i += 2 + # Handle single-quoted strings + elif char == "'" and not in_double_quote and not in_triple_single_quote and not in_triple_double_quote: + in_single_quote = not in_single_quote + # Handle double-quoted strings + elif char == '"' and not in_single_quote and not in_triple_single_quote and not in_triple_double_quote: + in_double_quote = not in_double_quote + # Handle single-line comments + elif char == '#' and not in_single_quote and not in_double_quote and not in_triple_single_quote and not in_triple_double_quote: + in_comment = True + # Handle end of line for single-line comments + elif char == '\n': + in_comment = False + + i += 1 + + return in_single_quote or in_double_quote or in_comment or in_triple_single_quote or in_triple_double_quote + indexes = [] line_nos = [] - for i in range(count): - index = file_content.find('@frappe.whitelist', indexes[i - 1] + len('@frappe.whitelist') if i > 0 else 0) - indexes.append(index) - line_nos.append((file_content.count('\n', 0, index)+1)) - + actual_count = count - return indexes, line_nos + start = 0 + while actual_count > 0: + index = file_content.find('@frappe.whitelist', start) + if index == -1: + break + if not is_in_string_or_comment(file_content, index): + indexes.append(index) + line_nos.append(file_content.count('\n', 0, index) + 1) + actual_count -= 1 + start = index + len('@frappe.whitelist') + + return indexes, line_nos, actual_count def get_api_details(file, file_content: str, indexes: list,line_nos:list, path: str): ''' @@ -98,7 +152,10 @@ def get_whitelist_details(file_content: str, index: int): ''' whitelist_end_index = file_content.find(')', index) whitelisted_content = file_content[index:whitelist_end_index + 1] - args = whitelisted_content.split("(")[1].split(")")[0].split(",") + if "(" in whitelisted_content and ")" in whitelisted_content: + args = whitelisted_content.split("(")[1].split(")")[0].split(",") + else: + args = [] request_types = [] xss_safe = False allow_guest = False @@ -159,7 +216,10 @@ def extract_arguments_from_def(api_def: str): ''' Extract arguments from def ''' - arguments_with_types_defaults = api_def.split("(")[1].split(")")[0].split(",") + if "(" not in api_def or ")" not in api_def: + arguments_with_types_defaults = [] + else: + arguments_with_types_defaults = api_def.split("(")[1].split(")")[0].split(",") arguments = [] for arg in arguments_with_types_defaults: @@ -202,7 +262,28 @@ def find_function_end_lines(source_code: str,function_name:str): for node in ast.walk(tree): if isinstance(node, ast.FunctionDef): - end_line = node.end_lineno - function_end_lines[node.name] = end_line + decorators = get_decorators(node) + if 'whitelist' in decorators: + end_line = node.end_lineno + function_end_lines[node.name] = end_line + + return function_end_lines.get(function_name,0) - return function_end_lines.get(function_name,0) \ No newline at end of file +def get_decorator_name(node): + if isinstance(node, ast.Call): + if isinstance(node.func, ast.Name): + return node.func.id + elif isinstance(node.func, ast.Attribute): + return node.func.attr + elif isinstance(node, ast.Attribute): + return node.attr + else: + return None + +def get_decorators(node): + decorators = [] + for decorator in node.decorator_list: + decorator_name = get_decorator_name(decorator) + if decorator_name is not None: + decorators.append(decorator_name) + return decorators \ No newline at end of file From b60042b6c0f6a2fabce7a97f51dac4ad41148ebe Mon Sep 17 00:00:00 2001 From: Sumit Jain Date: Sun, 26 May 2024 17:54:37 +0530 Subject: [PATCH 2/5] feat:boot added and react-sdk package updated --- commit/commit/github_connect/connected_app.py | 7 ------- dashboard/index.html | 4 ++++ dashboard/package.json | 2 +- dashboard/yarn.lock | 3 ++- 4 files changed, 7 insertions(+), 9 deletions(-) delete mode 100644 commit/commit/github_connect/connected_app.py diff --git a/commit/commit/github_connect/connected_app.py b/commit/commit/github_connect/connected_app.py deleted file mode 100644 index 78bfa88..0000000 --- a/commit/commit/github_connect/connected_app.py +++ /dev/null @@ -1,7 +0,0 @@ -import frappe - -@frappe.whitelist(allow_guest=True) -def trial(): - connected_app = frappe.get_doc("Connected App", "fa306fb623") - return connected_app.initiate_web_application_flow() - \ No newline at end of file diff --git a/dashboard/index.html b/dashboard/index.html index 250ced6..2eaa979 100644 --- a/dashboard/index.html +++ b/dashboard/index.html @@ -17,6 +17,10 @@
+ diff --git a/dashboard/package.json b/dashboard/package.json index 84eaafd..d713012 100644 --- a/dashboard/package.json +++ b/dashboard/package.json @@ -40,7 +40,7 @@ "class-variance-authority": "^0.6.1", "clsx": "^1.2.1", "cmdk": "^0.2.0", - "frappe-react-sdk": "^1.2.8", + "frappe-react-sdk": "^1.7.0", "install": "^0.13.0", "lodash": "^4.17.21", "lodash.isplainobject": "^4.0.6", diff --git a/dashboard/yarn.lock b/dashboard/yarn.lock index f9fb09c..1aa5e8f 100644 --- a/dashboard/yarn.lock +++ b/dashboard/yarn.lock @@ -2646,7 +2646,7 @@ frappe-js-sdk@^1.5.0: dependencies: axios "^1.6.7" -frappe-react-sdk@^1.2.8: +frappe-react-sdk@^1.7.0: version "1.7.0" resolved "https://registry.yarnpkg.com/frappe-react-sdk/-/frappe-react-sdk-1.7.0.tgz#6d7430f2e606dbe733ffbe1fda7566fc815b6a45" integrity sha512-UBch8j7nWCkYWQjGG7tOds5tFif/hFdAbg8T+FlOiJAyFmtpbVdBOsWx2ime9aBkNa2U8zqjOkQLLIvL2/qOrg== @@ -4415,6 +4415,7 @@ stdin-discarder@^0.1.0: bl "^5.0.0" "string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0: + name string-width-cjs version "4.2.3" resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== From 372cb2722c16fa7dc53570b41934fdd7137742ac Mon Sep 17 00:00:00 2001 From: Sumit Jain Date: Sun, 26 May 2024 17:55:22 +0530 Subject: [PATCH 3/5] fix:socket issue by adding sitename --- dashboard/src/App.tsx | 12 +++++++++++- .../src/components/features/api_viewer/APIList.tsx | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/dashboard/src/App.tsx b/dashboard/src/App.tsx index 5ce8661..c9d2db2 100644 --- a/dashboard/src/App.tsx +++ b/dashboard/src/App.tsx @@ -8,8 +8,18 @@ import { CreateERD } from './pages/features/erd/meta/CreateERDForMeta' function App() { + const getSiteName = () => { + // @ts-ignore + if (window.frappe?.boot?.versions?.frappe && (window.frappe.boot.versions.frappe.startsWith('15') || window.frappe.boot.versions.frappe.startsWith('16'))) { + // @ts-ignore + return window.frappe?.boot?.sitename ?? import.meta.env.VITE_SITE_NAME + } + return import.meta.env.VITE_SITE_NAME + + } + return ( - + {/* */} diff --git a/dashboard/src/components/features/api_viewer/APIList.tsx b/dashboard/src/components/features/api_viewer/APIList.tsx index 95cce1f..964788d 100644 --- a/dashboard/src/components/features/api_viewer/APIList.tsx +++ b/dashboard/src/components/features/api_viewer/APIList.tsx @@ -65,7 +65,7 @@ export const APIList = ({ apiList, app_name, branch_name, setSelectedEndpoint }: {/* fixed height container */} -
+
From e928df2b9ea5868b48af73c023b219d04edd34e2 Mon Sep 17 00:00:00 2001 From: Sumit Jain Date: Sun, 26 May 2024 17:55:45 +0530 Subject: [PATCH 4/5] feat:create branch modal fix --- .../features/projects/Org/CreateOrgModal.tsx | 10 +- .../components/features/projects/Projects.tsx | 99 ++++++++----------- 2 files changed, 47 insertions(+), 62 deletions(-) diff --git a/dashboard/src/components/features/projects/Org/CreateOrgModal.tsx b/dashboard/src/components/features/projects/Org/CreateOrgModal.tsx index 8cb2a8c..14e1592 100644 --- a/dashboard/src/components/features/projects/Org/CreateOrgModal.tsx +++ b/dashboard/src/components/features/projects/Org/CreateOrgModal.tsx @@ -6,6 +6,8 @@ import { Label } from "@/components/ui/label" import { useFrappeCreateDoc } from 'frappe-react-sdk' import { useToast } from '@/components/ui/use-toast' import { DialogClose } from '@radix-ui/react-dialog' +import { KeyedMutator } from 'swr' +import { ProjectData } from '../Projects' type FormFields = { organization_name: string, @@ -14,16 +16,20 @@ type FormFields = { } -const CreateOrgModal = () => { +const CreateOrgModal = ({ mutate }: { + mutate: KeyedMutator<{ + message: ProjectData[]; + }> +}) => { const { toast } = useToast() const methods = useForm() const { createDoc, reset } = useFrappeCreateDoc() const onSubmit: SubmitHandler = (data) => { - // console.log(data); createDoc('Commit Organization', data) .then(() => { + mutate() reset() }).then(() => toast({ description: "Organization Added", diff --git a/dashboard/src/components/features/projects/Projects.tsx b/dashboard/src/components/features/projects/Projects.tsx index 065a799..936846c 100644 --- a/dashboard/src/components/features/projects/Projects.tsx +++ b/dashboard/src/components/features/projects/Projects.tsx @@ -7,7 +7,7 @@ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@ import { CommitProject } from "@/types/commit/CommitProject" import { CommitProjectBranch } from "@/types/commit/CommitProjectBranch" import { AvatarImage } from "@radix-ui/react-avatar" -import { useFrappeEventListener, useFrappeGetCall } from "frappe-react-sdk" +import { useFrappeGetCall } from "frappe-react-sdk" import { useEffect, useMemo, useState } from "react" import { AiOutlineApi } from "react-icons/ai" import { BsDatabase } from "react-icons/bs" @@ -35,19 +35,6 @@ export interface ProjectData extends CommitProjectBranch { } export const Projects = () => { - const [branchCreation, setBranchCreation] = useState<{ branch_name: string, project: string }[] | null>(null) - - useFrappeEventListener('creating_branch', (data) => { - console.log("fired") - setBranchCreation([branchCreation, ...data]) - }) - - useFrappeEventListener('branch_created', (data) => { - console.log("fired2") - const filtered = branchCreation?.filter((doc) => doc.branch_name !== data.branch_name) - setBranchCreation(filtered ?? null) - }) - const isCreateAccess = isSystemManager() const { data, error, isLoading, mutate } = useFrappeGetCall<{ message: ProjectData[] }>('commit.api.commit_project.commit_project.get_project_list_with_branches') @@ -70,29 +57,28 @@ export const Projects = () => { - - {isCreateAccess && + {isCreateAccess && + - - } - - + + } -
    +
      {data.message.map((org: ProjectData) => { - return + return })}
    @@ -102,17 +88,16 @@ export const Projects = () => { } -export const OrgComponent = ({ org, mutate, branchCreation }: { +export const OrgComponent = ({ org, mutate }: { org: ProjectData, mutate: KeyedMutator<{ message: ProjectData[]; - }>, branchCreation: { - branch_name: string; - project: string; - }[] | null + }> }) => { const isCreateAccess = isSystemManager() + const [createProject, setCreateProject] = useState(false) + return (
    @@ -122,7 +107,7 @@ export const OrgComponent = ({ org, mutate, branchCreation }: { {isCreateAccess &&
    - +
    }
    + + {org?.projects?.length === 0 &&
    No Projects Found, Click to add a new project. +
    }
    {org.projects.map((project => { return ( - + ) } ))}
    - ) } @@ -157,15 +145,11 @@ export interface ProjectCardProps { org: ProjectData mutate: KeyedMutator<{ message: ProjectData[]; - }>, - branchCreation: { - branch_name: string; - project: string; - }[] | null + }> } -export const ProjectCard = ({ project, org, mutate, branchCreation }: ProjectCardProps) => { +export const ProjectCard = ({ project, org, mutate }: ProjectCardProps) => { const navigate = useNavigate() @@ -180,14 +164,12 @@ export const ProjectCard = ({ project, org, mutate, branchCreation }: ProjectCar return project.display_name.split('_').map((word) => word[0]).join('').toUpperCase() }, [project]) - const creationBranchName = useMemo(() => { - const find = branchCreation?.find((doc) => doc.project === project.name) - if (find) return find.branch_name - return undefined - }, [branchCreation, project]) - const isCreateAccess = isSystemManager() + const [open, setOpen] = useState(false) + + const [selectOpen, setSelectOpen] = useState(false) + return (
  • @@ -208,9 +190,7 @@ export const ProjectCard = ({ project, org, mutate, branchCreation }: ProjectCar

    {org.organization_name}

    {project.description} - {creationBranchName && - - } + {/* */} @@ -219,13 +199,15 @@ export const ProjectCard = ({ project, org, mutate, branchCreation }: ProjectCar
    - + + + - + diff --git a/dashboard/src/components/features/projects/Projects.tsx b/dashboard/src/components/features/projects/Projects.tsx index 936846c..4d435f8 100644 --- a/dashboard/src/components/features/projects/Projects.tsx +++ b/dashboard/src/components/features/projects/Projects.tsx @@ -8,7 +8,7 @@ import { CommitProject } from "@/types/commit/CommitProject" import { CommitProjectBranch } from "@/types/commit/CommitProjectBranch" import { AvatarImage } from "@radix-ui/react-avatar" import { useFrappeGetCall } from "frappe-react-sdk" -import { useEffect, useMemo, useState } from "react" +import { useMemo, useState } from "react" import { AiOutlineApi } from "react-icons/ai" import { BsDatabase } from "react-icons/bs" import { MdAdd, MdAddBox } from "react-icons/md" @@ -190,9 +190,6 @@ export const ProjectCard = ({ project, org, mutate }: ProjectCardProps) => {

    {org.organization_name}

    {project.description} - - {/* */} - @@ -239,46 +236,3 @@ export const ProjectCard = ({ project, org, mutate }: ProjectCardProps) => {
  • ) } - - -const BranchCreationLoadingState = ({ branch_name }: { branch_name: string }) => { - - const [currentLoadingText, setCurrentLoadingText] = useState("") - const loadingTexts = [ - "May the forks be with you...", - "A commit a day keeps the mobs away...", - "Tickling the servers...", - "Why so serious?...", - "It's not you. It's me...", - "Counting backwards from Infinity...", - "Don't panic...", - "Don't break your screen yet!...", - "I swear it's almost done...", - "Let's take a mindfulness minute...", - ] - - useEffect(() => { - const changeInterval = setInterval(() => setCurrentLoadingText(loadingTexts[Math.floor(Math.random() * 10)]), 3000) - - return () => { - clearInterval(changeInterval); - } - }, []) - - return ( -
    -
    -
    -
    - {currentLoadingText} - {'Loading branch '} - {branch_name} -
    -
    - ) -} - -export default BranchCreationLoadingState -