Skip to content

Commit 74cad24

Browse files
b3nk3Ben Szabofgreinacher
authored
feat: add HTTP(S) proxy support (#310)
Co-authored-by: Ben Szabo <[email protected]> Co-authored-by: Florian Greinacher <[email protected]>
1 parent f74cf8c commit 74cad24

File tree

8 files changed

+16156
-1847
lines changed

8 files changed

+16156
-1847
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ Create a [personal access token](https://docs.gitlab.com/ce/user/profile/persona
5656
| `GL_TOKEN` or `GITLAB_TOKEN` | **Required.** The token used to authenticate with GitLab. |
5757
| `GL_URL` or `GITLAB_URL` | The GitLab endpoint. |
5858
| `GL_PREFIX` or `GITLAB_PREFIX` | The GitLab API prefix. |
59+
| `HTTP_PROXY` or `HTTPS_PROXY` | HTTP or HTTPS proxy to use. |
60+
61+
#### Proxy configuration
62+
63+
The plugin supports passing requests through a proxy server.
64+
65+
You can configure a proxy server via the `HTTPS_PROXY` environment variable: `HTTPS_PROXY=http://proxyurl.com:8080`
66+
67+
If your proxy server requires authentication embed the username and password in the URL: `HTTPS_PROXY=http://user:[email protected]:8080`
68+
69+
If your GitLab instance is exposed via plain HTTP (not recommended!) use `HTTP_PROXY` instead.
5970

6071
### Options
6172

lib/publish.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = async (pluginConfig, context) => {
1818
nextRelease: {gitTag, gitHead, notes},
1919
logger,
2020
} = context;
21-
const {gitlabToken, gitlabUrl, gitlabApiUrl, assets, milestones} = resolveConfig(pluginConfig, context);
21+
const {gitlabToken, gitlabUrl, gitlabApiUrl, assets, milestones, proxy} = resolveConfig(pluginConfig, context);
2222
const assetsList = [];
2323
const repoId = getRepoId(context, gitlabUrl, repositoryUrl);
2424
const encodedRepoId = encodeURIComponent(repoId);
@@ -67,7 +67,7 @@ module.exports = async (pluginConfig, context) => {
6767

6868
let response;
6969
try {
70-
response = await got.post(uploadEndpoint, {...apiOptions, body: form}).json();
70+
response = await got.post(uploadEndpoint, {...apiOptions, ...proxy, body: form}).json();
7171
} catch (error) {
7272
logger.error('An error occurred while uploading %s to the GitLab project uploads API:\n%O', file, error);
7373
throw error;
@@ -109,6 +109,7 @@ module.exports = async (pluginConfig, context) => {
109109
try {
110110
await got.post(createReleaseEndpoint, {
111111
...apiOptions,
112+
...proxy,
112113
json,
113114
});
114115
} catch (error) {

lib/resolve-config.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const {castArray, isNil} = require('lodash');
22
const urlJoin = require('url-join');
3+
const {HttpProxyAgent, HttpsProxyAgent} = require('hpagent');
34

45
module.exports = (
56
{gitlabUrl, gitlabApiPathPrefix, assets, milestones, successComment},
@@ -15,6 +16,8 @@ module.exports = (
1516
GITLAB_URL,
1617
GL_PREFIX,
1718
GITLAB_PREFIX,
19+
HTTP_PROXY,
20+
HTTPS_PROXY,
1821
},
1922
}
2023
) => {
@@ -41,5 +44,40 @@ module.exports = (
4144
assets: assets ? castArray(assets) : assets,
4245
milestones: milestones ? castArray(milestones) : milestones,
4346
successComment,
47+
proxy: getProxyConfiguration(defaultedGitlabUrl, HTTP_PROXY, HTTPS_PROXY),
4448
};
4549
};
50+
51+
function getProxyConfiguration(gitlabUrl, HTTP_PROXY, HTTPS_PROXY) {
52+
const sharedParameters = {
53+
keepAlive: true,
54+
keepAliveMsecs: 1000,
55+
maxSockets: 256,
56+
maxFreeSockets: 256,
57+
scheduling: 'lifo',
58+
};
59+
60+
if (HTTP_PROXY && gitlabUrl.startsWith('http://')) {
61+
return {
62+
agent: {
63+
http: new HttpProxyAgent({
64+
...sharedParameters,
65+
proxy: HTTP_PROXY,
66+
}),
67+
},
68+
};
69+
}
70+
71+
if (HTTPS_PROXY && gitlabUrl.startsWith('https://')) {
72+
return {
73+
agent: {
74+
https: new HttpsProxyAgent({
75+
...sharedParameters,
76+
proxy: HTTPS_PROXY,
77+
}),
78+
},
79+
};
80+
}
81+
82+
return {};
83+
}

lib/success.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = async (pluginConfig, context) => {
1414
commits,
1515
releases,
1616
} = context;
17-
const {gitlabToken, gitlabUrl, gitlabApiUrl, successComment} = resolveConfig(pluginConfig, context);
17+
const {gitlabToken, gitlabUrl, gitlabApiUrl, successComment, proxy} = resolveConfig(pluginConfig, context);
1818
const repoId = getRepoId(context, gitlabUrl, repositoryUrl);
1919
const encodedRepoId = encodeURIComponent(repoId);
2020
const apiOptions = {headers: {'PRIVATE-TOKEN': gitlabToken}};
@@ -32,6 +32,7 @@ module.exports = async (pluginConfig, context) => {
3232
: getSuccessComment(issue, releaseInfos, nextRelease);
3333
return got.post(issueNotesEndpoint, {
3434
...apiOptions,
35+
...proxy,
3536
json: {body},
3637
});
3738
};
@@ -47,6 +48,7 @@ module.exports = async (pluginConfig, context) => {
4748
: getSuccessComment({isMergeRequest: true, ...mergeRequest}, releaseInfos, nextRelease);
4849
return got.post(mergeRequestNotesEndpoint, {
4950
...apiOptions,
51+
...proxy,
5052
json: {body},
5153
});
5254
};
@@ -60,6 +62,7 @@ module.exports = async (pluginConfig, context) => {
6062
const relatedMergeRequests = await got
6163
.get(relatedMergeRequestsEndpoint, {
6264
...apiOptions,
65+
...proxy,
6366
})
6467
.json();
6568

@@ -75,6 +78,7 @@ module.exports = async (pluginConfig, context) => {
7578
const relatedIssues = await got
7679
.get(relatedIssuesEndpoint, {
7780
...apiOptions,
81+
...proxy,
7882
})
7983
.json();
8084

lib/verify.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = async (pluginConfig, context) => {
2323
logger,
2424
} = context;
2525
const errors = [];
26-
const {gitlabToken, gitlabUrl, gitlabApiUrl, assets} = resolveConfig(pluginConfig, context);
26+
const {gitlabToken, gitlabUrl, gitlabApiUrl, assets, proxy} = resolveConfig(pluginConfig, context);
2727
const repoId = getRepoId(context, gitlabUrl, repositoryUrl);
2828
debug('apiUrl: %o', gitlabApiUrl);
2929
debug('repoId: %o', repoId);
@@ -52,6 +52,7 @@ module.exports = async (pluginConfig, context) => {
5252
} = await got
5353
.get(urlJoin(gitlabApiUrl, `/projects/${encodeURIComponent(repoId)}`), {
5454
headers: {'PRIVATE-TOKEN': gitlabToken},
55+
...proxy,
5556
})
5657
.json());
5758

0 commit comments

Comments
 (0)