@@ -160,7 +160,70 @@ func (cli *Client) ContentUpdate(content Content) (Content, error) {
160160}
161161
162162//从指定空间查找或创建指定标题的内容
163- func (cli * Client ) PageFindOrCreateBySpaceAndTitle (space , parentId , title , wikiDirPrefix , data , extraInfo string ) (Content , error ) {
163+ func (cli * Client ) DrawFile (space , parentId , title , wikiDirPrefix , data string ) (Content , error ) {
164+ //内容中的空行会被Confluence保存时自动去掉
165+ //因此前先去掉,以避免对比内容变化时受到影响
166+ data = strings .TrimSuffix (strings .TrimPrefix (data , "\n " ), "\n " )
167+
168+ //获取当前页面的内容
169+ content , err := cli .ContentBySpaceAndTitle (space , title )
170+ if err != nil {
171+ return Content {}, fmt .Errorf ("查找%s出错: %s" , title , err )
172+ }
173+
174+ // 不存在则创建
175+ if content .Id == "" {
176+ return cli .PageCreateInSpace (space , parentId , title , data )
177+ }
178+
179+ // 获取文件的路径
180+ pagePath := ""
181+ lastAncestorId := ""
182+ for _ , ancestor := range content .Ancestors {
183+ pagePath += "/" + ancestor .Title
184+ lastAncestorId = ancestor .Id
185+ }
186+ pagePath += "/" + content .Title
187+
188+ //存在,但不在指定的路径下,报错结束
189+ if ! strings .HasPrefix (pagePath , wikiDirPrefix ) {
190+ return Content {}, fmt .Errorf ("一个标题为 '%v' 的页面已经存在于该空间中。为您的页面输入一个不同的标题。" , title )
191+ } else {
192+ //存在:对比内容是否有变化
193+ newValue , err := cli .ContentBodyConvertTo (data , "storage" , "view" )
194+ if err != nil {
195+ return Content {}, fmt .Errorf ("转换新内容失败: %s" , err )
196+ }
197+
198+ // 去除原文件的备注宏
199+ if content .Body .Storage .Value != "" {
200+ content .Body .Storage .Value = strings .Split (content .Body .Storage .Value , ConfluenceNoteSplite )[0 ]
201+ }
202+ oldValue , err := cli .ContentBodyConvertTo (content .Body .Storage .Value , "storage" , "view" )
203+ if err != nil {
204+ return Content {}, fmt .Errorf ("转换旧内容失败: %s" , err )
205+ }
206+
207+ if newValue == oldValue && lastAncestorId == parentId {
208+ return content , nil
209+ }
210+
211+ // 存在则否则更新
212+ content .Space .Key = space
213+ content .Version .Number += 1
214+ content .Version .Message = time .Now ().Local ().Format ("机器人更新于2006-01-02 15:04:05" )
215+ content .SetStorageBody (data )
216+
217+ //设置父页面
218+ if parentId != "" {
219+ content .Ancestors = []Content {Content {Id : parentId }}
220+ }
221+
222+ return cli .ContentUpdate (content )
223+ }
224+ }
225+
226+ func (cli * Client ) DrawFileWithNoteMacro (space , parentId , title , wikiDirPrefix , data , extraInfo string ) (Content , error ) {
164227 //内容中的空行会被Confluence保存时自动去掉
165228 //因此前先去掉,以避免对比内容变化时受到影响
166229 data = strings .TrimSuffix (strings .TrimPrefix (data , "\n " ), "\n " )
@@ -245,7 +308,7 @@ type DrawModifyPageOption struct {
245308}
246309
247310//从指定空间查找或创建指定标题的内容
248- func (cli * Client ) DrawModifyOrCreatePage (options * DrawModifyPageOption ) (Content , error ) {
311+ func (cli * Client ) DrawFileWithNewNoteMacro (options * DrawModifyPageOption ) (Content , error ) {
249312 //内容中的空行会被Confluence保存时自动去掉
250313 //因此前先去掉,以避免对比内容变化时受到影响
251314 data := strings .TrimSuffix (strings .TrimPrefix (options .Data , "\n " ), "\n " )
@@ -328,6 +391,5 @@ func GetConfluenceNoteMacro(options *DrawModifyPageOption) (string, error) {
328391 if err != nil {
329392 return "" , err
330393 }
331- fmt .Println ("Macro " , outPut .String ())
332394 return outPut .String (), nil
333395}
0 commit comments