Skip to content

Commit

Permalink
Tidy ups, add git workflow job and fix broken strings
Browse files Browse the repository at this point in the history
  • Loading branch information
nwmac committed Sep 27, 2024
1 parent e069375 commit 3b7e5d0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 94 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,23 @@ jobs:
# Falure won't fail the job (remove -x when all current issues are fixed)
./scripts/check-i18n -s -x
check-i18n-links:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 1
- uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'

- name: Install packages
run: yarn install:ci

- name: Run check of all http links in the i18n files (broken lnk check)
run: |
# Falure won't fail the job (remove -x when all current issues are fixed)
./scripts/check-i18n-links -x
# coverage:
# runs-on: ubuntu-latest
Expand Down
100 changes: 12 additions & 88 deletions scripts/check-i18n-links
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ const pkgFolder = path.resolve(base, 'pkg');
const reset = "\x1b[0m";
const cyan = `\x1b[96m`;
const yellow = `\x1b[33m`;
const white = `\x1b[97m`;
const bold = `\x1b[1m`;
const bg_red = `\x1b[41m`;

const DOCS_BASE_REGEX = /export const DOCS_BASE = '([^']*)';/;

Expand All @@ -46,9 +44,7 @@ const CATEGORIES = [

let docsBaseUrl = '';

// -s flag will show the details of all of the unused strings
// -x flag will cause script to return 0, even if there are errors
let showUnused = false;
let doNotReturnError = false;

// Simple arg parsing
Expand All @@ -57,9 +53,7 @@ if (process.argv.length > 2) {
process.argv.shift();

process.argv.forEach((arg) => {
if (arg === '-s') {
showUnused = true;
} else if (arg === '-x') {
if (arg === '-x') {
doNotReturnError = true;
}
});
Expand All @@ -73,52 +67,6 @@ if (docsBaseFileMatches && docsBaseFileMatches.length === 2) {
docsBaseUrl = docsBaseFileMatches[1];
}

function parseReference(ref) {
if (!ref.includes('$') || ref.startsWith('${')) {
return ref;
}

let out = '';
let inVar = false;
let variable = '';
let vars = 0;

for (let i=0;i<ref.length;i++) {
if (inVar) {
if (ref[i] === '}') {
inVar = false;
} else {
variable += ref[i];
}
} else if (ref[i] === '{') {
inVar = true;
vars++;
variable = '';
} else {
out += ref[i];
}
}

let p = out.replaceAll('"', '').split('.');

// Check for patterns like this: resourceDetail.detailTop.${annotationsVisible? 'hideAnnotations' : 'showAnnotations'}
if (vars === 1 && variable.includes('?')) {
const options = variable.substr(variable.indexOf('?') + 1);
const opts = options.split(':').map((o) => o.trim().replaceAll('\'', ''));

if (opts.length === 2) {
const a = p.map((o) => o === '$' ? opts[0] : o).join('.');
const b = p.map((o) => o === '$' ? opts[1] : o).join('.');

return [a, b];
}
}

p = p.map((a) => a.startsWith('$') ? `.*${ a.substr(1) }` : a);

return new RegExp(p.join('\\.'), 'g');
}

function readAndParseTranslations(filePath) {
const data = fs.readFileSync(filePath, 'utf8');

Expand Down Expand Up @@ -182,30 +130,6 @@ function parseLinks(str) {
return links;
}

function makeRegex(str) {
if (str.startsWith('/') && str.endsWith('/')) {
return new RegExp(str.substr(1, str.length - 2), 'g');
} else {
// String
// Support .* for a simple wildcard match
if (str.includes('*')) {
const parts = str.split('.');
const p = parts.map((s, i) => s === '*' ? (i === parts.length -1 ? '.*' : '[^\.]*') : s);

return new RegExp(`^${ p.join('\\.') }$`);
} else {
return str;
}
}
}

function doesMatch(stringOrRegex, str) {
if (typeof stringOrRegex === 'string') {
return str === stringOrRegex;
} else {
return str.match(stringOrRegex);
}
}
function loadI18nFiles(folder) {
let res = {};

Expand Down Expand Up @@ -247,11 +171,6 @@ i18n = { ...i18n, ...loadI18nFiles(pkgFolder) };

console.log(`Read ${cyan}${ Object.keys(i18n).length }${reset} translations`); // eslint-disable-line no-console

//..console.log(`Found ${cyan}${ Object.keys(references).length }${reset} i18n references in code`); // eslint-disable-line no-console

let unused = 0;
const unusedKeys = [];

const links = [];

// Look for translations that are not used
Expand Down Expand Up @@ -291,7 +210,7 @@ function showByCategory(linksToShow, prefixLabel, otherLabel, color) {
});

CATEGORIES.forEach((category) => {
if (byCategory[category.name].length) {
if (byCategory[category.name]?.length) {
console.log(`${color}${prefixLabel} ${category.name}${reset}`); // eslint-disable-line no-console
byCategory[category.name].forEach((link) => console.log(` ${link}`)); // eslint-disable-line no-console
}
Expand All @@ -304,7 +223,7 @@ function showByCategory(linksToShow, prefixLabel, otherLabel, color) {
}

async function check(links) {
const badLinks = [];
const brokenLinks = [];

for(let i =0; i<links.length; i++) {
const link = links[i];
Expand Down Expand Up @@ -332,23 +251,28 @@ async function check(links) {

console.log(` ${ link } : ${ sc } ${ statusMessage ? statusMessage : '' }`); // eslint-disable-line no-console

badLinks.push(link);
brokenLinks.push(link);
}
};

console.log(''); // eslint-disable-line no-console

if (badLinks.length === 0) {
if (brokenLinks.length === 0) {
console.log(`${cyan}${bold}Links Checked - all links could be fetched okay${reset}`); // eslint-disable-line no-console
} else {
console.log(`${yellow}${bold}Found ${badLinks.length} link(s) that could not be fetched${reset}`); // eslint-disable-line no-console
console.log(`${yellow}${bold}Found ${brokenLinks.length} link(s) that could not be fetched${reset}`); // eslint-disable-line no-console
}

console.log(''); // eslint-disable-line no-console

showByCategory(badLinks, 'Broken links in', 'Other Broken links', yellow);
showByCategory(brokenLinks, 'Broken links in', 'Other Broken links', yellow);

console.log(''); // eslint-disable-line no-console

// Return with error code if broken links found
if (!doNotReturnError && brokenLinks.length > 0) {
process.exit(1);
}
}

console.log(`${cyan}Checking doc links ...${reset}`); // eslint-disable-line no-console
Expand Down
6 changes: 3 additions & 3 deletions shell/assets/translations/en-us.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ authConfig:
5: 'Upload the downloaded JSON file in the OAuth credentials box.'
3:
title: 'Create Service Account credentials'
introduction: 'Follow <a href="{docsBase}/admin-settings/authentication/google/#3-creating-service-account-credentials" target="_blank" rel="noopener noreferrer nofollow">this</a> guide to:'
introduction: 'Follow <a href="{docsBase}/how-to-guides/new-user-guides/authentication-permissions-and-global-configuration/authentication-config/configure-google-oauth#3-creating-service-account-credentials" target="_blank" rel="noopener noreferrer nofollow">this</a> guide to:'
body:
1: Create a service account.
2: Generate a key for the service account.
Expand Down Expand Up @@ -1134,7 +1134,7 @@ cis:
alertNeeded: |-
Alerting must be enabled within the CIS chart values.yaml.
This requires that the <a tabindex="0" href="{link}">{vendor} Monitoring and Alerting app</a> is installed
and the Receivers and Routes are <a target="_blank" rel='noopener noreferrer nofollow' href='{docsBase}/monitoring-alerting/configuration/#alertmanager-configuration/'> configured to send out alerts.</a>
and the Receivers and Routes are <a target="_blank" rel='noopener noreferrer nofollow' href='{docsBase}/how-to-guides/advanced-user-guides/monitoring-v2-configuration-guides/advanced-configuration/alertmanager'> configured to send out alerts.</a>
alertOnComplete: Alert on scan completion
alertOnFailure: Alert on scan failure
benchmarkVersion: Benchmark Version
Expand Down Expand Up @@ -3484,7 +3484,7 @@ monitoring:
keyFilePath:
label: Key File Path
placeholder: e.g. ./key-file.pfx
secretsBanner: The file paths below must be referenced in <pre class="inline-block m-0 p-0 vertical-middle">alertmanager.alertmanagerSpec.secrets</pre> when deploying the Monitoring chart. For more information see our <a href="{docsBase}/monitoring-alerting/" target="_blank" rel="noopener noreferrer nofollow">documentation</a>.
secretsBanner: The file paths below must be referenced in <pre class="inline-block m-0 p-0 vertical-middle">alertmanager.alertmanagerSpec.secrets</pre> when deploying the Monitoring chart. For more information see our <a href="{docsBase}/how-to-guides/advanced-user-guides/monitoring-alerting-guides" target="_blank" rel="noopener noreferrer nofollow">documentation</a>.
projectMonitoring:
detail:
error: "Unable to fetch Dashboard values with status: "
Expand Down
6 changes: 3 additions & 3 deletions shell/assets/translations/zh-hans.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ authConfig:
5: '在 OAuth 凭证框中,上传下载的 JSON 文件。'
3:
title: '创建 Service Account 凭证'
introduction: '参照本<a href="{docsBase}/admin-settings/authentication/google/#3-creating-service-account-credentials" target="_blank" rel="noopener noreferrer nofollow">指南</a>:'
introduction: '参照本<a href="{docsBase}/how-to-guides/new-user-guides/authentication-permissions-and-global-configuration/authentication-config/configure-google-oauth#3-creating-service-account-credentials" target="_blank" rel="noopener noreferrer nofollow">指南</a>:'
body:
1: 创建一个 Service Account。
2: 为这个 Service Account 生成密钥。
Expand Down Expand Up @@ -1049,7 +1049,7 @@ cis:
alertNeeded: |-
必须在 CIS Chart values.yaml 中开启告警。
这要求<a tabindex="0" href="{link}"> {vendor} 的 Monitoring 与 Alerting 应用</a>已经安装
,而且接收器和路由<a target="_blank" rel='noopener noreferrer nofollow' href='{docsBase}/monitoring-alerting/configuration/#alertmanager-configuration/'>已配置告警发送</a>。
,而且接收器和路由<a target="_blank" rel='noopener noreferrer nofollow' href='{docsBase}/how-to-guides/advanced-user-guides/monitoring-v2-configuration-guides/advanced-configuration/alertmanager'>已配置告警发送</a>。
alertOnComplete: 扫描完成告警
alertOnFailure: 扫描失败告警
benchmarkVersion: Benchmark 版本
Expand Down Expand Up @@ -3235,7 +3235,7 @@ monitoring:
keyFilePath:
label: 密钥文件路径
placeholder: 例如:./key-file.pfx
secretsBanner: 部署 Monitoring Chart 时,必须在<<pre class="inline-block m-0 p-0 vertical-middle">alertmanager.alertmanagerSpec.secrets</pre>中引用以下文件路径。详情请查看<a href="{docsBase}/monitoring-alerting/" target="_blank" rel="noopener noreferrer nofollow">官方文档</a>。
secretsBanner: 部署 Monitoring Chart 时,必须在<<pre class="inline-block m-0 p-0 vertical-middle">alertmanager.alertmanagerSpec.secrets</pre>中引用以下文件路径。详情请查看<a href="{docsBase}/how-to-guides/advanced-user-guides/monitoring-alerting-guides" target="_blank" rel="noopener noreferrer nofollow">官方文档</a>。
projectMonitoring:
detail:
error: "无法获取具有状态的仪表板值: "
Expand Down

0 comments on commit 3b7e5d0

Please sign in to comment.