Skip to content

Commit

Permalink
Continue plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
lowlighter committed Feb 10, 2021
1 parent 65c7354 commit 0f6d018
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
return ({
user:{
repository:{
licenseInfo:{name:"MIT License"},
licenseInfo:{spdxId:"MIT", name:"MIT License", nickname:null, key:"mit"},
url:"https://github.com/lowlighter/metrics",
databaseId:293860197,
},
Expand Down
14 changes: 14 additions & 0 deletions source/plugins/licenses/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
### 📜 Licenses

⚠️ This is NOT legal advice, use at your own risk

The *licenses* plugin lets you display license informations like permissions, limitations and conditions along with additional metrics about dependencies.

<table>
<td align="center">
<img src="">
<img width="900" height="1" alt="">
</td>
</table>

Project must be setup with dependencies using `plugin_licenses_setup` option (for example, `npm ci` for a NodeJS project).

Dependencies will be analyzed with [github/licensed](https://github.com/github/licensed) and comparaed against GitHub known licenses.

#### ℹ️ Examples workflows

[➡️ Available options for this plugin](metadata.yml)
Expand All @@ -15,5 +23,11 @@
- uses: lowlighter/metrics@latest
with:
# ... other options
template: repository
user: repository-owner
query: '{"repo":"repository-name"}'
plugin_licenses: yes
plugin_licenses_setup: npm ci # Command to setup target repository
plugin_licenses_ratio: yes # Display used licenses ratio
plugin_licenses_legal: yes # Display permissions, limitations and conditions
```
41 changes: 27 additions & 14 deletions source/plugins/licenses/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
return null

//Load inputs
let {setup} = imports.metadata.plugins.licenses.inputs({data, account, q})
let {setup, ratio, legal} = imports.metadata.plugins.licenses.inputs({data, account, q})

//Initialization
const {user:{repository}} = await graphql(queries.licenses.repository({owner:data.repo.owner.login, name:data.repo.name, account}))
const _result = {licensed:{available:false}}

const {licenses} = await graphql(queries.licenses())

console.log(repository, imports.util.inspect(licenses, {depth:1/0}))
const result = {ratio, legal, default:repository.licenseInfo, licensed:{available:false}, text:{}, list:[], dependencies:0, known:0, unknown:0}

//Register existing licenses properties
const licenses = Object.fromEntries((await graphql(queries.licenses())).licenses.map(license => [license.key, license]))
for (const license of Object.values(licenses))
[...license.limitations, ...license.conditions, ...license.permissions].flat().map(({key, label}) => result.text[key] = label)

//Check if licensed exists
if (await imports.which("licensed")) {
Expand Down Expand Up @@ -47,15 +47,28 @@
else
console.debug(`metrics/compute/${login}/plugins > licenses > licensed not available`)

//Results
return {
total:10,
dependencies:24,
list:[
{name:"MIT", value:0.5, count:10, x:0, color:"#e34c26"},
{name:"Other", value:0.5, count:10, x:0.5, color:"#384d54"},
],
//List licenses properties
const base = {permissions:new Set(), limitations:new Set(), conditions:new Set()}
const combined = {permissions:new Set(), limitations:new Set(), conditions:new Set()}
for (const properties of Object.keys(base)) {
//Base license
licenses[repository.licenseInfo.key][properties].map(({key}) => base[properties].add(key))
//Combined licenses
for (const {key} of result.list)
licenses[key][properties].map(({key}) => combined[properties].add(key))
}

//Todo
//{name:"", key:"", value:NaN, count:NaN, x:NaN, color:""},

//Merge limitations and conditions
for (const properties of ["limitations", "conditions"])
result[properties] = [[...base[properties]].map(key => ({key, text:result.text[key], inherited:false})), [...combined[properties]].filter(key => !base[properties].has(key)).map(key => ({key, text:result.text[key], inherited:true}))].flat()
//Remove base permissions conflicting with inherited limitations
result.permissions = [...base.permissions].filter(key => !combined.limitations.has(key)).map(key => ({key, text:result.text[key]}))

//Results
return result
}
//Handle errors
catch (error) {
Expand Down
15 changes: 14 additions & 1 deletion source/plugins/licenses/metadata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,17 @@ inputs:
description: Command to setup target repository
type: string
default: ""
example: npm ci
example: npm ci

# Display used licenses from both repository license and dependencies licenses ratio
plugin_licenses_ratio:
description: Display used licenses ratio
type: boolean
default: no

# Display permissions, limitations and conditions from both repository license and dependencies licenses
# Note that this is NOT legal advice, use at your own risk
plugin_licenses_legal:
description: Display legal informations about used licenses
type: boolean
default: yes
3 changes: 3 additions & 0 deletions source/plugins/licenses/queries/repository.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ query LicensesRepository {
$account(login: "$owner") {
repository(name: "$name") {
licenseInfo {
spdxId
name
nickname
key
}
url
databaseId
Expand Down
44 changes: 28 additions & 16 deletions source/templates/repository/partials/licenses.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@
<section>
<div class="field">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path d="M2.75 1.5a.25.25 0 00-.25.25v11.5c0 .138.112.25.25.25h3.5a.75.75 0 010 1.5h-3.5A1.75 1.75 0 011 13.25V1.75C1 .784 1.784 0 2.75 0h8a1.75 1.75 0 011.508.862.75.75 0 11-1.289.768.25.25 0 00-.219-.13h-8z"></path><path fill-rule="evenodd" d="M8 7a4 4 0 116.49 3.13l.995 4.973a.75.75 0 01-.991.852l-2.409-.876a.25.25 0 00-.17 0l-2.409.876a.75.75 0 01-.991-.852l.994-4.973A3.993 3.993 0 018 7zm4-2.5a2.5 2.5 0 100 5 2.5 2.5 0 000-5zm0 6.5a4 4 0 001.104-.154l.649 3.243-1.155-.42c-.386-.14-.81-.14-1.196 0l-1.155.42.649-3.243A4 4 0 0012 11z"></path></svg>
<%= repo.licenseInfo.name %>
<%= plugins.licenses.default.name %>
</div>
<div class="field">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M1.5 8a6.5 6.5 0 1113 0 6.5 6.5 0 01-13 0zM0 8a8 8 0 1116 0A8 8 0 010 8zm11.78-1.72a.75.75 0 00-1.06-1.06L6.75 9.19 5.28 7.72a.75.75 0 00-1.06 1.06l2 2a.75.75 0 001.06 0l4.5-4.5z"></path></svg>
No conflicts
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M6.122.392a1.75 1.75 0 011.756 0l5.25 3.045c.54.313.872.89.872 1.514V7.25a.75.75 0 01-1.5 0V5.677L7.75 8.432v6.384a1 1 0 01-1.502.865L.872 12.563A1.75 1.75 0 010 11.049V4.951c0-.624.332-1.2.872-1.514L6.122.392zM7.125 1.69l4.63 2.685L7 7.133 2.245 4.375l4.63-2.685a.25.25 0 01.25 0zM1.5 11.049V5.677l4.75 2.755v5.516l-4.625-2.683a.25.25 0 01-.125-.216zm10.828 3.684a.75.75 0 101.087 1.034l2.378-2.5a.75.75 0 000-1.034l-2.378-2.5a.75.75 0 00-1.087 1.034L13.501 12H10.25a.75.75 0 000 1.5h3.251l-1.173 1.233z"></path></svg>
<%= plugins.licenses.dependencies %> dependenc<%= s(plugins.licenses.dependencies, "y") %>
</div>
</section>
<section>
<div class="field">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M6.122.392a1.75 1.75 0 011.756 0l5.25 3.045c.54.313.872.89.872 1.514V7.25a.75.75 0 01-1.5 0V5.677L7.75 8.432v6.384a1 1 0 01-1.502.865L.872 12.563A1.75 1.75 0 010 11.049V4.951c0-.624.332-1.2.872-1.514L6.122.392zM7.125 1.69l4.63 2.685L7 7.133 2.245 4.375l4.63-2.685a.25.25 0 01.25 0zM1.5 11.049V5.677l4.75 2.755v5.516l-4.625-2.683a.25.25 0 01-.125-.216zm11.672-.282a.75.75 0 10-1.087-1.034l-2.378 2.5a.75.75 0 000 1.034l2.378 2.5a.75.75 0 101.087-1.034L11.999 13.5h3.251a.75.75 0 000-1.5h-3.251l1.173-1.233z"></path></svg>
<%= plugins.licenses.dependencies %> dependenc<%= s(plugins.licenses.dependencies, "y") %>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M9.585.52a2.678 2.678 0 00-3.17 0l-.928.68a1.178 1.178 0 01-.518.215L3.83 1.59a2.678 2.678 0 00-2.24 2.24l-.175 1.14a1.178 1.178 0 01-.215.518l-.68.928a2.678 2.678 0 000 3.17l.68.928c.113.153.186.33.215.518l.175 1.138a2.678 2.678 0 002.24 2.24l1.138.175c.187.029.365.102.518.215l.928.68a2.678 2.678 0 003.17 0l.928-.68a1.17 1.17 0 01.518-.215l1.138-.175a2.678 2.678 0 002.241-2.241l.175-1.138c.029-.187.102-.365.215-.518l.68-.928a2.678 2.678 0 000-3.17l-.68-.928a1.179 1.179 0 01-.215-.518L14.41 3.83a2.678 2.678 0 00-2.24-2.24l-1.138-.175a1.179 1.179 0 01-.518-.215L9.585.52zM7.303 1.728c.415-.305.98-.305 1.394 0l.928.68c.348.256.752.423 1.18.489l1.136.174c.51.078.909.478.987.987l.174 1.137c.066.427.233.831.489 1.18l.68.927c.305.415.305.98 0 1.394l-.68.928a2.678 2.678 0 00-.489 1.18l-.174 1.136a1.178 1.178 0 01-.987.987l-1.137.174a2.678 2.678 0 00-1.18.489l-.927.68c-.415.305-.98.305-1.394 0l-.928-.68a2.678 2.678 0 00-1.18-.489l-1.136-.174a1.178 1.178 0 01-.987-.987l-.174-1.137a2.678 2.678 0 00-.489-1.18l-.68-.927a1.178 1.178 0 010-1.394l.68-.928c.256-.348.423-.752.489-1.18l.174-1.136c.078-.51.478-.909.987-.987l1.137-.174a2.678 2.678 0 001.18-.489l.927-.68zM11.28 6.78a.75.75 0 00-1.06-1.06L7 8.94 5.78 7.72a.75.75 0 00-1.06 1.06l1.75 1.75a.75.75 0 001.06 0l3.75-3.75z"></path></svg>
<%= plugins.licenses.known %> known license<%= s(plugins.licenses.known) %> used
</div>
<div class="field">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M6.415.52a2.678 2.678 0 013.17 0l.928.68c.153.113.33.186.518.215l1.138.175a2.678 2.678 0 012.241 2.24l.175 1.138c.029.187.102.365.215.518l.68.928a2.678 2.678 0 010 3.17l-.68.928a1.179 1.179 0 00-.215.518l-.175 1.138a2.678 2.678 0 01-2.241 2.241l-1.138.175a1.179 1.179 0 00-.518.215l-.928.68a2.678 2.678 0 01-3.17 0l-.928-.68a1.179 1.179 0 00-.518-.215L3.83 14.41a2.678 2.678 0 01-2.24-2.24l-.175-1.138a1.179 1.179 0 00-.215-.518l-.68-.928a2.678 2.678 0 010-3.17l.68-.928a1.17 1.17 0 00.215-.518l.175-1.14a2.678 2.678 0 012.24-2.24l1.138-.175c.187-.029.365-.102.518-.215l.928-.68zm2.282 1.209a1.178 1.178 0 00-1.394 0l-.928.68a2.678 2.678 0 01-1.18.489l-1.136.174a1.178 1.178 0 00-.987.987l-.174 1.137a2.678 2.678 0 01-.489 1.18l-.68.927c-.305.415-.305.98 0 1.394l.68.928c.256.348.423.752.489 1.18l.174 1.136c.078.51.478.909.987.987l1.137.174c.427.066.831.233 1.18.489l.927.68c.415.305.98.305 1.394 0l.928-.68a2.678 2.678 0 011.18-.489l1.136-.174c.51-.078.909-.478.987-.987l.174-1.137c.066-.427.233-.831.489-1.18l.68-.927c.305-.415.305-.98 0-1.394l-.68-.928a2.678 2.678 0 01-.489-1.18l-.174-1.136a1.178 1.178 0 00-.987-.987l-1.137-.174a2.678 2.678 0 01-1.18-.489l-.927-.68zM9 11a1 1 0 11-2 0 1 1 0 012 0zM6.92 6.085c.081-.16.19-.299.34-.398.145-.097.371-.187.74-.187.28 0 .553.087.738.225A.613.613 0 019 6.25c0 .177-.04.264-.077.318a.956.956 0 01-.277.245c-.076.051-.158.1-.258.161l-.007.004c-.093.056-.204.122-.313.195a2.416 2.416 0 00-.692.661.75.75 0 001.248.832.956.956 0 01.276-.245 6.3 6.3 0 01.26-.16l.006-.004c.093-.057.204-.123.313-.195.222-.149.487-.355.692-.662.214-.32.329-.702.329-1.15 0-.76-.36-1.348-.862-1.725A2.76 2.76 0 008 4c-.631 0-1.154.16-1.572.438-.413.276-.68.638-.849.977a.75.75 0 001.342.67z"></path></svg>
<%= plugins.licenses.dependencies %> dependenc<%= s(plugins.licenses.dependencies, "y") %>
<%= plugins.licenses.known %> unknown license<%= s(plugins.licenses.unknown) %> used
</div>
</section>
</div>
Expand Down Expand Up @@ -66,28 +66,40 @@
<section class="licenses">
<div class="column">
<div class="title">Permissions</div>
<% for (const permission of ["Commercial use", "Distribution", "Modification", "Patent use", "Private use"]) { %>
<div class="permission">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg>
<%= permission %>
<% for (const {text, disabled} of plugins.licenses.permissions) { %>
<div class="permission <%= disabled ? "disabled" : "" %>">
<% if (disabled) { %>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M6.122.392a1.75 1.75 0 011.756 0l5.25 3.045c.54.313.872.89.872 1.514V7.25a.75.75 0 01-1.5 0V5.677L7.75 8.432v6.384a1 1 0 01-1.502.865L.872 12.563A1.75 1.75 0 010 11.049V4.951c0-.624.332-1.2.872-1.514L6.122.392zM7.125 1.69l4.63 2.685L7 7.133 2.245 4.375l4.63-2.685a.25.25 0 01.25 0zM1.5 11.049V5.677l4.75 2.755v5.516l-4.625-2.683a.25.25 0 01-.125-.216zm10.828 3.684a.75.75 0 101.087 1.034l2.378-2.5a.75.75 0 000-1.034l-2.378-2.5a.75.75 0 00-1.087 1.034L13.501 12H10.25a.75.75 0 000 1.5h3.251l-1.173 1.233z"></path></svg>
<% } else { %>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg>
<% } %>
<%= text %>
</div>
<% } %>
</div>
<div class="column">
<div class="title">Limitations</div>
<% for (const limitation of ["Liability", "Trademark use", "Warranty"]) { %>
<% for (const {text, inherited} of plugins.licenses.limitations) { %>
<div class="limitation">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M3.72 3.72a.75.75 0 011.06 0L8 6.94l3.22-3.22a.75.75 0 111.06 1.06L9.06 8l3.22 3.22a.75.75 0 11-1.06 1.06L8 9.06l-3.22 3.22a.75.75 0 01-1.06-1.06L6.94 8 3.72 4.78a.75.75 0 010-1.06z"></path></svg>
<%= limitation %>
<% if (inherited) { %>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M6.122.392a1.75 1.75 0 011.756 0l5.25 3.045c.54.313.872.89.872 1.514V7.25a.75.75 0 01-1.5 0V5.677L7.75 8.432v6.384a1 1 0 01-1.502.865L.872 12.563A1.75 1.75 0 010 11.049V4.951c0-.624.332-1.2.872-1.514L6.122.392zM7.125 1.69l4.63 2.685L7 7.133 2.245 4.375l4.63-2.685a.25.25 0 01.25 0zM1.5 11.049V5.677l4.75 2.755v5.516l-4.625-2.683a.25.25 0 01-.125-.216zm10.828 3.684a.75.75 0 101.087 1.034l2.378-2.5a.75.75 0 000-1.034l-2.378-2.5a.75.75 0 00-1.087 1.034L13.501 12H10.25a.75.75 0 000 1.5h3.251l-1.173 1.233z"></path></svg>
<% } else { %>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M3.72 3.72a.75.75 0 011.06 0L8 6.94l3.22-3.22a.75.75 0 111.06 1.06L9.06 8l3.22 3.22a.75.75 0 11-1.06 1.06L8 9.06l-3.22 3.22a.75.75 0 01-1.06-1.06L6.94 8 3.72 4.78a.75.75 0 010-1.06z"></path></svg>
<% } %>
<%= text %>
</div>
<% } %>
</div>
<div class="column">
<div class="title">Conditions</div>
<% for (const condition of ["Disclose source", "License and copyright notice", "Network use is distribution", "Same license", "State changes"]) { %>
<% for (const {text, inherited} of plugins.licenses.conditions) { %>
<div class="condition">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M8 1.5a6.5 6.5 0 100 13 6.5 6.5 0 000-13zM0 8a8 8 0 1116 0A8 8 0 010 8zm6.5-.25A.75.75 0 017.25 7h1a.75.75 0 01.75.75v2.75h.25a.75.75 0 010 1.5h-2a.75.75 0 010-1.5h.25v-2h-.25a.75.75 0 01-.75-.75zM8 6a1 1 0 100-2 1 1 0 000 2z"></path></svg>
<%= condition %>
<% if (inherited) { %>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M6.122.392a1.75 1.75 0 011.756 0l5.25 3.045c.54.313.872.89.872 1.514V7.25a.75.75 0 01-1.5 0V5.677L7.75 8.432v6.384a1 1 0 01-1.502.865L.872 12.563A1.75 1.75 0 010 11.049V4.951c0-.624.332-1.2.872-1.514L6.122.392zM7.125 1.69l4.63 2.685L7 7.133 2.245 4.375l4.63-2.685a.25.25 0 01.25 0zM1.5 11.049V5.677l4.75 2.755v5.516l-4.625-2.683a.25.25 0 01-.125-.216zm10.828 3.684a.75.75 0 101.087 1.034l2.378-2.5a.75.75 0 000-1.034l-2.378-2.5a.75.75 0 00-1.087 1.034L13.501 12H10.25a.75.75 0 000 1.5h3.251l-1.173 1.233z"></path></svg>
<% } else { %>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M8 1.5a6.5 6.5 0 100 13 6.5 6.5 0 000-13zM0 8a8 8 0 1116 0A8 8 0 010 8zm6.5-.25A.75.75 0 017.25 7h1a.75.75 0 01.75.75v2.75h.25a.75.75 0 010 1.5h-2a.75.75 0 010-1.5h.25v-2h-.25a.75.75 0 01-.75-.75zM8 6a1 1 0 100-2 1 1 0 000 2z"></path></svg>
<% } %>
<%= text %>
</div>
<% } %>
</div>
Expand Down

0 comments on commit 0f6d018

Please sign in to comment.