Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: delete Org, Project and Manage Branches #39

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from commit.commit.code_analysis.apis import find_all_occurrences_of_whitelist
from commit.commit.code_analysis.doctypes import get_doctypes_in_module, get_doctype_json
from frappe.utils import now
from frappe.app import handle_exception
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this import



class CommitProjectBranch(Document):
Expand Down Expand Up @@ -46,8 +47,7 @@ def clone_repo(self):
# print("Folder path", folder_path)
# print("Repo url", repo_url)
# print("Branch name", self.branch_name)
repo = git.Repo.clone_from(
repo_url, folder_path, branch=self.branch_name, single_branch=True)
repo = git.Repo.clone_from(repo_url, folder_path, branch=self.branch_name, single_branch=True)
# print("Cloned repo")
self.last_fetched = frappe.utils.now_datetime()
self.commit_hash = repo.head.object.hexsha
Expand Down Expand Up @@ -158,18 +158,36 @@ def background_fetch_process(project_branch):
'is_completed': True
}, user=frappe.session.user)



except frappe.DoesNotExistError:
except Exception as e:
# throw the error and delete the document
frappe.throw("Project Branch not found")
messages = [json.dumps({'message' :'There was an error while fetching branch repo.'})]
frappe.clear_messages()
frappe.publish_realtime('commit_branch_creation_error',
{
'branch_name': doc.branch_name,
'project': doc.project,
'error':{
"exception": frappe.get_traceback(),
"_server_messages": json.dumps(messages),
},
# 'response': handle_exception(e),
'is_completed': False
}, user=frappe.session.user)

frappe.delete_doc("Commit Project Branch", project_branch)
# frappe.throw("Project Branch not found")
frappe.log(frappe.get_traceback())

# raise e


@frappe.whitelist(allow_guest=True)
def fetch_repo(doc):
doc = json.loads(doc)
project_branch = frappe.get_doc("Commit Project Branch", doc.get("name"))
def fetch_repo(doc, name = None):
if name :
project_branch = frappe.get_doc("Commit Project Branch", name)
else:
doc = json.loads(doc)
project_branch = frappe.get_doc("Commit Project Branch", doc.get("name"))
project_branch.fetch_repo()
project_branch.save()
return "Hello"
Expand Down
8 changes: 6 additions & 2 deletions commit/public/dashboard/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
<meta name="msapplication-TileColor" content="#da532c">
<meta name="theme-color" content="#ffffff">
<title>Commit - Developer tooling for the Frappeverse</title>
<script type="module" crossorigin src="/assets/commit/dashboard/assets/index-2eacee2d.js"></script>
<link rel="stylesheet" href="/assets/commit/dashboard/assets/index-31456e03.css">
<script type="module" crossorigin src="/assets/commit/dashboard/assets/index-63f9cb36.js"></script>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this file not required

<link rel="stylesheet" href="/assets/commit/dashboard/assets/index-4a5a43c3.css">
</head>

<body>
<div id="root"></div>
<script>window.csrf_token = '{{ frappe.session.csrf_token }}';</script>
<script>
if (!window.frappe) window.frappe = {};
frappe.boot = JSON.parse({{ boot }});
</script>

</body>

Expand Down
8 changes: 6 additions & 2 deletions commit/www/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
<meta name="msapplication-TileColor" content="#da532c">
<meta name="theme-color" content="#ffffff">
<title>Commit - Developer tooling for the Frappeverse</title>
<script type="module" crossorigin src="/assets/commit/dashboard/assets/index-2eacee2d.js"></script>
<link rel="stylesheet" href="/assets/commit/dashboard/assets/index-31456e03.css">
<script type="module" crossorigin src="/assets/commit/dashboard/assets/index-63f9cb36.js"></script>
<link rel="stylesheet" href="/assets/commit/dashboard/assets/index-4a5a43c3.css">
</head>

<body>
<div id="root"></div>
<script>window.csrf_token = '{{ frappe.session.csrf_token }}';</script>
<script>
if (!window.frappe) window.frappe = {};
frappe.boot = JSON.parse({{ boot }});
</script>

</body>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,22 @@ const CreateBranchModal = ({ project, mutate, setBranch, setOpen }: BranchProps)
}
})

useFrappeEventListener('commit_branch_creation_error', (data) => {
if (data.branch_name === branchName && data.project === project.name) {
setDesc("")
setCreationError(data.error)
setEventLoading(false)
setBranch("")
}
})

const [creationError, setCreationError] = useState(null)


const handleClose = (name: string, branch_name: string) => {
setEventLoading(false)
setBranch(name)
setCreationError(null)
methods.reset()
toast({
description: `Branch ${branch_name} added for ${project.app_name}`
Expand Down Expand Up @@ -99,6 +112,7 @@ const CreateBranchModal = ({ project, mutate, setBranch, setOpen }: BranchProps)
</DialogDescription>
</DialogHeader>
{error && <ErrorBanner error={error} />}
{creationError && <ErrorBanner error={creationError} />}
<FormProvider {...methods}>
<form onSubmit={handleSubmit(onSubmit)}>
<Label htmlFor="branchname">Branch Name</Label>
Expand All @@ -110,11 +124,13 @@ const CreateBranchModal = ({ project, mutate, setBranch, setOpen }: BranchProps)
className="mb-3 p-3 w-full"
/>
<DialogFooter>
<Button type="submit" style={{ padding: '10px 20px', margin: '10px 0 0' }} disabled={loading || eventLoading}>
{(loading || eventLoading) && <div
<Button type="submit" disabled={loading || eventLoading}>
{(loading || eventLoading) &&
<div
className="inline-block h-4 w-4 mr-2 animate-spin rounded-full border-2 border-solid border-current text-gray-200 border-e-transparent align-[-0.125em] text-surface motion-reduce:animate-[spin_1.5s_linear_infinite] dark:text-white"
role="status">
</div>}
Submit
</Button>
</DialogFooter>
</form>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Button } from '@/components/ui/button'
import { toast } from '@/components/ui/use-toast'
import { convertFrappeTimestampToTimeAgo } from '@/components/utils/dateconversion'
import { CommitProjectBranch } from '@/types/commit/CommitProjectBranch'
import { useFrappeDeleteDoc, useFrappePostCall } from 'frappe-react-sdk'
import { AiOutlineDelete } from 'react-icons/ai'
import { IoMdSync } from 'react-icons/io'
import { ProjectData } from '../Projects'
import { KeyedMutator } from 'swr'


const ManageBranchItem = ({ branch, mutate }: { branch: CommitProjectBranch, mutate: KeyedMutator<{ message: ProjectData[]; }> }) => {
const { call, loading, reset: callReset } = useFrappePostCall<{ message: any }>('commit.commit.doctype.commit_project_branch.commit_project_branch.fetch_repo')


const { deleteDoc, reset, loading: deleteLoading } = useFrappeDeleteDoc()

const handleDelete = () => {
deleteDoc("Commit Project Branch", branch.name)
.then(() => {
mutate()
reset()
}).then(() => toast({
description: "Branch Deleted Successfully",
}))
}

const handleSync = () => {
call({
doc: {},
name: branch.name,
}).then(() => {
mutate()
callReset()
}).then(() => toast({
description: "Branch Synced!",
}))
}

return (
<li className="pl-0 pt-2 hover:shadow-sm flex justify-between">
<div className="flex flex-col items-start text-lg tracking-normal">
{branch.branch_name}
<div className='text-xs text-gray-500 pl-0'>
Last synced {convertFrappeTimestampToTimeAgo(branch.last_fetched)}
</div>
</div>
<div className="flex gap-2">
<Button
className="flex gap-2 text-sm"
variant="secondary"
onClick={() => handleSync()}
disabled={deleteLoading || loading}
>
<IoMdSync className={loading ? 'animate-spin' : ''} />
Sync
</Button>
<Button className="text-lg p-2" variant="destructive"
onClick={() => handleDelete()}
disabled={deleteLoading || loading}
>
<AiOutlineDelete />
</Button>
</div>
</li >
)
}

export default ManageBranchItem
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog"
import { ProjectData } from "../Projects"
import { Button } from "@/components/ui/button"
import { KeyedMutator } from "swr"
import { CommitProjectBranch } from "@/types/commit/CommitProjectBranch"
import ManageBranchItem from "./ManageBranchItem"

export interface ManageBranchModalProps {
branches: CommitProjectBranch[]
mutate: KeyedMutator<{ message: ProjectData[]; }>
setOpenManageModal: React.Dispatch<React.SetStateAction<boolean>>
}


const ManageBranchModal = ({ branches, mutate, setOpenManageModal }: ManageBranchModalProps) => {

return (
<DialogContent className="sm:max-w-[600px] sm:max-h-[800px] overflow-y-scroll">
<DialogHeader>
<DialogTitle>Manage Branches</DialogTitle>
<DialogDescription>
Update or Delete Branches
</DialogDescription>
</DialogHeader>
<ul role="list" className="divide-y divide-gray-200">
{branches?.map((branch: CommitProjectBranch) => {
return (
<ManageBranchItem key={branch.name} branch={branch} mutate={mutate} />
)
}
)}
</ul>
<DialogFooter>
<Button variant="outline" onClick={() => setOpenManageModal(false)}>
Close
</Button>
</DialogFooter>
</DialogContent>
)
}


export default ManageBranchModal
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ type FormFields = {
about: string,
}

interface CreateOrgModalProps {
mutate: KeyedMutator<{ message: ProjectData[]; }>
}


const CreateOrgModal = ({ mutate }: {
mutate: KeyedMutator<{
message: ProjectData[];
}>
}) => {
const CreateOrgModal = ({ mutate }: CreateOrgModalProps) => {
const { toast } = useToast()
const methods = useForm<FormFields>()

Expand Down Expand Up @@ -71,7 +71,7 @@ const CreateOrgModal = ({ mutate }: {
className="mb-3 p-3 w-full"
/>
<DialogFooter>
<Button type="submit">
<Button type="submit" >
Submit
</Button>
</DialogFooter>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger } from '@/components/ui/alert-dialog'
import { AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from '@/components/ui/alert-dialog'
import { Button } from '@/components/ui/button'
import { ProjectData } from '../Projects'
import { useFrappeDeleteDoc } from 'frappe-react-sdk'
import { toast } from '@/components/ui/use-toast'
import { KeyedMutator } from 'swr'

const DeleteOrgModal = ({ org, mutate }: {
org: ProjectData, mutate: KeyedMutator<{
message: ProjectData[];
}>
}) => {
interface DeleteOrgModalProps {
org: ProjectData,
mutate: KeyedMutator<{ message: ProjectData[]; }>
}


const DeleteOrgModal = ({ org, mutate }: DeleteOrgModalProps) => {

const { deleteDoc, reset } = useFrappeDeleteDoc()

Expand All @@ -21,7 +23,6 @@ const DeleteOrgModal = ({ org, mutate }: {
reset()
}).then(() => toast({
description: `Organization ${org.organization_name} Deleted`,
variant: "destructive"
}))
}
return (
Expand All @@ -36,7 +37,7 @@ const DeleteOrgModal = ({ org, mutate }: {
<AlertDialogFooter className='!justify-end '>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<Button variant="destructive" onClick={handleOrgDelete}>
Delete
Delete
</Button>
</AlertDialogFooter>
</AlertDialogContent>
Expand Down
Loading