support other repo
This commit is contained in:
+38
-27
@@ -1,17 +1,19 @@
|
||||
import * as core from '@actions/core';
|
||||
import * as semver from 'semver';
|
||||
import { lsRemote } from './git';
|
||||
import { RefDic, Sha, Version } from './interfaces';
|
||||
import { RefDic, Sha, Version, Repo } from './interfaces';
|
||||
|
||||
let VERSIONS: RefDic | undefined;
|
||||
|
||||
async function getVersions(): Promise<RefDic> {
|
||||
if (VERSIONS) return VERSIONS;
|
||||
return (VERSIONS = await lsRemote());
|
||||
const VERSIONS = new Map<Repo, RefDic>();
|
||||
async function getVersions(repo: Repo): Promise<RefDic> {
|
||||
const cache = VERSIONS.get(repo);
|
||||
if (cache) return cache;
|
||||
const result = await lsRemote(repo);
|
||||
VERSIONS.set(repo, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
class VersionImpl implements Version {
|
||||
constructor(readonly version: string, readonly sha: Sha, readonly type: Version['type'], toString: string) {
|
||||
constructor(readonly repo: Repo, readonly version: string, readonly sha: Sha, readonly type: Version['type'], toString: string) {
|
||||
this.#string = toString;
|
||||
}
|
||||
readonly #string: string;
|
||||
@@ -20,63 +22,63 @@ class VersionImpl implements Version {
|
||||
}
|
||||
}
|
||||
|
||||
async function selectBranch(branch: string): Promise<Version> {
|
||||
const versions = await getVersions();
|
||||
async function selectBranch(repo: Repo, branch: string): Promise<Version> {
|
||||
const versions = await getVersions(repo);
|
||||
if (branch in versions.heads) {
|
||||
return new VersionImpl(branch, versions.heads[branch], 'heads', `branch ${branch}`);
|
||||
return new VersionImpl(repo, branch, versions.heads[branch], 'heads', `branch ${branch}`);
|
||||
}
|
||||
throw new Error(`Branch ${branch} not found`);
|
||||
}
|
||||
|
||||
async function selectPr(pr: number): Promise<Version> {
|
||||
const versions = await getVersions();
|
||||
async function selectPr(repo: Repo, pr: number): Promise<Version> {
|
||||
const versions = await getVersions(repo);
|
||||
if (pr in versions.pull) {
|
||||
const prheads = versions.pull[pr];
|
||||
const sha = prheads.merge ?? prheads.head;
|
||||
if (sha) {
|
||||
return new VersionImpl(`pr#${pr}`, sha, 'pull', `pull request #${pr}`);
|
||||
return new VersionImpl(repo, `pr#${pr}`, sha, 'pull', `pull request #${pr}`);
|
||||
}
|
||||
}
|
||||
throw new Error(`Pull requrest #${pr} not found`);
|
||||
}
|
||||
|
||||
async function selectSemver(version: string): Promise<Version> {
|
||||
async function selectSemver(repo: Repo, version: string): Promise<Version> {
|
||||
// check version valid
|
||||
const v = new semver.Range(version);
|
||||
if (!v) {
|
||||
throw new Error(`Invalid semver`);
|
||||
}
|
||||
|
||||
const versions = await getVersions();
|
||||
const versions = await getVersions(repo);
|
||||
const ver = semver.maxSatisfying(Object.keys(versions.tags), v);
|
||||
if (!ver) {
|
||||
throw new Error(`No matched releases of xmake-version ${v.format()}`);
|
||||
}
|
||||
|
||||
const sha = versions.tags[ver];
|
||||
return new VersionImpl(ver, sha, 'tags', ver);
|
||||
return new VersionImpl(repo, ver, sha, 'tags', ver);
|
||||
}
|
||||
|
||||
async function selectSha(sha: string): Promise<Version> {
|
||||
async function selectSha(repo: Repo, sha: string): Promise<Version> {
|
||||
const shaValue = Sha(sha);
|
||||
const versions = await getVersions();
|
||||
const versions = await getVersions(repo);
|
||||
for (const branch in versions.heads) {
|
||||
if (versions.heads[branch] === shaValue) {
|
||||
return selectBranch(branch);
|
||||
return selectBranch(repo, branch);
|
||||
}
|
||||
}
|
||||
for (const tag in versions.tags) {
|
||||
if (versions.tags[tag] === shaValue) {
|
||||
return selectSemver(tag);
|
||||
return selectSemver(repo, tag);
|
||||
}
|
||||
}
|
||||
for (const pr in versions.pull) {
|
||||
const prData = versions.pull[pr];
|
||||
if ((prData.merge ?? prData.head) === shaValue) {
|
||||
return selectPr(Number.parseInt(pr));
|
||||
return selectPr(repo, Number.parseInt(pr));
|
||||
}
|
||||
}
|
||||
return Promise.resolve(new VersionImpl(`sha#${shaValue}`, shaValue, 'sha', `commit ${shaValue.substr(0, 8)}`));
|
||||
return Promise.resolve(new VersionImpl(repo, `sha#${shaValue}`, shaValue, 'sha', `commit ${shaValue.substr(0, 8)}`));
|
||||
}
|
||||
|
||||
export async function selectVersion(version?: string): Promise<Version> {
|
||||
@@ -84,11 +86,20 @@ export async function selectVersion(version?: string): Promise<Version> {
|
||||
version = (version ?? core.getInput('xmake-version')) || 'latest';
|
||||
if (version.toLowerCase() === 'latest') version = '';
|
||||
|
||||
let repo = Repo('xmake-io/xmake');
|
||||
let ret: Version | undefined;
|
||||
{
|
||||
const match = /^([^/#]+\/[^/#]+)#(.+)$/.exec(version);
|
||||
if (match) {
|
||||
repo = Repo(match[1]);
|
||||
version = match[2];
|
||||
}
|
||||
}
|
||||
|
||||
// select branch
|
||||
if (version.startsWith('branch@')) {
|
||||
const branch = version.substr('branch@'.length);
|
||||
ret = await selectBranch(branch);
|
||||
ret = await selectBranch(repo, branch);
|
||||
}
|
||||
// select pr
|
||||
if (version.startsWith('pr@')) {
|
||||
@@ -96,20 +107,20 @@ export async function selectVersion(version?: string): Promise<Version> {
|
||||
if (Number.isNaN(pr) || pr <= 0) {
|
||||
throw new Error(`Invalid pull requrest ${version.substr('pr@'.length)}, should be a positive integer`);
|
||||
}
|
||||
ret = await selectPr(pr);
|
||||
ret = await selectPr(repo, pr);
|
||||
}
|
||||
// select sha
|
||||
if (version.startsWith('sha@')) {
|
||||
const sha = version.substr('sha@'.length);
|
||||
ret = await selectSha(sha);
|
||||
ret = await selectSha(repo, sha);
|
||||
}
|
||||
// select version
|
||||
if (semver.validRange(version)) {
|
||||
ret = await selectSemver(version);
|
||||
ret = await selectSemver(repo, version);
|
||||
}
|
||||
if (!ret) {
|
||||
throw new Error(`Invalid input xmake-version ${core.getInput('xmake-version')}`);
|
||||
}
|
||||
core.info(`Selected xmake ${ret} (commit: ${ret.sha.substr(0, 8)})`);
|
||||
core.info(`Selected xmake ${String(ret)} (commit: ${ret.sha.substr(0, 8)})`);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user