-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
executable file
·126 lines (114 loc) · 3.55 KB
/
main.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env -S deno run -A
import { copyFile, readFile, writeFile } from "node:fs/promises";
import { $ } from "npm:zx";
import { temporaryDirectory, temporaryWrite } from "npm:tempy";
import process from "node:process";
import { join } from "node:path";
import * as core from "npm:@actions/core";
let collection = core.getInput("collection");
if (!collection.includes(":")) {
collection += ":latest";
}
console.log("collection", collection);
const [, owner, name] = collection.match(/^ghcr\.io\/([\w\-]*?)\/([\w\.\-]*?):.*?$/)
const url = new URL("https://github.com/search");
url.searchParams.set(
"q",
`owner:${owner} /${name}\\/.+/ package_type:container`
);
url.searchParams.set("type", "registrypackages");
console.log(url.href)
const response = await fetch(url);
const ids = (await response.json()).payload.results
.map((x) => x.name)
.filter((f) => f !== name)
.map((f) => f.split("/")[1]);
console.log(ids)
const devcontainerCollection = {
sourceInformation: {
source: "devcontainer-cli",
},
features: [] as any[],
templates: [] as any[],
};
for (const id of ids) {
const image = collection.replace(/:.*?$/, `/${id}:latest`)
const manifest = JSON.parse(
(await $`oras manifest fetch ${image}`).toString()
);
if (manifest.annotations["com.github.package.type"] === "devcontainer_feature") {
const f = JSON.parse(manifest.annotations["dev.containers.metadata"])
devcontainerCollection.features.push(f)
} else if (manifest.annotations["com.github.package.type"] === "devcontainer_template") {
const basename = image.split("/").pop().split(":")[0];
const tempDirPath = temporaryDirectory();
const oldCWD = $.cwd;
$.cwd = tempDirPath;
let templateManifest: any;
try {
await $`oras pull ${image}`;
await $`tar -xvf devcontainer-template-${basename}.tgz`;
templateManifest = JSON.parse(
await readFile(join($.cwd, "devcontainer-template.json"))
);
} finally {
$.cwd = oldCWD;
}
devcontainerCollection.templates.push(templateManifest)
}
}
{
const seenIds = new Set();
for (let i = 0; i < devcontainerCollection.features.length; i++) {
const f = devcontainerCollection.features[i];
if (seenIds.has(f.id)) {
devcontainerCollection.features.splice(i, 1);
i--;
} else {
seenIds.add(f.id);
}
}
}
{
const seenIds = new Set();
for (let i = 0; i < devcontainerCollection.templates.length; i++) {
const f = devcontainerCollection.templates[i];
if (seenIds.has(f.id)) {
devcontainerCollection.templates.splice(i, 1);
i--;
} else {
seenIds.add(f.id);
}
}
}
if (!devcontainerCollection.templates.length) {
delete devcontainerCollection.templates
}
if (!devcontainerCollection.features.length) {
delete devcontainerCollection.features
}
const tempDirPath = temporaryDirectory();
process.chdir(tempDirPath);
$.cwd = process.cwd();
await writeFile(
"devcontainer-collection.json",
JSON.stringify(devcontainerCollection, null, 2)
);
const annotations = {
$manifest: {
"com.github.package.type": "devcontainer_collection",
},
"devcontainer-collection.json": {
"org.opencontainers.image.title": "devcontainer-collection.json",
},
};
const annotationsPath = await temporaryWrite(
JSON.stringify(annotations, null, 2),
{ suffix: ".json" }
);
await $`tree -a`;
await $`oras push \
ghcr.io/${process.env.GITHUB_REPOSITORY}:latest \
--config /dev/null:application/vnd.devcontainers \
--annotation-file ${annotationsPath} \
devcontainer-collection.json:application/vnd.devcontainers.collection.layer.v1+json`;