Compare commits

..

1 Commits

Author SHA1 Message Date
GitHub Action ae59a723ba build 2025-11-13 22:17:51 +00:00
26 changed files with 215680 additions and 2258 deletions
+3 -3
View File
@@ -55,11 +55,11 @@ jobs:
# your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages
steps:
- name: Checkout repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@89a39a4e59826350b863aa6b6252a07ad50cf83e # v3.29.5
uses: github/codeql-action/init@0499de31b99561a6d14a36a5f662c2a54f91beee # v3.29.5
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}
@@ -86,6 +86,6 @@ jobs:
echo ' make release'
exit 1
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@89a39a4e59826350b863aa6b6252a07ad50cf83e # v3.29.5
uses: github/codeql-action/analyze@0499de31b99561a6d14a36a5f662c2a54f91beee # v3.29.5
with:
category: '/language:${{matrix.language}}'
+2 -2
View File
@@ -13,7 +13,7 @@ jobs:
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- uses: actions/stale@b5d41d4e1d5dceea10e7104786b73624c18a190f # v10.2.0
- uses: actions/stale@5f858e3efba33a5ca4407a664cc011ad407f2008 # v10.1.0
name: Setting Issue as Idle
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
@@ -24,7 +24,7 @@ jobs:
operations-per-run: 100
exempt-issue-labels: 'backlog'
- uses: actions/stale@b5d41d4e1d5dceea10e7104786b73624c18a190f # v10.2.0
- uses: actions/stale@5f858e3efba33a5ca4407a664cc011ad407f2008 # v10.1.0
name: Setting PR as Idle
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
+1 -1
View File
@@ -15,7 +15,7 @@ jobs:
steps:
- name: Checkout Source Code
id: checkout-code
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
- name: Npm Install and Build
id: npm-build
run: |
+2 -2
View File
@@ -10,10 +10,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
- name: Setup Node.js
uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v6.2.0
uses: actions/setup-node@2028fbc5c25fe9cf00d9f06a71cc4710d4507903 # v6.0.0
with:
node-version: 'lts/*'
cache: 'npm'
+1 -1
View File
@@ -13,7 +13,7 @@ jobs:
unit-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
- name: Run Unit Tests
run: |
npm install
-1
View File
@@ -331,4 +331,3 @@ node_modules
coverage
# Transpiled JS
lib/
-4
View File
@@ -107,7 +107,3 @@ provided by the bot. You will only need to do this once across all repos using o
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
### Local development
Use `npm run build` to bundle the action with esbuild (output is `lib/index.cjs`, referenced by `action.yml`) and `npm run test` to execute the Vitest suite, including an integration test that runs the built action.
+1 -1
View File
@@ -40,4 +40,4 @@ branding:
color: 'blue'
runs:
using: 'node20'
main: 'lib/index.cjs'
main: 'lib/index.js'
+7
View File
@@ -0,0 +1,7 @@
// babel.config.js
export default {
presets: [
'@babel/preset-env', // For handling ES modules
'@babel/preset-typescript' // For handling TypeScript
]
}
+28
View File
@@ -0,0 +1,28 @@
export default {
restoreMocks: true,
clearMocks: true,
resetMocks: true,
moduleFileExtensions: ['js', 'ts'],
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.ts$': 'ts-jest', // Use ts-jest for TypeScript files
'^.+\\.js$': 'babel-jest' // Transform TypeScript files
},
transformIgnorePatterns: [
'/node_modules/(?!@kubernetes/client-node)/' // Make sure to transform the Kubernetes client module
],
moduleNameMapper: {
'^.+\\.css$': 'jest-transform-stub' // Handle CSS imports (if any)
},
extensionsToTreatAsEsm: ['.ts', '.tsx'], // Treat TypeScript files as ESM
verbose: true,
coverageThreshold: {
global: {
branches: 0,
functions: 40,
lines: 22,
statements: 22
}
}
}
+205895
View File
File diff suppressed because one or more lines are too long
+3
View File
@@ -0,0 +1,3 @@
{
"type": "module"
}
+9690 -2096
View File
File diff suppressed because it is too large Load Diff
+18 -13
View File
@@ -2,12 +2,13 @@
"name": "k8s-set-context-action",
"version": "4.0.2",
"private": true,
"main": "lib/index.cjs",
"main": "lib/index.js",
"type": "module",
"scripts": {
"build": "tsc --noEmit && node ./scripts/build.mjs",
"test": "vitest run",
"test-coverage": "vitest run --coverage",
"prebuild": "npm i @vercel/ncc",
"build": "ncc build src/run.ts -o lib",
"test": "jest",
"test-coverage": "jest --coverage",
"format": "prettier --write .",
"format-check": "prettier --check .",
"prepare": "husky"
@@ -20,20 +21,24 @@
"author": "GitHub",
"license": "MIT",
"dependencies": {
"@actions/core": "^2.0.2",
"@actions/exec": "^2.0.0",
"@actions/core": "^1.11.1",
"@actions/exec": "^1.1.1",
"@actions/io": "^2.0.0",
"@kubernetes/client-node": "^1.4.0",
"husky": "^9.1.7",
"js-yaml": "^4.1.1"
"js-yaml": "^4.1.0"
},
"devDependencies": {
"@babel/preset-env": "^7.28.5",
"@babel/preset-typescript": "^7.28.5",
"@types/jest": "^30.0.0",
"@types/js-yaml": "^4.0.9",
"@types/node": "^25.0.9",
"@vitest/coverage-v8": "^2.0.4",
"esbuild": "^0.24.0",
"prettier": "^3.8.0",
"typescript": "^5.9.3",
"vitest": "^2.0.4"
"@types/node": "^24.9.2",
"@vercel/ncc": "^0.38.4",
"babel-jest": "^30.2.0",
"jest": "^30.2.0",
"prettier": "^3.6.2",
"ts-jest": "^29.4.5",
"typescript": "^5.9.3"
}
}
-26
View File
@@ -1,26 +0,0 @@
#!/usr/bin/env node
import {build} from 'esbuild'
import {mkdir} from 'fs/promises'
import {dirname} from 'path'
const outFile = 'lib/index.cjs'
const main = async () => {
await mkdir(dirname(outFile), {recursive: true})
await build({
entryPoints: ['src/run.ts'],
bundle: true,
platform: 'node',
target: 'node20',
format: 'cjs',
sourcemap: true,
outfile: outFile,
legalComments: 'none'
})
}
main().catch((err) => {
console.error(err)
process.exit(1)
})
+12 -38
View File
@@ -1,56 +1,30 @@
import {getRequiredInputError} from '../tests/util'
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'
import {run} from './action'
import * as fs from 'fs'
import * as path from 'path'
import os from 'os'
import fs from 'fs'
import * as utils from './utils'
import * as core from '@actions/core'
describe('Run', () => {
const initialEnv = {...process.env}
const tempDirs: string[] = []
beforeEach(() => {
vi.restoreAllMocks()
process.env = {...initialEnv}
vi.spyOn(core, 'warning').mockImplementation(() => {})
vi.spyOn(core, 'debug').mockImplementation(() => {})
})
afterEach(() => {
tempDirs.forEach((dir) => {
try {
fs.rmSync(dir, {recursive: true, force: true})
} catch {}
})
tempDirs.length = 0
})
it('throws error without cluster type', async () => {
await expect(run()).rejects.toThrow(getRequiredInputError('cluster-type'))
})
it('writes kubeconfig and sets context', async () => {
const kubeconfig = 'kubeconfig'
const fixedTimestamp = 42
process.env['INPUT_CLUSTER-TYPE'] = 'default'
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'kubeconfig-test-'))
tempDirs.push(tmpDir)
process.env['RUNNER_TEMP'] = tmpDir
process.env['RUNNER_TEMP'] = '/sample/path'
vi.spyOn(Date, 'now').mockReturnValue(fixedTimestamp)
vi.spyOn(utils, 'getKubeconfig').mockResolvedValue(kubeconfig)
vi.spyOn(utils, 'setContext').mockImplementation(() => kubeconfig)
jest
.spyOn(utils, 'getKubeconfig')
.mockImplementation(async () => kubeconfig)
jest.spyOn(fs, 'writeFileSync').mockImplementation(() => {})
jest.spyOn(fs, 'chmodSync').mockImplementation(() => {})
jest.spyOn(utils, 'setContext').mockImplementation(() => kubeconfig)
await expect(run()).resolves.toBeUndefined()
const outputPath = path.join(tmpDir, `kubeconfig_${fixedTimestamp}`)
expect(fs.existsSync(outputPath)).toBe(true)
expect(fs.readFileSync(outputPath, 'utf-8')).toBe(kubeconfig)
expect(process.env['KUBECONFIG']).toBe(outputPath)
expect(await run())
expect(utils.getKubeconfig).toHaveBeenCalled()
expect(utils.setContext).toHaveBeenCalledWith(kubeconfig)
expect(fs.writeFileSync).toHaveBeenCalled()
expect(fs.chmodSync).toHaveBeenCalled()
expect(utils.setContext).toHaveBeenCalled()
})
})
+5 -13
View File
@@ -1,5 +1,3 @@
import {beforeEach, describe, expect, it, test, vi} from 'vitest'
import * as core from '@actions/core'
import * as actions from '@actions/exec'
import * as io from '@actions/io'
import {getRequiredInputError} from '../../tests/util'
@@ -7,12 +5,6 @@ import {getArcKubeconfig, KUBECONFIG_LOCATION} from './arc'
import * as az from './azCommands'
describe('Arc kubeconfig', () => {
beforeEach(() => {
vi.restoreAllMocks()
vi.spyOn(core, 'warning').mockImplementation(() => {})
vi.spyOn(core, 'debug').mockImplementation(() => {})
})
test('it throws error without resource group', async () => {
await expect(getArcKubeconfig()).rejects.toThrow(
getRequiredInputError('resource-group')
@@ -36,11 +28,11 @@ describe('Arc kubeconfig', () => {
process.env['INPUT_RESOURCE-GROUP'] = group
process.env['INPUT_CLUSTER-NAME'] = name
vi.spyOn(io, 'which').mockResolvedValue(path)
vi.spyOn(az, 'runAzCliCommand').mockResolvedValue()
vi.spyOn(az, 'runAzKubeconfigCommandBlocking').mockResolvedValue(
kubeconfig
)
jest.spyOn(io, 'which').mockImplementation(async () => path)
jest.spyOn(az, 'runAzCliCommand').mockImplementation(async () => {})
jest
.spyOn(az, 'runAzKubeconfigCommandBlocking')
.mockImplementation(async () => kubeconfig)
})
it('throws an error without method', async () => {
+1 -4
View File
@@ -1,15 +1,12 @@
import {describe, expect, test, vi} from 'vitest'
import * as core from '@actions/core'
import * as actions from '@actions/exec'
import {runAzCliCommand} from './azCommands'
describe('Az commands', () => {
test('it runs an az cli command', async () => {
vi.spyOn(core, 'debug').mockImplementation(() => {})
const path = 'path'
const args = ['args']
vi.spyOn(actions, 'exec').mockResolvedValue(0)
jest.spyOn(actions, 'exec').mockImplementation(async () => 0)
expect(await runAzCliCommand(path, args))
expect(actions.exec).toHaveBeenCalledWith(path, args, {})
+3 -16
View File
@@ -1,22 +1,9 @@
import {afterEach, beforeEach, describe, expect, test, vi} from 'vitest'
import * as fs from 'fs'
import * as core from '@actions/core'
import {getRequiredInputError} from '../../tests/util'
import {createKubeconfig, getDefaultKubeconfig} from './default'
describe('Default kubeconfig', () => {
const originalEnv = {...process.env}
beforeEach(() => {
vi.restoreAllMocks()
vi.spyOn(core, 'warning').mockImplementation(() => {})
vi.spyOn(core, 'debug').mockImplementation(() => {})
})
afterEach(() => {
process.env = {...originalEnv}
})
test('it creates a kubeconfig with proper format', () => {
const certAuth = 'certAuth'
const token = 'token'
@@ -79,7 +66,7 @@ describe('Default kubeconfig', () => {
test('returns kubeconfig as plaintext when encoding is plaintext', () => {
const kc = 'example kc'
vi.spyOn(core, 'getInput').mockImplementation((name: string) => {
jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
if (name === 'method') return 'default'
if (name === 'kubeconfig-encoding') return 'plaintext'
if (name === 'kubeconfig') return kc
@@ -92,7 +79,7 @@ describe('Default kubeconfig', () => {
const kc = 'example kc'
const base64Kc = Buffer.from(kc, 'utf-8').toString('base64')
vi.spyOn(core, 'getInput').mockImplementation((name: string) => {
jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
if (name === 'method') return 'default'
if (name === 'kubeconfig-encoding') return 'base64'
if (name === 'kubeconfig') return base64Kc
@@ -106,7 +93,7 @@ describe('Default kubeconfig', () => {
const kc = 'example kc'
const unknownEncoding = 'foobar'
vi.spyOn(core, 'getInput').mockImplementation((name: string) => {
jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
if (name === 'method') return 'default'
if (name === 'kubeconfig-encoding') return unknownEncoding
if (name === 'kubeconfig') return kc
-1
View File
@@ -1,4 +1,3 @@
import {describe, expect, test} from 'vitest'
import {Cluster, parseCluster} from './cluster'
describe('Cluster type', () => {
-1
View File
@@ -1,4 +1,3 @@
import {describe, expect, test} from 'vitest'
import {parseK8sSecret, K8sSecret} from './k8sSecret'
describe('K8sSecret type', () => {
-1
View File
@@ -1,4 +1,3 @@
import {describe, expect, test} from 'vitest'
import {Method, parseMethod} from './method'
describe('Method type', () => {
+6 -12
View File
@@ -1,31 +1,25 @@
import {beforeEach, describe, expect, test, vi} from 'vitest'
import fs from 'fs'
import * as arc from './kubeconfigs/arc'
import * as def from './kubeconfigs/default'
import {Cluster} from './types/cluster'
import {getKubeconfig, setContext} from './utils'
import * as core from '@actions/core'
describe('Utils', () => {
beforeEach(() => {
vi.restoreAllMocks()
vi.spyOn(core, 'warning').mockImplementation(() => {})
vi.spyOn(core, 'debug').mockImplementation(() => {})
})
describe('get kubeconfig', () => {
test('it gets arc kubeconfig when type is arc', async () => {
const arcKubeconfig = 'arckubeconfig'
vi.spyOn(arc, 'getArcKubeconfig').mockResolvedValue(arcKubeconfig)
jest
.spyOn(arc, 'getArcKubeconfig')
.mockImplementation(async () => arcKubeconfig)
expect(await getKubeconfig(Cluster.ARC)).toBe(arcKubeconfig)
})
test('it defaults to default kubeconfig', async () => {
const defaultKubeconfig = 'arckubeconfig'
vi.spyOn(def, 'getDefaultKubeconfig').mockImplementation(
() => defaultKubeconfig
)
jest
.spyOn(def, 'getDefaultKubeconfig')
.mockImplementation(() => defaultKubeconfig)
expect(await getKubeconfig(undefined)).toBe(defaultKubeconfig)
expect(await getKubeconfig(Cluster.GENERIC)).toBe(defaultKubeconfig)
+1 -1
View File
@@ -3,5 +3,5 @@
* @param inputName Name of input
* @returns Error with explanation message
*/
export const getRequiredInputError = (inputName: string): Error =>
export const getRequiredInputError = (inputName) =>
Error(`Input required and not supplied: ${inputName}`)
+1 -1
View File
@@ -11,5 +11,5 @@
"@kubernetes/client-node": ["node_modules/@kubernetes/client-node"]
}
},
"exclude": ["node_modules", "lib"]
"exclude": ["node_modules", "tests", "src/**/*.test.ts"]
}
-20
View File
@@ -1,20 +0,0 @@
import {defineConfig} from 'vitest/config'
export default defineConfig({
test: {
environment: 'node',
include: ['**/*.test.ts'],
restoreMocks: true,
clearMocks: true,
mockReset: true,
coverage: {
provider: 'v8',
thresholds: {
statements: 22,
branches: 0,
functions: 40,
lines: 22
}
}
}
})