Skip to content

Commit df4dfee

Browse files
修正内容变化时未更新的问题
1 parent 4eea694 commit df4dfee

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

api_content.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net/http"
77
"net/url"
8+
"strings"
89
"time"
910
)
1011

@@ -54,7 +55,7 @@ func (cli *Client) ContentBySpaceAndTitle(space, title string) (Content, error)
5455
q := url.Values{
5556
"title": {title},
5657
"spaceKey": {space},
57-
"expand": {"version"},
58+
"expand": {"version,body.storage"},
5859
}
5960

6061
resp, err := cli.ApiGET("/content", q)
@@ -158,6 +159,10 @@ func (cli *Client) ContentUpdate(content Content) (Content, error) {
158159

159160
//从指定空间查找或创建指定标题的内容
160161
func (cli *Client) PageFindOrCreateBySpaceAndTitle(space, parentId, title, data string) (Content, error) {
162+
//内容中的空行会被Confluence保存时自动去掉
163+
//因此前先去掉,以避免对比内容变化时受到影响
164+
data = strings.TrimSuffix(strings.TrimPrefix(data, "\n"), "\n")
165+
161166
content, err := cli.ContentBySpaceAndTitle(space, title)
162167
if err != nil {
163168
return Content{}, fmt.Errorf("查找%s出错: %s", title, err)
@@ -168,8 +173,18 @@ func (cli *Client) PageFindOrCreateBySpaceAndTitle(space, parentId, title, data
168173
return cli.PageCreateInSpace(space, parentId, title, data)
169174
}
170175

171-
//存在但内容未变化,直接跳过
172-
if data != content.Body.Storage.Value {
176+
//存在:对比内容是否有变化
177+
newValue, err := cli.ContentBodyConvertTo(data, "storage", "view")
178+
if err != nil {
179+
return Content{}, fmt.Errorf("转换新内容失败: %s", err)
180+
}
181+
182+
oldValue, err := cli.ContentBodyConvertTo(content.Body.Storage.Value, "storage", "view")
183+
if err != nil {
184+
return Content{}, fmt.Errorf("转换旧内容失败: %s", err)
185+
}
186+
187+
if newValue == oldValue {
173188
return content, nil
174189
}
175190

api_contentbody.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package confluence
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
)
7+
8+
// 获取指定ID的内容
9+
func (cli *Client) ContentBodyConvertTo(value, from, to string) (string, error) {
10+
data := ContentBodyStorage{
11+
Value: value,
12+
Representation: from,
13+
}
14+
15+
resp, err := cli.ApiPOST("/contentbody/convert/"+to, data)
16+
if err != nil {
17+
return "", fmt.Errorf("执行请求失败: %s", err)
18+
}
19+
20+
defer resp.Body.Close()
21+
22+
var info struct {
23+
ErrorResp
24+
ContentBodyStorage
25+
}
26+
27+
err = json.NewDecoder(resp.Body).Decode(&info)
28+
if err != nil {
29+
return "", fmt.Errorf("解析响应失败: %s", err)
30+
}
31+
32+
if info.StatusCode != 0 {
33+
return "", fmt.Errorf("[%d]%s", info.StatusCode, info.Message)
34+
}
35+
36+
return info.ContentBodyStorage.Value, nil
37+
}

0 commit comments

Comments
 (0)