Skip to content

Commit

Permalink
Merge pull request #15 from erichbehrens/feature/handle-network-errors
Browse files Browse the repository at this point in the history
Feature/handle network errors
  • Loading branch information
erichbehrens authored Mar 31, 2018
2 parents c1d95c7 + 132b03b commit a8d63f0
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 35 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
# Change Log

## v1.5.1

- Improved network and auth error handling

## v1.4.0

- Autostart extension with VS Code

## v1.3.1

- Update readme with icons

## v1.3.0

- Add configuration for refresh interval and to show/hide closed/merged pull requests
- Major code cleanup

## v1.2.0

- Changed name to *GitHub Pull Request Monitor*
- Use more distinguishable icons for failing tests and conflicts
- Show icon for comments
- Improve showing icons for `approved` and `change requested`

## v1.1.0

- Save user settings
- Rename `setRepository` command to `enterRepositoryName`

## v1.0.4

- Use `isomorphic-fetch` to support systems other than Windows

## v1.0.0

- Initial release
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# GitHub Pull Request Monitor

[![Travis](https://img.shields.io/travis/erichbehrens/pull-request-monitor.svg)](https://travis-ci.org/erichbehrens/pull-request-monitor)
[![Marketplace Version](https://vsmarketplacebadge.apphb.com/version/erichbehrens.pull-request-monitor.svg)](https://marketplace.visualstudio.com/items?itemName=erichbehrens.pull-request-monitor)
[![Installs](https://vsmarketplacebadge.apphb.com/installs/erichbehrens.pull-request-monitor.svg)](https://marketplace.visualstudio.com/items?itemName=erichbehrens.pull-request-monitor)


This extension uses the GitHub api to monitor the state of your pull requests and let you know when it's time to merge or if someone requested changes.

![Statusbar items](images/statusBarItems.png)
Expand All @@ -18,6 +18,7 @@ Source code on GitHub: https://github.com/erichbehrens/pull-request-monitor
- Colors and icons to identify pull requests that require attention

### Colors

![color green](images/color-green.png) **Green**: there are no conflicts, build is passing (if any), reviews are approved (if any)

![color red](images/color-red.png) **Red**: opposite of green or pull request closed
Expand All @@ -27,7 +28,9 @@ Source code on GitHub: https://github.com/erichbehrens/pull-request-monitor
![color violet](images/color-violet.png) **Violet**: merged

### Icons

Note: white icons can become green or red depending on the pull request state.

#### State

![icon](images/icon-state-open.png) Open
Expand All @@ -43,13 +46,15 @@ Note: white icons can become green or red depending on the pull request state.
![icon](images/icon-build-ko.png) Build fails

#### Branch

![icon](images/icon-mergeable-ok.png) Mergeable

![icon](images/icon-mergeable-ko.png) Conflicts

![icon](images/icon-mergeable-unknown.png) Unknown mergeable state

#### Reviews

![icon](images/icon-reviews-ok.png) Approved reviews

![icon](images/icon-reviews-ko.png) Changes requested
Expand Down Expand Up @@ -84,7 +89,7 @@ Note: white icons can become green or red depending on the pull request state.

- `PullRequestMonitor.selectRepository`: select the repository to monitor through the list of your repositories (some private repositories will not appear here, in this case use `PullRequestMonitor.enterRepositoryName` )

- `PullRequestMonitor.enterRepositoryName`: set the private repository name you want to monitor. Something like `your-team-nam/awesome-project`
- `PullRequestMonitor.enterRepositoryName`: set the private repository name you want to monitor. Something like `your-team-name/awesome-project`

## Extension configuration

Expand All @@ -97,6 +102,7 @@ Note: white icons can become green or red depending on the pull request state.
- `pullRequestMonitor.autostart` `boolean`, automatically start the extension

### Default configuration

```json
{
"pullRequestMonitor.refreshInterval": 60,
Expand All @@ -105,10 +111,3 @@ Note: white icons can become green or red depending on the pull request state.
"pullRequestMonitor.autostart": true,
}
```
## Known Issues

Please report any bugs here: https://github.com/erichbehrens/pull-request-monitor/issues




2 changes: 1 addition & 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.4.0",
"version": "1.5.1",
"publisher": "erichbehrens",
"author": "Erich Behrens <[email protected]>",
"license": "MIT",
Expand Down
45 changes: 36 additions & 9 deletions src/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,34 @@ function createStatusBarItem(context, prId, url) {
statusBarItems[prId] = statusBarItem;
}

async function getPullRequests(context) {
let pullRequests = [];
let refreshButton;

async function getPullRequests(context, showError) {
clearTimeout(timer);
let overrideRefreshInterval;
try {
const mode = context.globalState.get('mode', MODES.VIEWER);
const repository = context.globalState.get('currentRepository');
const showMerged = vscode.workspace.getConfiguration('pullRequestMonitor').get('showMerged');
const showClosed = vscode.workspace.getConfiguration('pullRequestMonitor').get('showClosed');
const pullRequests = await loadPullRequests(context.globalState.get('token'), { mode, showMerged, showClosed, repository });
const updatedPullRequests = await loadPullRequests(context.globalState.get('token'), { mode, showMerged, showClosed, repository, showError });
if (updatedPullRequests.code === 401) {
refreshButton.command = 'PullRequestMonitor.setToken';
refreshButton.text = '$(key)';
refreshButton.tooltip = 'Set GitHub token for Pull Request Monitor';
return;
} else if (updatedPullRequests.status === 'error') {
refreshButton.command = 'PullRequestMonitor.refresh.showError';
refreshButton.text = '$(zap)';
refreshButton.tooltip = 'Connect Pull Request Monitor';
overrideRefreshInterval = 15;
} else {
refreshButton.command = 'PullRequestMonitor.refresh';
refreshButton.text = '$(sync)';
refreshButton.tooltip = 'Refresh Pull Request Monitor';
pullRequests = updatedPullRequests.data;
}
if (!statusBarItems) {
statusBarItems = {};
} else {
Expand Down Expand Up @@ -73,7 +93,7 @@ async function getPullRequests(context) {
vscode.window.showErrorMessage('Pull Request Monitor error rendering');
}
// we store the interval in seconds and prevent the users to set a value lower than 15s
const userRefreshInterval = Number.parseInt(vscode.workspace.getConfiguration('pullRequestMonitor').get('refreshInterval'), 10);
const userRefreshInterval = overrideRefreshInterval || Number.parseInt(vscode.workspace.getConfiguration('pullRequestMonitor').get('refreshInterval'), 10);
const refreshInterval = userRefreshInterval >= 15 ? userRefreshInterval : 60;
if (!refreshInterval) return;
timer = setTimeout(() => getPullRequests(context), refreshInterval * 1000);
Expand All @@ -93,12 +113,12 @@ function setRepository(context, nameWithOwner) {
}

function activate(context) {
const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right);
statusBarItem.command = 'PullRequestMonitor.refresh';
statusBarItem.text = '$(sync)';
statusBarItem.tooltip = 'Refresh Pull Request Monitor';
statusBarItem.show();
context.subscriptions.push(statusBarItem);
refreshButton = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right);
refreshButton.command = 'PullRequestMonitor.refresh';
refreshButton.text = '$(sync)';
refreshButton.tooltip = 'Refresh Pull Request Monitor';
refreshButton.show();
context.subscriptions.push(refreshButton);

let disposable = vscode.commands.registerCommand('PullRequestMonitor.start', (options = {}) => {
const { silent } = options;
Expand All @@ -122,6 +142,13 @@ function activate(context) {

context.subscriptions.push(disposable);

disposable = vscode.commands.registerCommand('PullRequestMonitor.refresh.showError', () => {
getPullRequests(context, true);
vscode.window.showInformationMessage('Pull Request Monitor refreshing');
});

context.subscriptions.push(disposable);

disposable = vscode.commands.registerCommand('PullRequestMonitor.setMode', async () => {
const selectedMode = await vscode.window.showQuickPick([MODES.REPOSITORY, MODES.VIEWER], { placeHolder: 'Please select the mode' });
if (selectedMode && context.globalState.get('mode') !== selectedMode) {
Expand Down
50 changes: 34 additions & 16 deletions src/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ const fetch = require('isomorphic-fetch');
const queries = require('./queries');
const { getStatesFilter } = require('./utils');

async function execQuery(token, query) {
let errorCount = 0;

async function execQuery(token, query, showError) {
if (showError) {
errorCount = 0;
}
if (!token) {
window.showWarningMessage('Pull Request Monitor needs a token');
return;
Expand All @@ -14,27 +19,40 @@ async function execQuery(token, query) {
headers: { Authorization: `bearer ${token}` },
body: JSON.stringify({ query }),
});
const { data } = await res.json();
return data;
if (res.status === 200) {
const { data } = await res.json();
return { status: 'ok', data };
}
if (res.status === 401 || res.status === 403) {
window.showErrorMessage('Pull Request Monitor token not authorized');
return { status: 'error', code: 401 };
}
} catch (e) {
console.error(e); // eslint-disable-line no-console
window.showErrorMessage('Pull Request Monitor error fetching data');
if (!showError) {
errorCount += 1;
}
if (showError || errorCount === 2) {
window.showErrorMessage('Pull Request Monitor error fetching data');
}
return { status: 'error' };
}
}

exports.loadPullRequests = async (token, { mode, showMerged, showClosed, repository }) => {
let query = queries[mode].replace('@states', getStatesFilter(showMerged, showClosed));
if (mode === 'repository') {
if (!repository) {
window.showWarningMessage('Pull Request Monitor needs a repository to watch');
return;
exports.loadPullRequests =
async (token, { mode, showMerged, showClosed, repository, showError }) => {
let query = queries[mode].replace('@states', getStatesFilter(showMerged, showClosed));
if (mode === 'repository') {
if (!repository) {
window.showWarningMessage('Pull Request Monitor needs a repository to watch');
return { status: 'error' };
}
query = query.replace('@owner', repository.owner).replace('@name', repository.name);
}
query = query.replace('@owner', repository.owner).replace('@name', repository.name);
}
const data = await execQuery(token, query);
const pullRequests = data[mode].pullRequests.nodes;
return pullRequests;
};
const { status, code, data } = await execQuery(token, query, showError);
const pullRequests = data && data[mode].pullRequests.nodes;
return { status, code, data: pullRequests };
};

exports.loadRepositories = async (token) => {
const data = await execQuery(token, queries.repositories);
Expand Down
1 change: 1 addition & 0 deletions test/extension.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ suite('Extension Tests', () => {
'PullRequestMonitor.start',
'PullRequestMonitor.stop',
'PullRequestMonitor.refresh',
'PullRequestMonitor.refresh.showError',
'PullRequestMonitor.setMode',
'PullRequestMonitor.selectRepository',
'PullRequestMonitor.enterRepositoryName',
Expand Down

0 comments on commit a8d63f0

Please sign in to comment.