Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ops-trello/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Trello App Key: https://trello.com/app-key
# also: https://developer.atlassian.com/cloud/trello/guides/rest-api/authorization/
TRELLO_APP_KEY=123123123

# Trello Auth Token
# https://trello.com/1/authorize?expiration=1day&name=< Name your App >&scope=read,write&response_type=token&key=< Replace with TRELLO_APP_KEY >
# (authorize Trello App Key to use _your_ account to query Trello API)
TRELLO_AUTH_TOKEN=890890890
15 changes: 15 additions & 0 deletions ops-trello/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
env: {
es6: true,
node: true,
},
extends: ['standard', 'prettier', 'plugin:prettier/recommended'],
parserOptions: {
ecmaVersion: 12,
sourceType: 'module',
},
rules: {
camelcase: 'off',
'prettier/prettier': 'error',
},
}
4 changes: 4 additions & 0 deletions ops-trello/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
node_modules/
local/
.env
1 change: 1 addition & 0 deletions ops-trello/.lintstagedrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "*.js": ["eslint --fix"] }
6 changes: 6 additions & 0 deletions ops-trello/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"singleQuote": true,
"jsxSingleQuote": true,
"trailingComma": "all",
"semi": false
}
19 changes: 19 additions & 0 deletions ops-trello/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# ops-trell

Run `yarn` to setup.

## Archive "elderly" Trello cards

Run script with a board ID. It defaults to a dry run, so script prints what it _would_ do.

```
$ bin/archiveCards.js --board 123123123
```

If results are the results you're looking for, drop dry run.

Pro tip: In case something goes horribly wrong, redirect output into a text file.

```
$ bin/archiveCards.js --board 123123123 --no-dry-run > ./closed.txt
```
109 changes: 109 additions & 0 deletions ops-trello/bin/archiveCards.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#!/usr/bin/env node
require('dotenv').config()

const Promise = require('bluebird')
const fetch = require('cross-fetch')
const moment = require('moment')
const yargs = require('yargs')

const { board, dryRun } = yargs
.option('board', { string: true, demandOption: true, description: 'https://trello.com/b/[ board ]/…' })
.option('dry-run', { boolean: true, default: true })
.argv

// https://developer.atlassian.com/cloud/trello/guides/rest-api/api-introduction/
const TRELLO_ENDPOINT = 'https://api.trello.com/1'

const {
TRELLO_APP_KEY,
TRELLO_AUTH_TOKEN
} = process.env

const isNotTemplate = (card) => !card.isTemplate
const isNotOnList = (idLIst) => (card) => card.idList !== idLIst
const isOlderThan = (than) => (card) =>
moment(card.due || card.dateLastActivity) < than

const run = async () => {
const cards = await fetch(
`${TRELLO_ENDPOINT}/boards/${board}/cards?filter=open&key=${TRELLO_APP_KEY}&token=${TRELLO_AUTH_TOKEN}`,
{ method: 'GET' },
)
.then((res) => {
if (!res.ok) {
throw Error(res.status)
}

return res.json()
})
.catch(e => {
console.warn(e)
return []
})
.then(cards => {
return cards
.filter(isNotTemplate)
.filter(isNotOnList('5bee8e757e178a5c7fffacb5')) // "Vorlage / Ideen"
.filter(isOlderThan(moment().subtract(2, 'months')))
})

console.log({ cards: cards.length })

await Promise.mapSeries(
cards,
async (card) => {
if (dryRun) {
console.log(
'(dry run, would archive)',
card.id,
card.shortUrl,
card.due || card.dateLastActivity,
card.name,
)

return
}

// Trello refers to archiving assets as "closing".
await fetch(
`${TRELLO_ENDPOINT}/cards/${card.id}?closed=true&key=${TRELLO_APP_KEY}&token=${TRELLO_AUTH_TOKEN}`,
{ method: 'PUT' },
)
.catch((e) => {
console.log(
'Error while fetching',
card.id,
card.shortUrl,
card.due || card.dateLastActivity,
card.name,
)

throw e
})
.then((res) => {
if (!res.ok) {
console.log(
'Error from Trello',
card.id,
card.shortUrl,
card.due || card.dateLastActivity,
card.name,
)

throw Error(res.status)
}

console.log(
card.id,
card.shortUrl,
card.due || card.dateLastActivity,
card.name,
)
})
},
)
.catch(console.error)
.finally(() => { console.log('Done.') })
}

run()
18 changes: 18 additions & 0 deletions ops-trello/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"dependencies": {
"bluebird": "^3.7.2",
"cross-fetch": "^3.1.5",
"dotenv": "^16.0.1",
"moment": "^2.29.3",
"yargs": "^17.5.1"
},
"devDependencies": {
"eslint": "^8.0.1",
"eslint-config-prettier": "^8.4.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-promise": "^6.0.0"
}
}
Loading