Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add wrapper for GitHub Actions #437

Merged
merged 35 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
a97c4b4
feat: add initial github action config
kanadgupta Feb 10, 2022
8776302
ci: add gh action test
kanadgupta Feb 11, 2022
d3a5c83
test: refactor test to check for exact UA match
kanadgupta Feb 11, 2022
4f3314a
feat: dynamically set user agent based on env
kanadgupta Feb 11, 2022
53b8b89
chore: temporarily skip test suite
kanadgupta Feb 11, 2022
69aa198
chore: silence npm i logs
kanadgupta Feb 11, 2022
e6da768
ci: restore tests, move action test to separate workflow file
kanadgupta Feb 11, 2022
243c598
Update action.yml
kanadgupta Feb 11, 2022
d531258
chore: update param names, job names
kanadgupta Feb 11, 2022
251bd1b
fix: attempt to get npm install working properly
kanadgupta Feb 11, 2022
09c3fe9
chore: update loglevel
kanadgupta Feb 11, 2022
90c4002
chore: ok updating log level AGAIN
kanadgupta Feb 11, 2022
33597ec
chore: set working directory instead
kanadgupta Feb 11, 2022
51e53f4
test: attempt to get CI working again
kanadgupta Feb 11, 2022
ea27084
test: how about this?
kanadgupta Feb 11, 2022
46d5dae
refactor: consolidate user-agent logic
kanadgupta Feb 11, 2022
88ea683
refactor: move path to `working_directory`
kanadgupta Feb 11, 2022
e3bac55
chore: typo
kanadgupta Feb 11, 2022
c54e3a1
chore: remove unnecessary quotes
kanadgupta Feb 11, 2022
6709f78
chore: oops
kanadgupta Feb 11, 2022
35d82e3
chore: does this look any cleaner?
kanadgupta Feb 11, 2022
a7f3908
fix: try this
kanadgupta Feb 11, 2022
a555401
revert: eh nvm
kanadgupta Feb 11, 2022
1fb457e
chore: removing quotes in one last place
kanadgupta Feb 11, 2022
e6336c1
revert: move this back
kanadgupta Feb 11, 2022
21b186a
ci: update test workflow to test it with another repo
kanadgupta Feb 12, 2022
e31c66b
ci: will this work?
kanadgupta Feb 12, 2022
d7bc88d
ci: add a token that hopefully works
kanadgupta Feb 12, 2022
d51b0dd
Merge branch 'main' into deprecate-our-github-action-rm-1944
kanadgupta Feb 12, 2022
72d184a
chore: consolidate CI
kanadgupta Feb 14, 2022
684b9b5
refactor: consolidate nock logic
kanadgupta Feb 14, 2022
4af153a
chore: small formatting tweaks
kanadgupta Feb 14, 2022
6ca3e57
ci: add upload step
kanadgupta Feb 14, 2022
e0000e1
fix: see if chalk prints colors nicely with this env var
kanadgupta Feb 14, 2022
1f8ce22
chore: update workflow docs a bit
kanadgupta Feb 14, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on: [push]

jobs:
build:
name: Test Suite
runs-on: ubuntu-latest
strategy:
matrix:
Expand All @@ -22,3 +23,30 @@ jobs:

- name: Run tests
run: npm test

action:
name: GitHub Action Dry Run
runs-on: ubuntu-latest
steps:
- name: Checkout GitHub Action
uses: actions/checkout@v2.4.0
with:
path: rdme-repo

- name: Checkout external repo containing OpenAPI file
uses: actions/checkout@v2.4.0
with:
path: oas-examples-repo
repository: readmeio/oas-examples
token: ${{ secrets.OAS_EXAMPLES_READ }}

- name: Run `validate` command
uses: ./rdme-repo/
with:
rdme: validate oas-examples-repo/3.1/json/petstore.json

# Docs: https://rdme-test.readme.io
- name: Run `openapi` command
uses: ./rdme-repo/
with:
rdme: openapi oas-examples-repo/3.1/json/petstore.json --key=${{ secrets.RDME_TEST_PROJECT_API_KEY }} --id=${{ secrets.RDME_TEST_PROJECT_API_SETTING }}
9 changes: 2 additions & 7 deletions __tests__/cmds/docs.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
const nock = require('nock');
const chalk = require('chalk');
const config = require('config');
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const frontMatter = require('gray-matter');

const APIError = require('../../src/lib/apiError');
const getApiNock = require('../get-api-nock');
const { userAgent } = require('../get-api-nock');

const DocsCommand = require('../../src/cmds/docs');
const DocsEditCommand = require('../../src/cmds/docs/edit');
Expand All @@ -23,11 +21,8 @@ const category = 'CATEGORY_ID';
const apiSetting = 'API_SETTING_ID';

function getNockWithVersionHeader(v) {
return nock(config.get('host'), {
reqheaders: {
'x-readme-version': v,
'User-Agent': userAgent,
},
return getApiNock({
'x-readme-version': v,
});
}

Expand Down
13 changes: 5 additions & 8 deletions __tests__/get-api-nock.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
const nock = require('nock');
const config = require('config');
const pkg = require('../package.json');

const userAgent = `rdme/${pkg.version}`;
const nock = require('nock');
const { getUserAgent } = require('../src/lib/fetch');

module.exports = function () {
module.exports = function (reqHeaders = {}) {
return nock(config.get('host'), {
reqheaders: {
'User-Agent': userAgent,
'User-Agent': getUserAgent(),
...reqHeaders,
},
});
};

module.exports.userAgent = userAgent;
34 changes: 32 additions & 2 deletions __tests__/lib/fetch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,38 @@ const config = require('config');
const fetch = require('../../src/lib/fetch');
const { cleanHeaders, handleRes } = require('../../src/lib/fetch');
const getApiNock = require('../get-api-nock');
const pkg = require('../../package.json');

describe('#fetch()', () => {
describe('GitHub Actions environment', () => {
beforeEach(() => {
process.env.GITHUB_ACTIONS = 'true';
});

afterEach(() => {
delete process.env.GITHUB_ACTIONS;
});

it('should use correct user-agent for requests in GitHub Action env', async () => {
const key = 'API_KEY';

const mock = getApiNock()
.get('/api/v1')
.basicAuth({ user: key })
.reply(200, function () {
return this.req.headers['user-agent'];
});

const userAgent = await fetch(`${config.get('host')}/api/v1`, {
method: 'get',
headers: cleanHeaders(key),
}).then(handleRes);

expect(userAgent.shift()).toBe(`rdme-github/${pkg.version}`);
mock.done();
});
});

it('should wrap all requests with a rdme User-Agent', async () => {
const key = 'API_KEY';

Expand All @@ -19,7 +49,7 @@ describe('#fetch()', () => {
headers: cleanHeaders(key),
}).then(handleRes);

expect(userAgent.shift()).toMatch(/rdme\/\d+.\d+.\d+/);
expect(userAgent.shift()).toBe(`rdme/${pkg.version}`);
mock.done();
});

Expand All @@ -32,7 +62,7 @@ describe('#fetch()', () => {

const userAgent = await fetch(`${config.get('host')}/api/v1/doesnt-need-auth`).then(handleRes);

expect(userAgent.shift()).toMatch(/rdme\/\d+.\d+.\d+/);
expect(userAgent.shift()).toBe(`rdme/${pkg.version}`);
mock.done();
});
});
Expand Down
28 changes: 28 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: rdme
author: ReadMe
branding:
color: blue
icon: book-open
description: Update your ReadMe developer hubs in a CI environment.
inputs:
rdme:
description: Command to pass into rdme
required: true
runs:
using: composite
steps:
# This is so we can guarantee a version of npm that supports lockfile@2
- name: setup node
uses: actions/setup-node@v2
with:
node-version: 16
# When we add debug support, we should make this a conditional step, see RM-3545
- name: Install rdme deps
run: npm install --production --silent
shell: bash
working-directory: ${{ github.action_path }}
- name: Execute rdme command
run: ${{ github.action_path }}/bin/rdme ${{ inputs.rdme }}
shell: bash
env:
FORCE_COLOR: '1'
16 changes: 12 additions & 4 deletions src/lib/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,27 @@ const APIError = require('./apiError');
*
*/
module.exports = (url, options = {}) => {
const userAgent = `rdme/${pkg.version}`;

if (!options.headers) {
options.headers = {
'User-Agent': userAgent,
'User-Agent': module.exports.getUserAgent(),
};
} else {
options.headers['User-Agent'] = userAgent;
options.headers['User-Agent'] = module.exports.getUserAgent();
}

return fetch(url, options);
};

/**
* Getter function for a string to be used in the user-agent header
* based on the current environment.
*
*/
module.exports.getUserAgent = function getUserAgent() {
const gh = process.env.GITHUB_ACTIONS === 'true' ? '-github' : '';
return `rdme${gh}/${pkg.version}`;
};

/**
* Small handler for transforming responses from our API into JSON and if there's errors, throwing
* an APIError exception.
Expand Down