Add tests and CI.

This commit is contained in:
Franz Diebold 2020-07-14 12:06:58 +02:00
parent a1deed7ade
commit 932bbbde38
11 changed files with 5392 additions and 84 deletions

3
.editorconfig Normal file
View file

@ -0,0 +1,3 @@
[*.js]
indent_style = space
indent_size = 2

15
.eslintrc.json Normal file
View file

@ -0,0 +1,15 @@
{
"env": {
"browser": true,
"commonjs": true,
"es2020": true
},
"extends": [
"google"
],
"parserOptions": {
"ecmaVersion": 11
},
"rules": {
}
}

20
.github/workflows/ci.yml vendored Normal file
View file

@ -0,0 +1,20 @@
name: Lint and Test
on: [push]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
persist-credentials: false
- uses: actions/setup-node@v1
with:
node-version: '12'
- name: Install dependencies
run: make install
- name: Lint
run: make lint
- name: Test
run: make test

View file

@ -7,7 +7,7 @@ jobs:
name: Linux Demo
runs-on: ubuntu-latest
steps:
- uses: franzdiebold/github-env-vars-action@v1.1.1
- uses: franzdiebold/github-env-vars-action@v1.1.2
- name: Print environment variables
run: |
echo "GITHUB_REPOSITORY_OWNER=$GITHUB_REPOSITORY_OWNER"
@ -19,7 +19,7 @@ jobs:
name: Windows Demo
runs-on: windows-latest
steps:
- uses: franzdiebold/github-env-vars-action@v1.1.1
- uses: franzdiebold/github-env-vars-action@v1.1.2
- name: Print environment variables
run: |
echo "GITHUB_REPOSITORY_OWNER=$Env:GITHUB_REPOSITORY_OWNER"
@ -31,7 +31,7 @@ jobs:
name: macOS Demo
runs-on: macos-latest
steps:
- uses: franzdiebold/github-env-vars-action@v1.1.1
- uses: franzdiebold/github-env-vars-action@v1.1.2
- name: Print environment variables
run: |
echo "GITHUB_REPOSITORY_OWNER=$GITHUB_REPOSITORY_OWNER"

15
Makefile Normal file
View file

@ -0,0 +1,15 @@
.PHONY: install
install:
npm install
.PHONY: lint
lint:
npm run lint
.PHONY: test
test:
npm run test
.PHONY: build
build:
npm run build

View file

@ -1,7 +1,7 @@
# :octocat: :rocket: GitHub Environment Variables Action
[![GitHub Action: View on Marketplace](https://img.shields.io/badge/GitHub%20Action-View_on_Marketplace-28a745?logo=github)](https://github.com/marketplace/actions/github-environment-variables-action)
[![Demo: available](https://img.shields.io/badge/Demo-available-orange)](https://github.com/FranzDiebold/github-env-vars-action/actions?query=workflow%3ADemo)
[![Demo: available](https://img.shields.io/badge/Demo-available-orange)](.github/workflows/demo.yml)
[version](https://img.shields.io/github/package-json/v/FranzDiebold/github-env-vars-action/primary?label=version)
[![license: MIT](https://img.shields.io/badge/license-MIT-brightgreen.svg)](./LICENSE)
@ -32,7 +32,7 @@ For a full list of default environment variables exposed by GitHub see [https://
```yaml
steps:
- uses: franzdiebold/github-env-vars-action@v1.1.1
- uses: franzdiebold/github-env-vars-action@v1.1.2
- name: Print environment variables
run: |
echo "GITHUB_REPOSITORY_OWNER=$GITHUB_REPOSITORY_OWNER"

64
dist/index.js vendored
View file

@ -157,67 +157,103 @@ module.exports = require("path");
/***/ }),
/***/ 622:
/***/ (function(__unusedmodule, __unusedexports, __webpack_require__) {
/***/ (function(module, __unusedexports, __webpack_require__) {
// Franz Diebold
const core = __webpack_require__(827);
function getShaShort(fullSha) {
return fullSha ? fullSha.substring(0, 8) : null;
}
/**
* Get the repository owner from the repository string.
* @param {string} repository
* @return {string} The owner of the repository.
*/
function getRepositoryOwner(repository) {
return repository ? repository.split('/')[0] : null;
}
/**
* Get the repository name from the repository string.
* @param {string} repository
* @return {string} The name of the repository.
*/
function getRepositoryName(repository) {
return repository ? repository.split('/')[1] : null;
}
/**
* Get the ref name from the ref string.
* @param {string} ref
* @return {string} The ref name.
*/
function getRefName(ref) {
return ref ? ref.split('/').slice(2).join('/') : null;
}
/**
* Get the short SHA from the full SHA.
* @param {string} fullSha
* @return {string} The short SHA.
*/
function getShaShort(fullSha) {
return fullSha ? fullSha.substring(0, 8) : null;
}
// https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables#default-environment-variables
try {
repository = process.env.GITHUB_REPOSITORY; // FranzDiebold/github-env-vars-action
// i.e. FranzDiebold/github-env-vars-action
repository = process.env.GITHUB_REPOSITORY;
repositoryOwner = getRepositoryOwner(repository);
if (repositoryOwner) {
core.exportVariable('GITHUB_REPOSITORY_OWNER', repositoryOwner);
core.info(`Set GITHUB_REPOSITORY_OWNER=${process.env.GITHUB_REPOSITORY_OWNER}`);
core.info(`Set GITHUB_REPOSITORY_OWNER=` +
`${process.env.GITHUB_REPOSITORY_OWNER}`);
} else {
core.warning('Environment variable "GITHUB_REPOSITORY" not set. Cannot set "GITHUB_REPOSITORY_OWNER".');
core.warning('Environment variable "GITHUB_REPOSITORY" not set. ' +
'Cannot set "GITHUB_REPOSITORY_OWNER".');
}
repositoryName = getRepositoryName(repository);
if (repositoryName) {
core.exportVariable('GITHUB_REPOSITORY_NAME', repositoryName);
core.info(`Set GITHUB_REPOSITORY_NAME=${process.env.GITHUB_REPOSITORY_NAME}`);
core.info(`Set GITHUB_REPOSITORY_NAME=` +
`${process.env.GITHUB_REPOSITORY_NAME}`);
} else {
core.warning('Environment variable "GITHUB_REPOSITORY" not set. Cannot set "GITHUB_REPOSITORY_NAME".');
core.warning('Environment variable "GITHUB_REPOSITORY" not set. ' +
'Cannot set "GITHUB_REPOSITORY_NAME".');
}
refName = getRefName(process.env.GITHUB_REF); // refs/heads/feat/feature-branch-1
// i.e. refs/heads/feat/feature-branch-1
refName = getRefName(process.env.GITHUB_REF);
if (refName) {
core.exportVariable('GITHUB_REF_NAME', refName);
core.info(`Set GITHUB_REF_NAME=${process.env.GITHUB_REF_NAME}`);
} else {
core.warning('Environment variable "GITHUB_REF" not set. Cannot set "GITHUB_REF_NAME".');
core.warning('Environment variable "GITHUB_REF" not set. ' +
'Cannot set "GITHUB_REF_NAME".');
}
shaShort = getShaShort(process.env.GITHUB_SHA); // ffac537e6cbbf934b08745a378932722df287a53
// i.e. ffac537e6cbbf934b08745a378932722df287a53
shaShort = getShaShort(process.env.GITHUB_SHA);
if (shaShort) {
core.exportVariable('GITHUB_SHA_SHORT', shaShort);
core.info(`Set GITHUB_SHA_SHORT=${process.env.GITHUB_SHA_SHORT}`);
} else {
core.warning('Environment variable "GITHUB_SHA" not set. Cannot set "GITHUB_SHA_SHORT".');
core.warning('Environment variable "GITHUB_SHA" not set. ' +
'Cannot set "GITHUB_SHA_SHORT".');
}
} catch (error) {
core.setFailed(error.message);
}
module.exports = {
getRepositoryOwner,
getRepositoryName,
getRefName,
getShaShort,
};
/***/ }),

View file

@ -2,57 +2,93 @@
const core = require('@actions/core');
function getShaShort(fullSha) {
return fullSha ? fullSha.substring(0, 8) : null;
}
/**
* Get the repository owner from the repository string.
* @param {string} repository
* @return {string} The owner of the repository.
*/
function getRepositoryOwner(repository) {
return repository ? repository.split('/')[0] : null;
}
/**
* Get the repository name from the repository string.
* @param {string} repository
* @return {string} The name of the repository.
*/
function getRepositoryName(repository) {
return repository ? repository.split('/')[1] : null;
}
/**
* Get the ref name from the ref string.
* @param {string} ref
* @return {string} The ref name.
*/
function getRefName(ref) {
return ref ? ref.split('/').slice(2).join('/') : null;
}
/**
* Get the short SHA from the full SHA.
* @param {string} fullSha
* @return {string} The short SHA.
*/
function getShaShort(fullSha) {
return fullSha ? fullSha.substring(0, 8) : null;
}
// https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables#default-environment-variables
try {
repository = process.env.GITHUB_REPOSITORY; // FranzDiebold/github-env-vars-action
// i.e. FranzDiebold/github-env-vars-action
repository = process.env.GITHUB_REPOSITORY;
repositoryOwner = getRepositoryOwner(repository);
if (repositoryOwner) {
core.exportVariable('GITHUB_REPOSITORY_OWNER', repositoryOwner);
core.info(`Set GITHUB_REPOSITORY_OWNER=${process.env.GITHUB_REPOSITORY_OWNER}`);
core.info(`Set GITHUB_REPOSITORY_OWNER=` +
`${process.env.GITHUB_REPOSITORY_OWNER}`);
} else {
core.warning('Environment variable "GITHUB_REPOSITORY" not set. Cannot set "GITHUB_REPOSITORY_OWNER".');
core.warning('Environment variable "GITHUB_REPOSITORY" not set. ' +
'Cannot set "GITHUB_REPOSITORY_OWNER".');
}
repositoryName = getRepositoryName(repository);
if (repositoryName) {
core.exportVariable('GITHUB_REPOSITORY_NAME', repositoryName);
core.info(`Set GITHUB_REPOSITORY_NAME=${process.env.GITHUB_REPOSITORY_NAME}`);
core.info(`Set GITHUB_REPOSITORY_NAME=` +
`${process.env.GITHUB_REPOSITORY_NAME}`);
} else {
core.warning('Environment variable "GITHUB_REPOSITORY" not set. Cannot set "GITHUB_REPOSITORY_NAME".');
core.warning('Environment variable "GITHUB_REPOSITORY" not set. ' +
'Cannot set "GITHUB_REPOSITORY_NAME".');
}
refName = getRefName(process.env.GITHUB_REF); // refs/heads/feat/feature-branch-1
// i.e. refs/heads/feat/feature-branch-1
refName = getRefName(process.env.GITHUB_REF);
if (refName) {
core.exportVariable('GITHUB_REF_NAME', refName);
core.info(`Set GITHUB_REF_NAME=${process.env.GITHUB_REF_NAME}`);
} else {
core.warning('Environment variable "GITHUB_REF" not set. Cannot set "GITHUB_REF_NAME".');
core.warning('Environment variable "GITHUB_REF" not set. ' +
'Cannot set "GITHUB_REF_NAME".');
}
shaShort = getShaShort(process.env.GITHUB_SHA); // ffac537e6cbbf934b08745a378932722df287a53
// i.e. ffac537e6cbbf934b08745a378932722df287a53
shaShort = getShaShort(process.env.GITHUB_SHA);
if (shaShort) {
core.exportVariable('GITHUB_SHA_SHORT', shaShort);
core.info(`Set GITHUB_SHA_SHORT=${process.env.GITHUB_SHA_SHORT}`);
} else {
core.warning('Environment variable "GITHUB_SHA" not set. Cannot set "GITHUB_SHA_SHORT".');
core.warning('Environment variable "GITHUB_SHA" not set. ' +
'Cannot set "GITHUB_SHA_SHORT".');
}
} catch (error) {
core.setFailed(error.message);
}
module.exports = {
getRepositoryOwner,
getRepositoryName,
getRefName,
getShaShort,
};

44
index.test.js Normal file
View file

@ -0,0 +1,44 @@
const {
getRepositoryOwner, getRepositoryName, getRefName, getShaShort,
} = require('./index');
test('gets repository owner', () => {
expect(getRepositoryOwner('FranzDiebold/github-env-vars-action'))
.toEqual('FranzDiebold');
});
test('gets repository owner for empty GITHUB_REPOSITORY', () => {
expect(getRepositoryOwner(undefined)).toBeFalsy();
});
test('gets repository name', () => {
expect(getRepositoryName('FranzDiebold/github-env-vars-action'))
.toEqual('github-env-vars-action');
});
test('gets repository name for empty GITHUB_REPOSITORY', () => {
expect(getRepositoryName(undefined)).toBeFalsy();
});
test('gets ref from simple ref name', () => {
expect(getRefName('refs/heads/feature-branch-1'))
.toEqual('feature-branch-1');
});
test('gets ref from complex ref name', () => {
expect(getRefName('refs/heads/feat/feature-branch-1'))
.toEqual('feat/feature-branch-1');
});
test('gets repository name for empty GITHUB_REF_NAME', () => {
expect(getRefName(undefined)).toBeFalsy();
});
test('gets short SHA', () => {
expect(getShaShort('ffac537e6cbbf934b08745a378932722df287a53'))
.toEqual('ffac537e');
});
test('gets short SHA for empty GITHUB_SHA', () => {
expect(getShaShort(undefined)).toBeFalsy();
});

5135
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,11 +1,12 @@
{
"name": "github-env-vars-action",
"version": "1.1.1",
"version": "1.1.2",
"description": "A GitHub Action to expose useful environment variables.",
"main": "index.js",
"scripts": {
"build": "ncc build index.js",
"test": "echo \"Error: no test specified\" && exit 1"
"lint": "eslint *.js",
"test": "jest",
"build": "ncc build index.js"
},
"repository": {
"type": "git",
@ -23,5 +24,10 @@
"homepage": "https://github.com/FranzDiebold/github-env-vars-action#readme",
"dependencies": {
"@actions/core": "^1.2.4"
},
"devDependencies": {
"eslint": "^7.4.0",
"eslint-config-google": "^0.14.0",
"jest": "^26.1.0"
}
}