Skip to content

Commit

Permalink
feat: add HTTP(S) proxy support (#310)
Browse files Browse the repository at this point in the history
Co-authored-by: Ben Szabo <ben.szabo@hl.co.uk>
Co-authored-by: Florian Greinacher <florian.greinacher@siemens.com>
  • Loading branch information
3 people committed Apr 5, 2022
1 parent f74cf8c commit 74cad24
Show file tree
Hide file tree
Showing 8 changed files with 16,156 additions and 1,847 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ Create a [personal access token](https://docs.gitlab.com/ce/user/profile/persona
| `GL_TOKEN` or `GITLAB_TOKEN` | **Required.** The token used to authenticate with GitLab. |
| `GL_URL` or `GITLAB_URL` | The GitLab endpoint. |
| `GL_PREFIX` or `GITLAB_PREFIX` | The GitLab API prefix. |
| `HTTP_PROXY` or `HTTPS_PROXY` | HTTP or HTTPS proxy to use. |

#### Proxy configuration

The plugin supports passing requests through a proxy server.

You can configure a proxy server via the `HTTPS_PROXY` environment variable: `HTTPS_PROXY=http://proxyurl.com:8080`

If your proxy server requires authentication embed the username and password in the URL: `HTTPS_PROXY=http://user:pwd@proxyurl.com:8080`

If your GitLab instance is exposed via plain HTTP (not recommended!) use `HTTP_PROXY` instead.

### Options

Expand Down
5 changes: 3 additions & 2 deletions lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = async (pluginConfig, context) => {
nextRelease: {gitTag, gitHead, notes},
logger,
} = context;
const {gitlabToken, gitlabUrl, gitlabApiUrl, assets, milestones} = resolveConfig(pluginConfig, context);
const {gitlabToken, gitlabUrl, gitlabApiUrl, assets, milestones, proxy} = resolveConfig(pluginConfig, context);
const assetsList = [];
const repoId = getRepoId(context, gitlabUrl, repositoryUrl);
const encodedRepoId = encodeURIComponent(repoId);
Expand Down Expand Up @@ -67,7 +67,7 @@ module.exports = async (pluginConfig, context) => {

let response;
try {
response = await got.post(uploadEndpoint, {...apiOptions, body: form}).json();
response = await got.post(uploadEndpoint, {...apiOptions, ...proxy, body: form}).json();
} catch (error) {
logger.error('An error occurred while uploading %s to the GitLab project uploads API:\n%O', file, error);
throw error;
Expand Down Expand Up @@ -109,6 +109,7 @@ module.exports = async (pluginConfig, context) => {
try {
await got.post(createReleaseEndpoint, {
...apiOptions,
...proxy,
json,
});
} catch (error) {
Expand Down
38 changes: 38 additions & 0 deletions lib/resolve-config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const {castArray, isNil} = require('lodash');
const urlJoin = require('url-join');
const {HttpProxyAgent, HttpsProxyAgent} = require('hpagent');

module.exports = (
{gitlabUrl, gitlabApiPathPrefix, assets, milestones, successComment},
Expand All @@ -15,6 +16,8 @@ module.exports = (
GITLAB_URL,
GL_PREFIX,
GITLAB_PREFIX,
HTTP_PROXY,
HTTPS_PROXY,
},
}
) => {
Expand All @@ -41,5 +44,40 @@ module.exports = (
assets: assets ? castArray(assets) : assets,
milestones: milestones ? castArray(milestones) : milestones,
successComment,
proxy: getProxyConfiguration(defaultedGitlabUrl, HTTP_PROXY, HTTPS_PROXY),
};
};

function getProxyConfiguration(gitlabUrl, HTTP_PROXY, HTTPS_PROXY) {
const sharedParameters = {
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
};

if (HTTP_PROXY && gitlabUrl.startsWith('http://')) {
return {
agent: {
http: new HttpProxyAgent({
...sharedParameters,
proxy: HTTP_PROXY,
}),
},
};
}

if (HTTPS_PROXY && gitlabUrl.startsWith('https://')) {
return {
agent: {
https: new HttpsProxyAgent({
...sharedParameters,
proxy: HTTPS_PROXY,
}),
},
};
}

return {};
}
6 changes: 5 additions & 1 deletion lib/success.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = async (pluginConfig, context) => {
commits,
releases,
} = context;
const {gitlabToken, gitlabUrl, gitlabApiUrl, successComment} = resolveConfig(pluginConfig, context);
const {gitlabToken, gitlabUrl, gitlabApiUrl, successComment, proxy} = resolveConfig(pluginConfig, context);
const repoId = getRepoId(context, gitlabUrl, repositoryUrl);
const encodedRepoId = encodeURIComponent(repoId);
const apiOptions = {headers: {'PRIVATE-TOKEN': gitlabToken}};
Expand All @@ -32,6 +32,7 @@ module.exports = async (pluginConfig, context) => {
: getSuccessComment(issue, releaseInfos, nextRelease);
return got.post(issueNotesEndpoint, {
...apiOptions,
...proxy,
json: {body},
});
};
Expand All @@ -47,6 +48,7 @@ module.exports = async (pluginConfig, context) => {
: getSuccessComment({isMergeRequest: true, ...mergeRequest}, releaseInfos, nextRelease);
return got.post(mergeRequestNotesEndpoint, {
...apiOptions,
...proxy,
json: {body},
});
};
Expand All @@ -60,6 +62,7 @@ module.exports = async (pluginConfig, context) => {
const relatedMergeRequests = await got
.get(relatedMergeRequestsEndpoint, {
...apiOptions,
...proxy,
})
.json();

Expand All @@ -75,6 +78,7 @@ module.exports = async (pluginConfig, context) => {
const relatedIssues = await got
.get(relatedIssuesEndpoint, {
...apiOptions,
...proxy,
})
.json();

Expand Down
3 changes: 2 additions & 1 deletion lib/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = async (pluginConfig, context) => {
logger,
} = context;
const errors = [];
const {gitlabToken, gitlabUrl, gitlabApiUrl, assets} = resolveConfig(pluginConfig, context);
const {gitlabToken, gitlabUrl, gitlabApiUrl, assets, proxy} = resolveConfig(pluginConfig, context);
const repoId = getRepoId(context, gitlabUrl, repositoryUrl);
debug('apiUrl: %o', gitlabApiUrl);
debug('repoId: %o', repoId);
Expand Down Expand Up @@ -52,6 +52,7 @@ module.exports = async (pluginConfig, context) => {
} = await got
.get(urlJoin(gitlabApiUrl, `/projects/${encodeURIComponent(repoId)}`), {
headers: {'PRIVATE-TOKEN': gitlabToken},
...proxy,
})
.json());

Expand Down

0 comments on commit 74cad24

Please sign in to comment.