migrate service tests to GH actions (#8421)

* run service tests with GHA

* rename GithubGist services to Gist

* completely delete circle ci config
This commit is contained in:
chris48s
2023-01-17 20:55:57 +00:00
committed by GitHub
parent 27a2deb7d2
commit e8c42e9ec1
12 changed files with 192 additions and 278 deletions

View File

@@ -1,77 +0,0 @@
version: 2
services_steps: &services_steps
steps:
- checkout
- run:
name: Install dependencies
command: |
npm ci
environment:
CYPRESS_INSTALL_BINARY: 0
- run:
name: Identify services tagged in the PR title
command: npm run test:services:pr:prepare
- run:
name: Run tests for tagged services
environment:
mocha_reporter: mocha-junit-reporter
MOCHA_FILE: junit/services/results.xml
command: RETRY_COUNT=3 npm run test:services:pr:run
- store_test_results:
path: junit
jobs:
services:
docker:
- image: cimg/node:16.15
<<: *services_steps
services@node-17:
docker:
- image: cimg/node:17.9
environment:
NPM_CONFIG_ENGINE_STRICT: 'false'
<<: *services_steps
workflows:
version: 2
on-commit:
jobs:
- services:
filters:
branches:
ignore:
- master
- gh-pages
- services@node-17:
filters:
branches:
ignore:
- master
- gh-pages
# on-commit-with-cache:
# jobs:
# - npm-install:
# filters:
# branches:
# ignore: gh-pages
# - services:
# requires:
# - npm-install
# filters:
# branches:
# ignore: master
# - services@node-latest:
# requires:
# - npm-install
# filters:
# branches:
# ignore: master

View File

@@ -0,0 +1,86 @@
name: 'Service tests'
description: 'Run tests for selected services'
inputs:
github-token:
description: 'The GITHUB_TOKEN secret'
required: true
librariesio-tokens:
description: 'The SERVICETESTS_LIBRARIESIO_TOKENS secret'
required: false
default: ''
obs-user:
description: 'The SERVICETESTS_OBS_USER secret'
required: false
default: ''
obs-pass:
description: 'The SERVICETESTS_OBS_PASS secret'
required: false
default: ''
sl-insight-user-uuid:
description: 'The SERVICETESTS_SL_INSIGHT_USER_UUID secret'
required: false
default: ''
sl-insight-api-token:
description: 'The SERVICETESTS_SL_INSIGHT_API_TOKEN secret'
required: false
default: ''
twitch-client-id:
description: 'The SERVICETESTS_TWITCH_CLIENT_ID secret'
required: false
default: ''
twitch-client-secret:
description: 'The SERVICETESTS_TWITCH_CLIENT_SECRET secret'
required: false
default: ''
wheelmap-token:
description: 'The SERVICETESTS_WHEELMAP_TOKEN secret'
required: false
default: ''
youtube-api-key:
description: 'The SERVICETESTS_YOUTUBE_API_KEY secret'
required: false
default: ''
runs:
using: 'composite'
steps:
- name: Derive list of service tests to run
# Note: In this step we are using an intermediate env var instead of
# passing github.event.pull_request.title as an argument
# to prevent a shell injection attack. Further reading:
# https://securitylab.github.com/research/github-actions-untrusted-input/#exploitability-and-impact
# https://securitylab.github.com/research/github-actions-untrusted-input/#remediation
if: always()
env:
TITLE: ${{ github.event.pull_request.title }}
run: npm run test:services:pr:prepare "$TITLE"
shell: bash
- name: Run service tests
if: always()
run: npm run test:services:pr:run -- --reporter json --reporter-option 'output=reports/service-tests.json'
shell: bash
env:
RETRY_COUNT: 3
GH_TOKEN: '${{ inputs.github-token }}'
LIBRARIESIO_TOKENS: '${{ inputs.librariesio-tokens }}'
OBS_USER: '${{ inputs.obs-user }}'
OBS_PASS: '${{ inputs.obs-pass }}'
SL_INSIGHT_USER_UUID: '${{ inputs.sl-insight-user-uuid }}'
SL_INSIGHT_API_TOKEN: '${{ inputs.sl-insight-api-token }}'
TWITCH_CLIENT_ID: '${{ inputs.twitch-client-id }}'
TWITCH_CLIENT_SECRET: '${{ inputs.twitch-client-secret }}'
WHEELMAP_TOKEN: '${{ inputs.wheelmap-token }}'
YOUTUBE_API_KEY: '${{ inputs.youtube-api-key }}'
- name: Write Markdown Summary
if: always()
run: |
if test -f 'reports/service-tests.json'; then
echo '# Services' >> $GITHUB_STEP_SUMMARY
sed -e 's/^/- /' pull-request-services.log >> $GITHUB_STEP_SUMMARY
node scripts/mocha2md.js Report reports/service-tests.json >> $GITHUB_STEP_SUMMARY
else
echo 'No services found. Nothing to do.' >> $GITHUB_STEP_SUMMARY
fi
shell: bash

40
.github/workflows/test-services-17.yml vendored Normal file
View File

@@ -0,0 +1,40 @@
name: Services@node 17
on:
pull_request:
types: [opened, edited, reopened, synchronize]
jobs:
test-services-17:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup
uses: ./.github/actions/setup
with:
node-version: 17
env:
NPM_CONFIG_ENGINE_STRICT: 'false'
- name: Service tests (triggered from local branch)
if: github.event.pull_request.head.repo.full_name == github.repository
uses: ./.github/actions/service-tests
with:
github-token: '${{ secrets.GH_PAT }}'
librariesio-tokens: '${{ secrets.SERVICETESTS_LIBRARIESIO_TOKENS }}'
obs-user: '${{ secrets.SERVICETESTS_OBS_USER }}'
obs-pass: '${{ secrets.SERVICETESTS_OBS_PASS }}'
sl-insight-user-uuid: '${{ secrets.SERVICETESTS_SL_INSIGHT_USER_UUID }}'
sl-insight-api-token: '${{ secrets.SERVICETESTS_SL_INSIGHT_API_TOKEN }}'
twitch-client-id: '${{ secrets.SERVICETESTS_TWITCH_CLIENT_ID }}'
twitch-client-secret: '${{ secrets.SERVICETESTS_TWITCH_CLIENT_SECRET }}'
wheelmap-token: '${{ secrets.SERVICETESTS_WHEELMAP_TOKEN }}'
youtube-api-key: '${{ secrets.SERVICETESTS_YOUTUBE_API_KEY }}'
- name: Service tests (triggered from fork)
if: github.event.pull_request.head.repo.full_name != github.repository
uses: ./.github/actions/service-tests
with:
github-token: '${{ secrets.GITHUB_TOKEN }}'

38
.github/workflows/test-services.yml vendored Normal file
View File

@@ -0,0 +1,38 @@
name: Services
on:
pull_request:
types: [opened, edited, reopened, synchronize]
jobs:
test-services:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup
uses: ./.github/actions/setup
with:
node-version: 16
- name: Service tests (triggered from local branch)
if: github.event.pull_request.head.repo.full_name == github.repository
uses: ./.github/actions/service-tests
with:
github-token: '${{ secrets.GH_PAT }}'
librariesio-tokens: '${{ secrets.SERVICETESTS_LIBRARIESIO_TOKENS }}'
obs-user: '${{ secrets.SERVICETESTS_OBS_USER }}'
obs-pass: '${{ secrets.SERVICETESTS_OBS_PASS }}'
sl-insight-user-uuid: '${{ secrets.SERVICETESTS_SL_INSIGHT_USER_UUID }}'
sl-insight-api-token: '${{ secrets.SERVICETESTS_SL_INSIGHT_API_TOKEN }}'
twitch-client-id: '${{ secrets.SERVICETESTS_TWITCH_CLIENT_ID }}'
twitch-client-secret: '${{ secrets.SERVICETESTS_TWITCH_CLIENT_SECRET }}'
wheelmap-token: '${{ secrets.SERVICETESTS_WHEELMAP_TOKEN }}'
youtube-api-key: '${{ secrets.SERVICETESTS_YOUTUBE_API_KEY }}'
- name: Service tests (triggered from fork)
if: github.event.pull_request.head.repo.full_name != github.repository
uses: ./.github/actions/service-tests
with:
github-token: '${{ secrets.GITHUB_TOKEN }}'

View File

@@ -1,102 +0,0 @@
/**
* @module
*/
import { URL, format as urlFormat } from 'url'
function formatSlug(owner, repo, pullRequest) {
return `${owner}/${repo}#${pullRequest}`
}
function parseGithubPullRequestUrl(url, options = {}) {
const { verifyBaseUrl } = options
const parsed = new URL(url)
const components = parsed.pathname.substr(1).split('/')
if (components[2] !== 'pull' || components.length !== 4) {
throw Error(`Invalid GitHub pull request URL: ${url}`)
}
const [owner, repo, , pullRequest] = components
parsed.pathname = ''
const baseUrl = urlFormat(parsed, {
auth: false,
fragment: false,
search: false,
}).replace(/\/$/, '')
if (verifyBaseUrl && baseUrl !== verifyBaseUrl) {
throw Error(`Expected base URL to be ${verifyBaseUrl} but got ${baseUrl}`)
}
return {
baseUrl,
owner,
repo,
pullRequest: +pullRequest,
slug: formatSlug(owner, repo, pullRequest),
}
}
function parseGithubRepoSlug(slug) {
const components = slug.split('/')
if (components.length !== 2) {
throw Error(`Invalid GitHub repo slug: ${slug}`)
}
const [owner, repo] = components
return { owner, repo }
}
function _inferPullRequestFromTravisEnv(env) {
const { owner, repo } = parseGithubRepoSlug(env.TRAVIS_REPO_SLUG)
const pullRequest = +env.TRAVIS_PULL_REQUEST
return {
owner,
repo,
pullRequest,
slug: formatSlug(owner, repo, pullRequest),
}
}
function _inferPullRequestFromCircleEnv(env) {
return parseGithubPullRequestUrl(
env.CI_PULL_REQUEST || env.CIRCLE_PULL_REQUEST
)
}
/**
* When called inside a CI build, infer the details
* of a pull request from the environment variables.
*
* @param {object} [env=process.env] Environment variables
* @returns {module:core/service-test-runner/infer-pull-request~PullRequest}
* Pull Request
*/
function inferPullRequest(env = process.env) {
if (env.TRAVIS) {
return _inferPullRequestFromTravisEnv(env)
} else if (env.CIRCLECI) {
return _inferPullRequestFromCircleEnv(env)
} else if (env.CI) {
throw Error(
'Unsupported CI system. Unable to obtain pull request information from the environment.'
)
} else {
throw Error(
'Unable to obtain pull request information from the environment. Is this running in CI?'
)
}
}
/**
* Pull Request
*
* @typedef PullRequest
* @property {string} pr.baseUrl (returned for travis CI only)
* @property {string} owner
* @property {string} repo
* @property {string} pullRequest PR/issue number
* @property {string} slug owner/repo/#pullRequest
*/
export { parseGithubPullRequestUrl, parseGithubRepoSlug, inferPullRequest }

View File

@@ -1,48 +0,0 @@
import { test, given, forCases } from 'sazerac'
import {
parseGithubPullRequestUrl,
inferPullRequest,
} from './infer-pull-request.js'
describe('Pull request inference', function () {
test(parseGithubPullRequestUrl, () => {
forCases([
given('https://github.com/badges/shields/pull/1234'),
given('https://github.com/badges/shields/pull/1234', {
verifyBaseUrl: 'https://github.com',
}),
]).expect({
baseUrl: 'https://github.com',
owner: 'badges',
repo: 'shields',
pullRequest: 1234,
slug: 'badges/shields#1234',
})
given('https://github.com/badges/shields/pull/1234', {
verifyBaseUrl: 'https://example.com',
}).expectError(
'Expected base URL to be https://example.com but got https://github.com'
)
})
test(inferPullRequest, () => {
const expected = {
owner: 'badges',
repo: 'shields',
pullRequest: 1234,
slug: 'badges/shields#1234',
}
given({
CIRCLECI: '1',
CI_PULL_REQUEST: 'https://github.com/badges/shields/pull/1234',
}).expect(Object.assign({ baseUrl: 'https://github.com' }, expected))
given({
TRAVIS: '1',
TRAVIS_REPO_SLUG: 'badges/shields',
TRAVIS_PULL_REQUEST: '1234',
}).expect(expected)
})
})

View File

@@ -1,5 +1,5 @@
// Infer the current PR from the Travis environment, and look for bracketed,
// space-separated service names in the pull request title.
// Derive a list of service tests to run based on
// space-separated service names in the PR title.
//
// Output the list of services.
//
@@ -8,54 +8,26 @@
// Output:
// travis
// sonar
//
// Example:
//
// TRAVIS=1 TRAVIS_REPO_SLUG=badges/shields TRAVIS_PULL_REQUEST=1108 npm run test:services:pr:prepare
import got from 'got'
import { inferPullRequest } from './infer-pull-request.js'
import servicesForTitle from './services-for-title.js'
async function getTitle(owner, repo, pullRequest) {
const {
body: { title },
} = await got(
`https://api.github.com/repos/${owner}/${repo}/pulls/${pullRequest}`,
{
headers: {
'User-Agent': 'badges/shields',
Authorization: `token ${process.env.GITHUB_TOKEN}`,
},
responseType: 'json',
}
)
return title
let title
try {
if (process.argv.length < 3) {
throw new Error()
}
title = process.argv[2]
} catch (e) {
console.error('Error processing arguments')
process.exit(1)
}
async function main() {
const { owner, repo, pullRequest, slug } = inferPullRequest()
console.error(`PR: ${slug}`)
const title = await getTitle(owner, repo, pullRequest)
console.error(`Title: ${title}\n`)
const services = servicesForTitle(title)
if (services.length === 0) {
console.error('No services found. Nothing to do.')
} else {
console.error(
`Services: (${services.length} found) ${services.join(', ')}\n`
)
console.log(services.join('\n'))
}
console.error(`Title: ${title}\n`)
const services = servicesForTitle(title)
if (services.length === 0) {
console.error('No services found. Nothing to do.')
} else {
console.error(`Services: (${services.length} found) ${services.join(', ')}\n`)
console.log(services.join('\n'))
}
;(async () => {
try {
await main()
} catch (e) {
console.error(e)
process.exit(1)
}
})()

View File

@@ -22,6 +22,10 @@ if (data.stats.passes > 0) {
if (data.stats.failures > 0) {
process.stdout.write(`${data.stats.failures} failed\n\n`)
}
if (data.stats.pending > 0) {
process.stdout.write(`${data.stats.pending} pending\n\n`)
process.exit(2)
}
if (data.stats.failures > 0) {
for (const test of data.tests) {

View File

@@ -1,6 +1,7 @@
import { ServiceTester } from '../../tester.js'
export const t = new ServiceTester({
id: 'GithubGistLastCommitRedirect',
id: 'GistLastCommitRedirect',
title: 'Github Gist Last Commit Redirect',
pathPrefix: '/github-gist',
})

View File

@@ -8,7 +8,7 @@ const schema = Joi.object({
updated_at: Joi.string().required(),
}).required()
export default class GithubGistLastCommit extends GithubAuthV3Service {
export default class GistLastCommit extends GithubAuthV3Service {
static category = 'activity'
static route = { base: 'github/gist/last-commit', pattern: ':gistId' }
static examples = [

View File

@@ -1,6 +1,6 @@
import { ServiceTester } from '../../tester.js'
export const t = new ServiceTester({
id: 'GithubGistStarsRedirect',
id: 'GistStarsRedirect',
title: 'Github Gist Stars Redirect',
pathPrefix: '/github',
})

View File

@@ -24,7 +24,7 @@ const documentation = `${commonDocumentation}
<p>This badge shows the number of stargazers for a gist. Gist id is accepted as input and 'gist not found' is returned if the gist is not found for the given gist id.
</p>`
export default class GithubGistStars extends GithubAuthV4Service {
export default class GistStars extends GithubAuthV4Service {
static category = 'social'
static route = {