-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.js
More file actions
68 lines (60 loc) · 1.94 KB
/
Copy pathaction.js
File metadata and controls
68 lines (60 loc) · 1.94 KB
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
const registry = "${{ inputs.registry }}";
const registryUrl = `https://${registry}`;
// Get OIDC token with registry as audience
if (!process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN) {
throw new Error('Missing ID request token. Did you specify the id-token: write permission?')
}
let token = null;
try {
token = await core.getIDToken(registryUrl);
} catch (e) {
throw new Error(`Request to GitHub IDToken API failed. ${e.message}`)
}
// Exchange OIDC token for registry access token
const res = await fetch(`${registryUrl}/login/oidc/github`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ id_token: token }),
});
if (!res.ok) {
let msg = `HTTP ${res.status} ${res.statusText}`;
try {
const data = await res.json();
msg = data.error_description || JSON.stringify(data);
} catch (e) {}
throw new Error(`${registry}: ${msg}`);
}
const data = await res.json();
core.setSecret(data.access_token);
core.setOutput("access_token", data.access_token);
// Write credentials to cue logins file if update_logins is true
if ("${{ inputs.update_logins }}" === "true") {
const fs = require("fs");
const path = require("path");
const homeDir = process.env.HOME || process.env.USERPROFILE;
const configDir = path.join(homeDir, ".config", "cue");
const loginsPath = path.join(configDir, "logins.json");
fs.mkdirSync(configDir, { recursive: true });
let logins = {};
if (fs.existsSync(loginsPath)) {
try {
const content = fs.readFileSync(loginsPath, "utf8");
if (content.trim()) {
logins = JSON.parse(content);
}
} catch (err) {
console.warn(
"Failed to parse existing logins.json, will overwrite:",
err.message,
);
}
}
logins.registries = logins.registries || {};
logins.registries[registry] = {
access_token: data.access_token,
token_type: "Bearer",
};
fs.writeFileSync(loginsPath, JSON.stringify(logins, null, 2) + "\n");
}