-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the feature to create pages from mm for both cloud and server
- Loading branch information
1 parent
789389f
commit 56bfd5a
Showing
24 changed files
with
1,040 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/mattermost/mattermost-server/v6/model" | ||
|
||
"github.com/mattermost/mattermost-plugin-confluence/server/config" | ||
"github.com/mattermost/mattermost-plugin-confluence/server/serializer" | ||
"github.com/mattermost/mattermost-plugin-confluence/server/utils/types" | ||
) | ||
|
||
const ( | ||
pageCreateSuccess = "You created a page [%s](%s) in space [%s](%s)" | ||
) | ||
|
||
func (p *Plugin) handleCreatePage(w http.ResponseWriter, r *http.Request) { | ||
params := mux.Vars(r) | ||
spaceKey := params["spaceKey"] | ||
channelID := params["channelID"] | ||
userID := r.Header.Get(config.HeaderMattermostUserID) | ||
|
||
instance, err := p.getInstanceFromURL(r.URL.Path) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
conn, err := p.userStore.LoadConnection(types.ID(instance.GetURL()), types.ID(userID)) | ||
if err != nil { | ||
p.LogAndRespondError(w, http.StatusInternalServerError, "error in loading connection.", err) | ||
return | ||
} | ||
|
||
client, err := instance.GetClient(conn) | ||
if err != nil { | ||
p.LogAndRespondError(w, http.StatusInternalServerError, "not able to get Client.", err) | ||
return | ||
} | ||
|
||
body, err := ioutil.ReadAll(r.Body) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
pageDetails, err := serializer.PageDetailsFromJSON(body) | ||
if err != nil { | ||
p.LogAndRespondError(w, http.StatusInternalServerError, "error decoding request body for page details.", err) | ||
return | ||
} | ||
|
||
createPageResponse, err := client.CreatePage(spaceKey, pageDetails) | ||
if err != nil { | ||
p.LogAndRespondError(w, http.StatusInternalServerError, "not able to create page.", err) | ||
return | ||
} | ||
|
||
post := &model.Post{ | ||
UserId: p.conf.botUserID, | ||
ChannelId: channelID, | ||
Message: fmt.Sprintf(pageCreateSuccess, pageDetails.Title, fmt.Sprintf("%s%s", createPageResponse.Links.BaseURL, createPageResponse.Links.Self), spaceKey, fmt.Sprintf("%s%s", createPageResponse.Links.BaseURL, createPageResponse.Space.Links.Self)), | ||
} | ||
_ = p.API.SendEphemeralPost(userID, post) | ||
ReturnStatusOK(w) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/mattermost/mattermost-plugin-confluence/server/config" | ||
"github.com/mattermost/mattermost-plugin-confluence/server/utils/types" | ||
) | ||
|
||
func (p *Plugin) handleGetSpacesForConfluenceURL(w http.ResponseWriter, r *http.Request) { | ||
userID := r.Header.Get(config.HeaderMattermostUserID) | ||
|
||
instance, err := p.getInstanceFromURL(r.URL.Path) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
conn, err := p.userStore.LoadConnection(types.ID(instance.GetURL()), types.ID(userID)) | ||
if err != nil { | ||
p.LogAndRespondError(w, http.StatusInternalServerError, "error in loading connection.", err) | ||
return | ||
} | ||
|
||
client, err := instance.GetClient(conn) | ||
if err != nil { | ||
p.LogAndRespondError(w, http.StatusInternalServerError, "not able to get Client.", err) | ||
return | ||
} | ||
|
||
spaces, err := client.GetSpacesForConfluenceURL() | ||
if err != nil { | ||
p.LogAndRespondError(w, http.StatusInternalServerError, "not able to get Spaces for confluence url.", err) | ||
return | ||
} | ||
b, _ := json.Marshal(spaces) | ||
w.Header().Set("Content-Type", "application/json") | ||
_, _ = w.Write([]byte(string(b))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package serializer | ||
|
||
import "encoding/json" | ||
|
||
type PageDetails struct { | ||
Title string `json:"title"` | ||
Description string `json:"description"` | ||
} | ||
|
||
func PageDetailsFromJSON(body []byte) (*PageDetails, error) { | ||
var pd PageDetails | ||
if err := json.Unmarshal(body, &pd); err != nil { | ||
return nil, err | ||
} | ||
return &pd, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.