initial commit

master
selfisekai 2020-08-05 23:33:36 +02:00
commit 7ddedb0b0d
16 changed files with 5060 additions and 0 deletions

23
.eslintrc.js Normal file
View File

@ -0,0 +1,23 @@
module.exports = {
env: {
es6: true,
node: true,
},
extends: ['airbnb-base', 'prettier'],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
},
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
},
plugins: ['@typescript-eslint', 'prettier'],
rules: {
'no-unused-vars': 'off', // broken with typescript
'import/no-unresolved': 'off', // conflicting with typescript
'import/extensions': 'off', // conflicting with typescript
},
};

305
.gitignore vendored Normal file
View File

@ -0,0 +1,305 @@
build
# fetched automatically with scripts/download-gql-schema.ts
schema.graphql
schema.json
# autogenerated from schema with graphql-codegen
api-types.ts
# Created by https://www.toptal.com/developers/gitignore/api/node,linux,macos,windows,jetbrains,visualstudiocode
# Edit at https://www.toptal.com/developers/gitignore?templates=node,linux,macos,windows,jetbrains,visualstudiocode
### JetBrains ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
# Generated files
.idea/**/contentModel.xml
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr
# CMake
cmake-build-*/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# File-based project format
*.iws
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
.idea/httpRequests
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
### JetBrains Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr
# Sonarlint plugin
# https://plugins.jetbrains.com/plugin/7973-sonarlint
.idea/**/sonarlint/
# SonarQube Plugin
# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin
.idea/**/sonarIssues.xml
# Markdown Navigator plugin
# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced
.idea/**/markdown-navigator.xml
.idea/**/markdown-navigator-enh.xml
.idea/**/markdown-navigator/
# Cache file creation bug
# See https://youtrack.jetbrains.com/issue/JBR-2257
.idea/$CACHE_FILE$
# CodeStream plugin
# https://plugins.jetbrains.com/plugin/12206-codestream
.idea/codestream.xml
### Linux ###
*~
# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*
# KDE directory preferences
.directory
# Linux trash folder which might appear on any partition or disk
.Trash-*
# .nfs files are created when an open file is removed but is still being accessed
.nfs*
### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
# parcel-bundler cache (https://parceljs.org/)
.cache
# Next.js build output
.next
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace
### VisualStudioCode Patch ###
# Ignore all local history of files
.history
### Windows ###
# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db
# Dump file
*.stackdump
# Folder config file
[Dd]esktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp
# Windows shortcuts
*.lnk
# End of https://www.toptal.com/developers/gitignore/api/node,linux,macos,windows,jetbrains,visualstudiocode

7
.prettierrc.js Normal file
View File

@ -0,0 +1,7 @@
module.exports = {
semi: true,
printWidth: 100,
singleQuote: true,
trailingComma: 'all',
bracketSpacing: true,
};

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"editor.tabSize": 2
}

38
package.json Normal file
View File

@ -0,0 +1,38 @@
{
"name": "git-copycat",
"version": "1.0.0",
"main": "index.js",
"author": "selfisekai <laura@selfisekai.rocks>",
"license": "GPL-3.0",
"scripts": {
"gql-codegen": "yarn gql-codegen:download && yarn gql-codegen:generate",
"gql-codegen:download": "ts-node scripts/download-gql-schema.ts",
"gql-codegen:generate": "ts-node scripts/generate-gql-types.ts"
},
"dependencies": {
"@iarna/toml": "^2.2.5",
"appdata-path": "^1.0.0",
"fs-extra": "^9.0.1",
"got": "^11.5.1",
"graphql": "^15.3.0",
"js-yaml": "^3.14.0",
"simple-git": "^2.15.0"
},
"devDependencies": {
"@graphql-codegen/cli": "1.17.6",
"@graphql-codegen/introspection": "1.17.6",
"@graphql-codegen/typescript": "1.17.6",
"@graphql-codegen/typescript-resolvers": "1.17.6",
"@types/fs-extra": "^9.0.1",
"@types/iarna__toml": "^2.0.0",
"@types/js-yaml": "^3.12.5",
"eslint": "^7.5.0",
"eslint-config-airbnb-base": "^14.2.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-prettier": "^3.1.4",
"prettier": "^2.0.5",
"ts-node": "^8.10.2",
"typescript": "^3.9.7"
}
}

View File

@ -0,0 +1,36 @@
import { getIntrospectionQuery } from 'graphql';
import got, { Options as GotOptions } from 'got';
import { createWriteStream } from 'fs';
import path from 'path';
import { pipeline as pipelineUnpromised } from 'stream';
import { promisify } from 'util';
const pipeline = promisify(pipelineUnpromised);
const introspection = {
method: 'POST' as 'POST',
body: JSON.stringify({ query: getIntrospectionQuery() }),
headers: {
'Content-Type': 'application/json',
},
};
const schemas: [
string, // vendor
'graphql' | 'json', // filetype
[string | URL, GotOptions & { isStream?: true | undefined }],
][] = [
['github', 'graphql', ['https://docs.github.com/public/schema.docs.graphql', {}]],
['gitlab', 'json', ['https://gitlab.com/api/graphql', introspection]],
];
Promise.all(
schemas.map(([vendor, filetype, downloadConfig]) =>
pipeline(
got.stream(...downloadConfig),
createWriteStream(
path.resolve(__dirname, '..', 'src', 'vendor', vendor, `schema.${filetype}`),
),
),
),
).then(() => console.log('done!'));

View File

@ -0,0 +1,15 @@
import { generate } from '@graphql-codegen/cli';
import { safeLoad as yamlLoad } from 'js-yaml';
import { readdirSync, existsSync, readFileSync } from 'fs';
import path from 'path';
const vendorPath = path.join(__dirname, '..', 'src', 'vendor');
const vendors = readdirSync(vendorPath);
const gqlVendors = vendors.filter((v) => existsSync(path.join(vendorPath, v, 'codegen.yml')));
Promise.all(
gqlVendors
.map((v) => path.join(vendorPath, v, 'codegen.yml'))
.map((codegenFile) =>
generate(yamlLoad(readFileSync(codegenFile).toString('utf-8')) as any, true),
),
).then(() => console.log('done!'));

25
src/copycat.ts Normal file
View File

@ -0,0 +1,25 @@
import path from 'path';
import { VendorManager, CopycatConfig, CopycatProfile } from './types';
import { getConfig, DEFAULT_CONFIG } from './utils';
export default class Copycat {
vendorManagers: VendorManager[] = [];
config: CopycatConfig = DEFAULT_CONFIG;
public async initialize() {
this.config = getConfig();
this.vendorManagers = await Promise.all(
this.config.vendorConfigs
.map(
(profile) =>
[
require(path.join(__dirname, 'vendor', profile.vendor.type, 'vendormgr')).default,
profile,
] as [any, CopycatProfile],
)
.map(([VendorMgr, vendor]) => new VendorMgr(vendor.config) as VendorManager)
.map((VendorMgr) => VendorMgr.initialize()),
);
return this;
}
}

77
src/types.ts Normal file
View File

@ -0,0 +1,77 @@
export interface CopycatConfig {
vendorConfigs: CopycatProfile[];
}
export interface CopycatProfile {
name: string;
vendor: Vendor;
/** authentication etc., always depends on vendor */
config: any;
}
/** indicates the used api */
export enum VENDOR_TYPE {
/** github, https://en.wikipedia.org/wiki/GitHub, both github.com (default) and github enterprise server */
GITHUB = 'github',
/** gitlab, https://en.wikipedia.org/wiki/GitLab */
GITLAB = 'gitlab',
/** gitea, https://en.wikipedia.org/wiki/Gitea */
GITEA = 'gitea',
}
export interface Vendor {
/** human-readable name like 'GitLab' */
display: string;
/** indicates the used api */
type: VENDOR_TYPE;
/** the host, like 'framagit.org' */
domain: string;
}
export interface VendorManager<T = any> {
vendor: Vendor;
config: T;
initialize: () => Promise<VendorManager<T>>;
getRepo: (path: string) => Promise<RepoManager>;
}
export interface Repo {
vendor: Vendor;
owner: Actor;
name: string;
}
export interface RepoManager {
repo: Repo;
initialize: () => Promise<RepoManager>;
getIssue: (id: string) => Promise<Issue>;
}
export interface Issue {
id: string;
title: string;
content: string;
repo: Repo;
}
/** the account that did an action */
export enum ACTOR_TYPE {
/** human person, absolutely not a sentient lizard */
USER = 'user',
/** organization */
ORG = 'org',
/** bot */
BOT = 'bot',
/** deleted account, imported actions or anything */
GHOST = 'ghost',
/** unknown (before initializing sth) */
UNKNOWN = 'unknown',
}
/** repository owner, issue/MR/comment/... creator */
export interface Actor {
type: ACTOR_TYPE;
username: string;
display_name?: string | null;
vendor: Vendor;
}

27
src/utils.ts Normal file
View File

@ -0,0 +1,27 @@
import { readFileSync, writeFileSync } from 'fs-extra';
import toml from '@iarna/toml';
import appdataPath from 'appdata-path';
import { CopycatConfig, CopycatProfile } from './types';
export const DEFAULT_CONFIG: CopycatConfig = {
vendorConfigs: [],
};
export const getConfigPath = () => appdataPath('copycat');
export const getConfig = () => {
try {
const file = readFileSync(getConfigPath());
return (toml.parse(file.toString('utf-8')) as unknown) as CopycatConfig;
} catch (err) {
if (err.code === 'ENOENT') {
setConfig(DEFAULT_CONFIG);
}
return DEFAULT_CONFIG;
}
};
export const setConfig = (config: CopycatConfig) =>
writeFileSync(getConfigPath(), toml.stringify({ ...DEFAULT_CONFIG, ...config } as any), {
encoding: 'utf-8',
});

9
src/vendor/github/codegen.yml vendored Normal file
View File

@ -0,0 +1,9 @@
overwrite: true
schema: src/vendor/github/schema.graphql
config:
typesPrefix: GH
generates:
src/vendor/github/api-types.ts:
plugins:
- 'typescript'
- 'typescript-resolvers'

100
src/vendor/github/repomgr.ts vendored Normal file
View File

@ -0,0 +1,100 @@
import { RepoManager, Issue, Repo, ACTOR_TYPE } from '../../types';
import GitHubVendorManager from './vendormgr';
import assert from 'assert';
export default class GitHubRepoManager implements RepoManager {
vendorMgr: GitHubVendorManager;
repo: Repo;
constructor(vendorMgr: GitHubVendorManager, repoPath: string) {
this.vendorMgr = vendorMgr;
const mobj = /^([^/\s]+)\/([^/\s]+)$/.exec(repoPath);
assert(mobj, 'invalid repo path');
const [, owner, repoName] = mobj;
this.repo = {
vendor: this.vendorMgr.vendor,
owner: {
type: ACTOR_TYPE.UNKNOWN,
username: owner,
vendor: this.vendorMgr.vendor,
},
name: repoName,
};
}
public async initialize() {
const meta = await this.vendorMgr._doRequest(
`
query Query($owner: String!, $name: String!) {
repository(owner: $owner, name: $name) {
name
owner {
login
__typename
}
}
}
`,
{
owner: this.repo.owner.username,
name: this.repo.name,
},
);
console.log(meta);
assert(meta.repository);
assert(meta.repository.owner);
this.repo.name = meta.repository.name;
this.repo.owner.username = meta.repository.owner.login;
// @ts-ignore graphql-codegen ignores built-in graphql values
switch (meta.repository.owner.__typename) {
case 'Organization':
this.repo.owner.type = ACTOR_TYPE.ORG;
break;
case 'User':
this.repo.owner.type = ACTOR_TYPE.USER;
break;
}
return this;
}
public async getIssue(number: string): Promise<Issue> {
const resp = await this.vendorMgr._doRequest(
`
query Query($owner: String!, $name: String!, $number: Int!) {
repository(owner: $owner, name: $name) {
issue(number: $number) {
number
title
body
closed
closedAt
labels(first: 0) {
nodes {
name
color
description
}
}
}
}
}
`,
{
owner: this.repo.owner.username,
name: this.repo.name,
number: parseInt(number, 10),
},
);
assert(resp.repository, 'no repository');
assert(resp.repository.issue, 'no issue');
const { issue } = resp.repository;
return {
id: issue.number.toString(),
content: issue.body,
title: issue.title,
repo: this.repo,
};
}
}

49
src/vendor/github/vendormgr.ts vendored Normal file
View File

@ -0,0 +1,49 @@
import got from 'got';
import { VendorManager, VENDOR_TYPE, Vendor, Issue, Repo, RepoManager } from '../../types';
import { GHRepository, GHQuery } from './api-types';
import assert from 'assert';
import GitHubRepoManager from './repomgr';
export interface GitHubConfig {
token: string;
domain?: string | null;
}
export default class GitHubVendorManager implements VendorManager<GitHubConfig> {
vendor: Vendor;
config: GitHubConfig;
gqlEndpoint: string;
constructor(config: GitHubConfig) {
this.vendor = {
display: 'Microsoft GitHub',
type: VENDOR_TYPE.GITHUB,
domain: config.domain || 'github.com',
};
this.config = config;
this.gqlEndpoint = `https://${
this.vendor.domain === 'github.com'
? 'api.github.com'
: /* github enterprise server */ `${this.vendor.domain}/api`
}/graphql`;
}
public async initialize() {
return this;
}
public async getRepo(path: string) {
return new GitHubRepoManager(this, path).initialize() as Promise<RepoManager>;
}
/** internal and for RepoManager */
public async _doRequest(query: string, variables: any) {
return got
.post(this.gqlEndpoint, {
body: JSON.stringify({ query, variables }),
headers: { Authorization: `Bearer ${this.config.token}` },
})
.then((res) => JSON.parse(res.body))
.then((res) => res.data) as Promise<GHQuery>;
}
}

9
src/vendor/gitlab/codegen.yml vendored Normal file
View File

@ -0,0 +1,9 @@
overwrite: true
schema: src/vendor/gitlab/schema.json
config:
typesPrefix: GL
generates:
src/vendor/gitlab/api-types.ts:
plugins:
- 'typescript'
- 'typescript-resolvers'

69
tsconfig.json Normal file
View File

@ -0,0 +1,69 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "esnext" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./build" /* Redirect output structure to the directory. */,
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true /* Enable all strict type-checking options. */,
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
/* Advanced Options */
"skipLibCheck": true /* Skip type checking of declaration files. */,
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}

4268
yarn.lock Normal file

File diff suppressed because it is too large Load Diff