Skip to content

Commit

Permalink
feat: diffs api support query param content
Browse files Browse the repository at this point in the history
  • Loading branch information
narasux committed Dec 9, 2024
1 parent 59b9e98 commit bb76dca
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
12 changes: 7 additions & 5 deletions cnb-builder-shim/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ tar -czvf /tmp/source.tgz -C examples/python-flask .

首先,通过环境变量设置一些关键参数:


```bash
# BUILDER_SHIM_IMAGE:构建工具镜像地址
export BUILDER_SHIM_IMAGE='mirrors.tencent.com/bkpaas/bk-builder-heroku-bionic:latest'
Expand All @@ -82,7 +81,7 @@ docker run --rm \
-e USE_DOCKER_DAEMON=true \
--mount type=bind,source=/var/run/docker.sock,target=/var/run/docker.sock \
--mount type=bind,source=/tmp/source.tgz,target=/tmp/source.tgz \
$BUILDER_SHIM_IMAGE
$BUILDER_SHIM_IMAGE
```

要点说明:
Expand All @@ -102,7 +101,7 @@ docker run --rm \
make heroku-dev
```

其将使用默认的“云原生 builder 镜像”,名称为 `mirrors.tencent.com/bkpaas/builder-heroku-bionic:latest`,你也可以传递环境变量修改该默认名:
其将使用默认的 “云原生 builder 镜像”,名称为 `mirrors.tencent.com/bkpaas/builder-heroku-bionic:latest`,你也可以传递环境变量修改该默认名:

```shell
BUILDER_IMAGE_NAME="my-builder-heroku-bionic" BUILDER_IMAGE_TAG="my-tag" DEV_IMAGE_NAME="bk-dev-heroku-bionic" DEV_IMAGE_TAG="latest" make heroku-dev
Expand All @@ -112,6 +111,7 @@ BUILDER_IMAGE_NAME="my-builder-heroku-bionic" BUILDER_IMAGE_TAG="my-tag" DEV_IMA
- `DEV_IMAGE_TAG`: 目标开发镜像 tag

### 2. 启动镜像

首先,通过环境变量设置一些关键参数。参数设置可选,仅当容器中默认的地址无效时,进行设置:

```shell
Expand All @@ -133,7 +133,8 @@ docker run -d --net=host \
bk-dev-heroku-bionic:latest
```

### 3. 通过 http 请求完成源码的 hot-reload
### 3. 通过 http 请求完成源码的 hot-reload

#### 3.1 请求源码部署

请求命令
Expand All @@ -149,7 +150,7 @@ curl --location 'http://{devsandbox_ip}:8000/deploys' \

```json
{
"deployID": "aaf79f28271e47bebf8448b63bddd04f"
"deployID": "aaf79f28271e47bebf8448b63bddd04f"
}
```

Expand All @@ -162,6 +163,7 @@ curl --location 'http://{devsandbox_ip}:8000/deploys' \
# deployID 是上一步请求部署时返回的 deployID
curl --location 'http://{devsandbox_ip}:8000/deploys/{deployID}/results?log=true' --header 'Authorization: Bearer xxx'
```

结果示例

```json
Expand Down
18 changes: 18 additions & 0 deletions cnb-builder-shim/internal/devsandbox/filediffer/filediffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,23 @@ var _ = Describe("Test Differ", func() {
{Action: FileActionAdded, Path: "webfe/templates/example.html", Content: ".html"},
}))
})

It("without content", func() {
differ := New()
Expect(differ.Prepare(tmpDir)).To(BeNil())

_ = initFile(path.Join(tmpDir, "webfe/static"), "example.css", "css")
_ = initFile(path.Join(tmpDir, "v3logs"), "celery.log", "celery is running...")
_ = editFile(tmpDir, "example.py", "python")
_ = os.Remove(path.Join(tmpDir, "example.go"))

files, err := differ.Diff()
Expect(err).To(BeNil())
Expect(files).To(Equal([]File{
{Action: FileActionDeleted, Path: "example.go", Content: ""},
{Action: FileActionModified, Path: "example.py", Content: ""},
{Action: FileActionAdded, Path: "webfe/static/example.css", Content: ""},
}))
})
})
})
10 changes: 8 additions & 2 deletions cnb-builder-shim/internal/devsandbox/webserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ func ProcessListHandler() gin.HandlerFunc {
// DiffsHandler 提供文件变更信息
func DiffsHandler() gin.HandlerFunc {
return func(c *gin.Context) {
// 由于目前 HTTP 附带文件的源码初始化逻辑不同,暂时不支持 TODO 后续重构时需要统一
// 由于目前 HTTP 附带文件的源码初始化逻辑不同,暂时不支持
// TODO 后续重构时需要统一
if config.G.SourceCode.FetchMethod != config.BK_REPO {
c.JSON(
http.StatusBadRequest,
Expand All @@ -295,8 +296,13 @@ func DiffsHandler() gin.HandlerFunc {
return
}

differ := filediffer.New(filediffer.WithContent())
// 初始化
opts := []filediffer.Option{}
if c.Query("content") == "true" {
opts = append(opts, filediffer.WithContent())
}
differ := filediffer.New(opts...)

if err := differ.Prepare(config.G.SourceCode.Workspace); err != nil {
c.JSON(
http.StatusInternalServerError,
Expand Down

0 comments on commit bb76dca

Please sign in to comment.