-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add scheduled workflow to pull in o11y packs
- Loading branch information
1 parent
4032c08
commit f008df1
Showing
5 changed files
with
770 additions
and
532 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
Oops, something went wrong.