Skip to content

Commit

Permalink
Merge pull request #28 from erichbehrens/feat/add-github-enterprise-s…
Browse files Browse the repository at this point in the history
…upport

Feat/add GitHub enterprise support
  • Loading branch information
erichbehrens committed Oct 9, 2018
2 parents 5787d04 + 733d3ee commit 36ed7c4
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## v1.7.0

- Add support for GitHub enterprise

## v1.6.1

- Security update on vscode dependency
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ Note: white icons can become green or red depending on the pull request state.

- `pullRequestMonitor.count`: `number` default = `6`, number of pull requests to show

- `pullRequestMonitor.githubEnterpriseUrl`: `string` default = `null`, GitHub enterprise url. Leave empty if you don't use GitHub enterprise.

- `pullRequestMonitor.allowUnsafeSSL`: `boolean` default = `false`, allow unsafe certificates for GitHub enterprise

### Default configuration

```json
Expand All @@ -113,5 +117,7 @@ Note: white icons can become green or red depending on the pull request state.
"pullRequestMonitor.showMerged": false,
"pullRequestMonitor.autostart": true,
"pullRequestMonitor.count": 6,
"pullRequestMonitor.githubEnterpriseUrl": null,
"pullRequestMonitor.allowUnsafeSSL": false,
}
```
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "GitHub Pull Request Monitor",
"description": "Monitors the status of GitHub pull requests. Checks for conflicts, status reports, reviews and whether the branch is up to date.",
"icon": "icon.png",
"version": "1.6.1",
"version": "1.7.0",
"publisher": "erichbehrens",
"author": "Erich Behrens <me@eb1.it>",
"license": "MIT",
Expand Down Expand Up @@ -86,6 +86,16 @@
"description": "Number of pull requests to show",
"type": "number",
"default": 6
},
"pullRequestMonitor.githubEnterpriseUrl": {
"description": "The Url to a Github Enterprise",
"type": "string",
"default": null
},
"pullRequestMonitor.allowUnsafeSSL": {
"description": "Stop the extension from verifying certs (turn on if you get an error)",
"type": "boolean",
"default": false
}
}
}
Expand All @@ -103,6 +113,7 @@
"eslint-plugin-import": "^2.13.0"
},
"dependencies": {
"https": "^1.0.0",
"isomorphic-fetch": "^2.2.1",
"vscode": "^1.1.18"
},
Expand Down
12 changes: 9 additions & 3 deletions src/extension.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const vscode = require('vscode'); // eslint-disable-line import/no-unresolved
const { clearTimeout, setTimeout } = require('timers');
const { getCommitIcon, getColor, getMergeableIcon, getMergeableState, getPullRequestStateIcon, getReviewState } = require('./utils');
const { getCommitIcon, getColor, getMergeableIcon, getMergeableState, getPullRequestStateIcon, getReviewState, getEndpointUrl } = require('./utils');
const { loadPullRequests, loadRepositories } = require('./requests');

const MODES = {
Expand Down Expand Up @@ -35,7 +35,11 @@ async function getPullRequests(context, showError) {
const showMerged = vscode.workspace.getConfiguration('pullRequestMonitor').get('showMerged');
const showClosed = vscode.workspace.getConfiguration('pullRequestMonitor').get('showClosed');
const count = vscode.workspace.getConfiguration('pullRequestMonitor').get('count');
const updatedPullRequests = await loadPullRequests(context.globalState.get('token'), { mode, showMerged, showClosed, repository, showError, count });
// configure the URL settings
const url = getEndpointUrl(vscode.workspace.getConfiguration('pullRequestMonitor').get('githubEnterpriseUrl'));
// configure SSL
const allowUnsafeSSL = vscode.workspace.getConfiguration('PullRequestMonitor').get('allowUnsafeSSL');
const updatedPullRequests = await loadPullRequests(context.globalState.get('token'), { mode, showMerged, showClosed, repository, showError, count, url, allowUnsafeSSL });
if (updatedPullRequests.code === 401) {
refreshButton.command = 'PullRequestMonitor.setToken';
refreshButton.text = '$(key)';
Expand Down Expand Up @@ -178,7 +182,9 @@ function activate(context) {
context.subscriptions.push(disposable);

disposable = vscode.commands.registerCommand('PullRequestMonitor.selectRepository', async () => {
const { data: repositories } = await loadRepositories(context.globalState.get('token'));
const url = getEndpointUrl(vscode.workspace.getConfiguration('pullRequestMonitor').get('githubEnterpriseUrl'));
const allowUnsafeSSL = vscode.workspace.getConfiguration('PullRequestMonitor').get('allowUnsafeSSL');
const { data: repositories } = await loadRepositories(context.globalState.get('token'), { url, allowUnsafeSSL });
if (!repositories) {
return;
}
Expand Down
25 changes: 18 additions & 7 deletions src/requests.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
const { window } = require('vscode'); // eslint-disable-line import/no-unresolved
const fetch = require('isomorphic-fetch');
const https = require('https');
const queries = require('./queries');
const { getStatesFilter } = require('./utils');

let errorCount = 0;

async function execQuery(token, query, showError) {
async function execQuery(token, query, showError, graphqlEndpoint, allowUnsafeSSL = false) {
if (!graphqlEndpoint.startsWith('https://') && showError) {
window.showErrorMessage('GitHub enterprise url must start with https://');
return { status: 'error' };
}
if (showError) {
errorCount = 0;
}
if (!token) {
window.showWarningMessage('Pull Request Monitor needs a token');
return;
return { status: 'error' };
}
try {
const res = await fetch('https://api.github.com/graphql', {
const res = await fetch(graphqlEndpoint, {
method: 'POST',
headers: { Authorization: `bearer ${token}` },
body: JSON.stringify({ query }),
agent: new https.Agent({ rejectUnauthorized: !allowUnsafeSSL }),
});
if (res.status === 200) {
const { data } = await res.json();
Expand All @@ -27,6 +33,8 @@ async function execQuery(token, query, showError) {
window.showErrorMessage('Pull Request Monitor token not authorized');
return { status: 'error', code: 401 };
}
window.showErrorMessage(`Pull Request Monitor http error ${res.status}`);
return { status: 'error', code: res.status };
} catch (e) {
console.error(e); // eslint-disable-line no-console
if (!showError) {
Expand All @@ -40,7 +48,9 @@ async function execQuery(token, query, showError) {
}

exports.loadPullRequests =
async (token, { mode, showMerged, showClosed, repository, showError, count }) => {
async (token,
{ mode, showMerged, showClosed, repository, showError, count, url, allowUnsafeSSL },
) => {
let query = queries[mode]
.replace('@states', getStatesFilter(showMerged, showClosed))
.replace('@count', count);
Expand All @@ -51,13 +61,14 @@ exports.loadPullRequests =
}
query = query.replace('@owner', repository.owner).replace('@name', repository.name);
}
const { status, code, data } = await execQuery(token, query, showError);
const { status, code, data } = await execQuery(token, query, showError, url, allowUnsafeSSL);
const pullRequests = data && data[mode].pullRequests.nodes;
return { status, code, data: pullRequests };
};

exports.loadRepositories = async (token) => {
const { status, code, data } = await execQuery(token, queries.repositories);
exports.loadRepositories = async (token, { url, allowUnsafeSSL }) => {
const query = queries.repositories;
const { status, code, data } = await execQuery(token, query, true, url, allowUnsafeSSL);
const repositories = data && data.viewer.repositories.nodes;
return { status, code, data: repositories };
};
7 changes: 7 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ exports.getReviewState = (reviews) => {
};
};

exports.getEndpointUrl = (githubEnterpriseUrl) => {
if (githubEnterpriseUrl) {
return `${githubEnterpriseUrl}/api/graphql`;
}
return 'https://api.github.com/graphql';
};

/* test exports */
exports.getReviewsByAuthor = getReviewsByAuthor;
exports.getLastStateByAuthor = getLastStateByAuthor;

0 comments on commit 36ed7c4

Please sign in to comment.