-
Notifications
You must be signed in to change notification settings - Fork 52
/
pinned_version.js
138 lines (125 loc) · 3.69 KB
/
pinned_version.js
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
import { handleBuildError } from '../error/handle.js'
import { getMajorVersion, isPrerelease } from '../utils/semver.js'
// Retrieve plugin's pinned major versions by fetching the latest `PluginRun`
// Only applies to `netlify.toml`-only installed plugins.
export const addPinnedVersions = async function ({ pluginsOptions, api, siteInfo: { id: siteId }, sendStatus }) {
if (!sendStatus || api === undefined || !siteId) {
return pluginsOptions
}
const packages = pluginsOptions.filter(shouldFetchPinVersion).map(getPackageName)
if (packages.length === 0) {
return pluginsOptions
}
const pluginRuns = await api.getLatestPluginRuns({ site_id: siteId, packages, state: 'success' })
const pluginsOptionsA = pluginsOptions.map((pluginOption) => addPinnedVersion(pluginOption, pluginRuns))
return pluginsOptionsA
}
const shouldFetchPinVersion = function ({ pinnedVersion, loadedFrom, origin }) {
return pinnedVersion === undefined && loadedFrom === 'auto_install' && origin === 'config'
}
const getPackageName = function ({ packageName }) {
return packageName
}
const addPinnedVersion = function (pluginOptions, pluginRuns) {
const foundPluginRun = pluginRuns.find((pluginRun) => pluginRun.package === pluginOptions.packageName)
if (foundPluginRun === undefined) {
return pluginOptions
}
const pinnedVersion = getMajorVersion(foundPluginRun.version)
return pinnedVersion === undefined ? pluginOptions : { ...pluginOptions, pinnedVersion }
}
// Send an API request to pin plugins' major versions.
// Only applies to UI-installed plugins.
export const pinPlugins = async function ({
pluginsOptions,
failedPlugins,
api,
siteInfo: { id: siteId },
childEnv,
mode,
netlifyConfig,
errorMonitor,
logs,
debug,
testOpts,
sendStatus,
}) {
if ((mode !== 'buildbot' && !sendStatus) || api === undefined || !siteId) {
return
}
const pluginsOptionsA = pluginsOptions.filter((pluginOptions) => shouldPinVersion({ pluginOptions, failedPlugins }))
await Promise.all(
pluginsOptionsA.map((pluginOptions) =>
pinPlugin({
pluginOptions,
api,
childEnv,
mode,
netlifyConfig,
errorMonitor,
logs,
debug,
testOpts,
siteId,
}),
),
)
}
// Only pin version if:
// - the plugin's version has not been pinned yet
// - the plugin was installed in the UI
// - both the build and the plugin succeeded
const shouldPinVersion = function ({
pluginOptions: {
packageName,
pinnedVersion,
pluginPackageJson: { version },
loadedFrom,
origin,
},
failedPlugins,
}) {
return (
!isPrerelease(version) &&
pinnedVersion === undefined &&
loadedFrom === 'auto_install' &&
origin === 'ui' &&
!failedPlugins.includes(packageName)
)
}
const pinPlugin = async function ({
pluginOptions: {
packageName,
pluginPackageJson: { version },
},
api,
childEnv,
mode,
netlifyConfig,
errorMonitor,
logs,
debug,
testOpts,
siteId,
}) {
const pinnedVersion = getMajorVersion(version)
try {
await api.updatePlugin({
package: encodeURIComponent(packageName),
site_id: siteId,
body: { pinned_version: pinnedVersion },
})
// Bitballoon API randomly fails with 502.
// Builds should be successful when this API call fails, but we still want
// to report the error both in logs and in error monitoring.
} catch (error) {
if (shouldIgnoreError(error)) {
return
}
await handleBuildError(error, { errorMonitor, netlifyConfig, childEnv, mode, logs, debug, testOpts })
}
}
// Status is 404 if the plugin is uninstalled while the build is ongoing.
const shouldIgnoreError = function ({ status }) {
return status === 404
}