From e0878e8e21fe641b9484a167084f661e31ca7ab3 Mon Sep 17 00:00:00 2001 From: NicolasMerget Date: Fri, 22 Jul 2022 12:49:33 +0200 Subject: [PATCH 1/4] fix: issue with cicd cleanup and deploy-gh-pages --- .github/workflows/03-deploy-gh-pages.yml | 4 +- .github/workflows/cleanup.yml | 6 +- .../github-version-switcher.tsx | 57 ++++++++++++++----- 3 files changed, 49 insertions(+), 18 deletions(-) diff --git a/.github/workflows/03-deploy-gh-pages.yml b/.github/workflows/03-deploy-gh-pages.yml index 2f9dc6f46..94aac46fe 100644 --- a/.github/workflows/03-deploy-gh-pages.yml +++ b/.github/workflows/03-deploy-gh-pages.yml @@ -36,9 +36,9 @@ jobs: export BRANCH=${GITHUB_REF#refs/heads/} if echo ${BRANCH} | grep -c "main" then - export URL=https://dbsystel.github.io/cicd-playground + export URL=https://dbui.github.io/elements else - export URL=https://dbsystel.github.io/cicd-playground/review/${BRANCH} + export URL=https://dbui.github.io/elements/review/${BRANCH} fi echo "URL: $URL" echo "### GH-Pages URL :rocket:

$URL" >> $GITHUB_STEP_SUMMARY diff --git a/.github/workflows/cleanup.yml b/.github/workflows/cleanup.yml index 07c19251e..f6c7268f0 100644 --- a/.github/workflows/cleanup.yml +++ b/.github/workflows/cleanup.yml @@ -14,8 +14,10 @@ jobs: - name: ⬇ Checkout repo uses: actions/checkout@v3 - - name: 🔄 Init Cache - uses: ./.github/actions/npm-cache + - name: 📥 Download deps default-npm-cache + uses: bahmutov/npm-install@v1 + with: + install-command: npm ci --ignore-scripts - name: 📥 Get gh-pages tar run: wget -q https://github.com/db-ui/elements/tarball/gh-pages diff --git a/packages/db-ui-elements-stencil/src/components/development/github-version-switcher/github-version-switcher.tsx b/packages/db-ui-elements-stencil/src/components/development/github-version-switcher/github-version-switcher.tsx index 3dcaebbd3..e4da2bb7a 100644 --- a/packages/db-ui-elements-stencil/src/components/development/github-version-switcher/github-version-switcher.tsx +++ b/packages/db-ui-elements-stencil/src/components/development/github-version-switcher/github-version-switcher.tsx @@ -21,7 +21,7 @@ export class GithubVersionSwitcher { */ @Prop({ reflect: false }) defaultBranch: string = 'main'; - @State() branches = []; + @State() groups = []; @State() currentBranch = this.defaultBranch; @State() cleanOwner; @State() cleanRepo; @@ -39,11 +39,34 @@ export class GithubVersionSwitcher { .then((response) => response.json()) .then((data) => { if (data) { - this.branches = data + const branchNames = data .map((branch) => branch.name) - .filter((branch) => branch !== 'gh-pages'); + .filter( + (branch) => + branch !== 'gh-pages' && !branch.includes('dependabot') + ); + const tmpGroups = [ + { name: 'Versions', branches: [] }, + { name: 'Features', branches: [] }, + { name: 'Bugfixes', branches: [] } + ]; + branchNames.forEach((branch) => { + if (branch.startsWith('feat') || branch.startsWith('feature')) { + tmpGroups[1].branches.push(branch); + } else if ( + branch.startsWith('fix') || + branch.startsWith('bugfix') + ) { + tmpGroups[2].branches.push(branch); + } else { + tmpGroups[0].branches.push(branch); + } + }); + // eslint-disable-next-line no-console + console.log(tmpGroups); + this.groups = tmpGroups; const currentUrl = window.location.href; - const foundBranch = this.branches.find((branch) => + const foundBranch = branchNames.find((branch) => currentUrl.includes(branch) ); if (foundBranch) { @@ -68,7 +91,7 @@ export class GithubVersionSwitcher { render() { return ( - {this.branches?.length > 0 && ( + {this.groups?.length > 0 && ( - {this.branches.map((branch: string, index: number) => ( - - ))} + {this.groups + .filter((group: any) => group.branches?.length > 0) + .map((group: any) => ( + + {group.branches.map((branch: string, index: number) => ( + + ))} + + ))} )} From 6ae446da4064cf5823fef052bf9f6aeda31a104e Mon Sep 17 00:00:00 2001 From: NicolasMerget Date: Fri, 22 Jul 2022 15:39:54 +0200 Subject: [PATCH 2/4] feat: add input for gh-pages action to define publish to gh-pages --- .github/actions/bundle-gh-page/action.yml | 19 ++- .../github-version-switcher.tsx | 123 +++++++++++------- .../github-version-switcher/readme.md | 9 +- 3 files changed, 95 insertions(+), 56 deletions(-) diff --git a/.github/actions/bundle-gh-page/action.yml b/.github/actions/bundle-gh-page/action.yml index d6b560259..535dba22e 100644 --- a/.github/actions/bundle-gh-page/action.yml +++ b/.github/actions/bundle-gh-page/action.yml @@ -1,5 +1,13 @@ name: 'Bundle GitHub Page' description: 'This action bundles the current gh-page branch with the out folder' +inputs: + latest: + description: 'This has to be true if you want to overwrite latest.' + required: false + default: 'false' + tag: + description: 'This has to be a tag like v1.0.0 if you want to push a tag to the /version folder' + required: false runs: using: 'composite' steps: @@ -10,7 +18,7 @@ runs: - name: ➕ Create temp or public dir run: | echo "Create dir for branch: ${{ steps.extract_branch.outputs.branch }}" - if echo ${{ steps.extract_branch.outputs.branch }} | grep -c "main" + if [ ${{ inputs.latest }} == "true" ]; then mkdir temp echo "Created 'temp' dir" @@ -24,7 +32,7 @@ runs: shell: bash - name: 📦 Unpack Tar run: | - if echo ${{ steps.extract_branch.outputs.branch }} | grep -c "main" + if [ ${{ inputs.latest }} == "true" ]; then tar -zxf gh-pages -C temp --strip-components 1 else @@ -33,12 +41,17 @@ runs: shell: bash - name: 📁 Move ./out folder to public run: | - if echo ${{ steps.extract_branch.outputs.branch }} | grep -c "main" + if [ ${{ inputs.latest }} == "true" ]; then mv ./out ./public if [ -d ./temp/review ]; then mv ./temp/review ./public fi + elif [ ! -d ${{ inputs.tag }}]; + if [ ! -d ./public/version ]; then + mkdir ./public/version + fi + mv ./out ./public/version/${{ inputs.tag }} else if [ ! -d ./public/review ]; then mkdir ./public/review diff --git a/packages/db-ui-elements-stencil/src/components/development/github-version-switcher/github-version-switcher.tsx b/packages/db-ui-elements-stencil/src/components/development/github-version-switcher/github-version-switcher.tsx index e4da2bb7a..73c82484d 100644 --- a/packages/db-ui-elements-stencil/src/components/development/github-version-switcher/github-version-switcher.tsx +++ b/packages/db-ui-elements-stencil/src/components/development/github-version-switcher/github-version-switcher.tsx @@ -6,6 +6,14 @@ import { Component, h, Host, Prop, State } from '@stencil/core'; shadow: true }) export class GithubVersionSwitcher { + private _defaultBranch = 'latest'; + get defaultBranch(): string { + return this._defaultBranch; + } + + set defaultBranch(value: string) { + this._defaultBranch = value; + } /** * Provides the owner of the repo */ @@ -16,13 +24,13 @@ export class GithubVersionSwitcher { */ @Prop({ reflect: false }) repo: string; - /** - * Provides the defaultBranch of the repo - */ - @Prop({ reflect: false }) defaultBranch: string = 'main'; - - @State() groups = []; - @State() currentBranch = this.defaultBranch; + @State() groups = [ + { name: 'Versions', branches: [] }, + { name: 'Features', branches: [] }, + { name: 'Bugfixes', branches: [] }, + { name: 'Other', branches: [] } + ]; + @State() currentBranch = this._defaultBranch; @State() cleanOwner; @State() cleanRepo; @@ -30,50 +38,60 @@ export class GithubVersionSwitcher { return value.replace(/[^a-zA-Z0-9-]/g, ''); }; - componentWillLoad() { + private fetchFromGitHubApi = async (url: string) => { + const response = await fetch(url); + return await response.json(); + }; + + private setCurrentBranch = (branchNames: string[]) => { + const currentUrl = window.location.href; + const foundBranch = branchNames.find((branch) => + currentUrl.includes(branch) + ); + if (foundBranch) { + this.currentBranch = foundBranch; + } + }; + + private setBranches = (data: any[]) => { + const branchNames = data + .map((branch) => branch.name) + .filter( + (branch) => branch !== 'gh-pages' && !branch.includes('dependabot') + ); + branchNames.forEach((branch) => { + if (branch.startsWith('feat') || branch.startsWith('feature')) { + this.groups[1].branches.push(branch); + } else if (branch.startsWith('fix') || branch.startsWith('bugfix')) { + this.groups[2].branches.push(branch); + } else { + this.groups[3].branches.push(branch); + } + }); + this.setCurrentBranch(branchNames); + }; + + private setTags = (data: any[]) => { + const tagNames = data.map((tag) => tag.name); + tagNames.forEach((tag: string) => { + this.groups[0].branches.push(tag); + }); + this.setCurrentBranch(tagNames); + }; + + async componentWillLoad() { const cOwner = this.stripString(this.owner); this.cleanOwner = cOwner; const cRepo = this.stripString(this.repo); this.cleanRepo = cRepo; - fetch(`https://api.github.com/repos/${cOwner}/${cRepo}/branches`) - .then((response) => response.json()) - .then((data) => { - if (data) { - const branchNames = data - .map((branch) => branch.name) - .filter( - (branch) => - branch !== 'gh-pages' && !branch.includes('dependabot') - ); - const tmpGroups = [ - { name: 'Versions', branches: [] }, - { name: 'Features', branches: [] }, - { name: 'Bugfixes', branches: [] } - ]; - branchNames.forEach((branch) => { - if (branch.startsWith('feat') || branch.startsWith('feature')) { - tmpGroups[1].branches.push(branch); - } else if ( - branch.startsWith('fix') || - branch.startsWith('bugfix') - ) { - tmpGroups[2].branches.push(branch); - } else { - tmpGroups[0].branches.push(branch); - } - }); - // eslint-disable-next-line no-console - console.log(tmpGroups); - this.groups = tmpGroups; - const currentUrl = window.location.href; - const foundBranch = branchNames.find((branch) => - currentUrl.includes(branch) - ); - if (foundBranch) { - this.currentBranch = foundBranch; - } - } - }); + const branchesData = await this.fetchFromGitHubApi( + `https://api.github.com/repos/${cOwner}/${cRepo}/branches` + ); + this.setBranches(branchesData); + const tagsData = await this.fetchFromGitHubApi( + `https://api.github.com/repos/${cOwner}/${cRepo}/tags` + ); + this.setTags(tagsData); } // eslint-disable-next-line @stencil/own-methods-must-be-private @@ -82,8 +100,11 @@ export class GithubVersionSwitcher { const currentUrl = top.location.href; const urlPaths = currentUrl.split('?'); const lastPath = urlPaths[urlPaths.length - 1]; + const isTag = branch.split('.').length === 3 && branch.startsWith('v'); top.location = `https://${localOwner}.github.io/${localRepo}${ - this.defaultBranch === branch ? '' : `/review/${branch}` + this._defaultBranch === branch + ? '' + : `${isTag ? '/version' : '/review'}/${branch}` }/?${lastPath}`; } } @@ -104,6 +125,12 @@ export class GithubVersionSwitcher { ) } > + {this.groups .filter((group: any) => group.branches?.length > 0) .map((group: any) => ( diff --git a/packages/db-ui-elements-stencil/src/components/development/github-version-switcher/readme.md b/packages/db-ui-elements-stencil/src/components/development/github-version-switcher/readme.md index a2c1d371e..60b82db5b 100644 --- a/packages/db-ui-elements-stencil/src/components/development/github-version-switcher/readme.md +++ b/packages/db-ui-elements-stencil/src/components/development/github-version-switcher/readme.md @@ -7,11 +7,10 @@ ## Properties -| Property | Attribute | Description | Type | Default | -| --------------- | ---------------- | -------------------------------------- | -------- | ----------- | -| `defaultBranch` | `default-branch` | Provides the defaultBranch of the repo | `string` | `'main'` | -| `owner` | `owner` | Provides the owner of the repo | `string` | `undefined` | -| `repo` | `repo` | Provides the name of the repo | `string` | `undefined` | +| Property | Attribute | Description | Type | Default | +| -------- | --------- | ------------------------------ | -------- | ----------- | +| `owner` | `owner` | Provides the owner of the repo | `string` | `undefined` | +| `repo` | `repo` | Provides the name of the repo | `string` | `undefined` | ## Dependencies From 49b71a7182e2c34bb8d13772d4aa34d56a67c3d3 Mon Sep 17 00:00:00 2001 From: NicolasMerget Date: Fri, 22 Jul 2022 16:00:23 +0200 Subject: [PATCH 3/4] fix: issue with elif in bundle-gh-page --- .github/actions/bundle-gh-page/action.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/actions/bundle-gh-page/action.yml b/.github/actions/bundle-gh-page/action.yml index 535dba22e..c78ea260f 100644 --- a/.github/actions/bundle-gh-page/action.yml +++ b/.github/actions/bundle-gh-page/action.yml @@ -47,7 +47,8 @@ runs: if [ -d ./temp/review ]; then mv ./temp/review ./public fi - elif [ ! -d ${{ inputs.tag }}]; + elif [ ! -d ${{ inputs.tag }} ]; + then if [ ! -d ./public/version ]; then mkdir ./public/version fi From 0bfd40ae5ded4b3234054408aba9734e8ec2bb67 Mon Sep 17 00:00:00 2001 From: NicolasMerget Date: Mon, 25 Jul 2022 08:21:56 +0200 Subject: [PATCH 4/4] fix: missing hyphen for deployed gh-pages URL --- .github/workflows/03-deploy-gh-pages.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/03-deploy-gh-pages.yml b/.github/workflows/03-deploy-gh-pages.yml index 94aac46fe..3b6e8050e 100644 --- a/.github/workflows/03-deploy-gh-pages.yml +++ b/.github/workflows/03-deploy-gh-pages.yml @@ -36,9 +36,9 @@ jobs: export BRANCH=${GITHUB_REF#refs/heads/} if echo ${BRANCH} | grep -c "main" then - export URL=https://dbui.github.io/elements + export URL=https://db-ui.github.io/elements else - export URL=https://dbui.github.io/elements/review/${BRANCH} + export URL=https://db-ui.github.io/elements/review/${BRANCH} fi echo "URL: $URL" echo "### GH-Pages URL :rocket:

$URL" >> $GITHUB_STEP_SUMMARY