-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin-uploader): support new Kintone UI (#2711)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: hung-nguyen <hung-nguyen@cybozu.vn>
- Loading branch information
1 parent
e884597
commit ca6ca58
Showing
7 changed files
with
351 additions
and
128 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
packages/plugin-uploader/src/controllers/ControllerBase.ts
This file contains 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,103 @@ | ||
import type { Browser, Page } from "puppeteer"; | ||
import puppeteer from "puppeteer"; | ||
import chalk from "chalk"; | ||
import type { BoundMessage } from "../messages"; | ||
|
||
const TIMEOUT_MS = 10000; | ||
|
||
export interface BasicAuth { | ||
username: string; | ||
password: string; | ||
} | ||
|
||
export abstract class ControllerBase { | ||
private _browser?: Browser; | ||
private _page?: Page; | ||
|
||
public get browser() { | ||
if (this._browser === undefined) { | ||
throw new Error( | ||
"Browser is not launched yet. Please call launchBrowser() first.", | ||
); | ||
} | ||
return this._browser; | ||
} | ||
|
||
public set browser(value) { | ||
this._browser = value; | ||
} | ||
|
||
public get page() { | ||
if (this._page === undefined) { | ||
throw new Error( | ||
"Page is not opened yet. Please call openNewPage() first.", | ||
); | ||
} | ||
return this._page; | ||
} | ||
|
||
public set page(value) { | ||
this._page = value; | ||
} | ||
|
||
public async launchBrowser( | ||
options: { | ||
proxy?: string; | ||
ignoreDefaultArgs?: string[]; | ||
} = {}, | ||
) { | ||
const args = options.proxy ? [`--proxy-server=${options.proxy}`] : []; | ||
this._browser = await puppeteer.launch({ | ||
args, | ||
ignoreDefaultArgs: options.ignoreDefaultArgs, | ||
headless: "shell", | ||
}); | ||
} | ||
|
||
public async closeBrowser() { | ||
return this.browser.close(); | ||
} | ||
|
||
public async openNewPage() { | ||
this._page = await this.browser.newPage(); | ||
} | ||
|
||
protected async login(options: { | ||
baseUrl: string; | ||
userName: string; | ||
password: string; | ||
boundMessage: BoundMessage; | ||
basicAuth?: BasicAuth; | ||
}): Promise<void> { | ||
const { baseUrl, userName, password, boundMessage, basicAuth } = options; | ||
const loginUrl = `${baseUrl}/login?saml=off`; | ||
|
||
if (basicAuth) { | ||
await this.page.authenticate(basicAuth); | ||
} | ||
|
||
console.log(`Open ${loginUrl}`); | ||
await this.page.goto(loginUrl); | ||
try { | ||
await this.page.waitForSelector(".form-username-slash", { | ||
timeout: TIMEOUT_MS, | ||
}); | ||
} catch (e) { | ||
throw chalk.red(boundMessage("Error_cannotOpenLogin")); | ||
} | ||
|
||
console.log("Trying to log in..."); | ||
await this.page.type(".form-username-slash > input.form-text", userName); | ||
await this.page.type(".form-password-slash > input.form-text", password); | ||
await this.page.click(".login-button"); | ||
|
||
try { | ||
await this.page.waitForNavigation({ | ||
timeout: TIMEOUT_MS, | ||
waitUntil: "domcontentloaded", | ||
}); | ||
} catch (e) { | ||
throw chalk.red(boundMessage("Error_failedLogin")); | ||
} | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
packages/plugin-uploader/src/controllers/PluginSystemController.ts
This file contains 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,86 @@ | ||
import type { Lang } from "../lang"; | ||
import chalk from "chalk"; | ||
import { getBoundMessage } from "../messages"; | ||
import { ReactPluginSystemPage } from "../pages/ReactPluginSystemPage"; | ||
import type { PluginSystemPageInterface } from "../pages/PluginSystemPageInterface"; | ||
import { | ||
IMPORT_BUTTON_SELECTOR, | ||
OldPluginSystemPage, | ||
} from "../pages/OldPluginSystemPage"; | ||
import { ControllerBase } from "./ControllerBase"; | ||
|
||
const DETECT_PAGE_TIMEOUT_MS = 10000; | ||
const NO_PRIVILEGE_STATUS_CODE = "CB_NO02"; | ||
|
||
export interface BasicAuth { | ||
username: string; | ||
password: string; | ||
} | ||
|
||
export default class PluginSystemController extends ControllerBase { | ||
private ui?: PluginSystemPageInterface; | ||
|
||
private async getUI() { | ||
if (this.ui) { | ||
return this.ui; | ||
} | ||
|
||
const isReactPage = await this.isReactPage(); | ||
this.ui = isReactPage | ||
? new ReactPluginSystemPage() | ||
: new OldPluginSystemPage(); | ||
|
||
return this.ui; | ||
} | ||
|
||
private async isReactPage(): Promise<boolean> { | ||
try { | ||
await this.page.waitForSelector(IMPORT_BUTTON_SELECTOR, { | ||
timeout: DETECT_PAGE_TIMEOUT_MS, | ||
}); | ||
return false; | ||
} catch (e) { | ||
return true; | ||
} | ||
} | ||
|
||
protected async goToPluginSystemPage(baseUrl: string) { | ||
const pluginSystemPageUri = `${baseUrl}/k/admin/system/plugin/`; | ||
console.log(`Navigate to ${pluginSystemPageUri}`); | ||
return this.page.goto(pluginSystemPageUri); | ||
} | ||
|
||
public async readyForUpload(options: { | ||
baseUrl: string; | ||
userName: string; | ||
password: string; | ||
lang: Lang; | ||
basicAuth?: BasicAuth; | ||
}): Promise<void> { | ||
const { baseUrl, userName, password, lang, basicAuth } = options; | ||
const boundMessage = getBoundMessage(lang); | ||
|
||
await this.login({ | ||
baseUrl, | ||
userName, | ||
password, | ||
boundMessage, | ||
basicAuth, | ||
}); | ||
|
||
const response = await this.goToPluginSystemPage(baseUrl); | ||
if ( | ||
!response || | ||
(response.headers()["x-cybozu-error"] && | ||
response.headers()["x-cybozu-error"] === NO_PRIVILEGE_STATUS_CODE) | ||
) { | ||
throw chalk.red(boundMessage("Error_adminPrivilege")); | ||
} | ||
|
||
await (await this.getUI()).readyForImportButton(this.page, boundMessage); | ||
} | ||
|
||
public async upload(pluginPath: string, lang: Lang): Promise<void> { | ||
await (await this.getUI()).upload(this.page, pluginPath, lang); | ||
} | ||
} |
This file contains 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.