/
index.ts
80 lines (76 loc) · 2.24 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { logger } from '../../logger';
import * as globalCache from '../../util/cache/global';
import { Http } from '../../util/http';
import { GetReleasesConfig, ReleaseResult } from '../common';
export const id = 'orb';
const http = new Http(id);
interface OrbRelease {
homeUrl?: string;
versions: {
version: string;
createdAt?: string;
}[];
}
/**
* orb.getReleases
*
* This function will fetch an orb from CircleCI and return all semver versions.
*/
export async function getReleases({
lookupName,
}: GetReleasesConfig): Promise<ReleaseResult | null> {
logger.debug({ lookupName }, 'orb.getReleases()');
const cacheNamespace = 'orb';
const cacheKey = lookupName;
const cachedResult = await globalCache.get<ReleaseResult>(
cacheNamespace,
cacheKey
);
// istanbul ignore if
if (cachedResult) {
return cachedResult;
}
const url = 'https://circleci.com/graphql-unstable';
const body = {
query: `{orb(name:"${lookupName}"){name, homeUrl, versions {version, createdAt}}}`,
variables: {},
};
try {
const res: OrbRelease = (
await http.postJson<{ data: { orb: OrbRelease } }>(url, {
body,
})
).body.data.orb;
if (!res) {
logger.debug({ lookupName }, 'Failed to look up orb');
return null;
}
// Simplify response before caching and returning
const dep: ReleaseResult = {
name: lookupName,
versions: {},
releases: null,
};
if (res.homeUrl && res.homeUrl.length) {
dep.homepage = res.homeUrl;
}
dep.homepage =
dep.homepage || `https://circleci.com/orbs/registry/orb/${lookupName}`;
dep.releases = res.versions.map(({ version, createdAt }) => ({
version,
releaseTimestamp: createdAt || null,
}));
logger.trace({ dep }, 'dep');
const cacheMinutes = 15;
await globalCache.set(cacheNamespace, cacheKey, dep, cacheMinutes);
return dep;
} catch (err) /* istanbul ignore next */ {
logger.debug({ err }, 'CircleCI Orb lookup error');
if (err.statusCode === 404 || err.code === 'ENOTFOUND') {
logger.debug({ lookupName }, `CircleCI Orb lookup failure: not found`);
return null;
}
logger.warn({ lookupName }, 'CircleCI Orb lookup failure: Unknown error');
return null;
}
}