From 63b00e80e0dd5e7c546098ae02846b905e822b2c Mon Sep 17 00:00:00 2001 From: Cintia Sanchez Garcia Date: Fri, 16 Jun 2023 10:14:26 +0200 Subject: [PATCH] Add URI parameter to Scorecard viewer Signed-off-by: Cintia Sanchez Garcia --- scorecards-site/static/viewer/index.html | 50 +++++++++++++++++------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/scorecards-site/static/viewer/index.html b/scorecards-site/static/viewer/index.html index 5be0ba3f..505f7331 100644 --- a/scorecards-site/static/viewer/index.html +++ b/scorecards-site/static/viewer/index.html @@ -6,15 +6,25 @@ Its behavior can be controlled with the following query string parameters: - - platform (required): VCS platform (eg. github.com). - - org (required): Name of the owner/organization of the repository. - - repo (required): Name of the repository. - - commit (optional): SHA1 commit hash expressed in hexadecimal format. - - theme (optional): Theme to use (light/dark). - - sort_by (optional): Sort criteria (check-name/risk-level/check-score) - - sort_direction (optional): Sort direction (asc/desc) - - embed (optional): Whether the rendered result would be embedded on another site (true/false). + (required) + + - uri: Repository URI (eg. github.com/ossf/scorecard) + + (or, alternatively, each component of the URI) + + - platform: VCS platform (eg. github.com). + - org: Name of the owner/organization of the repository. + - repo: Name of the repository. + + (optional) + + - commit: SHA1 commit hash expressed in hexadecimal format. + - theme: Theme to use (light/dark). + - sort_by: Sort criteria (check-name/risk-level/check-score) + - sort_direction: Sort direction (asc/desc) + - embed: Whether the rendered result would be embedded on another site (true/false). --> + @@ -1086,6 +1096,16 @@ document.getElementsByClassName('table-wrapper')[0].classList.add('embedded'); } + let platform, org, repo; + + if (params.uri) { + [platform, org, repo] = params.uri.split('/'); + } else { + platform = params.platform; + org = params.org; + repo = params.repo; + } + let sortBy = params.sort_by || DEFAULT_SORT_BY; let sortDirection = params.sort_direction || DEFAULT_SORT_DIRECTTION; let scoreData; @@ -1143,7 +1163,7 @@ Handlebars.registerHelper('getRepoLogo', () => { let icon = ''; - switch (params.platform) { + switch (platform) { case 'github.com': icon = ''; break; @@ -1265,11 +1285,11 @@ } } - if (params.platform && params.org && params.repo) { + if (platform && org && repo) { // Load placeholder - renderDataTemplate({ data: {...PLACEHOLDER, checks: sortChecks(PLACEHOLDER.checks), repo: {name: `${params.platform}/${params.org}/${params.repo}`, commit: ""}}, isPlaceholder: true, sortBy: sortBy, sortDirection: sortDirection }); + renderDataTemplate({ data: {...PLACEHOLDER, checks: sortChecks(PLACEHOLDER.checks), repo: {name: `${platform}/${org}/${repo}`, commit: ""}}, isPlaceholder: true, sortBy: sortBy, sortDirection: sortDirection }); - apiFetch(params.platform, params.org, params.repo, params.commit) + apiFetch(platform, org, repo, params.commit) .then((data) => { scoreData = data; renderDataTemplate({ data: {...data, checks: sortChecks(data.checks)}, isPlaceholder: false, sortBy: sortBy, sortDirection: sortDirection }); @@ -1297,13 +1317,13 @@ }); } else { let missingParams = []; - if (params.platform === null) { + if (platform === null || platform === undefined) { missingParams.push('platform'); } - if (params.repo === null) { + if (repo === null || repo === undefined) { missingParams.push('repo'); } - if (params.org === null) { + if (org === null || org === undefined) { missingParams.push('org'); } const template = Handlebars.compile(errorTmpl);