Skip to content

Commit

Permalink
added checks for linux platform
Browse files Browse the repository at this point in the history
  • Loading branch information
raianand committed Apr 2, 2024
1 parent 741ef06 commit 2edd389
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 50 deletions.
12 changes: 10 additions & 2 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ jest.mock('../src/main', () => ({
}))

describe('index', () => {
it('calls run when imported', async () => {
require('../src/index')
it('calls run when imported on linux', async () => {
const { init } = require('../src/index')
init('linux', 'x64')

expect(run).toHaveBeenCalled()
})

it('fails when imported on platform other than linux', async () => {
const { init } = require('../src/index')
init('darwin', 'x64')

expect(run).not.toHaveBeenCalled()
})
})
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
125 changes: 92 additions & 33 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 40 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,49 @@
const { run } = require('./main')
const { generateSummary } = require('./summary')
const core = require('@actions/core')
const os = require('os')

const isPost = core.getState('isPost')
const flag = isPost === 'true'
const boltFailed = core.getState('boltFailed')
const failedFlag = boltFailed === 'true'

if (flag) {
// Post
generateSummary()
} else {
if (!isPost) {
core.saveState('isPost', 'true')
function init(platform, arch) {
if (flag) {
if (failedFlag) {
core.info('Skipping post action as bolt failed')
return
}
// Post
generateSummary()
} else {
if (!isPost) {
core.saveState('isPost', 'true')
}

// 'win32' | 'darwin' | 'linux' | 'freebsd' | 'openbsd' | 'android' | 'cygwin' | 'sunos'
if (['linux'].indexOf(platform) === -1) {
core.saveState('boltFailed', 'true')
core.setFailed(`This action is not supported on ${platform}`)
return
}
// Possible Archs
// 'x64' | 'arm' | 'arm64' | 'ia32' | 'mips' | 'mipsel' | 'ppc' | 'ppc64' | 'riscv64' | 's390' | 's390x'
const allowedArch = ['x64', 'arm64', 'arm']
if (allowedArch.indexOf(arch) === -1) {
core.saveState('boltFailed', 'true')
core.setFailed(`This action is not supported on ${arch}`)
return
}

run()
}
run()
}

const platform = os.platform()
const arch = os.arch()
init(platform, arch)

module.exports = {
init
}
11 changes: 10 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,17 @@ async function run() {

// Changing boltUser will require changes in bolt.service and intercept.py
const boltUser = 'bolt'
core.saveState('boltUser', boltUser)

const outputFile = 'output.log'
core.saveState('outputFile', outputFile)

const homeDir = `/home/${boltUser}`
core.saveState('homeDir', homeDir)

const repoName = process.env.GITHUB_REPOSITORY; // e.g. koalalab-inc/bolt
const repoOwner = repoName.split('/')[0]; // e.g. koalalab-inc
core.saveState('boltUser', boltUser)


core.startGroup('create-bolt-user')
core.info('Creating bolt user...')
Expand Down Expand Up @@ -196,6 +204,7 @@ async function run() {
benchmark('setup-iptables-redirection')
} catch (error) {
// Fail the workflow run if an error occurs
core.saveState('boltFailed', 'true')
core.setFailed(error.message)
}
}
Expand Down
22 changes: 16 additions & 6 deletions src/summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ const { exec } = require('@actions/exec')
const fs = require('fs')
const YAML = require('yaml')

async function generateTestResults(boltUser) {
const filePath = 'output.log'
await exec(`sudo cp /home/${boltUser}/${filePath} output.log`)
await exec(`sudo chown -R runner:docker ${filePath}`)

async function generateTestResults(filePath) {
try {
// Read the entire file synchronously and split it into an array of lines
const fileContent = fs.readFileSync(filePath, 'utf-8')
Expand Down Expand Up @@ -63,12 +59,26 @@ function resultToRow(result) {
}

async function generateSummary() {
const outputFile = core.getState('outputFile')
const homeDir = core.getState('homeDir')
const boltUser = core.getState('boltUser')
const mode = core.getInput('mode')
const allowHTTP = core.getInput('allow_http')
const defaultPolicy = core.getInput('default_policy')
const egressRulesYAML = core.getInput('egress_rules')
const trustedGithubAccountsYAML = core.getInput('trusted_github_accounts')

if (!outputFile || !boltUser || !homeDir) {
core.info(`Invalid Bold run. Missing required state variables`)
return
}
if (!fs.existsSync(`${homeDir}/${outputFile}`)) {
core.info(`Bolt output file not found`)
return
}

await exec(`sudo cp ${homeDir}/${outputFile} ${outputFile}`)

// Verify that egress_rules_yaml is valid YAML
let egressRules
let trustedGithubAccounts
Expand All @@ -79,7 +89,7 @@ async function generateSummary() {
core.info(`Invalid YAML: ${error.message}`)
}

const results = await generateTestResults(boltUser)
const results = await generateTestResults(outputFile)

const uniqueResults = getUniqueBy(results, ['destination', 'scheme'])
// const uniqueResultRows = uniqueResults.map(resultToRow)
Expand Down

0 comments on commit 2edd389

Please sign in to comment.