Skip to content

Commit

Permalink
feat: Add scheduled workflow to pull in o11y packs
Browse files Browse the repository at this point in the history
  • Loading branch information
aswanson-nr committed Jun 16, 2021
1 parent 4032c08 commit f008df1
Show file tree
Hide file tree
Showing 5 changed files with 770 additions and 532 deletions.
100 changes: 100 additions & 0 deletions .github/workflows/fetch-observability-packs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
name: Fetch Observability Packs

on:
workflow_dispatch:
schedule:
- cron: '0 0 * * *' # midnight

env:
BOT_NAME: nr-opensource-bot
BOT_EMAIL: [email protected]
NODE_OPTIONS: '--max-old-space-size=4096'

jobs:
fetch-observability-packs:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v2
with:
ref: main

- name: Setup node.js
uses: actions/setup-node@v1
with:
node-version: 12

- name: Cache dependencies
id: yarn-cache
uses: actions/cache@v2
with:
path: '**/node_modules'
key: ${{ runner.os }}-node-modules-${{ hashFiles('**/yarn.lock') }}

- name: Install dependencies
if: steps.yarn-cache.outputs.cache-hit != 'true'
run: yarn install --frozen-lockfile

- name: Fetch observability packs
run: yarn run fetch-observability-packs
env:
NR_GQL_URL: 'https://api.newrelic.com/graphql'
NR_API_TOKEN: ${{ secrets.NR_API_TOKEN }}

- name: Temporarily disable branch protection
id: disable-branch-protection
uses: actions/github-script@v1
with:
github-token: ${{ secrets.OPENSOURCE_BOT_TOKEN }}
previews: luke-cage-preview
script: |
const result = await github.repos.updateBranchProtection({
owner: context.repo.owner,
repo: context.repo.repo,
branch: 'main',
required_status_checks: null,
restrictions: null,
enforce_admins: null,
required_pull_request_reviews: null
})
console.log("Result:", result)
- name: Commit changes
id: commit-changes
run: |
git config --local user.email "${{ env.BOT_EMAIL }}"
git config --local user.name "${{ env.BOT_NAME }}"
git add ./src/data/observability-packs.json
git diff-index --quiet HEAD ./src/data/observability-packs.json || git commit -m 'chore(observability-packs): updated observability packs'
echo "::set-output name=commit::true"
- name: Push Commit
if: steps.commit-changes.outputs.commit == 'true'
uses: ad-m/[email protected]
with:
github_token: ${{ secrets.OPENSOURCE_BOT_TOKEN }}
branch: main

- name: Re-enable branch protection
id: enable-branch-protection
if: always()
uses: actions/github-script@v1
with:
github-token: ${{ secrets.OPENSOURCE_BOT_TOKEN }}
previews: luke-cage-preview
script: |
const result = await github.repos.updateBranchProtection({
owner: context.repo.owner,
repo: context.repo.repo,
branch: 'main',
required_status_checks: {
strict: false,
contexts: [
'Gatsby Build'
]
},
restrictions: null,
enforce_admins: true,
required_pull_request_reviews: null
})
console.log("Result:", result)
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,9 @@ Please submit any issues or enhancement requests using one of our
[Issue Templates](https://github.com/newrelic/developer-website/issues/new/choose).
Please search for and review the existing open issues before submitting a new
issue to prevent the submission of duplicate issues.

## CI/CD
### fetch-observability-packs
* Purpose: This workflow pulls down Observability Packs from the GraphQL API, writes them to src/data/observability-packs.json (overwriting any previous content), and commits that file to the `main` branch.
* Trigger: Schedule, 12am everyday

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"build:production": "GATSBY_NEWRELIC_ENV=production gatsby build",
"build:staging": "GATSBY_NEWRELIC_ENV=staging gatsby build",
"build:related-content": "BUILD_RELATED_CONTENT=true yarn run build:production",
"fetch-observability-packs": "node ./scripts/actions/fetch-observability-packs.js",
"develop": "gatsby develop",
"format": "prettier --write \"**/*.{js,jsx,json,md}\"",
"start": "yarn run develop",
Expand Down
104 changes: 104 additions & 0 deletions scripts/actions/fetch-observability-packs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
'use strict';

/**
* This script is used to query the New Relic GraphQL API for Observability Packs.
* It then writes the array of Observability Packs to src/data/observability-packs.json
* It requires the following environment variables to be set:
* NR_GQL_URL - The New Relic GraphQL URL
* NR_API_TOKEN - A New Relic personal API token
**/

/* eslint-disable no-console */
const fs = require('fs');
const fetch = require('node-fetch');

const PACKS_FILE_PATH = './src/data/observability-packs.json';
const NR_GQL_URL = process.env.NR_GQL_URL;
const NR_API_TOKEN = process.env.NR_API_TOKEN;

const packQuery = `# gql
{
docs {
openInstallation {
observabilityPackSearch {
count
results {
observabilityPacks {
authors
dashboards {
description
name
screenshots
url
}
description
iconUrl
id
level
logoUrl
name
websiteUrl
}
}
}
}
}
}
`;

/**
* Queries graphql for the provided query
* @param {String} queryString the graphql query to send
* @param {String} url NR graphql endpoint
* @param {String} token NR api token
* @returns {Promise<Object[]|undefined>} returns the resulting array
* or `undefined` if there was an error
**/
const fetchPacks = async (queryString, url, token) => {
try {
const results = await fetch(url, {
method: 'post',
body: JSON.stringify({ query: queryString }),
headers: {
'Content-Type': 'application/json',
'Api-Key': token,
},
}).then((res) => res.json());

if (results.errors) {
console.log('GRAPHQL Errors:', JSON.stringify(results.errors, null, 2));
}

return results.data.docs.openInstallation.observabilityPackSearch.results;
} catch (error) {
console.error('Encountered a problem querying the graphql api', error);
}
};

/**
* Writes a JSON file
* @param {String} path the file path to write to
* @param {Object|Object[]} packs the contents to write
**/
const writePacks = (path, packs) => {
console.log(`Writing ${path}`);
fs.writeFileSync(path, JSON.stringify(packs, null, 2));
};

const main = async () => {
const results = await fetchPacks(packQuery, NR_GQL_URL, NR_API_TOKEN);

if (results) {
const packs = results.observabilityPacks;
console.log(`Found ${packs.length} packs.`);
writePacks(PACKS_FILE_PATH, packs);
} else {
console.log(
'No packs were returned from the api, check the logs for errors.'
);
}
};

main();

/* eslint-enable no-console */
Loading

0 comments on commit f008df1

Please sign in to comment.