Skip to content

Commit 53d7770

Browse files
committed
[MI-2814]: Review fixes done of github PR mattermost#636
1 parent 6429412 commit 53d7770

File tree

11 files changed

+82
-201
lines changed

11 files changed

+82
-201
lines changed

server/plugin/api.go

Lines changed: 15 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,7 @@ func (p *Plugin) initializeAPI() {
9797
apiRouter.HandleFunc("/create_issue", p.checkAuth(p.attachUserContext(p.createIssue), ResponseTypePlain)).Methods(http.MethodPost)
9898
apiRouter.HandleFunc("/close_or_reopen_issue", p.checkAuth(p.attachUserContext(p.closeOrReopenIssue), ResponseTypePlain)).Methods(http.MethodPost)
9999
apiRouter.HandleFunc("/update_issue", p.checkAuth(p.attachUserContext(p.updateIssue), ResponseTypePlain)).Methods(http.MethodPost)
100-
apiRouter.HandleFunc("/edit_issue_modal", p.checkAuth(p.attachUserContext(p.openIssueEditModal), ResponseTypePlain)).Methods(http.MethodPost)
101-
apiRouter.HandleFunc("/close_reopen_issue_modal", p.checkAuth(p.attachUserContext(p.openCloseOrReopenIssueModal), ResponseTypePlain)).Methods(http.MethodPost)
102-
apiRouter.HandleFunc("/attach_comment_issue_modal", p.checkAuth(p.attachUserContext(p.openAttachCommentIssueModal), ResponseTypePlain)).Methods(http.MethodPost)
100+
apiRouter.HandleFunc("/issue_info", p.checkAuth(p.attachUserContext(p.getIssueInfo), ResponseTypePlain)).Methods(http.MethodPost)
103101
apiRouter.HandleFunc("/create_issue_comment", p.checkAuth(p.attachUserContext(p.createIssueComment), ResponseTypePlain)).Methods(http.MethodPost)
104102
apiRouter.HandleFunc("/mentions", p.checkAuth(p.attachUserContext(p.getMentions), ResponseTypePlain)).Methods(http.MethodGet)
105103
apiRouter.HandleFunc("/unreads", p.checkAuth(p.attachUserContext(p.getUnreads), ResponseTypePlain)).Methods(http.MethodGet)
@@ -959,72 +957,7 @@ func (p *Plugin) updateSettings(c *serializer.UserContext, w http.ResponseWriter
959957
p.writeJSON(w, info.Settings)
960958
}
961959

962-
func (p *Plugin) openAttachCommentIssueModal(c *serializer.UserContext, w http.ResponseWriter, r *http.Request) {
963-
req := &serializer.OpenCreateCommentOrEditIssueModalRequestBody{}
964-
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
965-
c.Log.WithError(err).Warnf("Error decoding the JSON body")
966-
p.writeAPIError(w, &serializer.APIErrorResponse{ID: "", Message: "Please provide a valid JSON object.", StatusCode: http.StatusBadRequest})
967-
return
968-
}
969-
970-
userID := r.Header.Get(constants.HeaderMattermostUserID)
971-
post, appErr := p.API.GetPost(req.PostID)
972-
if appErr != nil {
973-
p.writeAPIError(w, &serializer.APIErrorResponse{ID: "", Message: fmt.Sprintf("failed to load the post %s", req.PostID), StatusCode: http.StatusInternalServerError})
974-
return
975-
}
976-
if post == nil {
977-
p.writeAPIError(w, &serializer.APIErrorResponse{ID: "", Message: fmt.Sprintf("failed to load the post %s : not found", req.PostID), StatusCode: http.StatusNotFound})
978-
return
979-
}
980-
981-
p.API.PublishWebSocketEvent(
982-
wsEventAttachCommentToIssue,
983-
map[string]interface{}{
984-
"postId": post.Id,
985-
"owner": req.RepoOwner,
986-
"repo": req.RepoName,
987-
"number": req.IssueNumber,
988-
},
989-
&model.WebsocketBroadcast{UserId: userID},
990-
)
991-
}
992-
993-
func (p *Plugin) openCloseOrReopenIssueModal(c *serializer.UserContext, w http.ResponseWriter, r *http.Request) {
994-
req := &serializer.OpenCreateCommentOrEditIssueModalRequestBody{}
995-
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
996-
c.Log.WithError(err).Warnf("Error decoding the JSON body")
997-
p.writeAPIError(w, &serializer.APIErrorResponse{ID: "", Message: "Please provide a valid JSON object.", StatusCode: http.StatusBadRequest})
998-
return
999-
}
1000-
1001-
userID := r.Header.Get(constants.HeaderMattermostUserID)
1002-
1003-
post, appErr := p.API.GetPost(req.PostID)
1004-
if appErr != nil {
1005-
p.writeAPIError(w, &serializer.APIErrorResponse{ID: "", Message: fmt.Sprintf("failed to load the post %s", req.PostID), StatusCode: http.StatusInternalServerError})
1006-
return
1007-
}
1008-
if post == nil {
1009-
p.writeAPIError(w, &serializer.APIErrorResponse{ID: "", Message: fmt.Sprintf("failed to load the post %s : not found", req.PostID), StatusCode: http.StatusNotFound})
1010-
return
1011-
}
1012-
1013-
p.API.PublishWebSocketEvent(
1014-
wsEventCloseOrReopenIssue,
1015-
map[string]interface{}{
1016-
"channel_id": post.ChannelId,
1017-
"owner": req.RepoOwner,
1018-
"repo": req.RepoName,
1019-
"number": req.IssueNumber,
1020-
"status": req.Status,
1021-
"postId": req.PostID,
1022-
},
1023-
&model.WebsocketBroadcast{UserId: userID},
1024-
)
1025-
}
1026-
1027-
func (p *Plugin) openIssueEditModal(c *serializer.UserContext, w http.ResponseWriter, r *http.Request) {
960+
func (p *Plugin) getIssueInfo(c *serializer.UserContext, w http.ResponseWriter, r *http.Request) {
1028961
req := &serializer.OpenCreateCommentOrEditIssueModalRequestBody{}
1029962
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
1030963
c.Log.WithError(err).Warnf("Error decoding the JSON body")
@@ -1080,7 +1013,6 @@ func (p *Plugin) openIssueEditModal(c *serializer.UserContext, w http.ResponseWr
10801013
milestoneNumber = *issue.Milestone.Number
10811014
}
10821015

1083-
userID := r.Header.Get(constants.HeaderMattermostUserID)
10841016
post, appErr := p.API.GetPost(req.PostID)
10851017
if appErr != nil {
10861018
p.writeAPIError(w, &serializer.APIErrorResponse{ID: "", Message: fmt.Sprintf("failed to load the post %s", req.PostID), StatusCode: http.StatusInternalServerError})
@@ -1091,24 +1023,20 @@ func (p *Plugin) openIssueEditModal(c *serializer.UserContext, w http.ResponseWr
10911023
return
10921024
}
10931025

1094-
p.API.PublishWebSocketEvent(
1095-
wsEventCreateOrUpdateIssue,
1096-
map[string]interface{}{
1097-
"title": *issue.Title,
1098-
"channel_id": post.ChannelId,
1099-
"postId": req.PostID,
1100-
"milestone_title": milestoneTitle,
1101-
"milestone_number": milestoneNumber,
1102-
"assignees": assignees,
1103-
"labels": labels,
1104-
"description": description,
1105-
"repo_full_name": fmt.Sprintf("%s/%s", req.RepoOwner, req.RepoName),
1106-
"issue_number": *issue.Number,
1107-
},
1108-
&model.WebsocketBroadcast{UserId: userID},
1109-
)
1026+
issueInfo := map[string]interface{}{
1027+
"title": *issue.Title,
1028+
"channel_id": post.ChannelId,
1029+
"postId": req.PostID,
1030+
"milestone_title": milestoneTitle,
1031+
"milestone_number": milestoneNumber,
1032+
"assignees": assignees,
1033+
"labels": labels,
1034+
"description": description,
1035+
"repo_full_name": fmt.Sprintf("%s/%s", req.RepoOwner, req.RepoName),
1036+
"issue_number": *issue.Number,
1037+
}
11101038

1111-
p.writeJSON(w, issue)
1039+
p.writeJSON(w, issueInfo)
11121040
}
11131041

11141042
func (p *Plugin) getIssueByNumber(c *serializer.UserContext, w http.ResponseWriter, r *http.Request) {

server/plugin/plugin.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,9 @@ const (
4040
wsEventConnect = "connect"
4141
wsEventDisconnect = "disconnect"
4242
// WSEventConfigUpdate is the WebSocket event to update the configurations on webapp.
43-
WSEventConfigUpdate = "config_update"
44-
wsEventRefresh = "refresh"
45-
wsEventCreateOrUpdateIssue = "createOrUpdateIssue"
46-
wsEventCloseOrReopenIssue = "closeOrReopenIssue"
47-
wsEventAttachCommentToIssue = "attachCommentToIssue"
43+
WSEventConfigUpdate = "config_update"
44+
wsEventRefresh = "refresh"
45+
wsEventCreateIssue = "createIssue"
4846

4947
WSEventRefresh = "refresh"
5048

@@ -482,7 +480,7 @@ func (p *Plugin) disconnectGitHubAccount(userID string) {
482480

483481
func (p *Plugin) openIssueCreateModal(userID string, channelID string, title string) {
484482
p.API.PublishWebSocketEvent(
485-
wsEventCreateOrUpdateIssue,
483+
wsEventCreateIssue,
486484
map[string]interface{}{
487485
"title": title,
488486
"channel_id": channelID,

webapp/src/actions/index.js

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -211,47 +211,11 @@ export function getMilestoneOptions(repo) {
211211
};
212212
}
213213

214-
export function attachCommentIssueModal(payload) {
214+
export function issueInfo(payload) {
215215
return async (dispatch, getState) => {
216216
let data;
217217
try {
218-
data = await Client.attachCommentIssueModal(payload);
219-
} catch (error) {
220-
return {error};
221-
}
222-
223-
const connected = await checkAndHandleNotConnected(data)(dispatch, getState);
224-
if (!connected) {
225-
return {error: data};
226-
}
227-
228-
return {data};
229-
};
230-
}
231-
232-
export function editIssueModal(payload) {
233-
return async (dispatch, getState) => {
234-
let data;
235-
try {
236-
data = await Client.editIssueModal(payload);
237-
} catch (error) {
238-
return {error};
239-
}
240-
241-
const connected = await checkAndHandleNotConnected(data)(dispatch, getState);
242-
if (!connected) {
243-
return {error: data};
244-
}
245-
246-
return {data};
247-
};
248-
}
249-
250-
export function closeOrReopenIssueModal(payload) {
251-
return async (dispatch, getState) => {
252-
let data;
253-
try {
254-
data = await Client.closeOrReopenIssueModal(payload);
218+
data = await Client.issueInfo(payload);
255219
} catch (error) {
256220
return {error};
257221
}

webapp/src/client/client.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,8 @@ import {ClientError} from 'mattermost-redux/client/client4';
77
import {id as pluginId} from '../manifest';
88

99
export default class Client {
10-
editIssueModal = async (payload) => {
11-
return this.doPost(`${this.url}/edit_issue_modal`, payload);
12-
}
13-
14-
closeOrReopenIssueModal = async (payload) => {
15-
return this.doPost(`${this.url}/close_reopen_issue_modal`, payload);
16-
}
17-
18-
attachCommentIssueModal = async (payload) => {
19-
return this.doPost(`${this.url}/attach_comment_issue_modal`, payload);
10+
issueInfo = async (payload) => {
11+
return this.doPost(`${this.url}/issue_info`, payload);
2012
}
2113

2214
setServerRoute(url) {

webapp/src/components/github_issue/index.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {Theme} from 'mattermost-redux/types/preferences';
44
import {Post} from 'mattermost-redux/types/posts';
55
import {useDispatch} from 'react-redux';
66

7-
import {attachCommentIssueModal, editIssueModal, closeOrReopenIssueModal} from '../../actions';
7+
import {openCreateCommentOnIssueModal, openCreateOrUpdateIssueModal, openCloseOrReopenIssueModal} from '../../actions';
88

99
type GithubIssueProps = {
1010
theme: Theme,
@@ -24,24 +24,25 @@ const GithubIssue = ({theme, post}: GithubIssueProps) => {
2424
issue_number: postProps.issue_number,
2525
postId: post.id,
2626
status: postProps.status,
27+
channel_id: post.channel_id,
2728
};
2829

2930
const content = (
3031
<div>
3132
<button
3233
style={{...style.button, ...style.other_buttons}}
3334
className='btn btn-primary'
34-
onClick={() => dispatch(attachCommentIssueModal(issue))}
35+
onClick={() => dispatch(openCreateCommentOnIssueModal(issue))}
3536
>{'Comment'}</button>
3637
<button
3738
style={{...style.button, ...style.other_buttons}}
3839
className='btn btn-primary'
39-
onClick={() => dispatch(editIssueModal(issue))}
40+
onClick={() => dispatch(openCreateOrUpdateIssueModal(issue))}
4041
>{'Edit'}</button>
4142
<button
4243
style={{...style.button, ...{...postProps.status === 'Close' ? style.close_or_reopen_button : style.other_buttons}}}
4344
className='btn btn-primary'
44-
onClick={() => dispatch(closeOrReopenIssueModal(issue))}
45+
onClick={() => dispatch(openCloseOrReopenIssueModal(issue))}
4546
>{postProps.status}</button>
4647
</div>
4748
);

webapp/src/components/modals/attach_comment_to_issue/attach_comment_to_issue.jsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ export default class AttachIssueModal extends PureComponent {
4040
}
4141

4242
if (!this.state.issueValue) {
43-
const {owner, repo, number} = this.props.messageData ?? {};
43+
const {repo_owner, repo_name, issue_number} = this.props.messageData ?? {};
4444
const issue = {
45-
owner,
46-
repo,
47-
number,
45+
owner: repo_owner,
46+
repo: repo_name,
47+
number: issue_number,
4848
comment: this.state.comment,
4949
post_id: this.props.post.id,
5050
show_attached_message: false,
@@ -114,9 +114,9 @@ export default class AttachIssueModal extends PureComponent {
114114
return null;
115115
}
116116

117-
const {number} = messageData ?? {};
118-
const modalTitle = number ? 'Create a comment to GitHub Issue' : 'Attach Message to GitHub Issue';
119-
const component = number ? (
117+
const {issue_number} = messageData ?? {};
118+
const modalTitle = issue_number ? 'Create a comment to GitHub Issue' : 'Attach Message to GitHub Issue';
119+
const component = issue_number ? (
120120
<div>
121121
<Input
122122
label='Create a comment'
@@ -138,7 +138,7 @@ export default class AttachIssueModal extends PureComponent {
138138
<Input
139139
label='Message Attached to GitHub Issue'
140140
type='textarea'
141-
isDisabled={true}
141+
isDisabled={false}
142142
value={post?.message}
143143
disabled={false}
144144
readOnly={true}

webapp/src/components/modals/close_reopen_issue/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ const CloseOrReopenIssueModal = ({theme}: {theme: Theme}) => {
3636
channel_id: messageData.channel_id,
3737
issue_comment: comment,
3838
status_reason: currentStatus,
39-
repo: messageData.repo,
40-
number: messageData.number,
41-
owner: messageData.owner,
39+
repo: messageData.repo_name,
40+
number: messageData.issue_number,
41+
owner: messageData.repo_owner,
4242
status: messageData.status,
4343
postId: messageData.postId,
4444
};

0 commit comments

Comments
 (0)