|
1 | | -import type { AuthContextOutput } from "./types.d.js"; |
2 | | -import process from "node:process"; |
3 | | -import core from "@actions/core"; |
4 | | -import playwright from "playwright"; |
| 1 | +import type {AuthContextOutput} from './types.d.js' |
| 2 | +import process from 'node:process' |
| 3 | +import core from '@actions/core' |
| 4 | +import playwright from 'playwright' |
5 | 5 |
|
6 | 6 | export default async function () { |
7 | | - core.info("Starting 'auth' action"); |
| 7 | + core.info("Starting 'auth' action") |
8 | 8 |
|
9 | | - let browser: playwright.Browser | undefined; |
10 | | - let context: playwright.BrowserContext | undefined; |
11 | | - let page: playwright.Page | undefined; |
| 9 | + let browser: playwright.Browser | undefined |
| 10 | + let context: playwright.BrowserContext | undefined |
| 11 | + let page: playwright.Page | undefined |
12 | 12 | try { |
13 | 13 | // Get inputs |
14 | | - const loginUrl = core.getInput("login_url", { required: true }); |
15 | | - const username = core.getInput("username", { required: true }); |
16 | | - const password = core.getInput("password", { required: true }); |
17 | | - core.setSecret(password); |
| 14 | + const loginUrl = core.getInput('login_url', {required: true}) |
| 15 | + const username = core.getInput('username', {required: true}) |
| 16 | + const password = core.getInput('password', {required: true}) |
| 17 | + core.setSecret(password) |
18 | 18 |
|
19 | 19 | // Launch a headless browser |
20 | 20 | browser = await playwright.chromium.launch({ |
21 | 21 | headless: true, |
22 | | - executablePath: process.env.CI ? "/usr/bin/google-chrome" : undefined, |
23 | | - }); |
| 22 | + executablePath: process.env.CI ? '/usr/bin/google-chrome' : undefined, |
| 23 | + }) |
24 | 24 | context = await browser.newContext({ |
25 | 25 | // Try HTTP Basic authentication |
26 | 26 | httpCredentials: { |
27 | 27 | username, |
28 | 28 | password, |
29 | 29 | }, |
30 | | - }); |
31 | | - page = await context.newPage(); |
| 30 | + }) |
| 31 | + page = await context.newPage() |
32 | 32 |
|
33 | 33 | // Navigate to login page |
34 | | - core.info("Navigating to login page"); |
35 | | - await page.goto(loginUrl); |
| 34 | + core.info('Navigating to login page') |
| 35 | + await page.goto(loginUrl) |
36 | 36 |
|
37 | 37 | // Check for a login form. |
38 | 38 | // If no login form is found, then either HTTP Basic auth succeeded, or the page does not require authentication. |
39 | | - core.info("Checking for login form"); |
| 39 | + core.info('Checking for login form') |
40 | 40 | const [usernameField, passwordField] = await Promise.all([ |
41 | 41 | page.getByLabel(/user ?name/i).first(), |
42 | 42 | page.getByLabel(/password/i).first(), |
43 | | - ]); |
44 | | - const [usernameFieldExists, passwordFieldExists] = await Promise.all([ |
45 | | - usernameField.count(), |
46 | | - passwordField.count(), |
47 | | - ]); |
| 43 | + ]) |
| 44 | + const [usernameFieldExists, passwordFieldExists] = await Promise.all([usernameField.count(), passwordField.count()]) |
48 | 45 | if (usernameFieldExists && passwordFieldExists) { |
49 | 46 | // Try form authentication |
50 | | - core.info("Filling username"); |
51 | | - await usernameField.fill(username); |
52 | | - core.info("Filling password"); |
53 | | - await passwordField.fill(password); |
54 | | - core.info("Logging in"); |
| 47 | + core.info('Filling username') |
| 48 | + await usernameField.fill(username) |
| 49 | + core.info('Filling password') |
| 50 | + await passwordField.fill(password) |
| 51 | + core.info('Logging in') |
55 | 52 | await page |
56 | 53 | .getByLabel(/password/i) |
57 | | - .locator("xpath=ancestor::form") |
58 | | - .evaluate((form) => (form as HTMLFormElement).submit()); |
| 54 | + .locator('xpath=ancestor::form') |
| 55 | + .evaluate(form => (form as HTMLFormElement).submit()) |
59 | 56 | } else { |
60 | | - core.info("No login form detected"); |
| 57 | + core.info('No login form detected') |
61 | 58 | // This occurs if HTTP Basic auth succeeded, or if the page does not require authentication. |
62 | 59 | } |
63 | 60 |
|
64 | 61 | // Output authenticated session state |
65 | | - const { cookies, origins } = await context.storageState(); |
| 62 | + const {cookies, origins} = await context.storageState() |
66 | 63 | const authContextOutput: AuthContextOutput = { |
67 | 64 | username, |
68 | 65 | password, |
69 | 66 | cookies, |
70 | 67 | localStorage: origins.reduce( |
71 | | - (acc, { origin, localStorage }) => { |
| 68 | + (acc, {origin, localStorage}) => { |
72 | 69 | acc[origin] = localStorage.reduce( |
73 | | - (acc, { name, value }) => { |
74 | | - acc[name] = value; |
75 | | - return acc; |
| 70 | + (acc, {name, value}) => { |
| 71 | + acc[name] = value |
| 72 | + return acc |
76 | 73 | }, |
77 | 74 | {} as Record<string, string>, |
78 | | - ); |
79 | | - return acc; |
| 75 | + ) |
| 76 | + return acc |
80 | 77 | }, |
81 | 78 | {} as Record<string, Record<string, string>>, |
82 | 79 | ), |
83 | | - }; |
84 | | - core.setOutput("auth_context", JSON.stringify(authContextOutput)); |
85 | | - core.debug("Output: 'auth_context'"); |
| 80 | + } |
| 81 | + core.setOutput('auth_context', JSON.stringify(authContextOutput)) |
| 82 | + core.debug("Output: 'auth_context'") |
86 | 83 | } catch (error) { |
87 | 84 | if (page) { |
88 | | - core.info(`Errored at page URL: ${page.url()}`); |
| 85 | + core.info(`Errored at page URL: ${page.url()}`) |
89 | 86 | } |
90 | | - core.setFailed(`${error}`); |
91 | | - process.exit(1); |
| 87 | + core.setFailed(`${error}`) |
| 88 | + process.exit(1) |
92 | 89 | } finally { |
93 | 90 | // Clean up |
94 | | - await context?.close(); |
95 | | - await browser?.close(); |
| 91 | + await context?.close() |
| 92 | + await browser?.close() |
96 | 93 | } |
97 | 94 |
|
98 | | - core.info("Finished 'auth' action"); |
| 95 | + core.info("Finished 'auth' action") |
99 | 96 | } |
0 commit comments