Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 48 additions & 17 deletions .github/workflows/pr-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,60 @@ jobs:
- name: Test 🧪
run: yarn test

- name: SonarCloud 🔬
uses: sonarsource/sonarcloud-github-action@v2.0.2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

- name: Build 🛠️
run: yarn build

- name: Build Storybook
run: yarn build:storybook

- name: Save PR number
- name: Create and Upload Storybook to PR directory
run: |
PR_NUMBER="PR-${{ github.event.number }}"

git config user.name "evargast"
git config user.email "52969178+evargast@users.noreply.github.com"

# Fetch the existing gh-pages branch
git fetch origin gh-pages

# Checkout the existing gh-pages branch
git checkout gh-pages

# Remove the PR directory if it exists
rm -rf "${PR_NUMBER}"

# Create the PR directory
mkdir "${PR_NUMBER}"

# Copy the contents of dist-storybook to the directory
cp -r dist-storybook/* "${PR_NUMBER}/"

rm -rf node_modules # Explicitly remove the node_modules directory
rm -rf dist-storybook # Explicitly remove the dist-storybook directory

# Add, commit, and push changes
git add "${PR_NUMBER}/."
git commit -m "Update Storybook for ${PR_NUMBER}"
git push -f origin gh-pages
env:
PR_NUMBER: ${{ github.event.number }}
PR_BRANCH: ${{ github.event.pull_request.head.ref }}
PR_BASE: ${{ github.event.pull_request.base.ref }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Add comment to the PR with Storybook URL
run: |
mkdir -p ./pr
echo $PR_NUMBER > ./pr/pr_number
echo $PR_BRANCH > ./pr/pr_branch
echo $PR_BASE > ./pr/pr_base
PR_NUMBER="${{ github.event.number }}"
COMMENT_BODY="🎨 Storybook -> https://opensource.adobe.com/react-spectrum-charts/PR-${PR_NUMBER}"

- name: Upload code coverage and storybook
uses: actions/upload-artifact@v4
with:
name: rsc-pr-build-artifacts
path: |
coverage/lcov.info
test-report.xml
dist-storybook/*
pr/
curl -sSL \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-X POST \
-d '{"body":"'"${COMMENT_BODY}"'"}' \
"https://api.github.com/repos/${{ github.repository }}/issues/${PR_NUMBER}/comments"

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ test-report.xml

# exclude vscode config except for our shared snippets
.vscode/*
!.vscode/reactSpectrumCharts.code-snippets
!.vscode/reactSpectrumCharts.code-snippets

# exclude sonar files
.scannerwork
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
"tsc": "tsc --noEmit --skipLibCheck",
"prepack": "yarn build",
"pack-test": "rm -rf dist & cross-env NODE_ENV=development npm pack",
"sonar": "yarn test && node ./scripts/runSonarCloud.js",
"sonar-no-test": "node ./scripts/runSonarCloud.js",
"start": "yarn storybook",
"storybook": "cross-env NODE_OPTIONS=--openssl-legacy-provider && storybook dev -p 6009",
"test": "cross-env BABEL_ENV=test jest",
Expand Down Expand Up @@ -102,6 +104,7 @@
"@types/react-dom": "18.3.0",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"axios": "^1.7.7",
"babel-jest": "^29.7.0",
"babel-loader": "^9.1.0",
"clean-webpack-plugin": "^4.0.0",
Expand Down
113 changes: 113 additions & 0 deletions scripts/runSonarCloud.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright 2024 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

/* eslint-disable @typescript-eslint/no-var-requires */
const { execSync } = require('child_process');
const fs = require('fs');
const axios = require('axios');

const colors = {
reset: '\x1b[0m',
bright: '\x1b[1m',
dim: '\x1b[2m',
underscore: '\x1b[4m',
blink: '\x1b[5m',
reverse: '\x1b[7m',
hidden: '\x1b[8m',

fgBlack: '\x1b[30m',
fgRed: '\x1b[31m',
fgGreen: '\x1b[32m',
fgYellow: '\x1b[33m',
fgBlue: '\x1b[34m',
fgMagenta: '\x1b[35m',
fgCyan: '\x1b[36m',
fgWhite: '\x1b[37m',
};

async function main() {
// Read the local.json file
const config = JSON.parse(fs.readFileSync('./local.json', 'utf8'));

// Extract tokens from local.json
const { SONAR_TOKEN, GITHUB_TOKEN } = config;

// Ensure tokens exists
if (!SONAR_TOKEN) {
console.error(`${colors.fgRed}Error: SONAR_TOKEN is missing from local.json.${colors.reset}`);
process.exit(1);
}
if (!GITHUB_TOKEN) {
console.error(`${colors.fgRed}Error: GITHUB_TOKEN is missing from local.json.${colors.reset}`);
process.exit(1);
}

// Get the current branch name
const branch = execSync('git rev-parse --abbrev-ref HEAD').toString().trim();

// Get the PR number from the github API
const prNumber = await getPrNumber(branch, GITHUB_TOKEN);

const sonarOptions = {
'-Dsonar.organization': 'adobeinc',
'-Dsonar.projectKey': 'adobe_react-spectrum-charts',
'-Dsonar.sources': '.',
'-Dsonar.host.url': 'https://sonarcloud.io',
'-Dsonar.pullrequest.key': prNumber,
'-Dsonar.pullrequest.branch': branch,
'-Dsonar.pullrequest.base': 'main',
};

console.log(
`${colors.fgBlue}Running sonar-scanner for PR ${colors.fgMagenta}${prNumber}${colors.fgBlue} on branch ${colors.fgMagenta}${branch}${colors.reset}`
);

// Run sonar-scanner
execSync(
`sonar-scanner ${Object.entries(sonarOptions)
.map((option) => option[0] + '=' + option[1])
.join(' ')}`,
{
env: { ...process.env, SONAR_TOKEN },
stdio: 'inherit',
}
);

console.log(`${colors.fgGreen}SonarCloud analysis completed successfully.${colors.reset}`);
}

async function getPrNumber(branch, token) {
const headers = {
Accept: 'application/vnd.github+json',
Authorization: `Bearer ${token}`,
'X-GitHub-Api-Version': '2022-11-28',
};
try {
const response = await axios.get(` https://api.github.com/repos/adobe/react-spectrum-charts/pulls?state=open`, {
headers,
});
const branchPr = response.data.find((pr) => pr.head.ref === branch);
if (!branchPr) {
console.error(`${colors.fgRed}Error: Open PR not found for branch ${branch}.${colors.reset}`);
process.exit(1);
}

console.log(
`${colors.fgGreen}PR number for branch ${colors.fgYellow}${branch}${colors.fgGreen} is ${colors.fgYellow}${branchPr.number}${colors.reset}`
);
return branchPr.number;
} catch (error) {
console.error(`${colors.fgRed}Error:`, error.response ? error.response.data : error.message, colors.reset);
}
}

main();
19 changes: 19 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6684,6 +6684,15 @@ axe-core@=4.7.0:
resolved "https://registry.yarnpkg.com/axe-core/-/axe-core-4.7.0.tgz#34ba5a48a8b564f67e103f0aa5768d76e15bbbbf"
integrity sha512-M0JtH+hlOL5pLQwHOLNYZaXuhqmvS8oExsqB1SBYgA4Dk7u/xx+YdGHXaK5pyUfed5mYXdlYiphWq3G8cRi5JQ==

axios@^1.7.7:
version "1.7.7"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.7.7.tgz#2f554296f9892a72ac8d8e4c5b79c14a91d0a47f"
integrity sha512-S4kL7XrjgBmvdGut0sN3yJxqYzrDOnivkBiN0OFs6hLiUam3UPvswUo0kqGyhqUZGEOytHyumEdXsAkgCOUf3Q==
dependencies:
follow-redirects "^1.15.6"
form-data "^4.0.0"
proxy-from-env "^1.1.0"

axobject-query@^3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/axobject-query/-/axobject-query-3.2.1.tgz#39c378a6e3b06ca679f29138151e45b2b32da62a"
Expand Down Expand Up @@ -9050,6 +9059,11 @@ follow-redirects@^1.0.0:
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.3.tgz#fe2f3ef2690afce7e82ed0b44db08165b207123a"
integrity sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q==

follow-redirects@^1.15.6:
version "1.15.9"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.9.tgz#a604fa10e443bf98ca94228d9eebcc2e8a2c8ee1"
integrity sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ==

for-each@^0.3.3:
version "0.3.3"
resolved "https://registry.yarnpkg.com/for-each/-/for-each-0.3.3.tgz#69b447e88a0a5d32c3e7084f3f1710034b21376e"
Expand Down Expand Up @@ -12222,6 +12236,11 @@ proxy-addr@~2.0.7:
forwarded "0.2.0"
ipaddr.js "1.9.1"

proxy-from-env@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2"
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==

psl@^1.1.33:
version "1.9.0"
resolved "https://registry.yarnpkg.com/psl/-/psl-1.9.0.tgz#d0df2a137f00794565fcaf3b2c00cd09f8d5a5a7"
Expand Down