Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
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
102 changes: 15 additions & 87 deletions server/plugin/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ func (p *Plugin) initializeAPI() {
apiRouter.HandleFunc("/create_issue", p.checkAuth(p.attachUserContext(p.createIssue), ResponseTypePlain)).Methods(http.MethodPost)
apiRouter.HandleFunc("/close_or_reopen_issue", p.checkAuth(p.attachUserContext(p.closeOrReopenIssue), ResponseTypePlain)).Methods(http.MethodPost)
apiRouter.HandleFunc("/update_issue", p.checkAuth(p.attachUserContext(p.updateIssue), ResponseTypePlain)).Methods(http.MethodPost)
apiRouter.HandleFunc("/edit_issue_modal", p.checkAuth(p.attachUserContext(p.openIssueEditModal), ResponseTypePlain)).Methods(http.MethodPost)
apiRouter.HandleFunc("/close_reopen_issue_modal", p.checkAuth(p.attachUserContext(p.openCloseOrReopenIssueModal), ResponseTypePlain)).Methods(http.MethodPost)
apiRouter.HandleFunc("/attach_comment_issue_modal", p.checkAuth(p.attachUserContext(p.openAttachCommentIssueModal), ResponseTypePlain)).Methods(http.MethodPost)
apiRouter.HandleFunc("/issue_info", p.checkAuth(p.attachUserContext(p.getIssueInfo), ResponseTypePlain)).Methods(http.MethodPost)
apiRouter.HandleFunc("/create_issue_comment", p.checkAuth(p.attachUserContext(p.createIssueComment), ResponseTypePlain)).Methods(http.MethodPost)
apiRouter.HandleFunc("/mentions", p.checkAuth(p.attachUserContext(p.getMentions), ResponseTypePlain)).Methods(http.MethodGet)
apiRouter.HandleFunc("/unreads", p.checkAuth(p.attachUserContext(p.getUnreads), ResponseTypePlain)).Methods(http.MethodGet)
Expand Down Expand Up @@ -959,72 +957,7 @@ func (p *Plugin) updateSettings(c *serializer.UserContext, w http.ResponseWriter
p.writeJSON(w, info.Settings)
}

func (p *Plugin) openAttachCommentIssueModal(c *serializer.UserContext, w http.ResponseWriter, r *http.Request) {
req := &serializer.OpenCreateCommentOrEditIssueModalRequestBody{}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
c.Log.WithError(err).Warnf("Error decoding the JSON body")
p.writeAPIError(w, &serializer.APIErrorResponse{ID: "", Message: "Please provide a valid JSON object.", StatusCode: http.StatusBadRequest})
return
}

userID := r.Header.Get(constants.HeaderMattermostUserID)
post, appErr := p.API.GetPost(req.PostID)
if appErr != nil {
p.writeAPIError(w, &serializer.APIErrorResponse{ID: "", Message: fmt.Sprintf("failed to load the post %s", req.PostID), StatusCode: http.StatusInternalServerError})
return
}
if post == nil {
p.writeAPIError(w, &serializer.APIErrorResponse{ID: "", Message: fmt.Sprintf("failed to load the post %s : not found", req.PostID), StatusCode: http.StatusNotFound})
return
}

p.API.PublishWebSocketEvent(
wsEventAttachCommentToIssue,
map[string]interface{}{
"postId": post.Id,
"owner": req.RepoOwner,
"repo": req.RepoName,
"number": req.IssueNumber,
},
&model.WebsocketBroadcast{UserId: userID},
)
}

func (p *Plugin) openCloseOrReopenIssueModal(c *serializer.UserContext, w http.ResponseWriter, r *http.Request) {
req := &serializer.OpenCreateCommentOrEditIssueModalRequestBody{}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
c.Log.WithError(err).Warnf("Error decoding the JSON body")
p.writeAPIError(w, &serializer.APIErrorResponse{ID: "", Message: "Please provide a valid JSON object.", StatusCode: http.StatusBadRequest})
return
}

userID := r.Header.Get(constants.HeaderMattermostUserID)

post, appErr := p.API.GetPost(req.PostID)
if appErr != nil {
p.writeAPIError(w, &serializer.APIErrorResponse{ID: "", Message: fmt.Sprintf("failed to load the post %s", req.PostID), StatusCode: http.StatusInternalServerError})
return
}
if post == nil {
p.writeAPIError(w, &serializer.APIErrorResponse{ID: "", Message: fmt.Sprintf("failed to load the post %s : not found", req.PostID), StatusCode: http.StatusNotFound})
return
}

p.API.PublishWebSocketEvent(
wsEventCloseOrReopenIssue,
map[string]interface{}{
"channel_id": post.ChannelId,
"owner": req.RepoOwner,
"repo": req.RepoName,
"number": req.IssueNumber,
"status": req.Status,
"postId": req.PostID,
},
&model.WebsocketBroadcast{UserId: userID},
)
}

func (p *Plugin) openIssueEditModal(c *serializer.UserContext, w http.ResponseWriter, r *http.Request) {
func (p *Plugin) getIssueInfo(c *serializer.UserContext, w http.ResponseWriter, r *http.Request) {
req := &serializer.OpenCreateCommentOrEditIssueModalRequestBody{}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
c.Log.WithError(err).Warnf("Error decoding the JSON body")
Expand Down Expand Up @@ -1080,7 +1013,6 @@ func (p *Plugin) openIssueEditModal(c *serializer.UserContext, w http.ResponseWr
milestoneNumber = *issue.Milestone.Number
}

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

p.API.PublishWebSocketEvent(
wsEventCreateOrUpdateIssue,
map[string]interface{}{
"title": *issue.Title,
"channel_id": post.ChannelId,
"postId": req.PostID,
"milestone_title": milestoneTitle,
"milestone_number": milestoneNumber,
"assignees": assignees,
"labels": labels,
"description": description,
"repo_full_name": fmt.Sprintf("%s/%s", req.RepoOwner, req.RepoName),
"issue_number": *issue.Number,
},
&model.WebsocketBroadcast{UserId: userID},
)
issueInfo := map[string]interface{}{
"title": *issue.Title,
"channel_id": post.ChannelId,
"postId": req.PostID,
"milestone_title": milestoneTitle,
"milestone_number": milestoneNumber,
"assignees": assignees,
"labels": labels,
"description": description,
"repo_full_name": fmt.Sprintf("%s/%s", req.RepoOwner, req.RepoName),
"issue_number": *issue.Number,
}

p.writeJSON(w, issue)
p.writeJSON(w, issueInfo)
}

func (p *Plugin) getIssueByNumber(c *serializer.UserContext, w http.ResponseWriter, r *http.Request) {
Expand Down
10 changes: 4 additions & 6 deletions server/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,9 @@ const (
wsEventConnect = "connect"
wsEventDisconnect = "disconnect"
// WSEventConfigUpdate is the WebSocket event to update the configurations on webapp.
WSEventConfigUpdate = "config_update"
wsEventRefresh = "refresh"
wsEventCreateOrUpdateIssue = "createOrUpdateIssue"
wsEventCloseOrReopenIssue = "closeOrReopenIssue"
wsEventAttachCommentToIssue = "attachCommentToIssue"
WSEventConfigUpdate = "config_update"
wsEventRefresh = "refresh"
wsEventCreateIssue = "createIssue"

WSEventRefresh = "refresh"

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

func (p *Plugin) openIssueCreateModal(userID string, channelID string, title string) {
p.API.PublishWebSocketEvent(
wsEventCreateOrUpdateIssue,
wsEventCreateIssue,
map[string]interface{}{
"title": title,
"channel_id": channelID,
Expand Down
40 changes: 2 additions & 38 deletions webapp/src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,47 +211,11 @@ export function getMilestoneOptions(repo) {
};
}

export function attachCommentIssueModal(payload) {
export function issueInfo(payload) {
return async (dispatch, getState) => {
let data;
try {
data = await Client.attachCommentIssueModal(payload);
} catch (error) {
return {error};
}

const connected = await checkAndHandleNotConnected(data)(dispatch, getState);
if (!connected) {
return {error: data};
}

return {data};
};
}

export function editIssueModal(payload) {
return async (dispatch, getState) => {
let data;
try {
data = await Client.editIssueModal(payload);
} catch (error) {
return {error};
}

const connected = await checkAndHandleNotConnected(data)(dispatch, getState);
if (!connected) {
return {error: data};
}

return {data};
};
}

export function closeOrReopenIssueModal(payload) {
return async (dispatch, getState) => {
let data;
try {
data = await Client.closeOrReopenIssueModal(payload);
data = await Client.issueInfo(payload);
} catch (error) {
return {error};
}
Expand Down
12 changes: 2 additions & 10 deletions webapp/src/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,8 @@ import {ClientError} from 'mattermost-redux/client/client4';
import {id as pluginId} from '../manifest';

export default class Client {
editIssueModal = async (payload) => {
return this.doPost(`${this.url}/edit_issue_modal`, payload);
}

closeOrReopenIssueModal = async (payload) => {
return this.doPost(`${this.url}/close_reopen_issue_modal`, payload);
}

attachCommentIssueModal = async (payload) => {
return this.doPost(`${this.url}/attach_comment_issue_modal`, payload);
issueInfo = async (payload) => {
return this.doPost(`${this.url}/issue_info`, payload);
}

setServerRoute(url) {
Expand Down
9 changes: 5 additions & 4 deletions webapp/src/components/github_issue/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {Theme} from 'mattermost-redux/types/preferences';
import {Post} from 'mattermost-redux/types/posts';
import {useDispatch} from 'react-redux';

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

type GithubIssueProps = {
theme: Theme,
Expand All @@ -24,24 +24,25 @@ const GithubIssue = ({theme, post}: GithubIssueProps) => {
issue_number: postProps.issue_number,
postId: post.id,
status: postProps.status,
channel_id: post.channel_id,
};

const content = (
<div>
<button
style={{...style.button, ...style.other_buttons}}
className='btn btn-primary'
onClick={() => dispatch(attachCommentIssueModal(issue))}
onClick={() => dispatch(openCreateCommentOnIssueModal(issue))}
>{'Comment'}</button>
<button
style={{...style.button, ...style.other_buttons}}
className='btn btn-primary'
onClick={() => dispatch(editIssueModal(issue))}
onClick={() => dispatch(openCreateOrUpdateIssueModal(issue))}
>{'Edit'}</button>
<button
style={{...style.button, ...{...postProps.status === 'Close' ? style.close_or_reopen_button : style.other_buttons}}}
className='btn btn-primary'
onClick={() => dispatch(closeOrReopenIssueModal(issue))}
onClick={() => dispatch(openCloseOrReopenIssueModal(issue))}
>{postProps.status}</button>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ export default class AttachIssueModal extends PureComponent {
}

if (!this.state.issueValue) {
const {owner, repo, number} = this.props.messageData ?? {};
const {repo_owner, repo_name, issue_number} = this.props.messageData ?? {};
const issue = {
owner,
repo,
number,
owner: repo_owner,
repo: repo_name,
number: issue_number,
comment: this.state.comment,
post_id: this.props.post.id,
show_attached_message: false,
Expand Down Expand Up @@ -72,7 +72,7 @@ export default class AttachIssueModal extends PureComponent {
owner,
repo,
number,
comment: this.props.post.message,
comment: this.state.comment,
post_id: this.props.post.id,
show_attached_message: true,
};
Expand Down Expand Up @@ -106,17 +106,25 @@ export default class AttachIssueModal extends PureComponent {
});
};

/* eslint-disable react/no-did-update-set-state*/
componentDidUpdate(prevProps) {
if (this.props.post && !this.props.messageData && !prevProps.post) {
this.setState({comment: this.props.post.message});
}
}
/* eslint-enable */

render() {
const {error, submitting, comment, issueValue} = this.state;
const {visible, theme, messageData, post} = this.props;
const {visible, theme, messageData} = this.props;
const style = getStyle(theme);
if (!visible) {
return null;
}

const {number} = messageData ?? {};
const modalTitle = number ? 'Create a comment to GitHub Issue' : 'Attach Message to GitHub Issue';
const component = number ? (
const {issue_number} = messageData ?? {};
const modalTitle = issue_number ? 'Create a comment to GitHub Issue' : 'Attach Message to GitHub Issue';
const component = issue_number ? (
<div>
<Input
label='Create a comment'
Expand All @@ -138,10 +146,9 @@ export default class AttachIssueModal extends PureComponent {
<Input
label='Message Attached to GitHub Issue'
type='textarea'
isDisabled={true}
value={post?.message}
value={comment}
disabled={false}
readOnly={true}
onChange={this.handleIssueCommentChange}
/>
</div>
);
Expand Down
6 changes: 3 additions & 3 deletions webapp/src/components/modals/close_reopen_issue/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ const CloseOrReopenIssueModal = ({theme}: {theme: Theme}) => {
channel_id: messageData.channel_id,
issue_comment: comment,
status_reason: currentStatus,
repo: messageData.repo,
number: messageData.number,
owner: messageData.owner,
repo: messageData.repo_name,
number: messageData.issue_number,
owner: messageData.repo_owner,
status: messageData.status,
postId: messageData.postId,
};
Expand Down
Loading