-
Notifications
You must be signed in to change notification settings - Fork 165
feat(frontend): detect new deployment and prompt to reload #5849
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
Open
Ma77Ball
wants to merge
17
commits into
apache:main
Choose a base branch
from
Ma77Ball:feat/deployment-version-check
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+612
−15
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f995c56
Added a deployment version service to check the running deployment vs…
Ma77Ball 2c873f9
lint
Ma77Ball 0317dce
added test cases and only poll when a new deployment is detected
Ma77Ball e9088af
Merge branch 'main' into feat/deployment-version-check
Ma77Ball 07f2c79
added build version types
Ma77Ball 786d247
apply auto generated version
Ma77Ball 88a7e8a
added test cases
Ma77Ball 146011f
Merge branch 'feat/deployment-version-check' of github.com:Ma77Ball/t…
Ma77Ball bf95cab
added deployment version check enabled that is set to off by default
Ma77Ball c3da613
Merge branch 'main' into feat/deployment-version-check
Ma77Ball 7fb231f
Merge branch 'main' into feat/deployment-version-check
Ma77Ball fc588cf
Merge branch 'main' into feat/deployment-version-check
Ma77Ball dd0ddb8
Merge branch 'main' into feat/deployment-version-check
Ma77Ball 6501b80
address review feedback on version check
Ma77Ball 7502d56
remove indempodent test case
Ma77Ball 3fba750
Merge branch 'main' into feat/deployment-version-check
Ma77Ball d94ac5a
rerun-ci
Ma77Ball File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -33,3 +33,4 @@ | |
|
|
||
| # Generated by build-version.js on prod builds. | ||
| /src/environments/version.prod.ts | ||
| /src/assets/version.json | ||
This file contains hidden or 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,24 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| // Types for build-version.js. | ||
| export function renderVersionArtifacts( | ||
| version: string, | ||
| buildNumber?: string | ||
| ): { buildNumber: string; prodTs: string; manifestJson: string }; |
This file contains hidden or 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 hidden or 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,159 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| import { HttpClientTestingModule } from "@angular/common/http/testing"; | ||
| import { CommonModule } from "@angular/common"; | ||
| import { ComponentFixture, TestBed } from "@angular/core/testing"; | ||
| import { RouterTestingModule } from "@angular/router/testing"; | ||
| import { AppComponent } from "./app.component"; | ||
| import { GuiConfigService } from "./common/service/gui-config.service"; | ||
| import { DeploymentVersionService } from "./common/service/deployment-version/deployment-version.service"; | ||
| import { NotificationService } from "./common/service/notification/notification.service"; | ||
| import { Version } from "../environments/version"; | ||
|
|
||
| // GuiConfigService stub whose env getter either returns a value or throws, | ||
| // mirroring "config loaded" vs "config failed to load by APP_INITIALIZER". | ||
| class StubGuiConfigService { | ||
| shouldThrow = false; | ||
| deploymentVersionCheckEnabled = true; | ||
| get env(): unknown { | ||
| if (this.shouldThrow) { | ||
| throw new Error("config not loaded"); | ||
| } | ||
| return { deploymentVersionCheckEnabled: this.deploymentVersionCheckEnabled }; | ||
| } | ||
| } | ||
|
|
||
| describe("AppComponent", () => { | ||
| let config: StubGuiConfigService; | ||
| // The real DeploymentVersionService, with its polling entry point spied so | ||
| // the test asserts on the wiring without kicking off real HTTP polling. | ||
| let startPollingSpy: ReturnType<typeof vi.spyOn>; | ||
|
|
||
| beforeEach(() => { | ||
| Version.buildNumber = "dev"; | ||
| config = new StubGuiConfigService(); | ||
|
|
||
| TestBed.configureTestingModule({ | ||
| imports: [CommonModule, RouterTestingModule, HttpClientTestingModule], | ||
| declarations: [AppComponent], | ||
| providers: [ | ||
| { provide: GuiConfigService, useValue: config }, | ||
| DeploymentVersionService, | ||
| // NotificationService is a transitive dependency of DeploymentVersionService. | ||
| { provide: NotificationService, useValue: { blank: vi.fn() } }, | ||
| ], | ||
| }); | ||
| const deploymentVersionService = TestBed.inject(DeploymentVersionService); | ||
| startPollingSpy = vi | ||
| .spyOn(deploymentVersionService, "startPollingForUpdates") | ||
| .mockReturnValue({ unsubscribe: () => undefined } as never); | ||
| }); | ||
|
|
||
| // Version is a shared module singleton; restore the dev default so a test | ||
| // that flips buildNumber cannot leak into other suites in the same worker. | ||
| afterEach(() => { | ||
| Version.buildNumber = "dev"; | ||
| }); | ||
|
|
||
| function create(): ComponentFixture<AppComponent> { | ||
| return TestBed.createComponent(AppComponent); | ||
| } | ||
|
|
||
| describe("config-loaded detection", () => { | ||
| it("marks config as loaded when env is accessible", () => { | ||
| config.shouldThrow = false; | ||
| const component = create().componentInstance; | ||
| expect(component.configLoaded).toBe(true); | ||
| }); | ||
|
|
||
| it("marks config as not loaded when accessing env throws", () => { | ||
| config.shouldThrow = true; | ||
| const component = create().componentInstance; | ||
| expect(component.configLoaded).toBe(false); | ||
| }); | ||
|
|
||
| it("renders the configuration-error panel when config is not loaded", () => { | ||
| config.shouldThrow = true; | ||
| const fixture = create(); | ||
| fixture.detectChanges(); | ||
| const el: HTMLElement = fixture.nativeElement; | ||
| expect(el.querySelector("#config-error")).not.toBeNull(); | ||
| }); | ||
|
|
||
| it("does not render the configuration-error panel when config is loaded", () => { | ||
| config.shouldThrow = false; | ||
| const fixture = create(); | ||
| fixture.detectChanges(); | ||
| const el: HTMLElement = fixture.nativeElement; | ||
| expect(el.querySelector("#config-error")).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("deployment-version polling guard", () => { | ||
| it("does not start polling for the 'dev' placeholder build", () => { | ||
| config.deploymentVersionCheckEnabled = true; | ||
| Version.buildNumber = "dev"; | ||
| create(); | ||
| expect(startPollingSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("does not start polling when the config flag is disabled", () => { | ||
| config.deploymentVersionCheckEnabled = false; | ||
| Version.buildNumber = "prod-build-123"; | ||
| create(); | ||
| expect(startPollingSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("does not start polling when config failed to load", () => { | ||
| config.shouldThrow = true; | ||
| Version.buildNumber = "prod-build-123"; | ||
| create(); | ||
| expect(startPollingSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("starts polling for a real build when the config flag is enabled", () => { | ||
| config.deploymentVersionCheckEnabled = true; | ||
| Version.buildNumber = "prod-build-123"; | ||
| create(); | ||
| expect(startPollingSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe("retry", () => { | ||
| it("reloads the page", () => { | ||
| const reload = vi.fn(); | ||
| // location.reload is a non-writable, non-configurable own property (and is | ||
| // absent from Location.prototype) under this jsdom build, so it cannot be | ||
| // spied or reassigned directly. window.location itself is configurable, | ||
| // so swap the whole object for the test, then restore it. | ||
| const original = window.location; | ||
| Object.defineProperty(window, "location", { | ||
| configurable: true, | ||
| value: { ...original, reload }, | ||
| }); | ||
| try { | ||
| create().componentInstance.retry(); | ||
| expect(reload).toHaveBeenCalledTimes(1); | ||
| } finally { | ||
| Object.defineProperty(window, "location", { configurable: true, value: original }); | ||
| } | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it the only way to write a file? how about use an env variable?
either way we have to take care of the file life cycle: who is going to delete this file/env variable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An env var won't work: the browser fetches
version.jsonat runtime to read the deployed version, and it can't read a server-side env var. Both generated files are gitignored and rewritten in place every build, so there's nothing to clean up.