Skip to content

Commit

Permalink
docs: automated announcement bar (medusajs#2231)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahednasser authored Sep 20, 2022
1 parent 8eac3ba commit 31fb4c3
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 12 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/docs-new-announcement.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Generate Docs Release Announcement Bar
on:
release:
types: [published]

jobs:
add-announcement:
runs-on: ubuntu-latest
env:
GH_TOKEN: ${{ secrets.REFERENCE_PAT }}
steps:
- name: Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.9.1
with:
access_token: ${{ github.token }}

- name: Checkout
uses: actions/checkout@v2.3.5
with:
fetch-depth: 0

- name: Setup Node.js environment
uses: actions/setup-node@v2.4.1
with:
node-version: "14"
cache: "yarn"

- name: Install dependencies
uses: ./.github/actions/cache-deps
with:
extension: docs-release

- name: Build Packages
run: yarn build

- name: Generate Announcement Bar
run: yarn generate:announcement

- name: Create Pull Request
uses: peter-evans/create-pull-request@v4
with:
commit-message: 'chore(docs): Generated Docs Announcement Bar (automated)'
base: 'master'
title: 'chore(docs): Generated Docs Announcement Bar (automated)'
labels: 'type: chore'
add-paths: www/docs/**
branch: 'chore/generate-announcement'
branch-suffix: 'timestamp'
49 changes: 49 additions & 0 deletions .github/workflows/docs-remove-announcement.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Checks if the announcement bar info was last edited more than 6 days ago, and if so removes it
name: Remove Docs Release Announcement Bar
on:
schedule:
- cron: '0 0 * * *'

jobs:
remove-announcement:
runs-on: ubuntu-latest
env:
GH_TOKEN: ${{ secrets.REFERENCE_PAT }}
steps:
- name: Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.9.1
with:
access_token: ${{ github.token }}

- name: Checkout
uses: actions/checkout@v2.3.5
with:
fetch-depth: 0

- name: Setup Node.js environment
uses: actions/setup-node@v2.4.1
with:
node-version: "14"
cache: "yarn"

- name: Install dependencies
uses: ./.github/actions/cache-deps
with:
extension: docs-remove-announcement

- name: Build Packages
run: yarn build

- name: Remove Announcement Bar
run: yarn generate:announcement --expire

- name: Create Pull Request
uses: peter-evans/create-pull-request@v4
with:
commit-message: 'chore(docs): Removed Docs Announcement Bar (automated)'
base: 'master'
title: 'chore(docs): Removed Docs Announcement Bar (automated)'
labels: 'type: chore'
add-paths: www/docs/**
branch: 'chore/remove-announcement'
branch-suffix: 'timestamp'
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@
"generate:services": "typedoc --options typedoc.services.js",
"generate:js-client": "typedoc --options typedoc.js-client.js",
"generate:entities": "typedoc --options typedoc.entities.js",
"release:snapshot": "changeset publish --no-git-tags --snapshot --tag snapshot"
"release:snapshot": "changeset publish --no-git-tags --snapshot --tag snapshot",
"generate:announcement": "node ./scripts/doc-change-release.js"
},
"dependencies": {
"@changesets/changelog-github": "^0.4.5",
"@changesets/cli": "^2.23.0",
"@octokit/core": "^4.0.5",
"global": "^4.4.0",
"import-from": "^3.0.0",
"oas-normalize": "^5.0.1",
Expand Down
61 changes: 61 additions & 0 deletions scripts/doc-change-release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env node

const { Octokit } = require("@octokit/core");
const fs = require('fs');
const path = require('path');

const shouldExpire = process.argv.indexOf('--expire') !== -1;
const octokit = new Octokit({
auth: process.env.GH_TOKEN
});

async function main () {
let announcement = {};

if (shouldExpire) {
//check if the file was last updated 6 days ago
try {
const fileStat = fs.statSync(path.join(__dirname, '..', 'www', 'docs', 'announcement.json'));
if (dateDiffInDays(fileStat.mtime, new Date()) < 6) {
console.log("File was edited less than 6 days ago. Expiry canceled.");
return;
}
} catch (e) {
//file doesn't exist, continue
}
} else {
//retrieve the latest release
const response = await octokit.request('GET /repos/{owner}/{repo}/releases/latest', {
owner: 'medusajs',
repo: 'medusa'
});

const version = response.data.tag_name;

//add new announcement
announcement = {
id: `release-${version.replace(/\./g, '-')}`,
content: `New Release! Version ${version} of Medusa is out now! Read all about it <a href='${response.data.html_url}'>here</a>.`,
backgroundColor: '#7C53FF',
textColor: '#fff',
isCloseable: false,
}
}

//write new config file
fs.writeFileSync(path.join(__dirname, '..', 'www', 'docs', 'announcement.json'), JSON.stringify(announcement));
console.log(`Announcement Bar has been ${shouldExpire ? 'removed' : 'added'}`);
}

const _MS_PER_DAY = 1000 * 60 * 60 * 24;

// a and b are javascript Date objects
function dateDiffInDays(a, b) {
// Discard the time and time-zone information.
const utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
const utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());

return Math.floor((utc2 - utc1) / _MS_PER_DAY);
}

main();
1 change: 1 addition & 0 deletions www/docs/announcement.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"release-v1-4-1","content":"New Release! Version v1.4.1 of Medusa is out now! Read all about it <a href='https://github.com/medusajs/medusa/releases/tag/v1.4.1'>here</a>.","backgroundColor":"#7C53FF","textColor":"#fff","isCloseable":false}
21 changes: 11 additions & 10 deletions www/docs/docusaurus.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
const path = require("path")
const fs = require("fs")
const docsPath = path.join(__dirname, "../../docs/content")
const apisPath = path.join(__dirname, "../../docs/api")

const algoliaAppId = process.env.ALGOLIA_APP_ID || "temp"
const algoliaApiKey = process.env.ALGOLIA_API_KEY || "temp"

const announcementBar = JSON.parse(fs.readFileSync('./announcement.json'))

/** @type {import('@docusaurus/types').DocusaurusConfig} */
module.exports = {
const config = {
title: "Medusa",
tagline: "Explore and learn how to use Medusa",
url: "https://docs.medusajs.com",
Expand All @@ -32,14 +35,6 @@ module.exports = {
]
],
themeConfig: {
announcementBar: {
id: 'release-1-4-1',
content:
'New Release! Version 1.4.1 of Medusa is out now! Read all about it <a href="https://github.com/medusajs/medusa/releases/tag/v1.4.1">here</a>.',
backgroundColor: '#7C53FF',
textColor: '#fff',
isCloseable: false,
},
colorMode: {
defaultMode: 'light',
disableSwitch: false,
Expand Down Expand Up @@ -226,4 +221,10 @@ module.exports = {
},
],
],
}
}

if (Object.keys(announcementBar).length) {
config.themeConfig.announcementBar = announcementBar
}

module.exports = config
111 changes: 110 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4720,6 +4720,93 @@ __metadata:
languageName: node
linkType: hard

"@octokit/auth-token@npm:^3.0.0":
version: 3.0.1
resolution: "@octokit/auth-token@npm:3.0.1"
dependencies:
"@octokit/types": ^7.0.0
checksum: f52087d6680dd151ac5db49d330a544c07340680a6cc39a8a32ee366d34e57c00b7f0396f32644af2614afe158ee6a692a6f0a00cc949ea03828ea7e2532fefd
languageName: node
linkType: hard

"@octokit/core@npm:^4.0.5":
version: 4.0.5
resolution: "@octokit/core@npm:4.0.5"
dependencies:
"@octokit/auth-token": ^3.0.0
"@octokit/graphql": ^5.0.0
"@octokit/request": ^6.0.0
"@octokit/request-error": ^3.0.0
"@octokit/types": ^7.0.0
before-after-hook: ^2.2.0
universal-user-agent: ^6.0.0
checksum: 9de824e033a1f8f80156168322bba9933434d3435afed2b0eee315e86f44728f7b86e33064eff7823e6c6f1dc4ec836baf38cd62cf08fbcdc90b018d5ce3b438
languageName: node
linkType: hard

"@octokit/endpoint@npm:^7.0.0":
version: 7.0.2
resolution: "@octokit/endpoint@npm:7.0.2"
dependencies:
"@octokit/types": ^7.0.0
is-plain-object: ^5.0.0
universal-user-agent: ^6.0.0
checksum: 0a74012756159f3269d55f331c0c0e3b1e79b6d8c4a3cd3c1216c5b3fd0efd0ee183f65407160103e8507ab8c9a3ad58ace050b5bea76e9a9eb8900f7c118637
languageName: node
linkType: hard

"@octokit/graphql@npm:^5.0.0":
version: 5.0.1
resolution: "@octokit/graphql@npm:5.0.1"
dependencies:
"@octokit/request": ^6.0.0
"@octokit/types": ^7.0.0
universal-user-agent: ^6.0.0
checksum: 096ca4d78790b5e43422b5076b721b1b6d8b7b55fc5a33c5edca66a613ba043a072cf20a739ef2f76380fecaf1f9d2bf26af290aff2e158a354a4b2aea5b38e2
languageName: node
linkType: hard

"@octokit/openapi-types@npm:^13.11.0":
version: 13.12.0
resolution: "@octokit/openapi-types@npm:13.12.0"
checksum: 30ffc7ad56197b52b2898bb438cfe25f77d7c41d7f2f1dab6eff1015861966947fb557a7b54305b3381f028e49610e844c2951d2a142c5321d1c45aa70047678
languageName: node
linkType: hard

"@octokit/request-error@npm:^3.0.0":
version: 3.0.1
resolution: "@octokit/request-error@npm:3.0.1"
dependencies:
"@octokit/types": ^7.0.0
deprecation: ^2.0.0
once: ^1.4.0
checksum: 73389dcc36dc0e5fcf58c6e2763a907d0b304953393884623bf2e37705b4cafeb142f9b6d2f5d394617b49568e93ac0cf1b40491695fe1b18e10a8785c609fb9
languageName: node
linkType: hard

"@octokit/request@npm:^6.0.0":
version: 6.2.1
resolution: "@octokit/request@npm:6.2.1"
dependencies:
"@octokit/endpoint": ^7.0.0
"@octokit/request-error": ^3.0.0
"@octokit/types": ^7.0.0
is-plain-object: ^5.0.0
node-fetch: ^2.6.7
universal-user-agent: ^6.0.0
checksum: 61329ea64f032240a1ee6f77d94840f0aa1c24c2467acd747cad1ca78a49c4526116a09641f696f4e47cb5a82ffcd000555fcf6127f5b07d2f871285b9f5ee04
languageName: node
linkType: hard

"@octokit/types@npm:^7.0.0":
version: 7.5.0
resolution: "@octokit/types@npm:7.5.0"
dependencies:
"@octokit/openapi-types": ^13.11.0
checksum: 65501fd2dc106497d403be9d26763fb7a89f385bc8cba61a2ac842d273f1b2fb0b1db8b0081d2d42d37fe354f2e6b159b4fec694a51b7bf4c6cc1cb098d6ad57
languageName: node
linkType: hard

"@open-draft/until@npm:^1.0.3":
version: 1.0.3
resolution: "@open-draft/until@npm:1.0.3"
Expand Down Expand Up @@ -10870,6 +10957,13 @@ __metadata:
languageName: node
linkType: hard

"before-after-hook@npm:^2.2.0":
version: 2.2.2
resolution: "before-after-hook@npm:2.2.2"
checksum: 7457bfb8f40e8cbce943ea6e6531261925c6c8a451fea540762367a3e2e52b5979978963a7ec65f232a4f5b87310930bf152c9a055608c64ecee5115bad60b9a
languageName: node
linkType: hard

"better-opn@npm:^2.1.1":
version: 2.1.1
resolution: "better-opn@npm:2.1.1"
Expand Down Expand Up @@ -13922,6 +14016,13 @@ __metadata:
languageName: node
linkType: hard

"deprecation@npm:^2.0.0":
version: 2.3.1
resolution: "deprecation@npm:2.3.1"
checksum: 23d688ba66b74d09b908c40a76179418acbeeb0bfdf218c8075c58ad8d0c315130cb91aa3dffb623aa3a411a3569ce56c6460de6c8d69071c17fe6dd2442f032
languageName: node
linkType: hard

"des.js@npm:^1.0.0":
version: 1.0.1
resolution: "des.js@npm:1.0.1"
Expand Down Expand Up @@ -19793,7 +19894,7 @@ __metadata:
languageName: node
linkType: hard

"is-plain-object@npm:5.0.0":
"is-plain-object@npm:5.0.0, is-plain-object@npm:^5.0.0":
version: 5.0.0
resolution: "is-plain-object@npm:5.0.0"
checksum: 893e42bad832aae3511c71fd61c0bf61aa3a6d853061c62a307261842727d0d25f761ce9379f7ba7226d6179db2a3157efa918e7fe26360f3bf0842d9f28942c
Expand Down Expand Up @@ -29949,6 +30050,7 @@ __metadata:
"@babel/runtime": ^7.11.2
"@changesets/changelog-github": ^0.4.5
"@changesets/cli": ^2.23.0
"@octokit/core": ^4.0.5
"@redocly/cli": latest
"@typescript-eslint/eslint-plugin": ^5.36.2
"@typescript-eslint/parser": ^5.36.2
Expand Down Expand Up @@ -33692,6 +33794,13 @@ __metadata:
languageName: node
linkType: hard

"universal-user-agent@npm:^6.0.0":
version: 6.0.0
resolution: "universal-user-agent@npm:6.0.0"
checksum: ebeb0206963666c13bcf9ebc86d0577c7daed5870c05cd34d4972ee7a43b9ef20679baf2a8c83bf1b71d899bae67243ac4982d84ddaf9ba0355ff76595819961
languageName: node
linkType: hard

"universalify@npm:^0.1.0, universalify@npm:^0.1.2":
version: 0.1.2
resolution: "universalify@npm:0.1.2"
Expand Down

0 comments on commit 31fb4c3

Please sign in to comment.