Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiteMinds committed Jan 25, 2024
1 parent edeab5d commit e13e961
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 166 deletions.
17 changes: 15 additions & 2 deletions .github/workflows/update_report_snapshot.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: Update Report Snapshot

on:
push:
branches: [feat/338_WeeklyReportScript]
workflow_dispatch:
schedule:
- cron: '0 0 * * 1'
Expand All @@ -19,12 +21,17 @@ jobs:
ref: ${{ github.ref }}

- name: report:update
id: report_update
env:
# The script requires public_repo, read:org, read:project permissions, and needs a personal access token (classic) to obtain these permissions.
GITHUB_TOKEN: ${{ secrets.REPORT_GITHUB_TOKEN }}
run: |
yarn install
yarn workspace @magickbase-website/scripts report:update
{
echo 'DEVLOG<<EOF'
yarn workspace @magickbase-website/scripts report:update | sed '0,/^generateDevlogFromSnapshotsDiff():$/d'
echo EOF
} >> $GITHUB_OUTPUT
git add packages/scripts/snapshots
- name: Set GPG
Expand All @@ -40,6 +47,12 @@ jobs:
with:
title: Update Report Snapshot
commit-message: 'feat: update report snapshot'
body: 'After this PR is merged, a corresponding discussion will be automatically created'
body: '
After this PR is merged, a corresponding discussion will be automatically created
---
${{ steps.report_update.outputs.DEVLOG }}
'
committer: ${{ vars.COMMITTER }}
branch: update-report-snapshot
36 changes: 36 additions & 0 deletions packages/scripts/snapshots/current.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"CKB Explorer": [
{
"id": "PVTI_lADOBjLfC84AMiKwzgFHFUU",
"content": {
"title": "Make workflow of m-nft synchronization clear and maybe provide a standalone service",
"number": 49,
"url": "https://github.com/Magickbase/ckb-explorer-public-issues/issues/49"
},
"fieldValues": {
"nodes": [
{ "field": { "name": "Assignees" } },
{
"repository": {
"name": "ckb-explorer-public-issues",
"url": "https://github.com/Magickbase/ckb-explorer-public-issues"
},
"field": { "name": "Repository" }
},
{
"labels": {
"nodes": [{ "id": "LA_kwDOHFQx5M7tEEBJ", "name": "enhancement", "description": "New feature or request" }]
},
"field": { "name": "Labels" }
},
{
"text": "Make workflow of m-nft synchronization clear and maybe provide a standalone service",
"field": { "name": "Title" }
},
{ "name": "🆕 New", "field": { "name": "Status" } },
{ "name": "🏝 Low", "field": { "name": "Priority" } }
]
}
}
]
}
36 changes: 36 additions & 0 deletions packages/scripts/snapshots/prev.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"CKB Explorer": [
{
"id": "PVTI_lADOBjLfC84AMiKwzgFHFUU",
"content": {
"title": "Make workflow of m-nft synchronization clear and maybe provide a standalone service",
"number": 49,
"url": "https://github.com/Magickbase/ckb-explorer-public-issues/issues/49"
},
"fieldValues": {
"nodes": [
{ "field": { "name": "Assignees" } },
{
"repository": {
"name": "ckb-explorer-public-issues",
"url": "https://github.com/Magickbase/ckb-explorer-public-issues"
},
"field": { "name": "Repository" }
},
{
"labels": {
"nodes": [{ "id": "LA_kwDOHFQx5M7tEEBJ", "name": "enhancement", "description": "New feature or request" }]
},
"field": { "name": "Labels" }
},
{
"text": "Make workflow of m-nft synchronization clear and maybe provide a standalone service",
"field": { "name": "Title" }
},
{ "name": "🆕 New", "field": { "name": "Status" } },
{ "name": "🏝 Low", "field": { "name": "Priority" } }
]
}
}
]
}
140 changes: 2 additions & 138 deletions packages/scripts/src/compare_report_snapshot.ts
Original file line number Diff line number Diff line change
@@ -1,140 +1,4 @@
import './prepare'
import { join } from 'path'
import { mkdirSync, readFileSync } from 'fs'
import { ProjectItem, createDiscussion } from './utils/github'
import { assert } from './utils/error'
import { createDevlogDiscussion } from './utils/report'

const sortedStatusValues = [
'🆕 New',
'📫Hold On',
'📋 Backlog',
'📌Planning',
'🎨 Designing',
'🏗 In Progress',
'🔎 Code Review',
'👀 Testing',
'🚩Pre Release',
'✅ Done',
]

const folder = join(process.cwd(), 'snapshots')
mkdirSync(folder, { recursive: true })

const currentFilepath = join(folder, 'current.json')
const prevFilepath = join(folder, 'prev.json')

const currentProjectItemsMap = JSON.parse(readFileSync(currentFilepath).toString()) as Record<string, ProjectItem[]>
const prevProjectItemsMap = JSON.parse(readFileSync(prevFilepath).toString()) as Record<string, ProjectItem[]>

function itemsToItemMap(items: ProjectItem[]): Record<ProjectItem['id'], ProjectItem> {
const map: Record<ProjectItem['id'], ProjectItem> = {}
for (const item of items) {
map[item.id] = item
}
return map
}

function getField(item: ProjectItem, filedName: string) {
return item.fieldValues.nodes.find(n => n.field.name === filedName)
}

let devLog = ''

for (const [title, currentItems] of Object.entries(currentProjectItemsMap)) {
const prevItems = prevProjectItemsMap[title]
if (!prevItems) continue

const currentMap = itemsToItemMap(currentItems)
const prevMap = itemsToItemMap(prevItems)

const newItems: ProjectItem[] = []
const update: ProjectItem[] = []
const done: ProjectItem[] = []

for (const id of Object.keys(currentMap)) {
const currentItem = currentMap[id]
const prevItem = prevMap[id]
assert(currentItem)

const hasAnyChange = !prevItem || JSON.stringify(currentItem) !== JSON.stringify(prevItem)
const hasStatusChange = !prevItem || getField(currentItem, 'Status')?.name !== getField(prevItem, 'Status')?.name
if (!hasAnyChange) continue

const currentStatus = getField(currentItem, 'Status')?.name ?? ''
switch (currentStatus) {
case '🆕 New':
newItems.push(currentItem)
break
case '🚩Pre Release':
case '✅ Done':
const prevStatus = prevItem == null ? '' : getField(prevItem, 'Status')?.name ?? ''
const prevIsDone = ['🚩Pre Release', '✅ Done'].includes(prevStatus)
if (hasStatusChange && !prevIsDone) {
done.push(currentItem)
}
break
default:
update.push(currentItem)
break
}
}

;[newItems, update, done].forEach(items =>
items.sort((a, b) => {
const aStatus = getField(a, 'Status')?.name ?? ''
const bStatus = getField(b, 'Status')?.name ?? ''
return sortedStatusValues.indexOf(aStatus) - sortedStatusValues.indexOf(bStatus)
}),
)

const getStatusChangeLog = (item: ProjectItem) => {
const currentStatus = getField(item, 'Status')?.name
const prevItem = prevMap[item.id]
const prevStatus = prevItem == null ? undefined : getField(prevItem, 'Status')?.name
if (prevStatus == null || currentStatus === prevStatus) return ` [${currentStatus}]`
return ` [${prevStatus} -> ${currentStatus}]`
}

const getContentURL = (item: ProjectItem) =>
item.content.url != null ? ` [#${item.content.number}](${item.content.url})` : ''

const getDescription = (item: ProjectItem) => {
const desc = getField(item, 'Description')?.text
return desc != null ? ` [${desc}]` : ''
}

devLog += `# ${title}\n\n`

if (newItems.length > 0) {
devLog += '## [NEW]\n\n'
newItems.forEach((item, idx) => {
devLog += `${idx + 1}. ${item.content.title}${getContentURL(item)}${getDescription(item)}\n`
})
devLog += '\n'
}

if (update.length > 0) {
devLog += '## [UPDATE]\n\n'
update.forEach((item, idx) => {
devLog += `${idx + 1}.${getStatusChangeLog(item)} ${item.content.title}${getContentURL(item)}${getDescription(
item,
)}\n`
})
devLog += '\n'
}

if (done.length > 0) {
devLog += '## [DONE]\n\n'
done.forEach((item, idx) => {
devLog += `${idx + 1}. [${getField(item, 'Status')?.name}] ${item.content.title}${getContentURL(
item,
)}${getDescription(item)}\n`
})
}

devLog += '\n'
}

createDiscussion('Magickbase', 'shaping', 'Dev Log', `Dev Log ${new Date().toISOString().slice(0, 10)}`, devLog)
.then(res => console.log('Dev log created', res.id))
.catch(console.error)
await createDevlogDiscussion()
28 changes: 4 additions & 24 deletions packages/scripts/src/update_report_snapshot.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
import './prepare'
import { join } from 'path'
import { existsSync, mkdirSync, renameSync, writeFileSync } from 'fs'
import { ProjectItem, getOrganizationProjects, getProjectItems } from './utils/github'
import { assert } from './utils/error'
import { generateDevlogFromSnapshotsDiff, updateSnapshots } from './utils/report'

const projectNames = ['Neuron', 'CKB Explorer']
const projects = await getOrganizationProjects('Magickbase')
const filteredProjects = projects.filter(p => projectNames.includes(p.title))
assert(filteredProjects.length === projectNames.length)

const projectItemsMap: Record<string, ProjectItem[]> = {}
for (const project of filteredProjects) {
projectItemsMap[project.title] = await getProjectItems(project.id)
}

const folder = join(process.cwd(), 'snapshots')
mkdirSync(folder, { recursive: true })

const currentFilepath = join(folder, 'current.json')
const prevFilepath = join(folder, 'prev.json')

if (existsSync(currentFilepath)) {
renameSync(currentFilepath, prevFilepath)
}
writeFileSync(currentFilepath, JSON.stringify(projectItemsMap))
await updateSnapshots()
console.log('generateDevlogFromSnapshotsDiff():')
console.log(generateDevlogFromSnapshotsDiff())
8 changes: 8 additions & 0 deletions packages/scripts/src/utils/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { existsSync, mkdirSync } from 'fs'
import { dirname } from 'path'

export function ensureFileFolderExists(filePath: string) {
const folder = dirname(filePath)
if (existsSync(folder)) return
mkdirSync(folder, { recursive: true })
}
4 changes: 2 additions & 2 deletions packages/scripts/src/utils/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export interface ProjectItem {
}

export async function getProjectItems(id: string) {
const res = await octokit.graphql.paginate<{
const res = await octokit.graphql<{
node: {
items: {
nodes: ProjectItem[]
Expand All @@ -145,7 +145,7 @@ export async function getProjectItems(id: string) {
query($cursor: String, $id: ID!) {
node(id: $id) {
... on ProjectV2 {
items(first: 20, after: $cursor) {
items(first: 5, after: $cursor) {
nodes {
id
content {
Expand Down
Loading

0 comments on commit e13e961

Please sign in to comment.