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

fix(ui): fix sse when 401 occured #3345

Merged
merged 3 commits into from
Sep 21, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
59 changes: 28 additions & 31 deletions engine/api/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,22 +201,7 @@ func loadActionDependencies(db gorp.SqlExecutor, a *sdk.Action) error {
return fmt.Errorf("cannot loadActionChildren> %s", err)
}

// Requirements of children are requirement of parent
for _, c := range a.Actions {
// Now for each requirement of child, check if it exists in parent
for _, cr := range c.Requirements {
found := false
for _, pr := range a.Requirements {
if pr.Type == cr.Type && pr.Value == cr.Value {
found = true
break
}
}
if !found {
a.Requirements = append(a.Requirements, cr)
}
}
}
computeRequirements(a)

return nil
}
Expand Down Expand Up @@ -268,21 +253,7 @@ func UpdateActionDB(db gorp.SqlExecutor, a *sdk.Action, userID int64) error {

//TODO we don't need to compute all job requirements here, but only when running the job
// Requirements of children are requirement of parent
for _, c := range a.Actions {
// Now for each requirement of child, check if it exists in parent
for _, cr := range c.Requirements {
found := false
for _, pr := range a.Requirements {
if pr.Type == cr.Type && pr.Value == cr.Value {
found = true
break
}
}
if !found {
a.Requirements = append(a.Requirements, cr)
}
}
}
computeRequirements(a)

// Checks if multiple requirements have the same name
if err := isRequirementsValid(a.Requirements); err != nil {
Expand Down Expand Up @@ -522,3 +493,29 @@ func GetPipelineUsingAction(db gorp.SqlExecutor, name string) ([]PipelineUsingAc

return response, nil
}

func computeRequirements(a *sdk.Action) {
if a.Enabled {
// Requirements of children are requirement of parent
for _, c := range a.Actions {
if !c.Enabled { // If action is not enabled we don't need their requirements
continue
}
// Now for each requirement of child, check if it exists in parent
for _, cr := range c.Requirements {
found := false
for _, pr := range a.Requirements {
if pr.Type == cr.Type && pr.Value == cr.Value {
found = true
break
}
}
if !found {
a.Requirements = append(a.Requirements, cr)
}
}
}
} else {
a.Requirements = []sdk.Requirement{}
}
}
1 change: 1 addition & 0 deletions engine/api/api_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ func (api *API) InitRouter() {

// Users
r.Handle("/user", r.GET(api.getUsersHandler))
r.Handle("/user/me", r.GET(api.getUserMeHandler))
r.Handle("/user/favorite", r.POST(api.postUserFavoriteHandler))
r.Handle("/user/timeline", r.GET(api.getTimelineHandler))
r.Handle("/user/timeline/filter", r.GET(api.getTimelineFilterHandler), r.POST(api.postTimelineFilterHandler))
Expand Down
14 changes: 14 additions & 0 deletions engine/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"context"
"database/sql"
"fmt"
"net/http"
"time"

Expand Down Expand Up @@ -158,6 +159,19 @@ func (api *API) getUsersHandler() service.Handler {
}
}

// getUserMeHandler fetches current user data
func (api *API) getUserMeHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
u := getUser(ctx)
if u == nil {
return fmt.Errorf("getUserMeHandler> user is nil")
}
u.Groups = nil
u.Permissions = sdk.UserPermissions{}
return service.WriteJSON(w, *u, http.StatusOK)
}
}

func (api *API) getTimelineFilterHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
u := getUser(ctx)
Expand Down
2 changes: 1 addition & 1 deletion engine/service/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func WriteError(w http.ResponseWriter, r *http.Request, err error) {
// ErrAlreadyTaken and ErrWorkerModelAlreadyBooked are not useful to log in warning
if sdk.ErrorIs(errProcessed, sdk.ErrAlreadyTaken) ||
sdk.ErrorIs(errProcessed, sdk.ErrWorkerModelAlreadyBooked) ||
sdk.ErrorIs(errProcessed, sdk.ErrJobAlreadyBooked) {
sdk.ErrorIs(errProcessed, sdk.ErrJobAlreadyBooked) || r.URL.Path == "/user/me" {
log.Debug("%-7s | %-4d | %s \t %s", r.Method, errProcessed.Status, r.RequestURI, err)
} else {
log.Warning("%-7s | %-4d | %s \t %s", r.Method, errProcessed.Status, r.RequestURI, err)
Expand Down
2 changes: 1 addition & 1 deletion tests/clictl_action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ testcases:
- script: {{.cds.build.cdsctl}} action export CDS_TestIT_Disabled > ./tests/fixtures/clictl_action_CDS_TestIT_Disabled.exported
- script: {{.cds.build.cdsctl}} action export CDS_TestIT_Enabled > ./tests/fixtures/clictl_action_CDS_TestIT_Enabled.exported
- script: {{.cds.build.cdsctl}} action export CDS_TestIT_GitClone > ./tests/fixtures/clictl_action_CDS_TestIT_GitClone.exported
- script: diff ./tests/fixtures/clictl_action_CDS_TestIT_Disabled.exported ./tests/fixtures/action_disabled.yml
- script: diff ./tests/fixtures/clictl_action_CDS_TestIT_Disabled.exported ./tests/fixtures/action_disabled_expected.yml
- script: diff ./tests/fixtures/clictl_action_CDS_TestIT_Enabled.exported ./tests/fixtures/action_enabled.yml
- script: diff ./tests/fixtures/clictl_action_CDS_TestIT_GitClone.exported ./tests/fixtures/action_git_clone_default.yml
- name: action list
Expand Down
10 changes: 10 additions & 0 deletions tests/fixtures/action_disabled_expected.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: v1.0
name: CDS_TestIT_Disabled
enabled: false
steps:
- name: my empty script
script:
- '#!/bin/bash'
- set -e
- ""

2 changes: 1 addition & 1 deletion ui/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export class AppComponent implements OnInit {
urlSubscribe: environment.apiURL + '/events/subscribe',
urlUnsubscribe: environment.apiURL + 'events/unsubscribe',
sseURL: environment.apiURL + '/events',
pingURL: environment.apiURL + '/mon/version'
pingURL: environment.apiURL + '/user/me'
});
this.sseWorker.response().subscribe(e => {
if (e == null) {
Expand Down
1 change: 1 addition & 0 deletions ui/src/app/service/logout.interceptor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class LogoutInterceptor implements HttpInterceptor {

if (navigationExtras.queryParams.redirect) {
this._router.navigate(['/account/login'], navigationExtras);
this._authStore.removeUser();
}
} else {
// error formatted from CDS API
Expand Down
1 change: 1 addition & 0 deletions ui/src/assets/worker/sharedWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ setInterval(() => {
try {
var xhr = new XMLHttpRequest();
xhr.open('GET', pingUrl , false, null, null);
xhr.setRequestHeader(headerKey, headerValue);
Copy link
Collaborator

Choose a reason for hiding this comment

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

missing impact on webWorker.js ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

xhr.send(null);
if (xhr.status >= 400) {
if (!offline) {
Expand Down