UNPKG

@yuki-no/plugin-sdk

Version:

A GitHub Action that tracks changes between repositories. It creates GitHub issues based on commits from a head repository, making it ideal for documentation translation projects.

77 lines (76 loc) 2.85 kB
import { getBooleanInput, getInput, getMultilineInput } from './utils/input'; import { log } from './utils/log'; import path from 'node:path'; export const defaults = { userName: 'github-actions', email: 'action@github.com', branch: 'main', label: 'sync', }; export const createConfig = () => { log('I', 'createConfig :: Parsing configuration values'); // Required values validation const accessToken = getInput('ACCESS_TOKEN'); const headRepo = getInput('HEAD_REPO'); const trackFrom = getInput('TRACK_FROM'); assert(!!accessToken, '`accessToken` is required.'); assert(!!headRepo, '`headRepo` is required.'); assert(!!trackFrom, '`trackFrom` is required.'); // Optional values with defaults const userName = getInput('USER_NAME', defaults.userName); const email = getInput('EMAIL', defaults.email); const upstreamRepo = getInput('UPSTREAM_REPO'); const headRepoBranch = getInput('HEAD_REPO_BRANCH', defaults.branch); const upstreamRepoSpec = createRepoSpec(upstreamRepo || inferUpstreamRepo(), defaults.branch); const headRepoSpec = createRepoSpec(headRepo, headRepoBranch); const include = getMultilineInput('INCLUDE'); const exclude = getMultilineInput('EXCLUDE'); const labels = getMultilineInput('LABELS', [defaults.label]); const sortedLabels = labels.sort(); const plugins = getMultilineInput('PLUGINS'); const verbose = getBooleanInput('VERBOSE', true); process.env.VERBOSE = verbose.toString(); return { accessToken: accessToken, userName: userName, email: email, upstreamRepoSpec, headRepoSpec, trackFrom: trackFrom, include, exclude, labels: sortedLabels, plugins, verbose, }; }; const assert = (condition, message) => { if (!condition) { throw new Error(message); } }; export const inferUpstreamRepo = () => { const serverUrl = getInput('GITHUB_SERVER_URL', 'https://github.com'); const repository = getInput('GITHUB_REPOSITORY'); if (!repository) { throw new Error([ 'Failed to infer upstream repository: GITHUB_REPOSITORY environment variable is not set.', 'This typically happens when running outside of GitHub Actions.', 'For local development, please explicitly set the UPSTREAM_REPO environment variable.', ].join('\n')); } return `${serverUrl}/${repository}.git`; }; const createRepoSpec = (url, branch) => ({ owner: extractRepoOwner(url), name: extractRepoName(url), branch, }); const extractRepoOwner = (url) => { let dirname = path.dirname(url); if (dirname.includes(':')) { dirname = dirname.split(':').pop(); } return path.basename(dirname); }; const extractRepoName = (url) => path.basename(url, '.git');