Skip to content

Commit

Permalink
Merge branch 'master' into release/v1
Browse files Browse the repository at this point in the history
  • Loading branch information
sarisia committed Sep 2, 2020
2 parents b6ff9d4 + 0f1259d commit 97d816d
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ The result looks like:
| :-: | :-: | :-: | - | - |
| `url` | Yes | String | | URL of RSS feed (XML) |
| `file` | Yes | String | | Path to document file to process.<br>Can be relative path from repository root, or absolute path of Actions Runner. |
| `sort` | | Boolean | `true` | Sort feed entries by date in decending order. |
| `max_entry` | | Number | `5` | Number of feed entries to show |
| `format` | | String | `- ${monthshort} ${02day} - [${title}](${url})` | Feed entry format string.<br>See [Formatting](#formatting) for details. |
| `start_flag` | | String | `<!-- feed start -->` | Flag string to declare start of feed block |
Expand Down Expand Up @@ -140,8 +141,7 @@ steps:
with:
url: 'https://blog.example.com/feed.xml'
file: 'README.md'
- uses: sarisia/step-to-run-if-changed@master
if: ${{ steps.feed.outputs.changed == true }}
- if: ${{ steps.feed.outputs.changed == true }}
run: echo "changed!"
```

Expand Down
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ inputs:
file:
description: 'Path to Markdown file to update'
required: true
sort:
description: 'Sort feed entries by date in decending order'
required: false
default: 'true'
max_entry:
description: 'Number of entries to show'
required: false
default: 5
default: '5'
format:
description: 'Feed format string'
required: false
Expand Down
23 changes: 22 additions & 1 deletion src/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,32 @@ const parser = new Parser({
timeout: 5000
})

export async function getFeedItems(url:string): Promise<Array<rss.Item>> {
export async function getFeedItems(url:string): Promise<rss.Item[]> {
const feed = await parser.parseURL(url)
if (!feed.items) {
throw new Error('item not found in feed.')
}

return feed.items
}

export function sortItems(items: rss.Item[]): rss.Item[] {
// to avoid instantiate `Date` every time,
// we make sort map first
const mapped = items.map((v, i) => {
if (!v.isoDate) {
throw new Error('cannot perform sort without `date` in your feed')
}

return {
index: i,
date: new Date(v.isoDate)
}
})

mapped.sort((a, b) =>
b.date.getTime() - a.date.getTime()
)

return mapped.map((v) => items[v.index])
}
13 changes: 12 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as core from '@actions/core'
import * as rss from 'rss-parser'
import { getFeedItems } from './feed'
import { getFeedItems, sortItems } from './feed'
import { getLines, writeLines, updateFeed, isChanged } from './markdown'
import { formatFeeds } from './format'

Expand All @@ -18,6 +18,7 @@ async function run() {
return
}

const sort = core.getInput('sort').toLowerCase() === 'true'
const maxEntry = parseInt(core.getInput('max_entry')) || 5
// DO NOT USE core.getInput cuz it trims spaces / breaks at the end of line
const format = process.env['INPUT_FORMAT'] || '- ${monthshort} ${02day} - [${title}](${url})'
Expand Down Expand Up @@ -52,6 +53,16 @@ async function run() {
return
}

// sort
if (sort) {
try {
allItems = sortItems(allItems)
} catch (e) {
core.error(`failed to sort feed items: ${e.message}`)
return
}
}

// construct feed lines
const items = allItems.slice(0, maxEntry)
const newLines = formatFeeds(items, format, startFlag, endFlag)
Expand Down
135 changes: 135 additions & 0 deletions tests/feed.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { sortItems } from '../src/feed'
import rss from 'rss-parser'

describe('sortItems', () => {
test('2 items', () => {
const feeds: Array<rss.Item> = [
{
link: "https://blog.example.com/entry1",
title: "example blog entry",
isoDate: "2015-11-12T21:16:39.000Z"
},
{
link: "https://blog.example.com/entry2",
title: "example blog entry 2",
isoDate: "2020-08-01T00:11:22.000Z"
}
]
const want = [
{
link: "https://blog.example.com/entry2",
title: "example blog entry 2",
isoDate: "2020-08-01T00:11:22.000Z"
},
{
link: "https://blog.example.com/entry1",
title: "example blog entry",
isoDate: "2015-11-12T21:16:39.000Z"
}
]

const got = sortItems(feeds)
expect(got).toStrictEqual(want)
})

test('3 items', () => {
const feeds: Array<rss.Item> = [
{
link: "https://blog.example.com/entry1",
title: "example blog entry",
isoDate: "2015-11-12T21:16:39.000Z"
},
{
link: "https://blog.example.com/entry3",
title: "example blog entry 3",
isoDate: "2020-08-01T00:11:22.000Z"
},
{
link: "https://blog.example.com/entry2",
title: "example blog entry 2",
isoDate: "2019-12-10T00:11:22.000Z"
}
]
const want: Array<rss.Item> = [
{
link: "https://blog.example.com/entry3",
title: "example blog entry 3",
isoDate: "2020-08-01T00:11:22.000Z"
},
{
link: "https://blog.example.com/entry2",
title: "example blog entry 2",
isoDate: "2019-12-10T00:11:22.000Z"
},
{
link: "https://blog.example.com/entry1",
title: "example blog entry",
isoDate: "2015-11-12T21:16:39.000Z"
}
]

const got = sortItems(feeds)
expect(got).toStrictEqual(want)
})

test('mixed timezone', () => {
const feeds: Array<rss.Item> = [
{
link: "https://blog.example.com/pdt",
title: "example blog entry pdt",
isoDate: "2019-12-10T00:11:22.000-07:00"
},
{
link: "https://blog.example.com/jst",
title: "example blog entry jst",
isoDate: "2019-12-10T00:11:22.000+09:00"
},
{
link: "https://blog.example.com/entry2",
title: "example blog entry 2",
isoDate: "2019-12-10T00:11:22.000Z"
}
]
const want: Array<rss.Item> = [
{
link: "https://blog.example.com/pdt",
title: "example blog entry pdt",
isoDate: "2019-12-10T00:11:22.000-07:00"
},
{
link: "https://blog.example.com/entry2",
title: "example blog entry 2",
isoDate: "2019-12-10T00:11:22.000Z"
},
{
link: "https://blog.example.com/jst",
title: "example blog entry jst",
isoDate: "2019-12-10T00:11:22.000+09:00"
}
]

const got = sortItems(feeds)
expect(got).toStrictEqual(want)
})

test('no date in feed', () => {
const feeds: Array<rss.Item> = [
{
link: "https://blog.example.com/pdt",
title: "example blog entry pdt",
isoDate: "2019-12-10T00:11:22.000-07:00"
},
{
link: "https://blog.example.com/jst",
title: "example blog entry jst",
isoDate: "2019-12-10T00:11:22.000+09:00"
},
{
link: "https://blog.example.com/entry2",
title: "example blog entry 2"
}
]

expect(() => { sortItems(feeds) }).toThrow()
})
})

0 comments on commit 97d816d

Please sign in to comment.