moving files to correct folder #2910
Workflow file for this run
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
name: Preview modified pages | |
on: [pull_request] | |
jobs: | |
preview: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
- name: Get branch name | |
id: get-branch-name | |
uses: tj-actions/branch-names@v7 | |
- name: Get changed markdown files | |
id: get-changed-markdown-files | |
uses: tj-actions/changed-files@v36 | |
with: | |
files: | | |
**.md | |
**.mdx | |
- name: Fetch navigation.json | |
run: curl -o ./navigation.json https://developers.vtex.com/navigation.json | |
- name: Map slugs to paths | |
id: map-slugs-to-paths | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const navigation = require('./navigation.json') | |
const paths = {} | |
const slugsToPaths = (slugPrefix, obj) => { | |
if (!obj || typeof obj !== 'object') return | |
if (obj.slug && obj.type === 'markdown') { | |
paths[obj.slug] = `${slugPrefix}${obj.slug}` | |
} | |
for (const key in obj) { | |
slugsToPaths(slugPrefix, obj[key]) | |
} | |
} | |
navigation.navbar.forEach(({ slugPrefix, categories }) => slugsToPaths(slugPrefix.endsWith('/') ? slugPrefix : `${slugPrefix}/`, categories)) | |
core.setOutput('slugs-to-paths', JSON.stringify(paths)) | |
- name: Get preview links | |
id: get-preview-links | |
uses: actions/github-script@v6 | |
env: | |
BRANCH: ${{ steps.get-branch-name.outputs.head_ref_branch }} | |
FILES: ${{ steps.get-changed-markdown-files.outputs.all_changed_files }} | |
PATHS: ${{ steps.map-slugs-to-paths.outputs.slugs-to-paths }} | |
with: | |
script: | | |
const branch = process.env.BRANCH | |
const paths = JSON.parse(process.env.PATHS) | |
const files = process.env.FILES.split(' ') | |
.map(file => file.trim()) | |
.filter(file => file !== '') | |
let output = '# Preview Links\n\n' | |
if (files.length === 0) { | |
output += 'No changes detected in any markdown/MDX file' | |
} else { | |
output += `Open [this URL](https://developers.vtex.com/api/preview?branch=${branch}) to set up the portal with this branch changes.\n\n` | |
output += 'You can now access the edited pages with the following URLs:\n' | |
for (const file of files) { | |
const parts = file.split('/'); | |
const category = parts[1]; | |
const slug = file.split('/').pop().split('.')[0]; | |
console.log(`Processing file: ${file}, category: ${category}, slug: ${slug}, path: ${paths[slug] || 'not found'}`) | |
if (!paths[slug]) { | |
output += `* Couldn't find the corresponding page for \`${file}\`. Have you added \`${slug}\` to the navigation.json file? `; | |
if (category === "troubleshooting") { | |
url = `https://developers.vtex.com/docs/troubleshooting/${slug}`; | |
} else if (category === "release-notes") { | |
url = `https://developers.vtex.com/updates/release-notes/${slug}`; | |
} else if (category === "faststore") { | |
const lastFolder = parts[parts.length - 2]; | |
url = `https://developers.vtex.com/docs/guides/faststore/${lastFolder}-${slug}`; | |
} else { | |
url = `https://developers.vtex.com/docs/guides/${slug}`; | |
} | |
console.log(url) | |
output += `Maybe you can find it in ${url}\n`; | |
} else { | |
output += `* https://developers.vtex.com/${paths[slug]}\n`; | |
} | |
} | |
} | |
core.setOutput('preview-links', output) | |
- name: Comment PR | |
uses: thollander/actions-comment-pull-request@v2 | |
with: | |
comment_tag: preview-links | |
message: ${{ steps.get-preview-links.outputs.preview-links }} | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |