From 2feaa3232f2859367898ed01eaf8cddb1713b0ac Mon Sep 17 00:00:00 2001 From: Opportunity Date: Sun, 24 May 2020 00:59:25 +0800 Subject: [PATCH] match pr --- dist/interfaces.js | 9 +++++++++ dist/versions.js | 18 ++++++++++++------ node_modules/.yarn-integrity | 6 ++++-- package.json | 3 ++- src/git.ts | 26 ++++---------------------- src/interfaces.ts | 33 +++++++++++++++++++++++++++++++++ src/unix-install.ts | 2 +- src/version.spec.ts | 24 ++++++++++++++++++++++++ src/versions.ts | 26 +++++++++++++------------- src/win-install.ts | 2 +- tsconfig.json | 23 +++++++++++------------ yarn.lock | 13 +++++++++---- 12 files changed, 123 insertions(+), 62 deletions(-) create mode 100644 dist/interfaces.js create mode 100644 src/interfaces.ts diff --git a/dist/interfaces.js b/dist/interfaces.js new file mode 100644 index 0000000..c8c798c --- /dev/null +++ b/dist/interfaces.js @@ -0,0 +1,9 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +function Sha(sha) { + sha = sha.trim().toLowerCase(); + if (!/^[a-f0-9]{40}$/g.test(sha)) + throw new Error(`Invalid sha value ${sha}`); + return sha; +} +exports.Sha = Sha; diff --git a/dist/versions.js b/dist/versions.js index 90a8519..fe92d4d 100644 --- a/dist/versions.js +++ b/dist/versions.js @@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); const core = require("@actions/core"); const semver = require("semver"); const git_1 = require("./git"); +const interfaces_1 = require("./interfaces"); let VERSIONS; async function getVersions() { if (VERSIONS) @@ -70,21 +71,26 @@ async function selectSemver(version) { return new VersionImpl(ver, sha, 'tags', ver); } async function selectSha(sha) { - sha = sha.toLowerCase(); - if (!/^[a-f0-9]{40}$/gi.test(sha)) - throw new Error(`Invalid sha value ${sha}`); + var _a; + const shaValue = interfaces_1.Sha(sha); const versions = await getVersions(); for (const branch in versions.heads) { - if (versions.heads[branch] === sha) { + if (versions.heads[branch] === shaValue) { return selectBranch(branch); } } for (const tag in versions.tags) { - if (versions.tags[tag] === sha) { + if (versions.tags[tag] === shaValue) { return selectSemver(tag); } } - return Promise.resolve(new VersionImpl(`sha#${sha}`, sha, 'sha', `commit ${sha.substr(0, 8)}`)); + for (const pr in versions.pull) { + const prData = versions.pull[pr]; + if (((_a = prData.merge) !== null && _a !== void 0 ? _a : prData.head) === shaValue) { + return selectPr(Number.parseInt(pr)); + } + } + return Promise.resolve(new VersionImpl(`sha#${shaValue}`, shaValue, 'sha', `commit ${shaValue.substr(0, 8)}`)); } async function selectVersion(version) { // get version string diff --git a/node_modules/.yarn-integrity b/node_modules/.yarn-integrity index c8b8a2e..2f1a01f 100644 --- a/node_modules/.yarn-integrity +++ b/node_modules/.yarn-integrity @@ -13,7 +13,7 @@ "@actions/io@^1.0.1", "@actions/tool-cache@^1.1.2", "@types/jest@^25.1.4", - "@types/node@^13.9.8", + "@types/node@^14.0.5", "@types/semver@^7.1.0", "@typescript-eslint/eslint-plugin@^2.26.0", "@typescript-eslint/parser@^2.26.0", @@ -25,6 +25,7 @@ "rimraf@^3.0.2", "semver@^7.1.3", "ts-jest@^25.3.0", + "type-fest@^0.14.0", "typescript@^3.8.3" ], "lockfileEntries": { @@ -97,7 +98,7 @@ "@types/jest@^25.1.4": "https://registry.npm.taobao.org/@types/jest/download/@types/jest-25.1.4.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40types%2Fjest%2Fdownload%2F%40types%2Fjest-25.1.4.tgz#9e9f1e59dda86d3fd56afce71d1ea1b331f6f760", "@types/json-schema@^7.0.3": "https://registry.npm.taobao.org/@types/json-schema/download/@types/json-schema-7.0.4.tgz#38fd73ddfd9b55abb1e1b2ed578cb55bd7b7d339", "@types/node@*": "https://registry.npm.taobao.org/@types/node/download/@types/node-13.9.2.tgz?cache=0&sync_timestamp=1584566829623&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40types%2Fnode%2Fdownload%2F%40types%2Fnode-13.9.2.tgz#ace1880c03594cc3e80206d96847157d8e7fa349", - "@types/node@^13.9.8": "https://registry.npm.taobao.org/@types/node/download/@types/node-13.9.8.tgz#09976420fc80a7a00bf40680c63815ed8c7616f4", + "@types/node@^14.0.5": "https://registry.npm.taobao.org/@types/node/download/@types/node-14.0.5.tgz?cache=0&sync_timestamp=1590092201933&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40types%2Fnode%2Fdownload%2F%40types%2Fnode-14.0.5.tgz#3d03acd3b3414cf67faf999aed11682ed121f22b", "@types/prettier@^1.19.0": "https://registry.npm.taobao.org/@types/prettier/download/@types/prettier-1.19.1.tgz#33509849f8e679e4add158959fdb086440e9553f", "@types/semver@^7.1.0": "https://registry.npm.taobao.org/@types/semver/download/@types/semver-7.1.0.tgz#c8c630d4c18cd326beff77404887596f96408408", "@types/stack-utils@^1.0.1": "https://registry.npm.taobao.org/@types/stack-utils/download/@types/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e", @@ -616,6 +617,7 @@ "type-check@~0.3.2": "https://registry.npm.taobao.org/type-check/download/type-check-0.3.2.tgz#5884cab512cf1d355e3fb784f30804b2b520db72", "type-detect@4.0.8": "https://registry.npm.taobao.org/type-detect/download/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c", "type-fest@^0.11.0": "https://registry.npm.taobao.org/type-fest/download/type-fest-0.11.0.tgz#97abf0872310fed88a5c466b25681576145e33f1", + "type-fest@^0.14.0": "https://registry.npm.taobao.org/type-fest/download/type-fest-0.14.0.tgz#758d800f2dcc0de47a946a120d91ab17883523ad", "type-fest@^0.8.1": "https://registry.npm.taobao.org/type-fest/download/type-fest-0.8.1.tgz#09e249ebde851d3b1e48d27c105444667f17b83d", "typedarray-to-buffer@^3.1.5": "https://registry.npm.taobao.org/typedarray-to-buffer/download/typedarray-to-buffer-3.1.5.tgz?cache=0&sync_timestamp=1580310064722&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Ftypedarray-to-buffer%2Fdownload%2Ftypedarray-to-buffer-3.1.5.tgz#a97ee7a9ff42691b9f783ff1bc5112fe3fca9080", "typescript@^3.8.3": "https://registry.npm.taobao.org/typescript/download/typescript-3.8.3.tgz?cache=0&sync_timestamp=1584688517009&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Ftypescript%2Fdownload%2Ftypescript-3.8.3.tgz#409eb8544ea0335711205869ec458ab109ee1061", diff --git a/package.json b/package.json index 08740d6..d836d6f 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ }, "devDependencies": { "@types/jest": "^25.1.4", - "@types/node": "^13.9.8", + "@types/node": "^14.0.5", "@types/semver": "^7.1.0", "@typescript-eslint/eslint-plugin": "^2.26.0", "@typescript-eslint/parser": "^2.26.0", @@ -37,6 +37,7 @@ "prettier": "^2.0.2", "rimraf": "^3.0.2", "ts-jest": "^25.3.0", + "type-fest": "^0.14.0", "typescript": "^3.8.3" } } diff --git a/src/git.ts b/src/git.ts index 1ad3fba..15ca51c 100644 --- a/src/git.ts +++ b/src/git.ts @@ -2,30 +2,12 @@ import { exec } from '@actions/exec'; import * as io from '@actions/io'; import * as os from 'os'; import * as path from 'path'; +import { Sha, RefDic } from './interfaces'; -function makeOpt(ref: string): { cwd: string } { +function makeOpt(ref: Sha): { cwd: string } { return { cwd: path.join(os.tmpdir(), `xmake-git-${ref}`) }; } -export type RefDic = { - /** branches */ - heads: Record; - /** tags */ - tags: Record; - /** pull requests */ - pull: Record< - number, - { - /** the current state of the pull request */ - head: string; - /** the current branch we're merging onto */ - base?: string; - /** merge result */ - merge?: string; - } - >; -}; - export async function lsRemote(): Promise { let out = ''; await exec('git', ['ls-remote', 'https://github.com/xmake-io/xmake.git'], { @@ -55,7 +37,7 @@ export async function lsRemote(): Promise { return data; } -export async function create(ref: string): Promise { +export async function create(ref: Sha): Promise { const opt = makeOpt(ref); await io.rmRF(opt.cwd); await io.mkdirP(opt.cwd); @@ -67,7 +49,7 @@ export async function create(ref: string): Promise { return opt.cwd; } -export async function cleanup(ref: string): Promise { +export async function cleanup(ref: Sha): Promise { const opt = makeOpt(ref); await io.rmRF(opt.cwd); } diff --git a/src/interfaces.ts b/src/interfaces.ts new file mode 100644 index 0000000..8f7fc91 --- /dev/null +++ b/src/interfaces.ts @@ -0,0 +1,33 @@ +import { Opaque } from 'type-fest'; + +export type Sha = Opaque; +export function Sha(sha: string): Sha { + sha = sha.trim().toLowerCase(); + if (!/^[a-f0-9]{40}$/g.test(sha)) throw new Error(`Invalid sha value ${sha}`); + return sha as Sha; +} + +export type RefDic = { + /** branches */ + heads: Record; + /** tags */ + tags: Record; + /** pull requests */ + pull: Record< + number, + { + /** the current state of the pull request */ + head: Sha; + /** the current branch we're merging onto */ + base?: Sha; + /** merge result */ + merge?: Sha; + } + >; +}; + +export interface Version { + version: string; + sha: Sha; + type: keyof RefDic | 'sha'; +} diff --git a/src/unix-install.ts b/src/unix-install.ts index 63608eb..a505b9e 100644 --- a/src/unix-install.ts +++ b/src/unix-install.ts @@ -6,7 +6,7 @@ import * as os from 'os'; import * as path from 'path'; import * as semver from 'semver'; import * as git from './git'; -import { Version } from './versions'; +import { Version } from './interfaces'; export async function unixInstall(version: Version): Promise { const ver = version.version; diff --git a/src/version.spec.ts b/src/version.spec.ts index ce47f69..a5900d7 100644 --- a/src/version.spec.ts +++ b/src/version.spec.ts @@ -332,6 +332,30 @@ describe('selectVersion', () => { }); }); + it('should return pr merge of sha', async () => { + await expect(selectVersion('sha@2f93475049575ed5e44699daa378239bc141aad3')).resolves.toEqual({ + version: 'pr#217', + sha: '2f93475049575ed5e44699daa378239bc141aad3', + type: 'pull', + }); + }); + + it('should not return pr head of sha if merge present', async () => { + await expect(selectVersion('sha@905a8607f3f31ab9efeb68684d40df8284683f3a')).resolves.toEqual({ + version: 'sha#905a8607f3f31ab9efeb68684d40df8284683f3a', + sha: '905a8607f3f31ab9efeb68684d40df8284683f3a', + type: 'sha', + }); + }); + + it('should return pr head of sha', async () => { + await expect(selectVersion('sha@af28dba1f52990cb7b6c3c8f69f1f1bcf017c90a')).resolves.toEqual({ + version: 'pr#708', + sha: 'af28dba1f52990cb7b6c3c8f69f1f1bcf017c90a', + type: 'pull', + }); + }); + it('should return correct normalized sha', async () => { await expect(selectVersion('sha@Af28dba1f52990cb7b6c3c8f69f1f1bcf017c90b')).resolves.toEqual({ version: 'sha#af28dba1f52990cb7b6c3c8f69f1f1bcf017c90b', diff --git a/src/versions.ts b/src/versions.ts index 7d5bd30..e7eb0f4 100644 --- a/src/versions.ts +++ b/src/versions.ts @@ -1,12 +1,7 @@ import * as core from '@actions/core'; import * as semver from 'semver'; -import { lsRemote, RefDic } from './git'; - -export interface Version { - version: string; - sha: string; - type: keyof RefDic | 'sha'; -} +import { lsRemote } from './git'; +import { RefDic, Sha, Version } from './interfaces'; let VERSIONS: RefDic | undefined; @@ -16,7 +11,7 @@ async function getVersions(): Promise { } class VersionImpl implements Version { - constructor(readonly version: string, readonly sha: string, readonly type: Version['type'], toString: string) { + constructor(readonly version: string, readonly sha: Sha, readonly type: Version['type'], toString: string) { this.#string = toString; } readonly #string: string; @@ -63,20 +58,25 @@ async function selectSemver(version: string): Promise { } async function selectSha(sha: string): Promise { - sha = sha.toLowerCase(); - if (!/^[a-f0-9]{40}$/gi.test(sha)) throw new Error(`Invalid sha value ${sha}`); + const shaValue = Sha(sha); const versions = await getVersions(); for (const branch in versions.heads) { - if (versions.heads[branch] === sha) { + if (versions.heads[branch] === shaValue) { return selectBranch(branch); } } for (const tag in versions.tags) { - if (versions.tags[tag] === sha) { + if (versions.tags[tag] === shaValue) { return selectSemver(tag); } } - return Promise.resolve(new VersionImpl(`sha#${sha}`, sha, 'sha', `commit ${sha.substr(0, 8)}`)); + for (const pr in versions.pull) { + const prData = versions.pull[pr]; + if ((prData.merge ?? prData.head) === shaValue) { + return selectPr(Number.parseInt(pr)); + } + } + return Promise.resolve(new VersionImpl(`sha#${shaValue}`, shaValue, 'sha', `commit ${shaValue.substr(0, 8)}`)); } export async function selectVersion(version?: string): Promise { diff --git a/src/win-install.ts b/src/win-install.ts index fb2c415..8fba392 100644 --- a/src/win-install.ts +++ b/src/win-install.ts @@ -5,7 +5,7 @@ import * as toolCache from '@actions/tool-cache'; import * as os from 'os'; import * as path from 'path'; import * as semver from 'semver'; -import { Version } from './versions'; +import { Version } from './interfaces'; function getInstallerUrl(version: Version): string { const ver = version.version; diff --git a/tsconfig.json b/tsconfig.json index 5519280..f46493a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,13 +1,12 @@ { - "include": [ - "src/**/*" - ], - "compileOnSave": true, - "compilerOptions": { - "strict": true, - "moduleResolution": "node", - "module": "commonjs", - "target": "es2018", - "outDir": "dist" - } -} \ No newline at end of file + "include": ["src/**/*"], + "compileOnSave": true, + "compilerOptions": { + "strict": true, + "moduleResolution": "node", + "module": "commonjs", + "target": "es2018", + "outDir": "dist", + "types": ["node", "jest"] + } +} diff --git a/yarn.lock b/yarn.lock index a7d1bfa..58a5fc0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -457,10 +457,10 @@ resolved "https://registry.npm.taobao.org/@types/node/download/@types/node-13.9.2.tgz?cache=0&sync_timestamp=1584566829623&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40types%2Fnode%2Fdownload%2F%40types%2Fnode-13.9.2.tgz#ace1880c03594cc3e80206d96847157d8e7fa349" integrity sha1-rOGIDANZTMPoAgbZaEcVfY5/o0k= -"@types/node@^13.9.8": - version "13.9.8" - resolved "https://registry.npm.taobao.org/@types/node/download/@types/node-13.9.8.tgz#09976420fc80a7a00bf40680c63815ed8c7616f4" - integrity sha1-CZdkIPyAp6AL9AaAxjgV7Yx2FvQ= +"@types/node@^14.0.5": + version "14.0.5" + resolved "https://registry.npm.taobao.org/@types/node/download/@types/node-14.0.5.tgz?cache=0&sync_timestamp=1590092201933&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40types%2Fnode%2Fdownload%2F%40types%2Fnode-14.0.5.tgz#3d03acd3b3414cf67faf999aed11682ed121f22b" + integrity sha1-PQOs07NBTPZ/r5ma7RFoLtEh8is= "@types/prettier@^1.19.0": version "1.19.1" @@ -3650,6 +3650,11 @@ type-fest@^0.11.0: resolved "https://registry.npm.taobao.org/type-fest/download/type-fest-0.11.0.tgz#97abf0872310fed88a5c466b25681576145e33f1" integrity sha1-l6vwhyMQ/tiKXEZrJWgVdhReM/E= +type-fest@^0.14.0: + version "0.14.0" + resolved "https://registry.npm.taobao.org/type-fest/download/type-fest-0.14.0.tgz#758d800f2dcc0de47a946a120d91ab17883523ad" + integrity sha1-dY2ADy3MDeR6lGoSDZGrF4g1I60= + type-fest@^0.8.1: version "0.8.1" resolved "https://registry.npm.taobao.org/type-fest/download/type-fest-0.8.1.tgz#09e249ebde851d3b1e48d27c105444667f17b83d"