diff --git a/action.yml b/action.yml index 4ae63a8..e350a85 100644 --- a/action.yml +++ b/action.yml @@ -3,8 +3,10 @@ description: 'Checkout a Git repository.' inputs: repository: description: 'Repository name' + default: ${{ gitea.repository }} ref: description: 'Ref to checkout (SHA, branch, tag)' + default: ${{ gitea.ref }} token: description: 'Access token for clone repository' default: ${{ gitea.token }} @@ -13,13 +15,18 @@ inputs: default: true submodules: description: 'Whether to include submodules: false to exclude submodules, true to include only one level of submodules, or recursive to recursively clone submodules; defaults to false' + default: 'false' lfs: description: 'Whether to download Git-LFS files; defaults to false' default: false fetch-depth: description: 'The depth of commits to ask Git to fetch; defaults to no limit' + default: '0' path: - description: 'Optional path to check out source code' + description: 'Optional path to check out source code' + default: ${{ gitea.workspace }} + server-url: + default: ${{ gitea.server_url }} runs: using: node20 main: dist/index.js diff --git a/package.json b/package.json index 27ed024..d46697b 100644 --- a/package.json +++ b/package.json @@ -1,89 +1,90 @@ { - "name": "checkout", - "description": "GitHub Actions TypeScript template", - "version": "0.0.0", - "author": "actions", - "private": true, - "homepage": "https://git.teamtekk.it/actions/checkout", - "repository": { - "type": "git", - "url": "git+https://git.teamtekk.it/actions/checkout.git" - }, - "bugs": { - "url": "https://git.teamtekk.it/actions/checkout/issues" - }, - "keywords": [ - "actions", - "node", - "checkout" + "name": "checkout", + "description": "GitHub Actions TypeScript template", + "version": "0.0.0", + "author": "actions", + "private": true, + "homepage": "https://git.teamtekk.it/actions/checkout", + "repository": { + "type": "git", + "url": "git+https://git.teamtekk.it/actions/checkout.git" + }, + "bugs": { + "url": "https://git.teamtekk.it/actions/checkout/issues" + }, + "keywords": [ + "actions", + "node", + "checkout" + ], + "exports": { + ".": "./dist/index.js" + }, + "engines": { + "node": ">=20" + }, + "scripts": { + "bundle": "npm run format:write && npm run package", + "ci-test": "jest", + "coverage": "make-coverage-badge --output-path ./badges/coverage.svg", + "format:write": "prettier --write **/*.ts", + "format:check": "prettier --check **/*.ts", + "lint": "npx eslint . -c ./.gitea/linters/.eslintrc.yml", + "package": "ncc build src/index.ts --license licenses.txt", + "package:watch": "npm run package -- --watch", + "test": "jest", + "all": "npm run format:write && npm run lint && npm run test && npm run coverage && npm run package" + }, + "license": "MIT", + "jest": { + "preset": "ts-jest", + "verbose": true, + "clearMocks": true, + "testEnvironment": "node", + "moduleFileExtensions": [ + "js", + "ts" ], - "exports": { - ".": "./dist/index.js" + "testMatch": [ + "**/*.test.ts" + ], + "testPathIgnorePatterns": [ + "/node_modules/", + "/dist/" + ], + "transform": { + "^.+\\.ts$": "ts-jest" }, - "engines": { - "node": ">=20" - }, - "scripts": { - "bundle": "npm run format:write && npm run package", - "ci-test": "jest", - "coverage": "make-coverage-badge --output-path ./badges/coverage.svg", - "format:write": "prettier --write **/*.ts", - "format:check": "prettier --check **/*.ts", - "lint": "npx eslint . -c ./.gitea/linters/.eslintrc.yml", - "package": "ncc build src/index.ts --license licenses.txt", - "package:watch": "npm run package -- --watch", - "test": "jest", - "all": "npm run format:write && npm run lint && npm run test && npm run coverage && npm run package" - }, - "license": "MIT", - "jest": { - "preset": "ts-jest", - "verbose": true, - "clearMocks": true, - "testEnvironment": "node", - "moduleFileExtensions": [ - "js", - "ts" - ], - "testMatch": [ - "**/*.test.ts" - ], - "testPathIgnorePatterns": [ - "/node_modules/", - "/dist/" - ], - "transform": { - "^.+\\.ts$": "ts-jest" - }, - "coverageReporters": [ - "json-summary", - "text", - "lcov" - ], - "collectCoverage": true, - "collectCoverageFrom": [ - "./src/**" - ] - }, - "dependencies": { - "@actions/core": "^1.10.1" - }, - "devDependencies": { - "@types/jest": "^29.5.11", - "@types/node": "^20.11.10", - "@typescript-eslint/eslint-plugin": "^6.19.1", - "@typescript-eslint/parser": "^6.19.1", - "@vercel/ncc": "^0.38.1", - "eslint": "^8.56.0", - "eslint-plugin-github": "^4.10.1", - "eslint-plugin-jest": "^27.6.3", - "eslint-plugin-jsonc": "^2.13.0", - "eslint-plugin-prettier": "^5.1.3", - "jest": "^29.7.0", - "make-coverage-badge": "^1.2.0", - "prettier": "^3.2.4", - "prettier-eslint": "^16.3.0", - "ts-jest": "^29.1.2", - "typescript": "^5.3.3" - } - } \ No newline at end of file + "coverageReporters": [ + "json-summary", + "text", + "lcov" + ], + "collectCoverage": true, + "collectCoverageFrom": [ + "./src/**" + ] + }, + "dependencies": { + "@actions/core": "^1.10.1", + "@actions/exec": "^1.1.1" + }, + "devDependencies": { + "@types/jest": "^29.5.11", + "@types/node": "^20.11.10", + "@typescript-eslint/eslint-plugin": "^6.19.1", + "@typescript-eslint/parser": "^6.19.1", + "@vercel/ncc": "^0.38.1", + "eslint": "^8.56.0", + "eslint-plugin-github": "^4.10.1", + "eslint-plugin-jest": "^27.6.3", + "eslint-plugin-jsonc": "^2.13.0", + "eslint-plugin-prettier": "^5.1.3", + "jest": "^29.7.0", + "make-coverage-badge": "^1.2.0", + "prettier": "^3.2.4", + "prettier-eslint": "^16.3.0", + "ts-jest": "^29.1.2", + "typescript": "^5.3.3" + } +} diff --git a/src/base64.ts b/src/base64.ts new file mode 100644 index 0000000..6ec3c28 --- /dev/null +++ b/src/base64.ts @@ -0,0 +1,125 @@ +const Base64 = { + _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", + + encode: function (input: string): string { + let output = ""; + let chr1, chr2, chr3, enc1, enc2, enc3, enc4; + let i = 0; + + input = Base64._utf8_encode(input); + + while (i < input.length) { + + chr1 = input.charCodeAt(i++); + chr2 = input.charCodeAt(i++); + chr3 = input.charCodeAt(i++); + + enc1 = chr1 >> 2; + enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); + enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); + enc4 = chr3 & 63; + + if (isNaN(chr2)) { + enc3 = enc4 = 64; + } else if (isNaN(chr3)) { + enc4 = 64; + } + + output = output + + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4); + } + return output; + }, + + decode: function (input: string): string { + let output = ""; + let chr1, chr2, chr3; + let enc1, enc2, enc3, enc4; + let i = 0; + + input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); + + while (i < input.length) { + + enc1 = this._keyStr.indexOf(input.charAt(i++)); + enc2 = this._keyStr.indexOf(input.charAt(i++)); + enc3 = this._keyStr.indexOf(input.charAt(i++)); + enc4 = this._keyStr.indexOf(input.charAt(i++)); + + chr1 = (enc1 << 2) | (enc2 >> 4); + chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); + chr3 = ((enc3 & 3) << 6) | enc4; + + output = output + String.fromCharCode(chr1); + + if (enc3 != 64) { + output = output + String.fromCharCode(chr2); + } + if (enc4 != 64) { + output = output + String.fromCharCode(chr3); + } + } + + output = Base64._utf8_decode(output); + + return output; + }, + + _utf8_encode: function (string: string): string { + string = string.replace(/\r\n/g, "\n"); + let utftext = ""; + + for (let n = 0; n < string.length; n++) { + + let c = string.charCodeAt(n); + + if (c < 128) { + utftext += String.fromCharCode(c); + } + else if ((c > 127) && (c < 2048)) { + utftext += String.fromCharCode((c >> 6) | 192); + utftext += String.fromCharCode((c & 63) | 128); + } + else { + utftext += String.fromCharCode((c >> 12) | 224); + utftext += String.fromCharCode(((c >> 6) & 63) | 128); + utftext += String.fromCharCode((c & 63) | 128); + } + } + return utftext; + }, + + _utf8_decode: function (utftext: string): string { + let string = ""; + let i = 0; + let c = 0; + let c1 = 0; + let c2 = 0; + let c3 = 0; + + while (i < utftext.length) { + + c = utftext.charCodeAt(i); + + if (c < 128) { + string += String.fromCharCode(c); + i++; + } + else if ((c > 191) && (c < 224)) { + c2 = utftext.charCodeAt(i + 1); + string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); + i += 2; + } + else { + c2 = utftext.charCodeAt(i + 1); + c3 = utftext.charCodeAt(i + 2); + string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); + i += 3; + } + } + return string; + } +}; + +export default Base64; \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 6174f03..4cbaf4b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,18 +1,40 @@ -import * as core from '@actions/core' +import core from '@actions/core'; +import { exec } from '@actions/exec'; +import { URL } from 'node:url'; +import Base64 from './base64' import { execSync } from 'child_process'; -async function constructRepoURL(repository: string, ref: string, token: string): Promise { - core.info(`repo: ${repository}`) - core.info(`ref: ${ref}`) - core.info(`token: ${token}`) - core.info(`GITEA_REPOSITORY: ${process.env.GITHUB_REPOSITORY}`) - core.info(`GITEA_ACTOR: ${process.env.GITHUB_ACTOR}`) - core.info(`GITEA_REF: ${process.env.GITHUB_REF}`) - core.info(`GITEA_HEAD_REF: ${process.env.GITHUB_HEAD_REF}`) - core.info(`GITEA_BASE_REF: ${process.env.GITHUB_BASE_REF}`) - core.info(`GITEA_SERVER_URL: ${process.env.GITHUB_SERVER_URL}`) +type CLIOutput = { + out: string, + err: string +}; - return ''; +async function cli(command: string): Promise { + let out: CLIOutput = { + out: '', + err: '' + }; + const options = { listeners: { stdout: (data: Buffer) => { out.out += data.toString(); }, stderr: (data: Buffer) => { out.err += data.toString(); }}}; + await exec('git', ['rev-parse', '--short', 'HEAD'], options); + return out +} + +async function constructRepoURL(server: string, repository: string, ref: string, token: string): Promise { + + const purl = new URL(server); + + let consurl: string = purl.protocol; + if (purl.password) { + consurl += `:${purl.password}@`; + core.info('URL came with token already!'); + } else { + consurl += `:${Base64.encode(token)}@` + core.info('Sad, I had to tag on the token manually'); + } + consurl += purl.hostname; + consurl += `/${repository}` + + return consurl; } /** @@ -29,9 +51,13 @@ export async function run(): Promise { const lfs: boolean = core.getBooleanInput('lfs'); const fetchdepth: string = core.getInput('fetch-depth'); const path: string = core.getInput('path'); + const serverurl: string = core.getInput('server-url'); - const url = await constructRepoURL(repository, ref, token); + const url = await constructRepoURL(serverurl, repository, ref, token); + + core.info(`URL: ${url}`); + // const output = execSync(`git clone ${repository}`, { encoding: 'utf-8' }); // the default is 'buffer' // core.debug(`Output was: ${output}\n`); diff --git a/yarn.lock b/yarn.lock index 6a1f124..a6b45fe 100644 --- a/yarn.lock +++ b/yarn.lock @@ -15,6 +15,13 @@ "@actions/http-client" "^2.0.1" uuid "^8.3.2" +"@actions/exec@^1.1.1": + version "1.1.1" + resolved "https://registry.yarnpkg.com/@actions/exec/-/exec-1.1.1.tgz#2e43f28c54022537172819a7cf886c844221a611" + integrity sha512-+sCcHHbVdk93a0XT19ECtO/gIXoxvdsgQLzb2fE2/5sIZmWQuluYyjPQtrtTHdU1YzTZ7bAPN4sITq2xi1679w== + dependencies: + "@actions/io" "^1.0.1" + "@actions/http-client@^2.0.1": version "2.2.0" resolved "https://registry.yarnpkg.com/@actions/http-client/-/http-client-2.2.0.tgz#f8239f375be6185fcd07765efdcf0031ad5df1a0" @@ -23,6 +30,11 @@ tunnel "^0.0.6" undici "^5.25.4" +"@actions/io@^1.0.1": + version "1.1.3" + resolved "https://registry.yarnpkg.com/@actions/io/-/io-1.1.3.tgz#4cdb6254da7962b07473ff5c335f3da485d94d71" + integrity sha512-wi9JjgKLYS7U/z8PPbco+PvTb/nRWjeoFlJ1Qer83k/3C5PHQi28hiVdeE2kHXmIL99mQFawx8qt/JPjZilJ8Q== + "@ampproject/remapping@^2.2.0": version "2.2.1" resolved "https://registry.yarnpkg.com/@ampproject/remapping/-/remapping-2.2.1.tgz#99e8e11851128b8702cd57c33684f1d0f260b630"