Skip to content

Commit

Permalink
feat: move GitServerClient to cwrc-git-dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
ajmacdonald committed Jul 16, 2019
1 parent 174b7c4 commit 5de4509
Show file tree
Hide file tree
Showing 14 changed files with 330 additions and 683 deletions.
15 changes: 0 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
},
"dependencies": {
"bootstrap": "^3.4.1",
"cwrc-git-server-client": "^1.9.0",
"jquery": "^3.4.1",
"js-cookie": "2.1.4",
"parse-link-header": "^1.0.1",
Expand Down
246 changes: 246 additions & 0 deletions src/GitServerClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
'use strict';

let $ = window.cwrcQuery
if ($ === undefined) {
$ = require('jquery');
}

const Cookies = require('js-cookie');

let baseUrl = '';
function setServerURL(url) {
baseUrl = url;
}

function callCWRCGitWithToken(ajaxConfig) {
ajaxConfig.crossDomain = true;
ajaxConfig.xhrFields = {withCredentials: true};
ajaxConfig.headers = {};
const theJWT = Cookies.get('cwrc-token');
if (theJWT) {
ajaxConfig.headers['cwrc-token'] = theJWT;
}
return $.ajax(ajaxConfig);
}

function getDetailsForGithubUser(user) {
var url = `${baseUrl}/users/${user}`;
var ajaxConfig = {
type: 'GET',
dataType: 'json',
url: url
};
return callCWRCGitWithToken(ajaxConfig);
}

function getDetailsForOrg(org) {
var url = `${baseUrl}/orgs/${org}`;
var ajaxConfig = {
type: 'GET',
dataType: 'json',
url: url
};
return callCWRCGitWithToken(ajaxConfig);
}

function createRepo(repo, description, isPrivate) {
const ajaxConfig = {
type: 'POST',
dataType: 'json',
data: {repo, isPrivate, description },
url: `${baseUrl}/user/repos`
};
return callCWRCGitWithToken(ajaxConfig);
}

function createOrgRepo(org, repo, description, isPrivate) {
const ajaxConfig = {
type: 'POST',
dataType: 'json',
data: {repo, isPrivate, description },
url: `${baseUrl}/orgs/${org}/repos`
};
return callCWRCGitWithToken(ajaxConfig);
}

function getReposForGithubUser(githubName, page = 1, per_page = 20) {
var url = `${baseUrl}/users/${githubName}/repos`;
var ajaxConfig = {
type: 'GET',
dataType: 'json',
url: url,
data: {page, per_page}
};
return callCWRCGitWithToken(ajaxConfig);
}

function getReposForAuthenticatedGithubUser(page, per_page, affiliation) {
if (Cookies.get('cwrc-token')) {
var url = `${baseUrl}/user/repos`;
var ajaxConfig = {
type: 'GET',
dataType: 'json',
url: url,
data: {page, per_page, affiliation}
};
return callCWRCGitWithToken(ajaxConfig).then(result=>result);
} else {
return $.Deferred().reject("login").promise();
}
}

function getRepoContents(githubName) {
var url = `${baseUrl}/repos/${githubName}`;
var ajaxConfig = {
type: 'GET',
dataType: 'json',
url: url
};
return callCWRCGitWithToken(ajaxConfig).then(result=>{
return result
}, error=>{
console.log('the error in gitserverclient.getRepoContents:');
console.log(error)
return error
});
}

function getRepoContentsByDrillDown(githubName) {
var url = `${baseUrl}/repos/${githubName}/full`;
var ajaxConfig = {
type: 'GET',
dataType: 'json',
url: url
};
return callCWRCGitWithToken(ajaxConfig);
}

// repoName here is the combined owner/repo, e.g., 'jchartrand/someRepoName'

function getDoc(repoName, branch, path){

const ajaxConfig = {
type: 'GET',
dataType: 'json',
data: {branch, path},
url: `${baseUrl}/repos/${repoName}/contents`
};
return callCWRCGitWithToken(ajaxConfig);
}

function getInfoForAuthenticatedUser() {
if (Cookies.get('cwrc-token')) {
var url = `${baseUrl}/users`;
var ajaxConfig = {
type: 'GET',
dataType: 'json',
url: url
};
return callCWRCGitWithToken(ajaxConfig).then(result=>result.data);
} else {
return $.Deferred().reject("login").promise();
}
}

function getPermissionsForGithubUser(owner, repo, username) {
var ajaxConfig = {
type: 'GET',
dataType: 'json',
url: `${baseUrl}/repos/${owner}/${repo}/collaborators/${username}/permission`
};
return callCWRCGitWithToken(ajaxConfig).then(result=>result.data.permission,(fail)=>'none')
}

// sha is optional.
// If provided, the doc will be updated against that SHA.
// If not, and there is an existing doc, the file will be updated against the latest SHA in the repo.
function saveDoc(repo, path, content, branch, message, sha) {
var data = {content, sha, branch, path, message};

var ajaxConfig = {
type: 'PUT',
dataType: 'json',
data: data,
url: `${baseUrl}/repos/${repo}/doc`
};
return callCWRCGitWithToken(ajaxConfig)
}

function saveAsPullRequest(repo, path, content, branch, message, title, sha) {
var data = {sha, branch, path, message, content, title}

var ajaxConfig = {
type: 'PUT',
dataType: 'json',
data: data,
url: `${baseUrl}/repos/${repo}/pr`
};
return callCWRCGitWithToken(ajaxConfig)
}

function getTemplates() {
var ajaxConfig = {
type: 'GET',
dataType: 'json',
url: `${baseUrl}/templates`
};
return callCWRCGitWithToken(ajaxConfig).then(result=>result.data)
}

function getTemplate(templateName) {
var ajaxConfig = {
type: 'GET',
dataType: 'xml',
url: `${baseUrl}/templates/${templateName}`
};
return callCWRCGitWithToken(ajaxConfig)
}

function searchCode(query, per_page, page) {
var ajaxConfig = {
type: 'GET',
dataType: 'json',
url: `${baseUrl}/search/code`,
data: {q: query, page, per_page}

};
return callCWRCGitWithToken(ajaxConfig).then(result=>{
return result
})
}

function searchRepos(query, per_page, page) {
var ajaxConfig = {
type: 'GET',
dataType: 'json',
url: `${baseUrl}/search/repositories`,
data: {q: query, page, per_page}

};
return callCWRCGitWithToken(ajaxConfig).then(result=>{
return result
})
}

module.exports = {
setServerURL: setServerURL,
getDetailsForGithubUser: getDetailsForGithubUser,
getDetailsForOrg: getDetailsForOrg,
getReposForGithubUser: getReposForGithubUser,
getPermissionsForGithubUser: getPermissionsForGithubUser,
getReposForAuthenticatedGithubUser: getReposForAuthenticatedGithubUser,
saveDoc: saveDoc,
saveAsPullRequest: saveAsPullRequest,
createRepo: createRepo,
createOrgRepo: createOrgRepo,
getRepoContents: getRepoContents,
getRepoContentsByDrillDown: getRepoContentsByDrillDown,
getDoc: getDoc,
getInfoForAuthenticatedUser: getInfoForAuthenticatedUser,
getTemplates: getTemplates,
getTemplate: getTemplate,
searchCode: searchCode,
searchRepos: searchRepos
}


27 changes: 22 additions & 5 deletions src/Load.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ if ($ === undefined) {
}

import React, {Component, Fragment} from 'react'
import { Modal, Button, Tabs, Tab, Well, Grid, Row, Col, PanelGroup, Panel, ListGroup, ListGroupItem, ToggleButtonGroup, ToggleButton, ControlLabel, FormControl, Glyphicon } from 'react-bootstrap';
import { Modal, Button, Tabs, Tab, Well, Grid, Row, Col, PanelGroup, Panel, ListGroup, ListGroupItem, ToggleButtonGroup, ToggleButton, ControlLabel, FormGroup, FormControl, Checkbox, Glyphicon } from 'react-bootstrap';
import parseLinks from 'parse-link-header';
import cwrcGit from 'cwrc-git-server-client';
import cwrcGit from './GitServerClient.js';

import RepoResultList from "./RepoResultList.js";
import SearchResultList from "./SearchResultList.js";
Expand Down Expand Up @@ -54,7 +54,7 @@ function getSearchResults(gitName, searchTerms, requestedPage, resultsPerPage=RE
let queryString = 'language:xml ';
if (searchTerms) queryString += '"' + searchTerms + '" ';
if (gitName) queryString += "user:" + gitName;
cwrcGit.search(queryString, resultsPerPage, requestedPage).then((results)=>{
cwrcGit.searchCode(queryString, resultsPerPage, requestedPage).then((results)=>{
dfd.resolve({
items: results.data.items,
lastPage: getLastPage(results, requestedPage)
Expand Down Expand Up @@ -83,6 +83,7 @@ class LoadDialog extends Component {
this.handleTabSelect = this.handleTabSelect.bind(this);
this.handleRepoTypeSelect = this.handleRepoTypeSelect.bind(this);
this.handleAffiliationSelect = this.handleAffiliationSelect.bind(this);
this.handleXMLOnlyChange = this.handleXMLOnlyChange.bind(this);
this.doSearch = this.doSearch.bind(this);
this.handleSearchClear = this.handleSearchClear.bind(this);
this.handleSearchChange = this.handleSearchChange.bind(this);
Expand All @@ -96,6 +97,7 @@ class LoadDialog extends Component {
isSearch: false,
searchFilter: '',
query: undefined,
xmlOnly: false,

repoType: 'private',
privateReposAffiliation: 'owner',
Expand All @@ -108,7 +110,8 @@ class LoadDialog extends Component {
}
}

componentWillMount() {
componentDidMount() {
cwrcGit.setServerURL(this.props.serverURL);
this.handleTabSelect('templates');
}

Expand Down Expand Up @@ -151,6 +154,14 @@ class LoadDialog extends Component {
});
}

handleXMLOnlyChange(event) {
const value = event.target.checked;
this.setState({xmlOnly: value});
setTimeout(()=>{
// this.getRepos();
});
}

doSearch(pageNum, value=this.state.query) {
this.setState({loading: true, error: '', isSearch: true, query: value});
let promise;
Expand Down Expand Up @@ -213,6 +224,7 @@ class LoadDialog extends Component {
const loading = this.state.loading;
const isSearch = this.state.isSearch;
const error = this.state.error;
const xmlOnly = this.state.xmlOnly;
const results = this.state.results || [];
const templates = (this.state.templates || []).map((item, key)=>(
<ListGroupItem key={key} onClick={onFileUpload.bind(this, item.download_url)}>{item.name.replace(/.xml$/, '')}</ListGroupItem>
Expand Down Expand Up @@ -279,6 +291,11 @@ class LoadDialog extends Component {
</Panel>
</PanelGroup>
<SearchInput placeholder="Search within repositories" style={{marginTop: '10px'}} onChange={this.handleSearchChange} onSearch={(value)=>{this.doSearch(1,value)}} onClear={this.handleSearchClear} />
{false ? <FormGroup style={{marginTop: '10px'}}>
<Checkbox checked={xmlOnly} onChange={this.handleXMLOnlyChange}>
Only show XML repositories
</Checkbox>
</FormGroup>:''}
</Col>
<Col sm={7}>
<h4>Results</h4>
Expand All @@ -298,7 +315,7 @@ class LoadDialog extends Component {
</Well>
:
<Well bsSize="small">
<RepoResultList selectCB={onFileSelect} repos={results} />
<RepoResultList serverURL={this.props.serverURL} selectCB={onFileSelect} repos={results} />
<Paginator pagingCB={this.getRepos} currentPage={this.state.currentPage} lastPage={this.state.lastPage} />
</Well>
)
Expand Down
10 changes: 0 additions & 10 deletions src/LogOut.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
'use strict';

let $ = window.cwrcQuery
if ($ === undefined) {
let prevJQuery = window.jQuery
$ = require('jquery')
window.jQuery = $
require('bootstrap')
window.jQuery = prevJQuery
window.cwrcQuery = $
}

let Cookies = require('js-cookie');

import React, {Component, Fragment} from 'react'
Expand Down
Loading

0 comments on commit 5de4509

Please sign in to comment.