Skip to content

Commit

Permalink
Merge pull request #7 from CityOfLosAngeles/ghi-6
Browse files Browse the repository at this point in the history
fix: refactor the src code & add initial ci tests
  • Loading branch information
jaylenw authored May 18, 2023
2 parents ae9a65f + 3aa0dfa commit 398fb8c
Show file tree
Hide file tree
Showing 5,030 changed files with 402,249 additions and 1,115 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
29 changes: 21 additions & 8 deletions .github/workflows/example-runs.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
name: Example Runs
name: Example Runs & CI Tests

on: push
on:
push:
workflow_dispatch:

jobs:
example:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- id: fetch-latest-release
- id: fetch-latest-github-release
uses: ./

- name: print out latest release tag
run: echo "The latest release tag is $TAG"
env:
TAG: ${{ steps.fetch-latest-release.outputs.tag_name }}
TAG: ${{ steps.fetch-latest-github-release.outputs.tag_name }}
customRepoExample:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- id: fetch-latest-release
- id: fetch-latest-github-release
uses: ./
with:
github_token: ${{ github.token }}
Expand All @@ -29,4 +31,15 @@ jobs:
- name: print out latest release tag
run: echo "The latest release tag is $TAG"
env:
TAG: ${{ steps.fetch-latest-release.outputs.tag_name }}
TAG: ${{ steps.fetch-latest-github-release.outputs.tag_name }}
JestTests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Use Node.js 16.x
uses: actions/setup-node@v3
with:
node-version: '16.x'
- name: Application Tests
run: npm test

19 changes: 19 additions & 0 deletions .github/workflows/test-coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This workflow runs the tests for the application and comments the coverage report to the pull requests that was open.
# It uses the Action described here https://github.com/marketplace/actions/jest-annotations-coverage.

name: Tests Coverage
on: pull_request
jobs:
coverage:
runs-on: ubuntu-22.04
steps:
- name: Checkout Respository
uses: actions/checkout@v3
- name: Use Node.js 16.x
uses: actions/setup-node@v2
with:
node-version: '16'
- name: Run Jest Test Coverage
uses: mattallty/jest-github-action@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
!node_modules
coverage
github_output_test.txt
1 change: 1 addition & 0 deletions .jest/setEnvVars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
process.env.INPUT_GITHUB_TOKEN = "1234567890abcdef1234567890abcdef12345678"
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

A tiny GitHub action to fetch the latest GitHub Release for a given repository. Originally forked from https://github.com/gregziegan/fetch-latest-release on 2023/04/27.

[![Development Example Test Runs & CI Tests](https://github.com/CityOfLosAngeles/fetch-latest-github-release/actions/workflows/example-runs.yml/badge.svg?branch=development)](https://github.com/CityOfLosAngeles/fetch-latest-github-release/actions/workflows/example-runs.yml)

[![Production Example Test Runs & CI Tests](https://github.com/CityOfLosAngeles/fetch-latest-github-release/actions/workflows/example-runs.yml/badge.svg?branch=production)](https://github.com/CityOfLosAngeles/fetch-latest-github-release/actions/workflows/example-runs.yml)

## Inputs

| Parameter | Description | Required | Default |
Expand Down Expand Up @@ -35,9 +39,12 @@ A tiny GitHub action to fetch the latest GitHub Release for a given repository.

```yaml
steps:
- id: fetch-latest-release
uses: thebritican/fetch-latest-release@v1
- id: fetch-latest-github-release
uses: cityoflosangeles/fetch-latest-github-release@{latest-release} # ex. v1, v2, v3 etc. See https://github.com/CityOfLosAngeles/fetch-latest-github-release/releases
with:
github_token: ${{ github.token }}
```
## CI / Unit Tests
To run the tests locally, run `npm test`.
112 changes: 80 additions & 32 deletions action.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
const core = require('@actions/core')
const github = require('@actions/github')
const process = require('process')
const core = require('@actions/core');
const github = require('@actions/github');
const process = require('process');
const fs = require('fs/promises');
const config = require('config');

const customRepo = (repoPath) => {
const segments = repoPath.split('/', 2)
const segments = repoPath.split('/', 2);

if (segments.length < 2) {
core.info('Please provide a repository in the format `owner/repo`.')
}

return segments
return segments;
}

const repoInput = core.getInput('repo_path')
const repoInput = core.getInput('repo_path');

const [owner, repo] = repoInput
? customRepo(repoInput)
: process.env['GITHUB_REPOSITORY'].split('/', 2)
: config.get('GH_REPOSITORY').split('/', 2);

const octokit = new github.GitHub(
core.getInput('github_token', { required: true })
)

async function appendGHOutputfile(content) {
try {
await fs.appendFile(config.get('GITHUB_OUTPUT'), content);
} catch (error) {
core.info('Could not write to the GITHUB_OUTPUT environment file.');
core.setFailed(`Action failed with error ${error}`);
}
}

async function run() {
let latestRelease

Expand All @@ -33,35 +44,72 @@ async function run() {
repo,
})
} catch (error) {
core.info('Could not fetch the latest release. Have you made one yet?')
core.setFailed(error)
core.info('Could not fetch the latest release. Have you made one yet?');
core.setFailed(`Action failed with error ${error}`);
}

const { data } = latestRelease;

const releaseAttributes = [
'url',
'assets_url',
'upload_url',
'html_url',
'id',
'node_id',
'tag_name',
'target_commitish',
'name',
'body',
'draft',
'prerelease',
'author_id',
'author_node_id',
'author_url',
'author_login',
'author_html_url',
'author_type',
'author_site_admin'
];

let github_output = "";
for (i=0; i < releaseAttributes.length; i++) {
switch(true) {
case (releaseAttributes[i] == "author_id"): // ex. author_id > data.author.id.toString()
tempData = data.author.id.toString();
github_output = releaseAttributes[i] + '=' + tempData + "\n" + github_output;
break;
case (releaseAttributes[i].includes("author")): // ex. author_url > data.author.url
tempAuthorAttribute = releaseAttributes[i];
tempAuthorAttribute = tempAuthorAttribute.replace("author_", "");
github_output = releaseAttributes[i] + '=' + data.author[tempAuthorAttribute] + "\n" + github_output;
break;
case (releaseAttributes[i] == "id"): // ex. id > data.id.toString()
tempData = data[releaseAttributes[i]].toString();
github_output = releaseAttributes[i] + '=' + tempData + "\n" + github_output;
break;
case (releaseAttributes[i] == "body"):
let EOF = config.get('EOF'); // see https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
github_output = releaseAttributes[i] + '<<' + EOF + '\n' + data[releaseAttributes[i]] + '\n' + EOF + "\n" + github_output;
break;
default: // ex. assets_url > data.assets_url
github_output = releaseAttributes[i] + '=' + data[releaseAttributes[i]] + "\n" + github_output;
}
}

const { data } = latestRelease

core.setOutput('url', data.url)
core.setOutput('assets_url', data.assets_url)
core.setOutput('upload_url', data.upload_url)
core.setOutput('html_url', data.html_url)
core.setOutput('id', data.id.toString())
core.setOutput('node_id', data.node_id)
core.setOutput('tag_name', data.tag_name)
core.setOutput('target_commitish', data.target_commitish)
core.setOutput('name', data.name)
core.setOutput('body', data.body)
core.setOutput('draft', data.draft)
core.setOutput('prerelease', data.prerelease)
core.setOutput('author_id', data.author.id.toString())
core.setOutput('author_node_id', data.author.node_id)
core.setOutput('author_url', data.author.url)
core.setOutput('author_login', data.author.login)
core.setOutput('author_html_url', data.author.html_url)
core.setOutput('author_type', data.author.type)
core.setOutput('author_site_admin', data.author.site_admin)
try {
await appendGHOutputfile(github_output);
} catch (error) {
core.setFailed(`Action failed with error ${error}`);
}
}

try {
run()
if(process.env.NODE_ENV != "test") { // don't execute run when jest tests are running as run is exported for jest testing
run();
}
} catch (error) {
core.setFailed(`Action failed with error ${error}`)
core.setFailed(`Action failed with error ${error}`);
}

module.exports.run = run;
8 changes: 4 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name: Fetch Latest Release
description: A tiny GitHub action to fetch the latest GitHub release for a given repository
author: Greg Ziegan
name: Fetch Latest GitHub Release
description: A tiny GitHub action to fetch the latest GitHub release for a given repository. Originally forked from https://github.com/gregziegan/fetch-latest-release on 2023/04/27.
author: City of Los Angeles
branding:
color: green
icon: tag

runs:
using: node12
using: node16
main: action.js

inputs:
Expand Down
10 changes: 10 additions & 0 deletions config/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const crypto = require('crypto');

module.exports = {
// see more information here https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
"GH_REPOSITORY": process.env.GITHUB_REPOSITORY,
// see more information here https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#example-of-setting-an-output-parameter
"GITHUB_OUTPUT": process.env.GITHUB_OUTPUT,
// see https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
"EOF": crypto.randomBytes(16).toString("hex")
}
11 changes: 11 additions & 0 deletions config/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const crypto = require('crypto');

module.exports = {
// set to the GITHUB_REPOSITORY environment variable if it exists by the GH runner, otherwise default to "hiimbex/testing-things" for testing
"GH_REPOSITORY": "hiimbex/testing-things",
// set to GITHUB_OUTPUT with the path from the Actions Runner, otherwise default to "./test/github_output_test.txt" for testing
// see more information here https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#example-of-setting-an-output-parameter
"GITHUB_OUTPUT": "./test/github_output_test.txt",
// see https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
"EOF": crypto.randomBytes(16).toString("hex")
}
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
roots: ["<rootDir>/test/"],
setupFiles: ["<rootDir>/.jest/setEnvVars.js"]
};
1 change: 1 addition & 0 deletions node_modules/.bin/browserslist

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

1 change: 1 addition & 0 deletions node_modules/.bin/esparse

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

1 change: 1 addition & 0 deletions node_modules/.bin/esvalidate

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

1 change: 1 addition & 0 deletions node_modules/.bin/import-local-fixture

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

1 change: 1 addition & 0 deletions node_modules/.bin/jest

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

1 change: 1 addition & 0 deletions node_modules/.bin/js-yaml

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

1 change: 1 addition & 0 deletions node_modules/.bin/jsesc

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

1 change: 1 addition & 0 deletions node_modules/.bin/json5

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

1 change: 1 addition & 0 deletions node_modules/.bin/parser

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

1 change: 1 addition & 0 deletions node_modules/.bin/resolve

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

1 change: 1 addition & 0 deletions node_modules/.bin/update-browserslist-db

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

1 change: 1 addition & 0 deletions node_modules/.bin/uuid

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

Loading

0 comments on commit 398fb8c

Please sign in to comment.