-
Notifications
You must be signed in to change notification settings - Fork 7
/
restoreImpl.ts
190 lines (166 loc) · 6.03 KB
/
restoreImpl.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import * as core from "@actions/core";
import { Events, Inputs, Outputs, State } from "./constants";
import * as inputs from "./inputs";
import {
IStateProvider,
NullStateProvider,
StateProvider
} from "./stateProvider";
import * as utils from "./utils/action";
import * as restore from "./utils/restore";
export async function restoreImpl(
stateProvider: IStateProvider
): Promise<string | undefined> {
try {
core.setOutput(Outputs.Hit, false);
core.setOutput(Outputs.HitPrimaryKey, false);
core.setOutput(Outputs.HitFirstMatch, false);
core.setOutput(Outputs.RestoredKey, false);
core.setOutput(Outputs.RestoredKeys, []);
if (!utils.isCacheFeatureAvailable()) {
return;
}
// Validate inputs, this can cause task failure
if (!utils.isValidEvent()) {
throw new Error(
`Event Validation Error: The event type ${
process.env[Events.Key]
} is not supported because it's not tied to a branch or tag ref.`
);
}
let restoredKey: string | undefined;
let lookedUpKey: string | undefined;
const restoredKeys: string[] = [];
const errorNot = (message: string) =>
new Error(
`
No cache with the given key ${message}.
Exiting as the input "${Inputs.FailOn}" is set.
`
);
const errorNotFound = errorNot("was found");
const errorNotRestored = errorNot("could be restored");
{
const primaryKey = inputs.primaryKey;
stateProvider.setState(State.CachePrimaryKey, primaryKey);
utils.info(`Searching for a cache with the key "${primaryKey}".`);
lookedUpKey = await utils.restoreCache({
primaryKey,
restoreKeys: [],
lookupOnly: true
});
if (!lookedUpKey) {
if (
inputs.failOn?.keyType == "primary" &&
inputs.failOn?.result == "miss"
) {
throw errorNotFound;
} else {
utils.info(`Could not find a cache.`);
}
} else if (utils.isExactKeyMatch(primaryKey, lookedUpKey)) {
utils.info(
`Found a cache with the given "${Inputs.PrimaryKey}".`
);
core.setOutput(Outputs.HitPrimaryKey, true);
if (!inputs.skipRestoreOnHitPrimaryKey) {
restoredKey = await restore.restoreCache(primaryKey);
if (restoredKey) {
restoredKeys.push(...[restoredKey]);
} else if (
inputs.failOn?.keyType == "primary" &&
inputs.failOn?.result == "not-restored"
) {
throw errorNotRestored;
}
}
}
}
if (
inputs.restorePrefixesFirstMatch.length > 0 &&
!restoredKey &&
!(inputs.skipRestoreOnHitPrimaryKey && lookedUpKey)
) {
utils.info(
`
Searching for a cache using the "${
Inputs.RestorePrefixesFirstMatch
}":
${JSON.stringify(inputs.restorePrefixesFirstMatch)}
`
);
const foundKey = await utils.restoreCache({
primaryKey: "",
restoreKeys: inputs.restorePrefixesFirstMatch,
lookupOnly: true
});
if (!foundKey) {
if (
inputs.failOn?.keyType == "first-match" &&
inputs.failOn.result == "miss"
) {
throw errorNotFound;
} else {
utils.info(`Could not find a cache.`);
}
} else {
utils.info(
`Found a cache using the "${Inputs.RestorePrefixesFirstMatch}".`
);
core.setOutput(Outputs.HitFirstMatch, true);
restoredKey = await restore.restoreCache(foundKey);
if (restoredKey) {
restoredKeys.push(...[restoredKey]);
} else if (
inputs.failOn?.keyType == "first-match" &&
inputs.failOn?.result == "not-restored"
) {
throw errorNotRestored;
}
}
}
if (!(inputs.skipRestoreOnHitPrimaryKey && lookedUpKey)) {
restoredKeys.push(...(await restore.restoreCaches()));
}
restoredKey ||= "";
// Store the matched cache key in states
stateProvider.setState(State.CacheRestoredKey, restoredKey);
core.setOutput(Outputs.Hit, true);
core.setOutput(Outputs.RestoredKey, restoredKey);
core.setOutput(Outputs.RestoredKeys, restoredKeys);
return restoredKey;
} catch (error: unknown) {
core.setFailed((error as Error).message);
}
}
async function run(
stateProvider: IStateProvider,
earlyExit: boolean | undefined
): Promise<void> {
try {
await restoreImpl(stateProvider);
} catch (err) {
console.error(err);
if (earlyExit) {
process.exit(1);
}
}
// node will stay alive if any promises are not resolved,
// which is a possibility if HTTP requests are dangling
// due to retries or timeouts. We know that if we got here
// that all promises that we care about have successfully
// resolved, so simply exit with success.
if (earlyExit) {
process.exit(0);
}
}
export async function restoreOnlyRun(
earlyExit?: boolean | undefined
): Promise<void> {
await run(new NullStateProvider(), earlyExit);
}
export async function restoreRun(
earlyExit?: boolean | undefined
): Promise<void> {
await run(new StateProvider(), earlyExit);
}