Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
lowlighter committed Jul 30, 2021
2 parents d2729a5 + 0127485 commit 113d098
Show file tree
Hide file tree
Showing 18 changed files with 464 additions and 1 deletion.
27 changes: 27 additions & 0 deletions source/app/mocks/api/github/graphql/discussions.categories.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**Mocked data */
export default function({faker, query, login = faker.internet.userName()}) {
console.debug("metrics/compute/mocks > mocking graphql api result > contributors/commit")
return /after: "MOCKED_CURSOR"/m.test(query)
? ({
user:{
repositoryDiscussions:{
edges:[],
nodes:[],
}
}
})
: ({
user:{
repositoryDiscussions:{
edges:new Array(100).fill(null).map(_ => ({cursor:"MOCKED_CURSOR"})),
nodes:new Array(100).fill(null).map(_ => ({
category:{
emoji:faker.random.arrayElement([":chart_with_upwards_trend:", ":chart_with_downwards_trend:", ":bar_char:"]),
name:faker.lorem.slug()
}
}))
}
}
})
}

11 changes: 11 additions & 0 deletions source/app/mocks/api/github/graphql/discussions.statistics.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**Mocked data */
export default function({faker, query, login = faker.internet.userName()}) {
console.debug("metrics/compute/mocks > mocking graphql api result > contributors/commit")
return ({
user:{
started:{totalCount:faker.datatype.number(1000)},
comments:{totalCount:faker.datatype.number(1000)},
answers:{totalCount:faker.datatype.number(1000)}
}
})
}
29 changes: 29 additions & 0 deletions source/app/mocks/api/github/graphql/repositories.repository.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**Mocked data */
export default function({faker, query, login = faker.internet.userName()}) {
console.debug("metrics/compute/mocks > mocking graphql api result > stars/default")
return ({
repository:{
createdAt: faker.date.past(),
description:"📊 An image generator with 20+ metrics about your GitHub account such as activity, community, repositories, coding habits, website performances, music played, starred topics, etc. that you can put on your profile or elsewhere !",
forkCount:faker.datatype.number(100),
isFork:false,
issues:{
totalCount:faker.datatype.number(100),
},
nameWithOwner:"lowlighter/metrics",
openGraphImageUrl:"https://repository-images.githubusercontent.com/293860197/7fd72080-496d-11eb-8fe0-238b38a0746a",
pullRequests:{
totalCount:faker.datatype.number(100),
},
stargazerCount:faker.datatype.number(10000),
licenseInfo:{
nickname:null,
name:"MIT License",
},
primaryLanguage:{
color:"#f1e05a",
name:"JavaScript",
},
},
})
}
40 changes: 39 additions & 1 deletion source/app/web/statics/app.placeholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,20 @@
},
})
: null),
//Discussions
...(set.plugins.enabled.discussions
? ({
discussions: {
categories: {
stats: { '🙏 Q&A': faker.datatype.number(100), '📣 Announcements': faker.datatype.number(100), '💡 Ideas': faker.datatype.number(100), '💬 General': faker.datatype.number(100) },
favorite: '📣 Announcements'
},
started: faker.datatype.number(1000),
comments: faker.datatype.number(1000),
answers: faker.datatype.number(1000),
},
})
: null),
//Posts
...(set.plugins.enabled.posts
? ({
Expand Down Expand Up @@ -664,7 +678,31 @@
},
})
: null),
//Stars
//Repositories
...(set.plugins.enabled.repositories
? ({
repositories: {
list: new Array(Number(options["repositories.featured"].split(",").length) - 1).fill(null).map((_, i) => ({
created: faker.date.past(),
description: faker.lorem.sentence(),
forkCount: faker.datatype.number(100),
isFork: faker.datatype.boolean(),
issues: {
totalCount: faker.datatype.number(100),
},
nameWithOwner: `${faker.random.word()}/${faker.random.word()}`,
openGraphImageUrl: faker.internet.url(),
pullRequests: {
totalCount: faker.datatype.number(100),
},
stargazerCount: faker.datatype.number(10000),
licenseInfo: { nickname: null, name: "License" },
primaryLanguage: { color: faker.internet.color(), name: faker.lorem.word() },
})),
},
})
: null),
//Stargazers
...(set.plugins.enabled.stargazers
? ({
get stargazers() {
Expand Down
21 changes: 21 additions & 0 deletions source/plugins/discussions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
### 💬 Discussions

The *discussions* plugin displays your GitHub discussions metrics.

<table>
<td align="center">
<img src="https://github.com/lowlighter/lowlighter/blob/master/metrics.plugin.discussions.svg">
<img width="900" height="1" alt="">
</td>
</table>

#### ℹ️ Examples workflows

[➡️ Available options for this plugin](metadata.yml)

```yaml
- uses: lowlighter/metrics@latest
with:
# ... other options
plugin_discussions: yes
```
46 changes: 46 additions & 0 deletions source/plugins/discussions/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//Setup
export default async function({login, q, imports, graphql, queries, data, account}, {enabled = false} = {}) {
//Plugin execution
try {
//Check if plugin is enabled and requirements are met
if ((!enabled)||(!q.discussions))
return null

//Load inputs
imports.metadata.plugins.discussions.inputs({data, account, q})
const discussions = {categories:{}}

//Fetch general statistics
const stats = Object.fromEntries(Object.entries((await graphql(queries.discussions.statistics({login}))).user).map(([key, value]) => [key, value.totalCount]))
Object.assign(discussions, stats)

//Load started discussions
{
const fetched = []
const categories = {}
let cursor = null
let pushed = 0
do {
console.debug(`metrics/compute/${login}/discussions > retrieving discussions after ${cursor}`)
const {user:{repositoryDiscussions:{edges = [], nodes = []} = {}}} = await graphql(queries.discussions.categories({login, after:cursor ? `after: "${cursor}"` : ""}))
cursor = edges?.[edges?.length - 1]?.cursor
fetched.push(...nodes)
pushed = nodes.length
console.debug(`metrics/compute/${login}/discussions > retrieved ${pushed} discussions after ${cursor}`)
} while ((pushed) && (cursor))

//Compute favorite category
for (const category of [...fetched.map(({category:{emoji, name}}) => `${imports.emoji.get(emoji)} ${name}`)])
categories[category] = (categories[category] ?? 0) + 1
discussions.categories.stats = categories
discussions.categories.favorite = Object.entries(categories).sort((a, b) => b[1] - a[1]).map(([name]) => name).shift() ?? null
}

//Results
return discussions
}
//Handle errors
catch (error) {
throw {error:{message:"An error occured", instance:error}}
}
}
12 changes: 12 additions & 0 deletions source/plugins/discussions/metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: "💬 Discussions"
cost: 1 GraphQL request + 1 GraphQL request per 100 discussions started
category: github
supports:
- user
inputs:

# Enable or disable plugin
plugin_discussions:
description: GitHub discussions metrics
type: boolean
default: no
15 changes: 15 additions & 0 deletions source/plugins/discussions/queries/categories.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
query DiscussionsCategories {
user(login: "$login") {
repositoryDiscussions($after first: 100, orderBy: {field: CREATED_AT, direction: DESC}) {
edges {
cursor
}
nodes {
category {
emoji
name
}
}
}
}
}
13 changes: 13 additions & 0 deletions source/plugins/discussions/queries/statistics.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
query DiscussionsStatistics {
user(login: "$login") {
started: repositoryDiscussions {
totalCount
}
comments: repositoryDiscussionComments {
totalCount
}
answers: repositoryDiscussionComments(onlyAnswers: true) {
totalCount
}
}
}
5 changes: 5 additions & 0 deletions source/plugins/discussions/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- name: Discussions plugin (default)
uses: lowlighter/metrics@latest
with:
token: MOCKED_TOKEN
plugin_discussions: yes
26 changes: 26 additions & 0 deletions source/plugins/repositories/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
### 📓 Repositories

The *repositories* plugin can display a list of chosen featured repositories.

<table>
<td align="center">
<img src="https://github.com/lowlighter/lowlighter/blob/master/metrics.plugin.repositories.svg">
<img width="900" height="1" alt="">
</td>
</table>

It is mostly intended for external usage as [pinned repositories](https://www.google.com/search?client=firefox-b-d&q=github+pinned+repositories) is probably a better alternative if you want to embed them on your profile.

Because of limitations of using SVG inside of `<img>` tags, people won't be able to click on it.

#### ℹ️ Examples workflows

[➡️ Available options for this plugin](metadata.yml)

```yaml
- uses: lowlighter/metrics@latest
with:
# ... other options
plugin_repositories: yes
plugin_repositories_list: lowlighter/metrics, denoland/deno # List of repositories you want to feature
```
38 changes: 38 additions & 0 deletions source/plugins/repositories/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//Setup
export default async function({login, q, imports, graphql, queries, data, account}, {enabled = false} = {}) {
//Plugin execution
try {
//Check if plugin is enabled and requirements are met
if ((!enabled)||(!q.repositories))
return null

//Load inputs
let {featured} = imports.metadata.plugins.repositories.inputs({data, account, q})

//Initialization
const repositories = {list:[]}

//Fetch repositories informations
for (const repo of featured) {
const {owner = login, name} = repo.match(/^(?:(?<owner>[\s\S]*)[/])?(?<name>[\s\S]+)$/)?.groups ?? {}
const {repository} = await graphql(queries.repositories.repository({owner, name}))
repositories.list.push(repository)

//Format date
const time = (Date.now() - new Date(repository.createdAt).getTime()) / (24 * 60 * 60 * 1000)
let created = new Date(repository.createdAt).toDateString().substring(4)
if (time < 1)
created = `${Math.ceil(time * 24)} hour${Math.ceil(time * 24) >= 2 ? "s" : ""} ago`
else if (time < 30)
created = `${Math.floor(time)} day${time >= 2 ? "s" : ""} ago`
repository.created = created
}

//Results
return repositories
}
//Handle errors
catch (error) {
throw {error:{message:"An error occured", instance:error}}
}
}
22 changes: 22 additions & 0 deletions source/plugins/repositories/metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: "📓 Repositories"
cost: 1 GraphQL request per repository
category: github
supports:
- user
- organization
inputs:

# Enable or disable plugin
plugin_repositories:
description: Display chosen featured repositories
type: boolean
default: no

# Featured repositories to display
# If no owner is specified, it will implicitly use the current account login as owner
plugin_repositories_featured:
description: List of repositories to display
type: array
format: comma-separated
default: ""
example: lowlighter/metrics
26 changes: 26 additions & 0 deletions source/plugins/repositories/queries/repository.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
query RepositoriesRepository {
repository(owner: "$owner", name: "$name") {
createdAt
description
forkCount
isFork
issues {
totalCount
}
nameWithOwner
openGraphImageUrl
licenseInfo {
nickname
spdxId
name
}
pullRequests {
totalCount
}
stargazerCount
primaryLanguage {
color
name
}
}
}
6 changes: 6 additions & 0 deletions source/plugins/repositories/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- name: Repositories plugin (default)
uses: lowlighter/metrics@latest
with:
token: MOCKED_TOKEN
plugin_repositories: yes
plugin_repositories_list: metrics
2 changes: 2 additions & 0 deletions source/templates/classic/partials/_.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"languages",
"notable",
"projects",
"repositories",
"gists",
"pagespeed",
"habits",
Expand All @@ -25,6 +26,7 @@
"anilist",
"wakatime",
"skyline",
"discussions",
"support",
"stackoverflow",
"stock",
Expand Down
Loading

0 comments on commit 113d098

Please sign in to comment.