Skip to content
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
dd48231
refactored the code and added flow manager
Kshitij-Katiyar Dec 23, 2024
8735616
removed unused code
Kshitij-Katiyar Dec 24, 2024
df14ae7
fixed lint
Kshitij-Katiyar Dec 24, 2024
b2b9222
added oauth setup and connect disconnect command
Kshitij-Katiyar Jan 2, 2025
a985f26
removed unrelated changes from package lock
Kshitij-Katiyar Jan 2, 2025
548de42
fixed lint
Kshitij-Katiyar Jan 2, 2025
4e352ae
fixed missing go mod entry
Kshitij-Katiyar Jan 2, 2025
828fddb
removed unwanted code and added mutliple status code handling
Kshitij-Katiyar Jan 16, 2025
b34d300
fixed lint
Kshitij-Katiyar Jan 16, 2025
d18f2bd
fixed step title
Kshitij-Katiyar Jan 16, 2025
a906811
removed unwanted variables
Kshitij-Katiyar Jan 17, 2025
dfdd4a4
removed extra lines in flow instructions
Kshitij-Katiyar Jan 27, 2025
ad309d0
Merge branch 'flow_setup' of github.com:Brightscout/mattermost-plugin…
Kshitij-Katiyar Jan 30, 2025
9551c0c
updated flow messages
Kshitij-Katiyar Feb 4, 2025
550b82e
review fixes
Kshitij-Katiyar Feb 21, 2025
2b88e55
Merge branch 'flow_setup' of github.com:mattermost-community/mattermo…
Kshitij-Katiyar Feb 21, 2025
8070b28
fixed callJSONwithURL call
Kshitij-Katiyar Feb 21, 2025
862b419
review fixes
Kshitij-Katiyar Feb 24, 2025
4a7d4be
fixed lint
Kshitij-Katiyar Feb 24, 2025
712958d
Merge branch 'flow_setup' of github.com:mattermost-community/mattermo…
Kshitij-Katiyar Feb 24, 2025
b5c88b6
fixed lint
Kshitij-Katiyar Feb 24, 2025
c43c30a
Added support for notification with server version greater than 9 (#127)
Kshitij-Katiyar Mar 10, 2025
ef69358
review fixes
Kshitij-Katiyar Mar 11, 2025
51969de
review fixes
Kshitij-Katiyar Mar 12, 2025
52329ad
removed unused code
Kshitij-Katiyar Mar 12, 2025
91b85ec
fixed go mod enteries
Kshitij-Katiyar Mar 12, 2025
ce2dc09
merge with base
Kshitij-Katiyar Mar 12, 2025
1478e36
removed external css sheet
Kshitij-Katiyar Mar 12, 2025
a20d86e
Merge branch 'oauth_epic' of github.com:mattermost-community/mattermo…
Kshitij-Katiyar Mar 13, 2025
d111eb3
review fixes
Kshitij-Katiyar Mar 13, 2025
2dbe9bd
Merge branch 'oauth_epic' of github.com:mattermost-community/mattermo…
Kshitij-Katiyar Mar 13, 2025
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
74 changes: 74 additions & 0 deletions assets/templates/api/v1/oauth2/complete.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!DOCTYPE html>
<html lang="en">
<head>
<style>
body {
color: rgb(23, 43, 77);
letter-spacing: -0.01em;
}

.flex-parent {
padding: 50px;
}

.btn {
-webkit-transition: all 0.15s ease;
-moz-transition: all 0.15s ease;
-o-transition: all 0.15s ease;
transition: all 0.15s ease false;
padding: 0.5em 1em;
font-size: inherit;
border: none;
height: auto;
border-radius: 4px;
cursor: pointer;
}

.btn-primary {
color: rgb(255, 255, 255);
background: rgb(0, 82, 204);
}

.btn-primary:hover,
.btn-primary:active {
background: rgb(0, 101, 255);
}

.accounts-container {
padding: 1.6em 0 0.8em;
opacity: 0.6;
}

.icon--check {
margin-right: 4px;
}

.message-container {
margin-top: 20px;
}
</style>
<script>
function closeTab() {
window.close();
}
</script>
</head>
<body>
<div class="flex-parent">
<h3>
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="15" class="icon--check" viewBox="0 0 18 14">
<path fill="#0052CC" d="M100.649576,76.1740942 C100.649576,76.4531124 100.537969,76.7321306 100.337075,76.9330237 L90.7388497,86.5312494 C90.5379566,86.7321425 90.2589384,86.8437498 89.9799202,86.8437498 C89.700902,86.8437498 89.4218838,86.7321425 89.2209908,86.5312494 L83.6629484,80.9732071 C83.4620553,80.772314 83.350448,80.4932958 83.350448,80.2142776 C83.350448,79.9352594 83.4620553,79.6562412 83.6629484,79.4553481 L85.1808074,77.9374892 C85.3817005,77.7365961 85.6607186,77.6249888 85.9397368,77.6249888 C86.218755,77.6249888 86.4977732,77.7365961 86.6986663,77.9374892 L89.9799202,81.2299038 L97.3013575,73.8973058 C97.5022506,73.6964127 97.7812688,73.5848054 98.060287,73.5848054 C98.3393052,73.5848054 98.6183234,73.6964127 98.8192165,73.8973058 L100.337075,75.4151648 C100.537969,75.6160579 100.649576,75.895076 100.649576,76.1740942 Z" transform="translate(-83 -73)"/>
</svg>
Mattermost user is now connected to Confluence
</h3>
<div class="accounts-container">
<div>Mattermost account: {{ .MattermostDisplayName }}</div>
<div>Confluence account: {{ .ConfluenceDisplayName }}</div>
</div>
<div class="message-container">
<p>You may now safely close this tab.</p>
<button class="btn btn-primary" onclick="closeTab()">Close Tab</button>
</div>
</div>
</body>
</html>
31 changes: 31 additions & 0 deletions assets/templates/other/message.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en">
<head>
<style>
body {
color: rgb(23, 43, 77);
letter-spacing: -0.01em;
}

.flex-parent {
padding: 50px;
}

.message-container {
padding: 1.6em 0 0.8em;
opacity: .6;
}
</style>
<link rel="stylesheet" href="https://unpkg.com/@atlaskit/css-reset@2.0.0/dist/bundle.css" media="all">
Comment thread
davidkrauser marked this conversation as resolved.
Outdated
</head>
<body>
<div class="flex-parent">
<h3>
{{ .Header}}
</h3>
<div class="message-container">
<div>{{ .Message }}</div>
</div>
</div>
</body>
</html>
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ toolchain go1.22.8
require (
bou.ke/monkey v1.0.2
github.com/gorilla/mux v1.8.1
github.com/levigross/exp-html v0.0.0-20120902181939-8df60c69a8f5
github.com/mattermost/mattermost/server/public v0.1.9
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.10.0
github.com/thoas/go-funk v0.9.3
go.uber.org/atomic v1.11.0
golang.org/x/oauth2 v0.21.0
)

require (
Expand Down Expand Up @@ -42,13 +45,19 @@ require (
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/philhofer/fwd v1.1.3-0.20240612014219-fbbf4953d986 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rudderlabs/analytics-go v3.3.3+incompatible // indirect
github.com/segmentio/backo-go v1.1.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/tidwall/gjson v1.17.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tinylib/msgp v1.2.0 // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/wiggin77/merror v1.0.5 // indirect
github.com/wiggin77/srslog v1.0.1 // indirect
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sys v0.22.0 // indirect
Expand Down
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/levigross/exp-html v0.0.0-20120902181939-8df60c69a8f5 h1:W7p+m/AECTL3s/YR5RpQ4hz5SjNeKzZBl1q36ws12s0=
github.com/levigross/exp-html v0.0.0-20120902181939-8df60c69a8f5/go.mod h1:QMe2wuKJ0o7zIVE8AqiT8rd8epmm6WDIZ2wyuBqYPzM=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lunixbochs/vtclean v1.0.0/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=
Expand Down Expand Up @@ -147,7 +149,11 @@ github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e/go.mod h1:daVV7q
github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/rudderlabs/analytics-go v3.3.3+incompatible h1:OG0XlKoXfr539e2t1dXtTB+Gr89uFW+OUNQBVhHIIBY=
github.com/rudderlabs/analytics-go v3.3.3+incompatible/go.mod h1:LF8/ty9kUX4PTY3l5c97K3nZZaX5Hwsvt+NBaRL/f30=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/segmentio/backo-go v1.1.0 h1:cJIfHQUdmLsd8t9IXqf5J8SdrOMn9vMa7cIvOavHAhc=
github.com/segmentio/backo-go v1.1.0/go.mod h1:ckenwdf+v/qbyhVdNPWHnqh2YdJBED1O9cidYyM5J18=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/shurcooL/component v0.0.0-20170202220835-f88ec8f54cc4/go.mod h1:XhFIlyj5a1fBNx5aJTbKoIq0mNaPvOagO+HjB3EtxrY=
github.com/shurcooL/events v0.0.0-20181021180414-410e4ca65f48/go.mod h1:5u70Mqkb5O5cxEA8nxTsgrgLehJeAw6Oc4Ab1c/P1HM=
Expand Down Expand Up @@ -179,12 +185,22 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
github.com/thoas/go-funk v0.9.3 h1:7+nAEx3kn5ZJcnDm2Bh23N2yOtweO14bi//dvRtgLpw=
github.com/thoas/go-funk v0.9.3/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q=
github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U=
github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tinylib/msgp v1.2.0 h1:0uKB/662twsVBpYUPbokj4sTSKhWFKB7LopO2kWK8lY=
github.com/tinylib/msgp v1.2.0/go.mod h1:2vIGs3lcUo8izAATNobrCHevYZC/LMsJtw4JPiYPHro=
github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU=
Expand All @@ -197,6 +213,8 @@ github.com/wiggin77/merror v1.0.5 h1:P+lzicsn4vPMycAf2mFf7Zk6G9eco5N+jB1qJ2XW3ME
github.com/wiggin77/merror v1.0.5/go.mod h1:H2ETSu7/bPE0Ymf4bEwdUoo73OOEkdClnoRisfw0Nm0=
github.com/wiggin77/srslog v1.0.1 h1:gA2XjSMy3DrRdX9UqLuDtuVAAshb8bE1NhX1YK0Qe+8=
github.com/wiggin77/srslog v1.0.1/go.mod h1:fehkyYDq1QfuYn60TDPu9YdY2bB85VUW2mvN1WynEls=
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c h1:3lbZUMbMiGUW/LMkfsEABsc5zNT9+b1CvsJx47JzJ8g=
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c/go.mod h1:UrdRz5enIKZ63MEE3IF9l2/ebyx59GyGgPi+tICQdmM=
go.opencensus.io v0.18.0/go.mod h1:vKdFvxhtzZ9onBp9VKHK8z/sRpBMnKAsufL7wlDrCOA=
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
Expand Down Expand Up @@ -226,6 +244,8 @@ golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAG
golang.org/x/oauth2 v0.0.0-20181017192945-9dcd33a902f4/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20181203162652-d668ce993890/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs=
golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/perf v0.0.0-20180704124530-6e6d33e29852/go.mod h1:JLpeXjPJfIyPr5TlbXLkXWLhP8nz10XfvxElABhCtcw=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
9 changes: 8 additions & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@
"type": "generated",
"help_text": "The secret used to authenticate the webhook to Mattermost.",
"regenerate_help_text": "Regenerates the secret for the webhook URL endpoint. Regenerating the secret invalidates your existing Confluence integrations."
}
},
{
"key": "EncryptionKey",
"display_name": "Encryption Key:",
"type": "generated",
"help_text": "The encryption key used to encrypt the authorization token.",
"regenerate_help_text": "Regenerates the encryption key for encrypting the authorization token. Regenerating the authorization invalidates your existing Confluence connection."
}
]
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller
package main

import (
"html/template"
Expand All @@ -18,7 +18,7 @@ var atlassianConnectJSON = &Endpoint{
RequiresAdmin: false,
}

func renderAtlassianConnectJSON(w http.ResponseWriter, r *http.Request) {
func renderAtlassianConnectJSON(w http.ResponseWriter, r *http.Request, _ *Plugin) {
Comment thread
raghavaggarwal2308 marked this conversation as resolved.
conf := config.GetConfig()
if status, err := verifyHTTPSecret(conf.Secret, r.FormValue("secret")); err != nil {
http.Error(w, err.Error(), status)
Expand Down
131 changes: 131 additions & 0 deletions server/auth_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License for license information.

package main

import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"encoding/json"
"io"

"github.com/pkg/errors"
"golang.org/x/oauth2"

"github.com/mattermost/mattermost-plugin-confluence/server/config"
)

type AuthToken struct {
Token *oauth2.Token `json:"token,omitempty"`
}

func (p *Plugin) NewEncodedAuthToken(token *oauth2.Token) (encodedToken string, returnErr error) {
encryptionSecret := config.GetConfig().EncryptionKey

t := AuthToken{
Token: token,
}

jsonBytes, err := json.Marshal(t)
if err != nil {
return "", err
}

encrypted, err := encrypt(jsonBytes, []byte(encryptionSecret))
if err != nil {
return "", err
}

return encode(encrypted), nil
}

func (p *Plugin) ParseAuthToken(encoded string) (token *oauth2.Token, returnErr error) {
t := AuthToken{}
encryptionSecret := config.GetConfig().EncryptionKey

decoded, err := decode(encoded)
if err != nil {
return nil, err
}

jsonBytes, err := decrypt(decoded, []byte(encryptionSecret))
if err != nil {
return nil, err
}

if err = json.Unmarshal(jsonBytes, &t); err != nil {
return nil, err
}

return t.Token, nil
}

func encode(encrypted []byte) string {
encoded := make([]byte, base64.URLEncoding.EncodedLen(len(encrypted)))
base64.URLEncoding.Encode(encoded, encrypted)
return string(encoded)
}

func encrypt(plain, secret []byte) ([]byte, error) {
if len(secret) == 0 {
return plain, nil
}

block, err := aes.NewCipher(secret)
if err != nil {
return nil, err
}

aesgcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}

nonce := make([]byte, aesgcm.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return nil, err
}

sealed := aesgcm.Seal(nil, nonce, plain, nil)
return append(nonce, sealed...), nil
}

func decode(encoded string) ([]byte, error) {
decoded := make([]byte, base64.URLEncoding.DecodedLen(len(encoded)))
n, err := base64.URLEncoding.Decode(decoded, []byte(encoded))
if err != nil {
return nil, err
}
return decoded[:n], nil
}

func decrypt(encrypted, secret []byte) ([]byte, error) {
if len(secret) == 0 {
return encrypted, nil
}

block, err := aes.NewCipher(secret)
if err != nil {
return nil, err
}

aesgcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}

nonceSize := aesgcm.NonceSize()
if len(encrypted) < nonceSize {
return nil, errors.New("token too short")
}

nonce, encrypted := encrypted[:nonceSize], encrypted[nonceSize:]
plain, err := aesgcm.Open(nil, nonce, encrypted, nil)
if err != nil {
return nil, err
}

return plain, nil
}
18 changes: 18 additions & 0 deletions server/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import "github.com/mattermost/mattermost-plugin-confluence/server/util/types"

// Client is the combined interface for all upstream APIs and convenience methods.
type Client interface {
RESTService
}

// RESTService is the low-level interface for invoking the upstream service.
// Endpoint can be a "short" API URL path, including the version desired, like "v3/user",
// or a fully-qualified URL, with a non-empty scheme.
type RESTService interface {
GetSelf() (*types.ConfluenceUser, error)
GetSpaceData(string) (*SpaceResponse, error)
GetPageData(int) (*PageResponse, error)
GetSpaceKeyFromSpaceID(int64) (string, error)
}
Loading