Commit 2faeaf3

Anton Golub <antongolub@antongolub.com>
2024-12-04 15:35:53
refactor: assert ghUser & ghToken values (#87)
* refactor: assert ghUser & ghToken values * feat: extract username from datafile name
1 parent 1e78aee
src/context.ts
@@ -1,7 +1,4 @@
 import minimist from 'minimist'
-import { Octokit } from 'octokit'
-import { retry } from '@octokit/plugin-retry'
-import { throttling } from '@octokit/plugin-throttling'
 import path from 'node:path'
 import fs from 'node:fs'
 
@@ -25,7 +22,6 @@ export type Context = {
   ghRepoName: string
   ghUser: string
   ghToken: string
-  octokit: Octokit
 
   dryrun: boolean
   badgesCompact: boolean
@@ -62,11 +58,13 @@ export function createCtx(
     params: taskParams,
     'skip-task': taskSkip = '',
   } = argv
-  const octokit = getOctokit(ghToken)
   const cwd = path.resolve(_cwd)
   const dataDir = path.resolve(cwd, DATA_DIR)
   const dataFile = path.resolve(dataDir, data || `${ghUser}.json`)
-  const dataTasks = path.resolve(dataDir, `${ghUser}.tasks.json`)
+  const dataTasks = path.resolve(
+    dataDir,
+    `${ghUser || path.parse(dataFile).name}.tasks.json`,
+  )
   const gitDir = path.resolve(cwd, GIT_DIR)
   const badgesDir = path.resolve(gitDir, BADGES_DIR)
   const badgesDatafile = path.resolve(badgesDir, data || BADGES_DATAFILE)
@@ -82,7 +80,6 @@ export function createCtx(
 
   return {
     cwd,
-    octokit,
     gitName: GIT_NAME,
     gitEmail: GIT_EMAIL,
     gitDir,
@@ -105,33 +102,3 @@ export function createCtx(
     taskSkip,
   }
 }
-
-const MyOctokit = Octokit.plugin(retry, throttling)
-
-function getOctokit(token: string) {
-  return new MyOctokit({
-    auth: token,
-    log: console,
-    throttle: {
-      onRateLimit: (retryAfter, options: any, octokit, retryCount) => {
-        octokit.log.warn(
-          `Request quota exhausted for request ${options.method} ${options.url}`,
-        )
-        if (retryCount <= 3) {
-          octokit.log.info(`Retrying after ${retryAfter} seconds!`)
-          return true
-        }
-      },
-      onSecondaryRateLimit: (retryAfter, options: any, octokit, retryCount) => {
-        octokit.log.warn(
-          `SecondaryRateLimit detected for request ${options.method} ${options.url}`,
-        )
-        if (retryCount <= 3) {
-          octokit.log.info(`Retrying after ${retryAfter} seconds!`)
-          return true
-        }
-      },
-    },
-    retry: { doNotRetry: ['429'] },
-  })
-}
src/process-tasks.ts
@@ -6,14 +6,15 @@ import allTasks from './task/index.js'
 
 import { type Context } from './context.js'
 import { createBatcher } from './batch.js'
+import { getOctokit } from './utils.js'
 
 const MAX_ATTEMPTS = 3
 
 export async function processTasks(
   ctx: Pick<
     Context,
-    | 'octokit'
     | 'ghUser'
+    | 'ghToken'
     | 'dataDir'
     | 'dataFile'
     | 'dataTasks'
@@ -23,7 +24,7 @@ export async function processTasks(
   >,
 ): Promise<[boolean, Data]> {
   const {
-    octokit,
+    ghToken,
     ghUser: username,
     dataFile,
     dataTasks,
@@ -31,7 +32,13 @@ export async function processTasks(
     taskName,
     taskParams,
   } = ctx
+
+  if (!ghToken) throw new Error('GitHub token is required for data gathering')
+  if (!username)
+    throw new Error('GitHub username is required for data gathering')
+
   const taskSkipSet = new Set(taskSkip?.split(',') || [])
+  const octokit = getOctokit(ghToken)
 
   let data: Data = {
     user: null!,
src/utils.ts
@@ -1,12 +1,43 @@
-import { spawnSync } from 'node:child_process'
 import { Octokit } from 'octokit'
 import { Query, Variables } from 'megaera'
 import { Commit } from './task/commits/commits.graphql.js'
 import { PullRequest } from './task/pulls/pulls.graphql.js'
 import { Issue } from './task/issues/issues.graphql.js'
+import { retry } from '@octokit/plugin-retry'
+import { throttling } from '@octokit/plugin-throttling'
 
 export { $, type TShellSync } from 'zurk'
 
+const MyOctokit = Octokit.plugin(retry, throttling)
+
+export function getOctokit(token: string) {
+  return new MyOctokit({
+    auth: token,
+    log: console,
+    throttle: {
+      onRateLimit: (retryAfter, options: any, octokit, retryCount) => {
+        octokit.log.warn(
+          `Request quota exhausted for request ${options.method} ${options.url}`,
+        )
+        if (retryCount <= 3) {
+          octokit.log.info(`Retrying after ${retryAfter} seconds!`)
+          return true
+        }
+      },
+      onSecondaryRateLimit: (retryAfter, options: any, octokit, retryCount) => {
+        octokit.log.warn(
+          `SecondaryRateLimit detected for request ${options.method} ${options.url}`,
+        )
+        if (retryCount <= 3) {
+          octokit.log.info(`Retrying after ${retryAfter} seconds!`)
+          return true
+        }
+      },
+    },
+    retry: { doNotRetry: ['429'] },
+  })
+}
+
 export function query<T extends Query>(
   octokit: Octokit,
   query: T,
@@ -52,21 +83,6 @@ export function parseMask(value: string): RegExp {
   return new RegExp(`^${value}$`.replace('*', '.+'))
 }
 
-export function exec(command: string, args: string[]): void {
-  const p = spawnSync(command, args, { stdio: 'inherit' })
-  if (p.status !== 0) {
-    throw new Error(`Command failed: ${command} ${args.join(' ')}`)
-  }
-}
-
-export function execWithOutput(command: string, args: string[]): string {
-  const p = spawnSync(command, args, { stdio: 'pipe' })
-  if (p.status !== 0) {
-    throw new Error(`Command failed: ${command} ${args.join(' ')}`)
-  }
-  return p.stdout.toString()
-}
-
 export function latest(a: Commit, b: Commit) {
   return (
     new Date(b.committedDate).getTime() - new Date(a.committedDate).getTime()