Skip to content

Commit

Permalink
refactor: update workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
toFrankie committed Aug 11, 2024
1 parent 67e7e25 commit be9a98b
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 30 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/update-readme.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
push:
paths:
- 'docs/templates/readme.md'
- 'docs/traffic-views.svg'

jobs:
update-readme:
Expand All @@ -16,7 +17,7 @@ jobs:
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v2
uses: actions/setup-node@v4
with:
node-version: '20'

Expand All @@ -26,7 +27,7 @@ jobs:
- name: Pull latest changes
run: git pull origin main

- name: Run update script
- name: Run update-readme script
env:
GITHUB_TOKEN: ${{ secrets.WORKFLOW_GITHUB_TOKEN }}
run: node scripts/update-readme.js
Expand All @@ -44,6 +45,8 @@ jobs:
if: env.file_changed == 'true'
env:
GITHUB_TOKEN: ${{ secrets.WORKFLOW_GITHUB_TOKEN }}
GITHUB_USER: toFrankie
GITHUB_REPO: blog
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email '41898282+github-actions[bot]@users.noreply.github.com'
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/update-recent-articles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v2
uses: actions/setup-node@v4
with:
node-version: '20'

Expand All @@ -24,9 +24,11 @@ jobs:
- name: Pull latest changes
run: git pull origin main

- name: Run update script
- name: Run update-recent-articles script
env:
GITHUB_TOKEN: ${{ secrets.WORKFLOW_GITHUB_TOKEN }}
GITHUB_USER: toFrankie
GITHUB_REPO: blog
run: node scripts/update-recent-articles.js

- name: Check if recent-articles.md has changed
Expand Down
49 changes: 49 additions & 0 deletions .github/workflows/update-traffic-views.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Update Traffic Views

on:
schedule:
- cron: '0 */4 * * *'

jobs:
update-badge:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Install dependencies
run: npm install

- name: Pull latest changes
run: git pull origin main

- name: Run update-traffic-views script
env:
GITHUB_TOKEN: ${{ secrets.WORKFLOW_GITHUB_TOKEN }}
GITHUB_USER: toFrankie
GITHUB_REPO: blog
run: node scripts/update-traffic-views.js

- name: Check if traffic-views.svg has changed
id: check_changes
run: |
if git diff --exit-code docs/traffic-views.svg; then
echo "file_changed=false" >> $GITHUB_ENV
else
echo "file_changed=true" >> $GITHUB_ENV
fi
- name: Commit changes
if: env.file_changed == 'true'
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email '41898282+github-actions[bot]@users.noreply.github.com'
git add docs/traffic-views.svg
git commit -m "docs: update traffic views"
git push origin main
1 change: 0 additions & 1 deletion docs/templates/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
- [github-blogger](https://github.com/toFrankie/github-blogger)
- [terminal-dark-theme](https://github.com/toFrankie/terminal-dark-theme)


#### 有趣的链接

- [Developer Roadmap](https://github.com/kamranahmedse/developer-roadmap)
Expand Down
File renamed without changes
File renamed without changes
17 changes: 16 additions & 1 deletion scripts/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const getGithubUser = () => process.env.GITHUB_USER || 'toFrankie'

export const getGithubToken = () => process.env.GITHUB_TOKEN || ''

export async function fetchIssues(state = 'all') {
export async function fetchAllIssue(state = 'all') {
const issues = []
let page = 1
let hasNextPage = true
Expand All @@ -30,6 +30,21 @@ export async function fetchIssues(state = 'all') {
return issues
}

export async function fetchRecentIssues(state = 'all') {
const octokit = new Octokit({ auth: getGithubToken() })

const { data: issues } = await octokit.issues.listForRepo({
owner: getGithubUser(),
repo: getGithubRepo(),
state,
sort: 'updated',
direction: 'desc',
per_page: 10,
})

return issues
}

export async function getTrafficViews() {
try {
const octokit = new Octokit({ auth: getGithubToken() })
Expand Down
4 changes: 2 additions & 2 deletions scripts/fetch-all-issues.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import dayjs from 'dayjs'
import dotenv from 'dotenv'

// eslint-disable-next-line import/extensions
import { fetchIssues } from './common.js'
import { fetchAllIssue } from './common.js'

dotenv.config()

//
;(async function main() {
try {
const issues = await fetchIssues()
const issues = await fetchAllIssue()
console.log(`Fetched ${issues.length} issues`)

for (const issue of issues) {
Expand Down
4 changes: 2 additions & 2 deletions scripts/update-readme.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from 'path'
import dotenv from 'dotenv'

// eslint-disable-next-line import/extensions
import { fetchIssues, getGithubRepo, getGithubUser } from './common.js'
import { fetchAllIssue, getGithubRepo, getGithubUser } from './common.js'

dotenv.config()

Expand All @@ -14,7 +14,7 @@ const OUTPUT_FILE = path.resolve('README.md')
;(async function main() {
let templateContent = await fs.readFile(TEMPLATE_FILE, 'utf-8')

const issues = await fetchIssues()
const issues = await fetchAllIssue()

const yearLinks = await genYearLinks(issues)

Expand Down
25 changes: 5 additions & 20 deletions scripts/update-recent-articles.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
import fs from 'node:fs/promises'
import path from 'node:path'

import { Octokit } from '@octokit/rest'
import dotenv from 'dotenv'

const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN,
})

const owner = 'toFrankie'
const repo = 'blog'
// eslint-disable-next-line import/extensions
import { fetchRecentIssues } from './common.js'

async function getRecentIssues() {
const { data: issues } = await octokit.rest.issues.listForRepo({
owner,
repo,
state: 'all',
sort: 'updated',
direction: 'desc',
per_page: 10,
})

return issues
}
dotenv.config()

async function updateRecentArticles() {
const issues = await getRecentIssues()
const issues = await fetchRecentIssues()

const lines = ['# 近期更新', '']

Expand Down
File renamed without changes.

0 comments on commit be9a98b

Please sign in to comment.