Skip to content

Commit 53ae695

Browse files
done
Signed-off-by: Tsung-Ju Lii <usefulalgorithm@gmail.com>
1 parent 113e246 commit 53ae695

File tree

3 files changed

+85
-19
lines changed

3 files changed

+85
-19
lines changed

.github/scripts/pull_album_info/app/Main.hs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import System.Environment (getArgs, getProgName, lookupEnv)
1313
-- Third-party library imports
1414
import Control.Lens (Identity (runIdentity), (^?))
1515
import Data.Aeson (FromJSON (parseJSON), ToJSON,
16-
Value (Object), decodeStrict, encode,
17-
(.:))
16+
Value (Object), decodeStrict, (.:))
1817
import Data.Aeson.Lens (AsNumber (_Integer), key, nth)
1918
import Data.ByteString (ByteString)
2019
import qualified Data.ByteString.Char8 as BS
@@ -28,6 +27,7 @@ import System.FilePath (takeDirectory)
2827
import Text.Ginger (IncludeResolver, SourcePos, Template,
2928
ToGVal (..), dict, easyRender,
3029
parseGinger)
30+
import Control.Exception (try, SomeException)
3131

3232
-- Data type definitions
3333
data MainRelease = MainRelease {
@@ -123,25 +123,36 @@ getMainRelease releaseId = do
123123
nullResolver :: IncludeResolver Identity
124124
nullResolver = const $ return Nothing
125125

126-
-- | This is our template. Because 'parseGinger' wants a monad (as loading
127-
-- includes would normally go through some sort of monadic API like 'IO'), we
128-
-- use 'Identity' here.
129126
getTemplate :: String -> Template SourcePos
130127
getTemplate content = either (error . show) id . runIdentity $
131128
parseGinger nullResolver Nothing content
132129

130+
templatePath :: IO String
131+
templatePath = do
132+
progName <- getProgName
133+
return $ takeDirectory progName ++ "/app/templates/post.md"
134+
135+
runGenAlbumPost :: String -> String -> IO String
136+
runGenAlbumPost artistName albumName = do
137+
-- Get the MainRelease of the album
138+
release <- getMasterReleaseId artistName albumName
139+
>>= getMainReleaseId
140+
>>= getMainRelease
141+
content <- templatePath >>= readFile
142+
return $ T.unpack . easyRender release $ getTemplate content
143+
133144
-- Main function
134145
main :: IO ()
135146
main = do
136147
args <- getArgs
137148
case args of
138-
[artistName, albumName] -> do
139-
release <- getMasterReleaseId artistName albumName
140-
>>= getMainReleaseId
141-
>>= getMainRelease
142-
putStrLn $ BS.unpack $ BS.toStrict $ encode release
143-
content <- getProgName >>= readFile . (++ "/app/templates/post.md") . takeDirectory
144-
let template = getTemplate content
145-
let output = T.unpack $ easyRender release template
146-
putStrLn output
147-
_ -> putStrLn "Usage: pull_album_info <artist_name> <album_name>"
149+
[artistName, albumName, branchName] -> do
150+
result <- try $ runGenAlbumPost artistName albumName :: IO (Either SomeException String)
151+
post <- case result of
152+
Left _ -> do
153+
_ <- putStrLn "Cannot get album info from Discog, falling back to default post template"
154+
templatePath >>= readFile
155+
Right output -> return output
156+
writeFile branchName post
157+
putStrLn "done"
158+
_ -> putStrLn "Usage: pull_album_info <artist_name> <album_name> <branch_name>"

.github/scripts/pull_album_info/app/templates/post.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ tags: {{year}}
88
![{{imageUrl}}]({{imageUrl}})
99

1010
<!--
11-
Whatever you write here will be the post.
11+
Write your post here!
1212
-->
1313

1414
---
@@ -17,8 +17,6 @@ Fav tracks:
1717

1818
Score: /10
1919

20-
Released: {{released}}
20+
Release date: {{released}}
2121

2222
Labels: {{labels}}
23-
24-
Bandcamp: []()
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: Create Album Post
2+
description: Creates a PR to add a new album post to the blog.
3+
4+
on:
5+
workflow_dispatch:
6+
inputs:
7+
album_title:
8+
type: string
9+
required: true
10+
description: Title of the album.
11+
artist_name:
12+
type: string
13+
required: true
14+
description: Name of the artist.
15+
16+
jobs:
17+
create-branch-and-pr:
18+
runs-on: ubuntu-latest
19+
20+
steps:
21+
- name: Checkout repository
22+
uses: actions/checkout@v4
23+
24+
- name: Setup Haskell
25+
uses: haskell-actions/setup@v2.7.10
26+
with:
27+
cabal-version: 2.4
28+
ghc-version: 9.4.8
29+
30+
- name: Pull album info
31+
id: pull_album_info
32+
run: |
33+
BRANCH_NAME=$(echo "${{ github.event.inputs.album_title }}-${{ github.event.inputs.artist_name }}" | tr '[:upper:]' '[:lower:]' | tr -cd 'a-z0-9-')
34+
35+
# Build album template post
36+
cd .github/scripts/pull_album_info
37+
cabal build
38+
cabal run pull-album-info ${{ github.event.inputs.artist_name }} ${{ github.event.inputs.album_title }} $BRANCH_NAME
39+
mv $BRANCH_NAME ${{ github.workspace }}/drafts/$BRANCH_NAME
40+
41+
# Switch to bot account
42+
git config --global user.name 'github-actions[bot]'
43+
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
44+
45+
# Set branch name to output
46+
echo "branch_name=$branch_name" >> $GITHUB_OUTPUT
47+
48+
- name: Create Pull Request
49+
id: create_pr
50+
uses: peter-evans/create-pull-request@v7
51+
with:
52+
token: ${{ secrets.GITHUB_TOKEN }}
53+
branch: ${{ steps.pull_album_info.outputs.branch_name }}
54+
base: main
55+
title: post/${{ steps.pull_album_info.outputs.branch_name }}
56+
body-path: '${{ github.workspace }}/drafts/${{ steps.pull_album_info.outputs.branch_name }}'
57+
labels: 'post'

0 commit comments

Comments
 (0)