Skip to content

Commit

Permalink
"Added js script to update go file when latest gets a push" (#120)
Browse files Browse the repository at this point in the history
Co-authored-by: FriendlyBandit <[email protected]>
  • Loading branch information
John-Memphis and FriendlyBandit authored Sep 7, 2023
1 parent ea519eb commit 092e93f
Show file tree
Hide file tree
Showing 4 changed files with 325 additions and 0 deletions.
198 changes: 198 additions & 0 deletions .github/scripts/quickstart_go/package-lock.json

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

17 changes: 17 additions & 0 deletions .github/scripts/quickstart_go/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "update_quickstart_go",
"version": "1.0.0",
"description": "Scrapes readme's hopefully",
"main": "update_quickstart_go.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"run": "node update_quickstart_go.js"
},
"author": "Memphis",
"license": "ISC",
"type": "module",
"dependencies": {
"@octokit/request": "^8.1.1",
"@octokit/rest": "^20.0.1"
}
}
85 changes: 85 additions & 0 deletions .github/scripts/quickstart_go/update_quickstart_go.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const git_token = process.env.DOCS_ACTION_TOKEN

import { Octokit } from "@octokit/rest";

const octokit = new Octokit({
auth: git_token
})

const repos = [
{
repo_name: 'memphis.go',
doc_path: 'docs/sdk/client-libraries/go/quick-start.md',
language_name: 'Go'
},
]

let did_error = false;
const failed_languages = [];
for (let repo of repos){
try {
await update_file(repo.repo_name, repo.doc_path, repo.language_name)
} catch (error) {
console.log();(error);
did_error = true;
failed_languages.push(repo.language_name)
}
}

if (did_error){
throw new Error(`Failed to update one or more languages. \n Failed Languages: ${failed_languages.join(', ')}`)
}

async function update_file(repo_name, doc_path, language_name){
console.log(`Updating ${language_name} Quickstart`);
console.log(`Repo: ${repo_name}`);

console.log(`Getting ${language_name} README`);
let req = await octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
owner: 'memphisdev',
repo: repo_name,
path: 'README.md',
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
})

console.log(`Getting ${language_name} Quickstart SHA`);
let quick_start = await octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
owner: 'memphisdev',
repo: 'documentation',
path: doc_path,
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
})

const quick_start_sha = quick_start.data.sha;

console.log(`Quickstart SHA: ${quick_start_sha}`);

const readme_content = atob(req.data.content)
const readme_h3_to_h2 = readme_content.replace(/###/g, '##')
const commit_string = `---
title: ${language_name} Quickstart
description: A quickstart on how to use the ${language_name} client library
---`+ '\n' + readme_h3_to_h2

console.log(`Updating ${language_name} Quickstart`);
await octokit.request('PUT /repos/{owner}/{repo}/contents/{path}', {
owner: 'memphisdev',
repo: 'documentation',
path: doc_path,
message: `Updating ${language_name} SDK Quick-Start`,
committer: {
name: 'Automated Workflow',
email: '[email protected]'
},
content: btoa(commit_string),
headers: {
'X-GitHub-Api-Version': '2022-11-28'
},
sha: quick_start_sha
})
}

25 changes: 25 additions & 0 deletions .github/workflows/auto_update_quick_start.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Auto-Update Go Quickstart Page
on:
push:
branches:
- latest
jobs:
update:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v3
with:
sparse-checkout: .github
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 'latest'
- name: Run quickstart script
env:
REPO_TOKEN: ${{ secrets.DOCS_ACTION_TOKEN }}
run: |
cd ./.github/scripts/quickstart_go
ls
npm ci
node update_quickstart_go.js

0 comments on commit 092e93f

Please sign in to comment.