feat(devstar home): upload new created public key
This commit is contained in:
		@@ -356,15 +356,25 @@ header("Allow: GET, POST, OPTIONS, PUT, DELETE");
 | 
			
		||||
                .then(async userPublicKey => {
 | 
			
		||||
                    if (userPublicKey == '') {
 | 
			
		||||
                        await createUserPublicKeyByVSCode()
 | 
			
		||||
                        await getUserPublicKeyFromVSCode()
 | 
			
		||||
                        .then(async userPublicKey => {
 | 
			
		||||
                            // upload new created public key
 | 
			
		||||
                        .then(async () => {
 | 
			
		||||
                            // only upload new created public key
 | 
			
		||||
                            await getUserPublicKeyFromVSCode()
 | 
			
		||||
                            .then(async userPublicKey => {
 | 
			
		||||
                                const dataFromResp = await communicateVSCodeByWebview('getMachineName', {});
 | 
			
		||||
                                const machineName = dataFromResp.machineName;
 | 
			
		||||
 | 
			
		||||
                                const keyTitle = `${USERNAME}-${machineName}`
 | 
			
		||||
                                await uploadNewCreatedPublicKey(userPublicKey, keyTitle);
 | 
			
		||||
                            })
 | 
			
		||||
                            .catch(error => {
 | 
			
		||||
                                console.error("Failed to get NEW CREATED user public key from vscode: ", error)
 | 
			
		||||
                            })
 | 
			
		||||
                        })
 | 
			
		||||
 | 
			
		||||
                    }
 | 
			
		||||
                })
 | 
			
		||||
                
 | 
			
		||||
                .catch(error => {
 | 
			
		||||
                    console.error("Failed to get user public key from vscode: ", error)
 | 
			
		||||
                })
 | 
			
		||||
 | 
			
		||||
                closeLoginModal()
 | 
			
		||||
            })
 | 
			
		||||
@@ -460,6 +470,36 @@ header("Allow: GET, POST, OPTIONS, PUT, DELETE");
 | 
			
		||||
            })
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        async function uploadNewCreatedPublicKey(userPublicKey, keyTitle) {
 | 
			
		||||
            const postData = {
 | 
			
		||||
                "key": userPublicKey,
 | 
			
		||||
                "title": keyTitle
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            const uploadUrl = DEVSTAR_HOME + `/api/v1/user/keys`;
 | 
			
		||||
 | 
			
		||||
            fetch(uploadUrl, {
 | 
			
		||||
                method: 'POST',
 | 
			
		||||
                headers: {
 | 
			
		||||
                    'Content-Type': 'application/json',
 | 
			
		||||
                    'Authorization': 'token ' + USERTOKEN 
 | 
			
		||||
                },
 | 
			
		||||
                body: JSON.stringify(postData)
 | 
			
		||||
            })
 | 
			
		||||
            .then(response => {
 | 
			
		||||
                if (!response.ok) {
 | 
			
		||||
                    throw new Error('Error in logging ' + response.statusText);
 | 
			
		||||
                }
 | 
			
		||||
                return response.json();
 | 
			
		||||
            })
 | 
			
		||||
            .then(async data => {
 | 
			
		||||
                console.log(data)
 | 
			
		||||
            })
 | 
			
		||||
            .catch(error => {
 | 
			
		||||
                console.error('There has been a problem with your fetch operation:', error);
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // ===================================== Repo ===========================
 | 
			
		||||
        function loadRepositories() {
 | 
			
		||||
            // clear old data
 | 
			
		||||
@@ -685,16 +725,13 @@ header("Allow: GET, POST, OPTIONS, PUT, DELETE");
 | 
			
		||||
        // TODO: create container
 | 
			
		||||
        async function createDevContainer(repoId) {
 | 
			
		||||
            return new Promise(async (resolve, reject) => {
 | 
			
		||||
                // get public key
 | 
			
		||||
                const publicKey = await getDefaultPublicKeyFromVSCode()
 | 
			
		||||
 | 
			
		||||
                // request creating container
 | 
			
		||||
                const url = DEVSTAR_HOME + "/api/devcontainer"  
 | 
			
		||||
                var token = USERTOKEN
 | 
			
		||||
 | 
			
		||||
                const postData = {
 | 
			
		||||
                    "repoId": repoId.toString(),
 | 
			
		||||
                    "sshPublicKeyList": [publicKey]
 | 
			
		||||
                    // "sshPublicKeyList": [publicKey]
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                fetch(url, {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user