Skip to content

Commit

Permalink
Merge pull request #1010 from pascalbreuninger/feat/force-git-branch-…
Browse files Browse the repository at this point in the history
…and-commit

feat/force git branch and commit
  • Loading branch information
89luca89 authored Apr 17, 2024
2 parents 09beb31 + a5c98a2 commit 16968bc
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 4 deletions.
4 changes: 4 additions & 0 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func NewBuildCmd(flags *flags.GlobalFlags) *cobra.Command {
cmd.DevContainerPath,
sshConfigPath,
nil,
cmd.GitBranch,
cmd.GitCommit,
false,
log.Default,
)
Expand Down Expand Up @@ -112,6 +114,8 @@ func NewBuildCmd(flags *flags.GlobalFlags) *cobra.Command {
buildCmd.Flags().StringVar(&cmd.Repository, "repository", "", "The repository to push to")
buildCmd.Flags().StringSliceVar(&cmd.Platform, "platform", []string{}, "Set target platform for build")
buildCmd.Flags().BoolVar(&cmd.SkipPush, "skip-push", false, "If true will not push the image to the repository, useful for testing")
buildCmd.Flags().StringVar(&cmd.GitBranch, "git-branch", "", "The git branch to use")
buildCmd.Flags().StringVar(&cmd.GitCommit, "git-commit", "", "The git commit SHA to use")

// TESTING
buildCmd.Flags().BoolVar(&cmd.ForceBuild, "force-build", false, "TESTING ONLY")
Expand Down
4 changes: 4 additions & 0 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ func NewUpCmd(flags *flags.GlobalFlags) *cobra.Command {
cmd.DevContainerPath,
cmd.SSHConfigPath,
source,
cmd.GitBranch,
cmd.GitCommit,
true,
logger,
)
Expand Down Expand Up @@ -146,6 +148,8 @@ func NewUpCmd(flags *flags.GlobalFlags) *cobra.Command {
upCmd.Flags().BoolVar(&cmd.OpenIDE, "open-ide", true, "If this is false and an IDE is configured, DevPod will only install the IDE server backend, but not open it")
upCmd.Flags().BoolVar(&cmd.ForceCredentials, "force-credentials", false, "If true will always use local credentials")
_ = upCmd.Flags().MarkHidden("force-credentials")
upCmd.Flags().StringVar(&cmd.GitBranch, "git-branch", "", "The git branch to use")
upCmd.Flags().StringVar(&cmd.GitCommit, "git-commit", "", "The git commit SHA to use")

upCmd.Flags().BoolVar(&cmd.DisableDaemon, "disable-daemon", false, "If enabled, will not install a daemon into the target machine to track activity")
upCmd.Flags().StringVar(&cmd.Source, "source", "", "Optional source for the workspace. E.g. git:https://github.com/my-org/my-repo")
Expand Down
2 changes: 2 additions & 0 deletions desktop/src/client/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,7 @@ export const DEVPOD_FLAG_DEVCONTAINER_PATH = "--devcontainer-path"
export const DEVPOD_FLAG_WORKSPACE_ID = "--workspace-id"
export const DEVPOD_FLAG_WORKSPACE_UID = "--workspace-uid"
export const DEVPOD_FLAG_LOGIN = "--login"
export const DEVPOD_FLAG_GIT_BRANCH = "--git-branch"
export const DEVPOD_FLAG_GIT_COMMIT = "--git-commit"

export const DEVPOD_UI_ENV_VAR = "DEVPOD_UI"
14 changes: 14 additions & 0 deletions desktop/src/client/workspaces/workspaceCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
DEVPOD_FLAG_DEBUG,
DEVPOD_FLAG_DEVCONTAINER_PATH,
DEVPOD_FLAG_FORCE,
DEVPOD_FLAG_GIT_BRANCH,
DEVPOD_FLAG_GIT_COMMIT,
DEVPOD_FLAG_ID,
DEVPOD_FLAG_IDE,
DEVPOD_FLAG_JSON_LOG_OUTPUT,
Expand Down Expand Up @@ -121,6 +123,16 @@ export class WorkspaceCommands {
? toMultipleFlagArg(WorkspaceCommands.ADDITIONAL_FLAGS)
: []

const maybeGitBranch = config.sourceConfig?.gitBranch
const gitBranchFlag = exists(maybeGitBranch)
? [toFlagArg(DEVPOD_FLAG_GIT_BRANCH, maybeGitBranch)]
: []

const maybeGitCommit = config.sourceConfig?.gitCommit
const gitCommitFlag = exists(maybeGitCommit)
? [toFlagArg(DEVPOD_FLAG_GIT_COMMIT, maybeGitCommit)]
: []

return WorkspaceCommands.newCommand([
DEVPOD_COMMAND_UP,
identifier,
Expand All @@ -129,6 +141,8 @@ export class WorkspaceCommands {
...maybeProviderFlag,
...maybePrebuildRepositories,
...maybeDevcontainerPath,
...gitBranchFlag,
...gitCommitFlag,
...additionalFlags,
DEVPOD_FLAG_JSON_LOG_OUTPUT,
])
Expand Down
2 changes: 2 additions & 0 deletions desktop/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ export type TWorkspaceStartConfig = Readonly<{
// Instead of starting a workspace just by ID, the sourceConfig starts it with a `source/ID` combination
sourceConfig?: Readonly<{
source: string
gitBranch: string | undefined
gitCommit: string | undefined
}>
}>
export const SUPPORTED_IDES = ["vscode", "intellj"] as const
Expand Down
45 changes: 44 additions & 1 deletion desktop/src/views/Workspaces/CreateWorkspace/CreateWorkspace.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExampleCard, Form, IDEIcon, WarningMessageBox } from "@/components"
import { CollapsibleSection, ExampleCard, Form, IDEIcon, WarningMessageBox } from "@/components"
import {
Box,
Button,
Expand Down Expand Up @@ -71,6 +71,8 @@ export function CreateWorkspace() {
defaultIDE,
workspaceSource,
devcontainerPath,
gitBranch,
gitCommit,
}: TCreateWorkspaceArgs) => {
const actionID = workspace.create({
id: workspaceID,
Expand All @@ -80,6 +82,8 @@ export function CreateWorkspace() {
ideConfig: { name: defaultIDE },
sourceConfig: {
source: workspaceSource,
gitBranch,
gitCommit,
},
})

Expand Down Expand Up @@ -109,6 +113,8 @@ export function CreateWorkspace() {
idError,
prebuildRepositoryError,
devcontainerPathError,
gitBranchError,
gitCommitError,
} = useFormErrors(Object.values(FieldName), formState)

const providerOptions = useMemo<TSelectProviderOptions>(() => {
Expand Down Expand Up @@ -436,6 +442,43 @@ export function CreateWorkspace() {
{/* placholder box */}
<Box width={"full"} />
</HStack>

<CollapsibleSection title="Git Options" showIcon>
<HStack spacing="8" alignItems={"top"} width={"100%"} justifyContent={"start"}>
<FormControl isInvalid={exists(gitBranchError)}>
<FormLabel>Git Branch</FormLabel>
<Input
spellCheck={false}
placeholder="main"
type="text"
{...register(FieldName.GIT_BRANCH)}
/>
{exists(gitBranchError) ? (
<FormErrorMessage>{gitBranchError.message ?? "Error"}</FormErrorMessage>
) : (
<FormHelperText>
Optionally specify the branch name for this workspace.
</FormHelperText>
)}
</FormControl>
<FormControl isInvalid={exists(gitCommitError)}>
<FormLabel>Git Commit</FormLabel>
<Input
spellCheck={false}
placeholder="SHA256"
type="text"
{...register(FieldName.GIT_COMMIT)}
/>
{exists(gitCommitError) ? (
<FormErrorMessage>{gitCommitError.message ?? "Error"}</FormErrorMessage>
) : (
<FormHelperText>
Optionally specify the commit SHA256 for this workspace.
</FormHelperText>
)}
</FormControl>
</HStack>
</CollapsibleSection>
</VStack>

<HStack
Expand Down
6 changes: 6 additions & 0 deletions desktop/src/views/Workspaces/CreateWorkspace/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export const FieldName = {
PROVIDER: "provider",
PREBUILD_REPOSITORY: "prebuildRepository",
DEVCONTAINER_PATH: "devcontainerPath",
GIT_BRANCH: "gitBranch",
GIT_COMMIT: "gitCommit",
} as const

export type TFormValues = {
Expand All @@ -18,6 +20,8 @@ export type TFormValues = {
[FieldName.ID]: string
[FieldName.PREBUILD_REPOSITORY]: string
[FieldName.DEVCONTAINER_PATH]?: string
[FieldName.GIT_BRANCH]?: string
[FieldName.GIT_COMMIT]?: string
}

export type TCreateWorkspaceSearchParams = ReturnType<
Expand All @@ -30,6 +34,8 @@ export type TCreateWorkspaceArgs = Readonly<{
defaultIDE: string
workspaceSource: string
devcontainerPath: string | undefined
gitBranch: string | undefined
gitCommit: string | undefined
}>
export type TSelectProviderOptions = Readonly<{
installed: readonly TNamedProvider[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ export function useCreateWorkspaceForm(
const prebuildRepositories = data[FieldName.PREBUILD_REPOSITORY]
? [data[FieldName.PREBUILD_REPOSITORY]]
: []
const gitBranch = data[FieldName.GIT_BRANCH]
const gitCommit = data[FieldName.GIT_COMMIT]

onCreateWorkspace({
workspaceID,
Expand All @@ -229,6 +231,8 @@ export function useCreateWorkspaceForm(
defaultIDE,
workspaceSource,
devcontainerPath: maybeDevcontainerPath,
gitBranch,
gitCommit,
})
})(event),
[
Expand Down
2 changes: 2 additions & 0 deletions pkg/provider/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ type CLIOptions struct {
DisableDaemon bool `json:"disableDaemon,omitempty"`
DaemonInterval string `json:"daemonInterval,omitempty"`
ForceCredentials bool `json:"forceCredentials,omitempty"`
GitBranch string `json:"gitBranch,omitempty"`
GitCommit string `json:"gitCommit,omitempty"`

// build options
Repository string `json:"repository,omitempty"`
Expand Down
48 changes: 45 additions & 3 deletions pkg/workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func ResolveWorkspace(
devContainerPath string,
sshConfigPath string,
source *provider2.WorkspaceSource,
gitBranch, gitCommit string,
changeLastUsed bool,
log log.Logger,
) (client.BaseWorkspaceClient, error) {
Expand All @@ -176,7 +177,20 @@ func ResolveWorkspace(
}

// resolve workspace
provider, workspace, machine, err := resolveWorkspace(ctx, devPodConfig, args, desiredID, desiredMachine, providerUserOptions, sshConfigPath, source, changeLastUsed, log)
provider, workspace, machine, err := resolveWorkspace(
ctx,
devPodConfig,
args,
desiredID,
desiredMachine,
providerUserOptions,
sshConfigPath,
source,
gitBranch,
gitCommit,
changeLastUsed,
log,
)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -247,6 +261,7 @@ func resolveWorkspace(
providerUserOptions []string,
sshConfigPath string,
source *provider2.WorkspaceSource,
gitBranch, gitCommit string,
changeLastUsed bool,
log log.Logger,
) (*provider2.ProviderConfig, *provider2.Workspace, *provider2.Machine, error) {
Expand Down Expand Up @@ -280,7 +295,20 @@ func resolveWorkspace(
}

// create workspace
provider, workspace, machine, err := createWorkspace(ctx, devPodConfig, workspaceID, name, desiredMachine, providerUserOptions, sshConfigPath, source, isLocalPath, log)
provider, workspace, machine, err := createWorkspace(
ctx,
devPodConfig,
workspaceID,
name,
desiredMachine,
providerUserOptions,
sshConfigPath,
source,
isLocalPath,
gitBranch,
gitCommit,
log,
)
if err != nil {
_ = clientimplementation.DeleteWorkspaceFolder(devPodConfig.DefaultContext, workspaceID, sshConfigPath, log)
return nil, nil, nil, err
Expand All @@ -299,6 +327,7 @@ func createWorkspace(
sshConfigPath string,
source *provider2.WorkspaceSource,
isLocalPath bool,
gitBranch, gitCommit string,
log log.Logger,
) (*provider2.ProviderConfig, *provider2.Workspace, *provider2.Machine, error) {
// get default provider
Expand All @@ -316,7 +345,7 @@ func createWorkspace(
}

// resolve workspace
workspace, err := resolve(provider, devPodConfig, name, workspaceID, workspaceFolder, source, isLocalPath, sshConfigPath)
workspace, err := resolve(provider, devPodConfig, name, workspaceID, workspaceFolder, source, isLocalPath, sshConfigPath, gitBranch, gitCommit)
if err != nil {
return nil, nil, nil, err
}
Expand Down Expand Up @@ -420,6 +449,7 @@ func resolve(
source *provider2.WorkspaceSource,
isLocalPath bool,
sshConfigPath string,
fallbackGitBranch, fallbackGitCommit string,
) (*provider2.Workspace, error) {
now := types.Now()
uid := encoding.CreateNewUID(devPodConfig.DefaultContext, workspaceID)
Expand Down Expand Up @@ -460,6 +490,12 @@ func resolve(
GitCommit: gitCommit,
GitSubPath: gitSubdir,
}
if workspace.Source.GitCommit == "" && fallbackGitCommit != "" {
workspace.Source.GitCommit = fallbackGitCommit
}
if workspace.Source.GitBranch == "" && fallbackGitBranch != "" {
workspace.Source.GitBranch = fallbackGitBranch
}
return workspace, nil
}

Expand All @@ -480,9 +516,15 @@ func resolve(
if gitPRReference != "" {
workspace.Source.GitPRReference = gitPRReference
}
if fallbackGitBranch != "" {
workspace.Source.GitBranch = fallbackGitBranch
}
if gitBranch != "" {
workspace.Source.GitBranch = gitBranch
}
if fallbackGitCommit != "" {
workspace.Source.GitCommit = fallbackGitCommit
}
if gitCommit != "" {
workspace.Source.GitCommit = gitCommit
}
Expand Down

0 comments on commit 16968bc

Please sign in to comment.