-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from humanitec/ts
chore: port to typescript & basic test
- Loading branch information
Showing
16 changed files
with
12,838 additions
and
3,835 deletions.
There are no files selected for viewing
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
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
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
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,110 @@ | ||
import {describe, expect, test, beforeEach, afterAll} from '@jest/globals'; | ||
import {join as pathJoin} from 'node:path'; | ||
import {runAction} from './action'; | ||
import {randomBytes} from 'crypto'; | ||
import fetch from 'node-fetch'; | ||
import {mkdir} from 'node:fs/promises'; | ||
|
||
// Emulate https://github.com/actions/toolkit/blob/819157bf8/packages/core/src/core.ts#L128 | ||
const setInput = (name: string, value: string): void => { | ||
process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] = value; | ||
}; | ||
|
||
const fixtures = pathJoin(__dirname, './fixtures'); | ||
|
||
const ensureEnv = (name: string): string => { | ||
const val = process.env[name]; | ||
if (!val) { | ||
throw new Error(`Required environment variables ${name} is empty.`); | ||
} | ||
|
||
return val; | ||
}; | ||
|
||
const token = ensureEnv('HUMANITEC_TOKEN'); | ||
const orgId = ensureEnv('HUMANITEC_ORG'); | ||
|
||
const tenMinInMs = 10 * 60 * 1000; | ||
|
||
describe('action', () => { | ||
let repo: string; | ||
let commit: string; | ||
|
||
|
||
afterAll(async () => { | ||
const res = await fetch( | ||
`https://api.humanitec.io/orgs/${orgId}/artefacts?type=container`, { | ||
method: 'GET', | ||
headers: { | ||
'Authorization': `Bearer ${token}`, | ||
'Content-Type': 'application/json', | ||
'User-Agent': 'gh-action-build-push-to-humanitec/latest', | ||
}, | ||
}); | ||
|
||
expect(res.status).toBe(200); | ||
|
||
const body = await res.json(); | ||
|
||
for (const artefact of body) { | ||
if (!artefact.name.startsWith(`registry.humanitec.io/${orgId}/test-`)) { | ||
continue; | ||
} | ||
|
||
if (Date.now() - Date.parse(artefact.createdAt) < tenMinInMs) { | ||
continue; | ||
} | ||
|
||
const res = await fetch( | ||
`https://api.humanitec.io/orgs/${orgId}/artefacts/${artefact.id}`, { | ||
method: 'DELETE', | ||
headers: { | ||
'Authorization': `Bearer ${token}`, | ||
'Content-Type': 'application/json', | ||
'User-Agent': 'gh-action-build-push-to-humanitec/latest', | ||
}, | ||
}); | ||
expect(res.status).toBe(204); | ||
} | ||
}); | ||
|
||
beforeEach(async () => { | ||
await mkdir(pathJoin(fixtures, '.git')); | ||
|
||
setInput('humanitec-token', token); | ||
setInput('organization', orgId); | ||
setInput('context', '.'); | ||
|
||
commit = randomBytes(20).toString('hex'); | ||
repo = `test-${randomBytes(20).toString('hex')}`; | ||
|
||
process.env['GITHUB_WORKSPACE'] = fixtures; | ||
process.env['GITHUB_SHA'] = commit; | ||
process.env['GITHUB_REPOSITORY'] = repo; | ||
}); | ||
|
||
test('succeeds', async () => { | ||
await runAction(); | ||
expect(process.exitCode).toBeFalsy; | ||
|
||
const res = await fetch( | ||
`https://api.humanitec.io/orgs/${orgId}/artefact-versions`, { | ||
method: 'GET', | ||
headers: { | ||
'Authorization': `Bearer ${token}`, | ||
'Content-Type': 'application/json', | ||
'User-Agent': 'gh-action-build-push-to-humanitec/latest', | ||
}, | ||
}); | ||
|
||
expect(res.status).toBe(200); | ||
|
||
const body = await res.json(); | ||
|
||
expect(body).toEqual( | ||
expect.arrayContaining( | ||
[expect.objectContaining({commit: commit})], | ||
), | ||
); | ||
}); | ||
}); |
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
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,68 @@ | ||
export const args = (str: string): string[] => { | ||
const args = []; | ||
str = str.trim(); | ||
let currentArg = ''; | ||
for (let i = 0; i < str.length; i++) { | ||
switch (str[i]) { | ||
case '\'': | ||
const endQuoteIndex = str.indexOf('\'', i+1); | ||
if (endQuoteIndex < 0) { | ||
throw 'single quote not closed'; | ||
} | ||
currentArg = currentArg + str.substring(i+1, endQuoteIndex); | ||
i = endQuoteIndex; | ||
break; | ||
case '"': | ||
// Double quotes can contain escaped characters | ||
for (i++; i < str.length && str[i] !== '"'; i++) { | ||
if (str[i] === '\\' && (i+1) < str.length) { | ||
i++; | ||
switch (str[i]) { | ||
case 'n': | ||
currentArg += '\n'; | ||
break; | ||
case 'r': | ||
currentArg += '\r'; | ||
break; | ||
case 't': | ||
currentArg += '\t'; | ||
break; | ||
default: | ||
currentArg += str[i]; | ||
} | ||
} else { | ||
currentArg += str[i]; | ||
} | ||
} | ||
if (i >= str.length) { | ||
throw 'double quote not closed'; | ||
} | ||
break; | ||
case ' ': | ||
case '\t': | ||
args.push(currentArg); | ||
currentArg = ''; | ||
while (i < str.length && (str[i] === ' ' || str[i] === '\t')) { | ||
i++; | ||
} | ||
// We will have advanced to the next non-whitespace | ||
i--; | ||
break; | ||
case '\\': | ||
i++; | ||
if (i < str.length) { | ||
currentArg = currentArg + str[i]; | ||
} else { | ||
throw 'uncompleted escape character'; | ||
} | ||
break; | ||
default: | ||
currentArg = currentArg + str[i]; | ||
break; | ||
} | ||
} | ||
if (currentArg != '') { | ||
args.push(currentArg); | ||
} | ||
return args; | ||
} |
Oops, something went wrong.