This commit is contained in:
Opportunity
2020-05-24 00:59:25 +08:00
Unverified
parent 77cd51991f
commit 2feaa3232f
12 changed files with 123 additions and 62 deletions
+4 -22
View File
@@ -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<string, string>;
/** tags */
tags: Record<string, string>;
/** 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<RefDic> {
let out = '';
await exec('git', ['ls-remote', 'https://github.com/xmake-io/xmake.git'], {
@@ -55,7 +37,7 @@ export async function lsRemote(): Promise<RefDic> {
return data;
}
export async function create(ref: string): Promise<string> {
export async function create(ref: Sha): Promise<string> {
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<string> {
return opt.cwd;
}
export async function cleanup(ref: string): Promise<void> {
export async function cleanup(ref: Sha): Promise<void> {
const opt = makeOpt(ref);
await io.rmRF(opt.cwd);
}
+33
View File
@@ -0,0 +1,33 @@
import { Opaque } from 'type-fest';
export type Sha = Opaque<string, 'sha'>;
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<string, Sha>;
/** tags */
tags: Record<string, Sha>;
/** 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';
}
+1 -1
View File
@@ -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<void> {
const ver = version.version;
+24
View File
@@ -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',
+13 -13
View File
@@ -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<RefDic> {
}
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<Version> {
}
async function selectSha(sha: string): Promise<Version> {
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<Version> {
+1 -1
View File
@@ -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;