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: git checkout command #1591

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions pkg/agent/toolbox/git/checkout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2024 Daytona Platforms Inc.
// SPDX-License-Identifier: Apache-2.0

package git

import (
"github.com/daytonaio/daytona/pkg/git"
"github.com/gin-gonic/gin"
)

func CheckoutBranch(c *gin.Context) {
var req GitCheckoutRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.AbortWithError(400, err)
return
}

gitService := git.Service{
ProjectDir: req.Path,
}

if err := gitService.Checkout(req.Branch); err != nil {
c.AbortWithError(400, err)
return
}

c.Status(200)
}
5 changes: 5 additions & 0 deletions pkg/agent/toolbox/git/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,8 @@ type GitRepoRequest struct {
Username *string `json:"username,omitempty" validate:"optional"`
Password *string `json:"password,omitempty" validate:"optional"`
} // @name GitRepoRequest

type GitCheckoutRequest struct {
Path string `json:"path" validate:"required"`
Branch string `json:"branch" validate:"required"`
Copy link
Member

Choose a reason for hiding this comment

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

I suggest these props to be consistent with GitCloneRequest and to be more explicit:

	Branch   *string `json:"branch,omitempty" validate:"optional"`
	CommitID *string `json:"commit_id,omitempty" validate:"optional"`

P.S. It actually might be better to refactor CommitID to SHA so that is consistent with our GitRepository domain object.

} // @name GitCheckoutRequest
1 change: 1 addition & 0 deletions pkg/agent/toolbox/toolbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (s *Server) Start() error {

gitController.POST("/add", git.AddFiles)
gitController.POST("/branches", git.CreateBranch)
gitController.POST("/checkout", git.CheckoutBranch)
gitController.POST("/clone", git.CloneRepository)
gitController.POST("/commit", git.CommitChanges)
gitController.POST("/pull", git.PullChanges)
Expand Down
17 changes: 17 additions & 0 deletions pkg/api/controllers/workspace/toolbox/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,20 @@ func GitPushChanges(ctx *gin.Context) {
func GitStatus(ctx *gin.Context) {
forwardRequestToToolbox(ctx)
}

// GitCheckoutBranch godoc
//
// @Tags workspace toolbox
// @Summary Checkout branch
// @Description Checkout branch or commit in git repository inside workspace project
// @Produce json
// @Param workspaceId path string true "Workspace ID or Name"
// @Param projectId path string true "Project ID"
// @Param params body GitCheckoutRequest true "GitCheckoutRequest"
// @Success 200
// @Router /workspace/{workspaceId}/{projectId}/toolbox/git/checkout [post]
//
// @id GitCheckoutBranch
func GitCheckoutBranch(ctx *gin.Context) {
forwardRequestToToolbox(ctx)
}
58 changes: 58 additions & 0 deletions pkg/api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2539,6 +2539,49 @@ const docTemplate = `{
}
}
},
"/workspace/{workspaceId}/{projectId}/toolbox/git/checkout": {
"post": {
"description": "Checkout branch or commit in git repository inside workspace project",
"produces": [
"application/json"
],
"tags": [
"workspace toolbox"
],
"summary": "Checkout branch",
"operationId": "GitCheckoutBranch",
"parameters": [
{
"type": "string",
"description": "Workspace ID or Name",
"name": "workspaceId",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Project ID",
"name": "projectId",
"in": "path",
"required": true
},
{
"description": "GitCheckoutRequest",
"name": "params",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/GitCheckoutRequest"
}
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/workspace/{workspaceId}/{projectId}/toolbox/git/clone": {
"post": {
"description": "Clone git repository inside workspace project",
Expand Down Expand Up @@ -3812,6 +3855,21 @@ const docTemplate = `{
}
}
},
"GitCheckoutRequest": {
"type": "object",
"required": [
"branch",
"path"
],
"properties": {
"branch": {
"type": "string"
},
"path": {
"type": "string"
}
}
},
"GitCloneRequest": {
"type": "object",
"required": [
Expand Down
58 changes: 58 additions & 0 deletions pkg/api/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2536,6 +2536,49 @@
}
}
},
"/workspace/{workspaceId}/{projectId}/toolbox/git/checkout": {
"post": {
"description": "Checkout branch or commit in git repository inside workspace project",
"produces": [
"application/json"
],
"tags": [
"workspace toolbox"
],
"summary": "Checkout branch",
"operationId": "GitCheckoutBranch",
"parameters": [
{
"type": "string",
"description": "Workspace ID or Name",
"name": "workspaceId",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Project ID",
"name": "projectId",
"in": "path",
"required": true
},
{
"description": "GitCheckoutRequest",
"name": "params",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/GitCheckoutRequest"
}
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/workspace/{workspaceId}/{projectId}/toolbox/git/clone": {
"post": {
"description": "Clone git repository inside workspace project",
Expand Down Expand Up @@ -3809,6 +3852,21 @@
}
}
},
"GitCheckoutRequest": {
"type": "object",
"required": [
"branch",
"path"
],
"properties": {
"branch": {
"type": "string"
},
"path": {
"type": "string"
}
}
},
"GitCloneRequest": {
"type": "object",
"required": [
Expand Down
39 changes: 39 additions & 0 deletions pkg/api/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,16 @@ definitions:
- name
- path
type: object
GitCheckoutRequest:
properties:
branch:
type: string
path:
type: string
required:
- branch
- path
type: object
GitCloneRequest:
properties:
branch:
Expand Down Expand Up @@ -2910,6 +2920,35 @@ paths:
summary: Create branch
tags:
- workspace toolbox
/workspace/{workspaceId}/{projectId}/toolbox/git/checkout:
post:
description: Checkout branch or commit in git repository inside workspace project
operationId: GitCheckoutBranch
parameters:
- description: Workspace ID or Name
in: path
name: workspaceId
required: true
type: string
- description: Project ID
in: path
name: projectId
required: true
type: string
- description: GitCheckoutRequest
in: body
name: params
required: true
schema:
$ref: '#/definitions/GitCheckoutRequest'
produces:
- application/json
responses:
"200":
description: OK
summary: Checkout branch
tags:
- workspace toolbox
/workspace/{workspaceId}/{projectId}/toolbox/git/clone:
post:
description: Clone git repository inside workspace project
Expand Down
1 change: 1 addition & 0 deletions pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ func (a *ApiServer) Start() error {

gitController.POST("/add", toolbox.GitAddFiles)
gitController.POST("/branches", toolbox.GitCreateBranch)
gitController.POST("/checkout", toolbox.GitCheckoutBranch)
gitController.POST("/clone", toolbox.GitCloneRepository)
gitController.POST("/commit", toolbox.GitCommitChanges)
gitController.POST("/pull", toolbox.GitPushChanges)
Expand Down
2 changes: 2 additions & 0 deletions pkg/apiclient/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ Class | Method | HTTP request | Description
*WorkspaceToolboxAPI* | [**GetProjectDir**](docs/WorkspaceToolboxAPI.md#getprojectdir) | **Get** /workspace/{workspaceId}/{projectId}/toolbox/project-dir | Get project dir
*WorkspaceToolboxAPI* | [**GitAddFiles**](docs/WorkspaceToolboxAPI.md#gitaddfiles) | **Post** /workspace/{workspaceId}/{projectId}/toolbox/git/add | Add files
*WorkspaceToolboxAPI* | [**GitBranchList**](docs/WorkspaceToolboxAPI.md#gitbranchlist) | **Get** /workspace/{workspaceId}/{projectId}/toolbox/git/branches | Get branch list
*WorkspaceToolboxAPI* | [**GitCheckoutBranch**](docs/WorkspaceToolboxAPI.md#gitcheckoutbranch) | **Post** /workspace/{workspaceId}/{projectId}/toolbox/git/checkout | Checkout branch
*WorkspaceToolboxAPI* | [**GitCloneRepository**](docs/WorkspaceToolboxAPI.md#gitclonerepository) | **Post** /workspace/{workspaceId}/{projectId}/toolbox/git/clone | Clone git repository
*WorkspaceToolboxAPI* | [**GitCommitChanges**](docs/WorkspaceToolboxAPI.md#gitcommitchanges) | **Post** /workspace/{workspaceId}/{projectId}/toolbox/git/commit | Commit changes
*WorkspaceToolboxAPI* | [**GitCommitHistory**](docs/WorkspaceToolboxAPI.md#gitcommithistory) | **Get** /workspace/{workspaceId}/{projectId}/toolbox/git/history | Get commit history
Expand Down Expand Up @@ -203,6 +204,7 @@ Class | Method | HTTP request | Description
- [GitAddRequest](docs/GitAddRequest.md)
- [GitBranch](docs/GitBranch.md)
- [GitBranchRequest](docs/GitBranchRequest.md)
- [GitCheckoutRequest](docs/GitCheckoutRequest.md)
- [GitCloneRequest](docs/GitCloneRequest.md)
- [GitCommitInfo](docs/GitCommitInfo.md)
- [GitCommitRequest](docs/GitCommitRequest.md)
Expand Down
45 changes: 45 additions & 0 deletions pkg/apiclient/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1845,6 +1845,38 @@ paths:
tags:
- workspace toolbox
x-codegen-request-body-name: params
/workspace/{workspaceId}/{projectId}/toolbox/git/checkout:
post:
description: Checkout branch or commit in git repository inside workspace project
operationId: GitCheckoutBranch
parameters:
- description: Workspace ID or Name
in: path
name: workspaceId
required: true
schema:
type: string
- description: Project ID
in: path
name: projectId
required: true
schema:
type: string
requestBody:
content:
'*/*':
schema:
$ref: '#/components/schemas/GitCheckoutRequest'
description: GitCheckoutRequest
required: true
responses:
"200":
content: {}
description: OK
summary: Checkout branch
tags:
- workspace toolbox
x-codegen-request-body-name: params
/workspace/{workspaceId}/{projectId}/toolbox/git/clone:
post:
description: Clone git repository inside workspace project
Expand Down Expand Up @@ -3015,6 +3047,19 @@ components:
- name
- path
type: object
GitCheckoutRequest:
example:
path: path
branch: branch
properties:
branch:
type: string
path:
type: string
required:
- branch
- path
type: object
GitCloneRequest:
example:
path: path
Expand Down
Loading
Loading