Skip to content

Commit

Permalink
feat: add gitlab support
Browse files Browse the repository at this point in the history
  • Loading branch information
ajmacdonald committed Jul 16, 2019
1 parent 5de4509 commit da186ca
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 14 deletions.
55 changes: 45 additions & 10 deletions src/GitServerClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ function setServerURL(url) {
baseUrl = url;
}

let isGitLab = false;
function useGitLab(useIt) {
isGitLab = useIt;
}

function callCWRCGitWithToken(ajaxConfig) {
ajaxConfig.crossDomain = true;
ajaxConfig.xhrFields = {withCredentials: true};
Expand Down Expand Up @@ -44,11 +49,15 @@ function getDetailsForOrg(org) {
}

function createRepo(repo, description, isPrivate) {
var url = `${baseUrl}/user/repos`;
if (isGitLab) {
url=`${baseUrl}/projects?name=`+repo
}
const ajaxConfig = {
type: 'POST',
dataType: 'json',
data: {repo, isPrivate, description },
url: `${baseUrl}/user/repos`
url: url
};
return callCWRCGitWithToken(ajaxConfig);
}
Expand All @@ -64,7 +73,10 @@ function createOrgRepo(org, repo, description, isPrivate) {
}

function getReposForGithubUser(githubName, page = 1, per_page = 20) {
var url = `${baseUrl}/users/${githubName}/repos`;
var url = `${baseUrl}/users/${githubName}/repos`;
if (isGitLab) {
url=`${baseUrl}/users/${githubName}/projects`;
}
var ajaxConfig = {
type: 'GET',
dataType: 'json',
Expand All @@ -77,6 +89,9 @@ function getReposForGithubUser(githubName, page = 1, per_page = 20) {
function getReposForAuthenticatedGithubUser(page, per_page, affiliation) {
if (Cookies.get('cwrc-token')) {
var url = `${baseUrl}/user/repos`;
if (isGitLab) {
url= `${baseUrl}/projects`;
}
var ajaxConfig = {
type: 'GET',
dataType: 'json',
Expand All @@ -90,7 +105,10 @@ function getReposForAuthenticatedGithubUser(page, per_page, affiliation) {
}

function getRepoContents(githubName) {
var url = `${baseUrl}/repos/${githubName}`;
var url = `${baseUrl}/repos/${githubName}`;
if (isGitLab) {
url= `${baseUrl}/projects/'${githubName}/repository/tree`;
}
var ajaxConfig = {
type: 'GET',
dataType: 'json',
Expand All @@ -106,7 +124,10 @@ function getRepoContents(githubName) {
}

function getRepoContentsByDrillDown(githubName) {
var url = `${baseUrl}/repos/${githubName}/full`;
var url = `${baseUrl}/repos/${githubName}/full`;
if (isGitLab) {
url = `${baseUrl}/projects/${githubName}/full`;
}
var ajaxConfig = {
type: 'GET',
dataType: 'json',
Expand All @@ -118,23 +139,29 @@ function getRepoContentsByDrillDown(githubName) {
// repoName here is the combined owner/repo, e.g., 'jchartrand/someRepoName'

function getDoc(repoName, branch, path){

var url = `${baseUrl}/repos/${repoName}/contents`
if (isGitLab) {
url = `${baseUrl}/projects/${repoName}/repository/files/${encodeURI(path)}/raw?ref=master`
}
const ajaxConfig = {
type: 'GET',
dataType: 'json',
data: {branch, path},
url: `${baseUrl}/repos/${repoName}/contents`
url: url
};
return callCWRCGitWithToken(ajaxConfig);
}

function getInfoForAuthenticatedUser() {
if (Cookies.get('cwrc-token')) {
var url = `${baseUrl}/users`;
if (isGitLab) {
url = `${baseUrl}/users`;
}
var ajaxConfig = {
type: 'GET',
dataType: 'json',
url: url
url: url
};
return callCWRCGitWithToken(ajaxConfig).then(result=>result.data);
} else {
Expand All @@ -156,12 +183,15 @@ function getPermissionsForGithubUser(owner, repo, username) {
// 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 url = `${baseUrl}/repos/${repo}/doc`
if (isGitLab) {
url = `${baseUrl}/projects/${repo}/repository/files/${path}`
}
var ajaxConfig = {
type: 'PUT',
dataType: 'json',
data: data,
url: `${baseUrl}/repos/${repo}/doc`
url: url
};
return callCWRCGitWithToken(ajaxConfig)
}
Expand Down Expand Up @@ -197,10 +227,14 @@ function getTemplate(templateName) {
}

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

};
Expand All @@ -224,6 +258,7 @@ function searchRepos(query, per_page, page) {

module.exports = {
setServerURL: setServerURL,
useGitLab: useGitLab,
getDetailsForGithubUser: getDetailsForGithubUser,
getDetailsForOrg: getDetailsForOrg,
getReposForGithubUser: getReposForGithubUser,
Expand Down
3 changes: 2 additions & 1 deletion src/Load.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class LoadDialog extends Component {

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

Expand Down Expand Up @@ -315,7 +316,7 @@ class LoadDialog extends Component {
</Well>
:
<Well bsSize="small">
<RepoResultList serverURL={this.props.serverURL} selectCB={onFileSelect} repos={results} />
<RepoResultList serverURL={this.props.serverURL} isGitLab={this.props.isGitLab} selectCB={onFileSelect} repos={results} />
<Paginator pagingCB={this.getRepos} currentPage={this.state.currentPage} lastPage={this.state.lastPage} />
</Well>
)
Expand Down
1 change: 1 addition & 0 deletions src/RepoResultList.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class RepoResultList extends Component {

componentDidMount() {
cwrcGit.setServerURL(this.props.serverURL);
cwrcGit.useGitLab(this.props.isGitLab);
}

toggleFolder(path) {
Expand Down
2 changes: 2 additions & 0 deletions src/Save.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ class SaveCmp extends Component {
return (
<VerifyRepo
serverURL={this.props.serverURL}
isGitLab={this.props.isGitLab}
user={user}
owner={owner}
repo={repo}
Expand All @@ -200,6 +201,7 @@ class SaveCmp extends Component {
return (
<SaveToPath
serverURL={this.props.serverURL}
isGitLab={this.props.isGitLab}
owner={owner}
repo={repo}
path={path}
Expand Down
1 change: 1 addition & 0 deletions src/SaveToPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class SaveToPath extends Component {

componentDidMount() {
cwrcGit.setServerURL(this.props.serverURL);
cwrcGit.useGitLab(this.props.isGitLab);
this.setState({checkingPath: true})
cwrcGit.getDoc(this.getFullRepoPath(), 'master', this.props.path).then(
(result)=>{
Expand Down
1 change: 1 addition & 0 deletions src/VerifyRepo.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class VerifyRepo extends Component {

componentDidMount() {
cwrcGit.setServerURL(this.props.serverURL);
cwrcGit.useGitLab(this.props.isGitLab);
this.isOwnerUserOrOrg();
}

Expand Down
1 change: 1 addition & 0 deletions src/authenticate.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class AuthenticateDialog extends Component {

componentDidMount() {
cwrcGit.setServerURL(this.props.serverURL);
cwrcGit.useGitLab(this.props.isGitLab);
if (isAuthenticated() && this.state.user === undefined) {
this.doGetUserInfo();
}
Expand Down
13 changes: 10 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import LoadDialog from './Load.js';
import SaveCmp from './Save.js';

let serverURL = '';
let isGitLab = false;

let _writer;
let dialogId;
Expand All @@ -40,6 +41,10 @@ function setServerURL(url) {
serverURL = url;
}

function useGitLab(useIt) {
isGitLab = useIt;
}

function initDialogs(writer) {
_writer = writer;
dialogId = _writer.getUniqueId('git-dialogs-');
Expand Down Expand Up @@ -186,6 +191,7 @@ class GitDialog extends Component {

handleFileSelect(repo, path) {
cwrcGit.setServerURL(serverURL);
cwrcGit.useGitLab(isGitLab);
return cwrcGit.getDoc(repo, 'master', path)
.done((result)=>{
setDocumentInfo(repo, path);
Expand Down Expand Up @@ -263,7 +269,7 @@ class GitDialog extends Component {
} else {
return (
<Modal id={dialogId} show={true} animation={false}>
<AuthenticateDialog serverURL={serverURL} onUserAuthentication={this.handleAuthentication} />
<AuthenticateDialog serverURL={serverURL} isGitLab={isGitLab} onUserAuthentication={this.handleAuthentication} />
</Modal>
)
}
Expand Down Expand Up @@ -302,7 +308,7 @@ class GitDialog extends Component {
} else {
return (
<Modal id={dialogId} show={true} bsSize="large" animation={false}>
<LoadDialog serverURL={serverURL} isDocLoaded={isDocLoaded} user={user} onFileSelect={this.handleFileSelect} onFileUpload={this.handleFileUpload} handleClose={this.handleClose} />
<LoadDialog serverURL={serverURL} isGitLab={isGitLab} isDocLoaded={isDocLoaded} user={user} onFileSelect={this.handleFileSelect} onFileUpload={this.handleFileUpload} handleClose={this.handleClose} />
</Modal>
)
}
Expand All @@ -311,7 +317,7 @@ class GitDialog extends Component {
if (repoName === undefined) repoName = '';
return (
<Modal id={dialogId} show={true} animation={false}>
<SaveCmp serverURL={serverURL} user={user.userId} owner={owner} repo={repoName} path={path} handleClose={this.handleClose} getDocument={getDocument} handleRepoChange={setRepo} handlePathChange={setPath} handleSaved={this.handleSaved} />
<SaveCmp serverURL={serverURL} isGitLab={isGitLab} user={user.userId} owner={owner} repo={repoName} path={path} handleClose={this.handleClose} getDocument={getDocument} handleRepoChange={setRepo} handlePathChange={setPath} handleSaved={this.handleSaved} />
</Modal>
)

Expand All @@ -328,6 +334,7 @@ class GitDialog extends Component {

export {
setServerURL,
useGitLab,
saveWrap as save,
loadWrap as load,
getUserInfo,
Expand Down

0 comments on commit da186ca

Please sign in to comment.