Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MI-1993] Correct failing CI #13

Merged
merged 15 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion server/constants/oauth_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const (
BaseOauthURL = "https://app.vssps.visualstudio.com"

// Paths
PathAuth = "/oauth2/authorize"
PathAuth = "/oauth2/authorize"
// #nosec G101 -- This is a false positive
PathToken = "/oauth2/token"
)
19 changes: 1 addition & 18 deletions server/plugin/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ func (p *Plugin) InitRoutes() {
// Plugin APIs
s.HandleFunc("/tasks", p.handleAuthRequired(p.handleCreateTask)).Methods(http.MethodPost)
s.HandleFunc("/link", p.handleAuthRequired(p.handleLink)).Methods(http.MethodPost)

// TODO: for testing purpose, remove later
s.HandleFunc("/test", p.testAPI).Methods(http.MethodGet)
}

// API to create task of a project in an organization.
Expand Down Expand Up @@ -73,7 +70,7 @@ func (p *Plugin) handleCreateTask(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
}

message := fmt.Sprintf(constants.CreatedTask, task.Link.Html.Href)
message := fmt.Sprintf(constants.CreatedTask, task.Link.HTML.Href)

// Send message to DM.
p.DM(mattermostUserID, message)
Expand Down Expand Up @@ -169,20 +166,6 @@ func (p *Plugin) WithRecovery(next http.Handler) http.Handler {
})
}

// TODO: for testing purpose, remove later
func (p *Plugin) testAPI(w http.ResponseWriter, r *http.Request) {
// TODO: remove later
response, err := p.Client.TestApi()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
res, _ := json.Marshal(response)
w.Header().Add("Content-Type", "application/json")
w.Write(res)
}

// Handles the static files under the assets directory.
func (p *Plugin) HandleStaticFiles() {
bundlePath, err := p.API.GetBundlePath()
Expand Down
17 changes: 11 additions & 6 deletions server/plugin/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
)

type Client interface {
TestApi() (string, error) // TODO: remove later
GenerateOAuthToken(encodedFormValues url.Values) (*serializers.OAuthSuccessResponse, int, error)
CreateTask(body *serializers.CreateTaskRequestPayload, mattermostUserID string) (*serializers.TaskValue, int, error)
GetTask(organization, taskID, mattermostUserID string) (*serializers.TaskValue, int, error)
Expand All @@ -32,11 +31,6 @@ type ErrorResponse struct {
Message string `json:"message"`
}

// TODO: remove later
func (c *client) TestApi() (string, error) {
return "hello world", nil
}

func (c *client) GenerateOAuthToken(encodedFormValues url.Values) (*serializers.OAuthSuccessResponse, int, error) {
var oAuthSuccessResponse *serializers.OAuthSuccessResponse

Expand Down Expand Up @@ -105,6 +99,17 @@ func (c *client) GetTask(organization, taskID, mattermostUserID string) (*serial
return task, statusCode, nil
}

// TODO: Uncomment the code later when needed.
// Wrapper to make REST API requests with "application/json" type content
// func (c *client) callJSON(url, path, method, mattermostUserID string, in, out interface{}, formValues url.Values) (responseData []byte, err error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why commented call json function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was currently not used anywhere, so commented on it for now

// contentType := "application/json"
// buf := &bytes.Buffer{}
// if err = json.NewEncoder(buf).Encode(in); err != nil {
// return nil, err
// }
// return c.call(url, method, path, contentType, mattermostUserID, buf, out, formValues)
// }

// Wrapper to make REST API requests with "application/x-www-form-urlencoded" type content
func (c *client) callFormURLEncoded(url, path, method string, out interface{}, formValues url.Values) (responseData []byte, statusCode int, err error) {
contentType := "application/x-www-form-urlencoded"
Expand Down
27 changes: 18 additions & 9 deletions server/plugin/oAuth.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/pkg/errors"

"github.com/Brightscout/mattermost-plugin-azure-devops/server/constants"
"github.com/Brightscout/mattermost-plugin-azure-devops/server/serializers"
"github.com/Brightscout/mattermost-plugin-azure-devops/server/store"
)

Expand Down Expand Up @@ -73,7 +74,9 @@ func (p *Plugin) OAuthConnect(w http.ResponseWriter, r *http.Request) {

if isConnected := p.UserAlreadyConnected(mattermostUserID); isConnected {
p.closeBrowserWindowWithHTTPResponse(w)
p.DM(mattermostUserID, constants.UserAlreadyConnected)
if _, DMErr := p.DM(mattermostUserID, constants.UserAlreadyConnected); DMErr != nil {
p.handleError(w, r, &serializers.Error{Code: http.StatusBadRequest, Message: constants.UserAlreadyConnected})
}
return
}

Expand Down Expand Up @@ -114,8 +117,10 @@ func (p *Plugin) GenerateOAuthToken(code, state string) error {
mattermostUserID := strings.Split(state, "_")[1]

if err := p.Store.VerifyOAuthState(mattermostUserID, state); err != nil {
p.DM(mattermostUserID, constants.GenericErrorMessage)
return errors.Wrap(err, err.Error())
if _, DMErr := p.DM(mattermostUserID, constants.GenericErrorMessage); DMErr != nil {
return DMErr
}
return errors.Wrap(err, "failed to verify oAuth state")
}

oauthTokenFormValues := url.Values{
Expand All @@ -128,8 +133,10 @@ func (p *Plugin) GenerateOAuthToken(code, state string) error {

successResponse, _, err := p.Client.GenerateOAuthToken(oauthTokenFormValues)
if err != nil {
p.DM(mattermostUserID, constants.GenericErrorMessage)
return errors.Wrap(err, err.Error())
if _, DMErr := p.DM(mattermostUserID, constants.GenericErrorMessage); DMErr != nil {
return DMErr
}
return errors.Wrap(err, "failed to generate oAuth token")
}

encryptedAccessToken, err := p.encrypt([]byte(successResponse.AccessToken), []byte(p.getConfiguration().EncryptionSecret))
Expand All @@ -149,11 +156,13 @@ func (p *Plugin) GenerateOAuthToken(code, state string) error {
ExpiresIn: successResponse.ExpiresIn,
}

p.Store.StoreUser(&user)

fmt.Printf("%+v\n", successResponse) // TODO: remove later
if err := p.Store.StoreUser(&user); err != nil {
return err
}

p.DM(mattermostUserID, fmt.Sprintf("%s\n\n%s", constants.UserConnected, constants.HelpText))
if _, err := p.DM(mattermostUserID, fmt.Sprintf("%s\n\n%s", constants.UserConnected, constants.HelpText)); err != nil {
return err
}

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion server/plugin/taskLinkPreview.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (p *Plugin) postTaskPreview(linkData []string, userID, channelID string) (*
description = "No description"
}

taskTitle := fmt.Sprintf(constants.TaskTitle, task.Fields.Type, task.ID, task.Fields.Title, task.Link.Html.Href)
taskTitle := fmt.Sprintf(constants.TaskTitle, task.Fields.Type, task.ID, task.Fields.Title, task.Link.HTML.Href)
TaskPreviewMessage := fmt.Sprintf(constants.TaskPreviewMessage, task.Fields.State, assignedTo, description)
post := &model.Post{
UserId: userID,
Expand Down
12 changes: 0 additions & 12 deletions server/plugin/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,6 @@ func (p *Plugin) DM(mattermostUserID, format string, args ...interface{}) (strin
return sentPost.Id, nil
}

func (p *Plugin) createPost(channelID string, text string) *model.AppError {
post := &model.Post{
UserId: p.botUserID,
ChannelId: channelID,
Message: text,
}
if _, err := p.API.CreatePost(post); err != nil {
return err
}
return nil
}

// encode encodes bytes into base64 string
func (p *Plugin) encode(encrypted []byte) string {
encoded := make([]byte, base64.URLEncoding.EncodedLen(len(encrypted)))
Expand Down
2 changes: 1 addition & 1 deletion server/serializers/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type TaskFieldValue struct {
}

type Link struct {
Html Href `json:"html"`
HTML Href `json:"html"`
}

type Href struct {
Expand Down
6 changes: 6 additions & 0 deletions webapp/src/manifest.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {id, version} from './manifest';

test('Plugin id and version are defined', () => {
expect(id).toBeDefined();
expect(version).toBeDefined();
});
4 changes: 4 additions & 0 deletions webapp/tests/setup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import 'mattermost-webapp/tests/setup';