Skip to content

[jb-remote] hook gp cli #8025

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

Merged
merged 3 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,3 @@ components/ws-proxy/ws-proxy

# Logs
components/server/*.log

# IntelliJ
.idea/
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/gitpod.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions components/gitpod-cli/cmd/credential-helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (g *gitCommandInfo) Ok() bool {
return g.RepoUrl != "" && g.GitCommand != ""
}

var gitCommandRegExp = regexp.MustCompile(`git(\s+(?:[\w\-]+\s+)*)(push|clone|fetch|pull|diff|ls-remote)`)
var gitCommandRegExp = regexp.MustCompile(`git(?:\s+(?:\S+\s+)*)(push|clone|fetch|pull|diff|ls-remote)(?:\s+(?:\S+\s+)*)?`)
var repoUrlRegExp = regexp.MustCompile(`remote-https?\s([^\s]+)\s+(https?:[^\s]+)\s`)

// This method needs to be called multiple times to fill all the required info
Expand All @@ -190,8 +190,8 @@ var repoUrlRegExp = regexp.MustCompile(`remote-https?\s([^\s]+)\s+(https?:[^\s]+
// `/usr/lib/git-core/git push`
func (g *gitCommandInfo) parseGitCommandAndRemote(cmdLineString string) {
matchCommand := gitCommandRegExp.FindStringSubmatch(cmdLineString)
if len(matchCommand) == 3 {
g.GitCommand = matchCommand[2]
if len(matchCommand) == 2 {
g.GitCommand = matchCommand[1]
}

matchRepo := repoUrlRegExp.FindStringSubmatch(cmdLineString)
Expand Down
8 changes: 8 additions & 0 deletions components/gitpod-cli/cmd/credential-helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ func Test_parse_git_command_and_remote(t *testing.T) {
},
Expected: gitCommandInfo{RepoUrl: "https://github.com/jeanp413/test-private-package.git", GitCommand: "clone"},
},
{
Name: "JB push command",
Commands: []string{
"/usr/lib/git-core/git remote-https origin https://github.com/gitpod-io/spring-petclinic.git ",
"/bin/git -c core.quotepath=false -c log.showSignature=false push --progress --porcelain origin refs/heads/master:master ",
},
Expected: gitCommandInfo{RepoUrl: "https://github.com/gitpod-io/spring-petclinic.git", GitCommand: "push"},
},
}
for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ platformVersion=213-EAP-SNAPSHOT
platformDownloadSources=true
# Plugin Dependencies -> https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html
# Example: platformPlugins = com.intellij.java, com.jetbrains.php:203.4449.22
platformPlugins=
platformPlugins=Git4Idea
# Opt-out flag for bundling Kotlin standard library.
# See https://plugins.jetbrains.com/docs/intellij/kotlin.html#kotlin-standard-library for details.
kotlin.stdlib.default.dependency=false
Expand Down
31 changes: 31 additions & 0 deletions components/ide/jetbrains/backend-plugin/launch-dev-server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash
# Copyright (c) 2022 Gitpod GmbH. All rights reserved.
# Licensed under the GNU Affero General Public License (AGPL).
# See License-AGPL.txt in the project root for license information.

TEST_BACKEND_DIR=/workspace/ide-backend
if [ ! -d "$TEST_BACKEND_DIR" ]; then
mkdir -p $TEST_BACKEND_DIR
cp -r /ide-desktop/backend/* $TEST_BACKEND_DIR
fi

TEST_PLUGINS_DIR="$TEST_BACKEND_DIR/plugins"
TEST_PLUGIN_DIR="$TEST_PLUGINS_DIR/gitpod-remote"
rm -rf $TEST_PLUGIN_DIR

GITPOD_PLUGIN_DIR=/workspace/gitpod/components/ide/jetbrains/backend-plugin
$GITPOD_PLUGIN_DIR/gradlew buildPlugin

# TODO(ak) actually should be gradle task to make use of output
GITPOD_PLUGIN_DIST="$GITPOD_PLUGIN_DIR/build/distributions/gitpod-remote-0.0.1.zip"
unzip $GITPOD_PLUGIN_DIST -d $TEST_PLUGINS_DIR

TEST_REPO=https://github.com/gitpod-io/spring-petclinic
TEST_DIR=/workspace/spring-petclinic
if [ ! -d "$TEST_DIR" ]; then
git clone $TEST_REPO $TEST_DIR
fi

export JAVA_TOOL_OPTIONS="$JAVA_TOOL_OPTIONS -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:0"

$TEST_BACKEND_DIR/bin/remote-dev-server.sh run $TEST_DIR
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.

package io.gitpod.jetbrains.remote

import com.intellij.codeWithMe.ClientId
import com.intellij.ide.BrowserUtil
import com.intellij.ide.CommandLineProcessor
import com.intellij.openapi.client.ClientSession
import com.intellij.openapi.client.ClientSessionsManager
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.io.FileUtilRt
import io.netty.channel.ChannelHandlerContext
import io.netty.handler.codec.http.FullHttpRequest
import io.netty.handler.codec.http.QueryStringDecoder
import org.jetbrains.ide.RestService
import java.nio.file.InvalidPathException
import java.nio.file.Path


class GitpodCLIService : RestService() {

override fun getServiceName() = SERVICE_NAME

override fun execute(urlDecoder: QueryStringDecoder, request: FullHttpRequest, context: ChannelHandlerContext): String? {
val operation = getStringParameter("op", urlDecoder)
if (operation == "open") {
val fileStr = getStringParameter("file", urlDecoder)
if (fileStr.isNullOrBlank()) {
return "file is missing"
}
val file = parseFilePath(fileStr) ?: return "invalid file"
val shouldWait = getBooleanParameter("wait", urlDecoder)
return withClient(request, context) {
CommandLineProcessor.doOpenFileOrProject(file, shouldWait).future.get()
}
}
if (operation == "preview") {
val url = getStringParameter("url", urlDecoder)
if (url.isNullOrBlank()) {
return "url is missing"
}
return withClient(request, context) { project ->
BrowserUtil.browse(url, project)
}
}
return "invalid operation"
}

private fun withClient(request: FullHttpRequest, context: ChannelHandlerContext, action: (project: Project?) -> Unit): String? {
val project = getLastFocusedOrOpenedProject()
var session: ClientSession? = null
if (project != null) {
session = ClientSessionsManager.getProjectSessions(project, false).first()
}
if (session == null) {
session = ClientSessionsManager.getAppSessions(false).first()
}
if (session == null) {
return "no client"
}
ClientId.withClientId(session.clientId) {
action(project)
}
sendOk(request, context)
return null
}

private fun parseFilePath(path: String): Path? {
return try {
var file: Path = Path.of(FileUtilRt.toSystemDependentName(path)) // handle paths like '/file/foo\qwe'
if (!file.isAbsolute) {
file = file.toAbsolutePath()
}
file.normalize()
} catch (e: InvalidPathException) {
thisLogger().warn("gitpod cli: failed to parse file path:", e)
null
}
}

companion object {
const val SERVICE_NAME = "gitpod/cli"
}
}
Loading