57 lines
1.4 KiB
Go
57 lines
1.4 KiB
Go
|
|
package devcontainer
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"context"
|
||
|
|
"regexp"
|
||
|
|
|
||
|
|
"code.gitea.io/gitea/models/db"
|
||
|
|
"code.gitea.io/gitea/models/repo"
|
||
|
|
"code.gitea.io/gitea/models/user"
|
||
|
|
files_service "code.gitea.io/gitea/services/repository/files"
|
||
|
|
)
|
||
|
|
|
||
|
|
func GetDevcontainerConfigurationString(ctx context.Context, repo *repo.Repository) (string, error) {
|
||
|
|
configuration, err := GetFileContentByPath(ctx, repo, ".devcontainer/devcontainer.json")
|
||
|
|
if err != nil {
|
||
|
|
return "", err
|
||
|
|
}
|
||
|
|
cleanedContent, err := removeComments(configuration)
|
||
|
|
if err != nil {
|
||
|
|
return "", err
|
||
|
|
}
|
||
|
|
return cleanedContent, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// 移除 JSON 文件中的注释
|
||
|
|
func removeComments(data string) (string, error) {
|
||
|
|
// 移除单行注释 // ...
|
||
|
|
re := regexp.MustCompile(`//.*`)
|
||
|
|
data = re.ReplaceAllString(data, "")
|
||
|
|
|
||
|
|
// 移除多行注释 /* ... */
|
||
|
|
re = regexp.MustCompile(`/\*.*?\*/`)
|
||
|
|
data = re.ReplaceAllString(data, "")
|
||
|
|
|
||
|
|
return data, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func UpdateDevcontainerConfiguration(newContent string, repo *repo.Repository, doer *user.User) error {
|
||
|
|
// 更新devcontainer.json配置文件
|
||
|
|
_, err := files_service.ChangeRepoFiles(db.DefaultContext, repo, doer, &files_service.ChangeRepoFilesOptions{
|
||
|
|
Files: []*files_service.ChangeRepoFile{
|
||
|
|
{
|
||
|
|
Operation: "update",
|
||
|
|
TreePath: ".devcontainer/devcontainer.json",
|
||
|
|
ContentReader: bytes.NewReader([]byte(newContent)),
|
||
|
|
},
|
||
|
|
},
|
||
|
|
OldBranch: repo.DefaultBranch,
|
||
|
|
Message: "Update container",
|
||
|
|
})
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|