Skip to content

Commit

Permalink
Merge pull request #158 from CanDIG/sonchau/uuid_with_fk
Browse files Browse the repository at this point in the history
DIG-1355: Sonchau/UUID with fk
  • Loading branch information
daisieh authored Dec 4, 2023
2 parents 0f82bdc + 9ab3f17 commit 394f2b7
Show file tree
Hide file tree
Showing 60 changed files with 45,602 additions and 22,841 deletions.
42 changes: 42 additions & 0 deletions .github/commitFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
async function getFileSha(fileName, github_ref, octokit, context) {
const path = "chord_metadata_service/mohpackets/docs/" + fileName;
const { data } = await octokit.repos.getContent({
owner: context.repo.owner,
repo: context.repo.repo,
path,
ref: github_ref,
});
return data.sha;
}

async function commitAndPushChanges(
fileName,
github_ref,
fs,
octokit,
context
) {
const repoPath = "chord_metadata_service/mohpackets/docs/";
const fileSha = await getFileSha(fileName, github_ref, octokit, context);

// Read the content of the updated file
const fileContent = fs.readFileSync(`./${repoPath}${fileName}`, "utf8");

// Commit and push changes
await octokit.request(
`PUT /repos/{owner}/{repo}/contents/${repoPath}{fileName}`,
{
owner: context.repo.owner,
repo: context.repo.repo,
fileName,
message: `Update ${fileName}`,
content: Buffer.from(fileContent).toString("base64"),
sha: fileSha,
branch: github_ref,
}
);
}

module.exports = {
commitAndPushChanges,
};
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
name: Set up Python
with:
python-version: '3.10'
python-version: '3.12'
- name: Install flake8
run: python -m pip install flake8
- name: Run linter
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ jobs:
# If you want to test multiple python version(s)
strategy:
matrix:
python-version: ['3.9', '3.10', '3.11']
python-version: ['3.10', '3.11', '3.12']
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
submodules: true
- name: Set up Python ${{ matrix.python-version }}
Expand Down
120 changes: 45 additions & 75 deletions .github/workflows/update-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,44 @@ name: Docs
on:
pull_request:
types: [review_requested, ready_for_review]


jobs:
generate-moh-schema:
name: Update MoH schema
runs-on: ubuntu-latest
steps:
- name: Check out repo
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
submodules: true

- name: Install Python
uses: actions/setup-python@v4
with:
python-version: "3.10"
python-version: "3.12"

- name: Install dependencies
run: python -m pip install -r requirements/dev.txt
run: python -m pip install -r requirements/base.txt

- name: Generate new schema.json
run: |
export DJANGO_SETTINGS_MODULE=config.settings.base
python manage.py export_openapi_schema --api chord_metadata_service.mohpackets.apis.core.api | python -m json.tool > chord_metadata_service/mohpackets/docs/schema.json
- name: Update schema.json with new SHA
run: |
REPO_OWNER=${{ github.repository_owner }}
REPO_NAME=${{ github.repository }}
HEAD_REF=${{ github.head_ref }}
SHA=$(git rev-parse HEAD)
SCHEMA_PATH=chord_metadata_service/mohpackets/docs/schema.json
sed -i 's|"description": "This is the RESTful API for the MoH Service."|"description": "This is the RESTful API for the MoH Service. Based on https://raw.githubusercontent.com/'"$REPO_NAME"'/'"$SHA"'/'"$SCHEMA_PATH"'"|' $SCHEMA_PATH
- name: Set up Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 18

Expand All @@ -34,88 +51,41 @@ jobs:
- name: Install npm
run: npm install

- name: Install widdershins
run: npm install -g widdershins

- name: Generate new schema.yml
run: python manage.py spectacular --file ./chord_metadata_service/mohpackets/docs/schema.yml --validate --fail-on-warn

- name: Convert schema to OpenAPI documentation
run: |
npx widdershins ./chord_metadata_service/mohpackets/docs/schema.yml -o ./chord_metadata_service/mohpackets/docs/openapi.md -u ./chord_metadata_service/mohpackets/docs/widdershins/templates/openapi3 -c true --omitHeader true
- name: Install octokit/rest
run: npm install @octokit/rest

- name: Update schema.yml and openapi.md
- name: Commit and push changes to schema.json
uses: actions/github-script@v6
with:
script: |
const fs = require('fs');
const { Octokit } = require('@octokit/rest');
const octokit = new Octokit({ request: { fetch: fetch, }, auth: "${{ secrets.GITHUB_TOKEN }}" });
const { commitAndPushChanges } = require("./.github/commitFile.js");
await commitAndPushChanges('schema.json', `${{ github.head_ref }}`, fs, octokit, context);
// get SHA from each file
const repoPath = 'chord_metadata_service/mohpackets/docs/';
const getFileSha = async (path) => {
const { data } = await octokit.repos.getContent({
owner: context.repo.owner,
repo: context.repo.repo,
path: `${repoPath}${path}`,
ref: `${{ github.head_ref }}`
});
return data.sha;
}
// get last commit sha for repo
const getRepoSha = async () => {
const { data } = await octokit.rest.repos.getCommit({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `${{ github.head_ref }}`
});
return data.sha;
}
const schemaSha = await getFileSha('schema.yml');
const openApiSha = await getFileSha('openapi.md');
const repoSha = await getRepoSha();
// Read the content of updated files
let schemaYml = fs.readFileSync(`./${repoPath}schema.yml`, 'utf8');
const openApiMd = fs.readFileSync(`./${repoPath}openapi.md`, 'utf8');
// Update description to include sha of
let schemaLines = schemaYml.split('\n');
for (let i = 0; i < schemaLines.length; ++i) {
if ("description: This is the RESTful API for the MoH Service." === schemaLines[i].trim()) {
schemaLines[i] += ' Based on https://raw.githubusercontent.com/' + context.repo.owner + '/' + context.repo.repo + '/' + repoSha + '/' + repoPath + 'schema.yml';
}
}
schemaYml = schemaLines.join('\n');
fs.writeFileSync(`./${repoPath}schema.yml`, schemaYml, 'utf8');
// Commit and push changes
await octokit.request(`PUT /repos/{owner}/{repo}/contents/${repoPath}{path}`, {
owner: context.repo.owner,
repo: context.repo.repo,
path: 'schema.yml',
message: 'Update schema.yml',
content: Buffer.from(schemaYml).toString('base64'),
sha: schemaSha,
branch: `${{ github.head_ref }}`
});
await octokit.request(`PUT /repos/{owner}/{repo}/contents/${repoPath}{path}`, {
owner: context.repo.owner,
repo: context.repo.repo,
path: 'openapi.md',
message: 'Update openapi.md',
content: Buffer.from(openApiMd).toString('base64'),
sha: openApiSha,
branch: `${{ github.head_ref }}`
});
- name: Install widdershins
run: npm install -g widdershins

- name: Convert schema to OpenAPI documentation
run: |
npx widdershins ./chord_metadata_service/mohpackets/docs/schema.json -o ./chord_metadata_service/mohpackets/docs/schema.md -u ./chord_metadata_service/mohpackets/docs/widdershins/templates/openapi3 -c true --omitHeader true
- name: Install PyYAML
run: pip install PyYAML

- name: Convert schema.json to schema.yml
run: python -c 'import json, yaml; json.load(open("chord_metadata_service/mohpackets/docs/schema.json")); print(yaml.dump(json.load(open("chord_metadata_service/mohpackets/docs/schema.json"))))' > chord_metadata_service/mohpackets/docs/schema.yml

- name: Commit and push changes to schema.yml and schema.md
uses: actions/github-script@v6
with:
script: |
const fs = require('fs');
const { Octokit } = require('@octokit/rest');
const octokit = new Octokit({ request: { fetch: fetch, }, auth: "${{ secrets.GITHUB_TOKEN }}" });
const { commitAndPushChanges } = require("./.github/commitFile.js");
await commitAndPushChanges('schema.yml', `${{ github.head_ref }}`, fs, octokit, context);
await commitAndPushChanges('schema.md', `${{ github.head_ref }}`, fs, octokit, context);
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ py -3 -m venv .venv
With your virtual environment activated, navigate to the project directory and install the project dependencies:

```bash
pip install -r requirements/dev.txt
pip install -r requirements/local.txt
```

This will install all the packages needed for development.
Expand Down
Loading

0 comments on commit 394f2b7

Please sign in to comment.