The great refactor (#82)

This commit is contained in:
Simon Lecoq
2021-01-30 12:31:09 +01:00
committed by GitHub
parent f8c6d19a4e
commit 682e43e10b
158 changed files with 6738 additions and 5022 deletions

View File

@@ -0,0 +1,36 @@
### 🐤 Tweets
The recent *tweets* plugin displays your latest tweets from your [Twitter](https://twitter.com) account.
<table>
<td align="center">
<img src="https://github.com/lowlighter/lowlighter/blob/master/metrics.plugin.tweets.svg">
<img width="900" height="1" alt="">
</td>
</table>
<details>
<summary>💬 Obtaining a Twitter token</summary>
To get a Twitter token, you'll need to apply to the [developer program](https://apps.twitter.com).
It's a bit tedious, but it seems that requests are approved quite quickly.
Create an app from your [developer dashboard](https://developer.twitter.com/en/portal/dashboard) and register your bearer token in your repository secrets.
![Twitter token](/.github/readme/imgs/plugin_tweets_secrets.png)
</details>
#### Examples workflows
[➡️ Available options for this plugin](metadata.yml)
```yaml
- uses: lowlighter/metrics@latest
with:
# ... other options
plugin_tweets: yes
plugin_tweets_token: ${{ secrets.TWITTER_TOKEN }} # Required
plugin_tweets_limit: 2 # Limit to 2 tweets
plugin_tweets_user: .user.twitter # Defaults to your GitHub linked twitter username
```

View File

@@ -1,30 +1,34 @@
//Setup
export default async function ({login, imports, data, q}, {enabled = false, token = null} = {}) {
export default async function ({login, imports, data, q, account}, {enabled = false, token = ""} = {}) {
//Plugin execution
try {
//Check if plugin is enabled and requirements are met
if ((!enabled)||(!q.tweets))
return null
//Parameters override
let {"tweets.limit":limit = 2, "tweets.user":username = data.user.twitterUsername} = q
//Limit
limit = Math.max(1, Math.min(10, Number(limit)))
//Load inputs
let {limit, user:username} = imports.metadata.plugins.tweets.inputs({data, account, q})
//Load user profile
console.debug(`metrics/compute/${login}/plugins > tweets > loading twitter profile (@${username})`)
const {data:{data:profile = null}} = await imports.axios.get(`https://api.twitter.com/2/users/by/username/${username}?user.fields=profile_image_url,verified`, {headers:{Authorization:`Bearer ${token}`}})
//Load tweets
console.debug(`metrics/compute/${login}/plugins > tweets > querying api`)
const {data:{data:tweets = []}} = await imports.axios.get(`https://api.twitter.com/2/tweets/search/recent?query=from:${username}&tweet.fields=created_at&expansions=entities.mentions.username`, {headers:{Authorization:`Bearer ${token}`}})
//Load profile image
if (profile?.profile_image_url) {
console.debug(`metrics/compute/${login}/plugins > tweets > loading profile image`)
profile.profile_image = await imports.imgb64(profile.profile_image_url)
}
//Load tweets
console.debug(`metrics/compute/${login}/plugins > tweets > querying api`)
const {data:{data:tweets = []}} = await imports.axios.get(`https://api.twitter.com/2/tweets/search/recent?query=from:${username}&tweet.fields=created_at&expansions=entities.mentions.username`, {headers:{Authorization:`Bearer ${token}`}})
//Limit tweets
if (limit > 0) {
console.debug(`metrics/compute/${login}/plugins > tweets > keeping only ${limit} tweets`)
tweets.splice(limit)
}
//Format tweets
await Promise.all(tweets.map(async tweet => {
//Mentions
@@ -44,6 +48,7 @@
.replace(/https?:[/][/](t.co[/]\w+)/g, ` <span class="link">$1</span> `)
, {"&":true})
}))
//Result
return {username, profile, list:tweets}
}

View File

@@ -0,0 +1,33 @@
name: "🐤 Latest tweets"
cost: N/A
supports:
- user
- organization
inputs:
# Enable or disable plugin
plugin_tweets:
description: Display recent tweets
type: boolean
default: no
# Twitter API token
# See https://apps.twitter.com for more informations
plugin_tweets_token:
description: Twitter API token
type: token
default: ""
# Number of tweets to display
plugin_tweets_limit:
description: Maximum number of tweets to display
type: number
default: 2
min: 1
max: 10
# Twitter username
plugin_tweets_user:
description: Twitter username
type: string
default: .user.twitter