Skip to content

Commit

Permalink
refactor: overall
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita715 committed May 15, 2019
1 parent d2e034c commit 889a589
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,6 @@ class FileSystemSourceCodeStorage(
)
)
logger.info { "Storage: Saved base file with name = $fileName of repo ${repo.name}" }
} else {
logger.info { "Storage: Ignored base file with name = $fileName of repo ${repo.name}" }
}
}
}
Expand Down Expand Up @@ -187,12 +185,6 @@ class FileSystemSourceCodeStorage(
" repo ${pullRequest.repo.name}, user ${pullRequest.creatorName}"
}
}
// else {
// logger.info {
// "Storage: Ignored solution file with name = $fileName of" +
// " repo ${pullRequest.repo.name}, user ${pullRequest.creatorName}"
// }
// }
}
}

Expand All @@ -212,6 +204,14 @@ class FileSystemSourceCodeStorage(
}
}

override fun deleteBaseFile(repo: Repository, branch: String, fileName: String) {
File(pathToBase(repo.gitService, repo.name, branch, fileName)).deleteRecursively()
}

override fun deleteSolutionFile(repo: Repository, branch: String, creator: String, fileName: String) {
File(pathToSolution(repo.gitService, repo.name, branch, creator, fileName)).deleteRecursively()
}

private fun String.toFileExtension() =
substringAfterLast(".")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@ interface SourceCodeStorage {
* Delete files of the [analysis]
*/
fun deleteAnalysisFiles(analysis: Analysis): Unit

/**
* Delete solution file of the repository
*/
fun deleteSolutionFile(repo: Repository, branch: String, creator: String, fileName: String)

/**
* Delete base file of the repository
*/
fun deleteBaseFile(repo: Repository, branch: String, fileName: String)
}
45 changes: 37 additions & 8 deletions core/src/main/kotlin/io/gitplag/core/rest/RepositoryController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class RepositoryController(
/**
* Initiate the analysis async
*/
@PostMapping("/repositories/{id}/analyzeWithNoResponse")
@PostMapping("/repositories/{id}/analyze/detached")
fun analyzeDetached(@PathVariable id: Long, @RequestBody dto: AnalysisDto): Boolean {
val repoValue = repositoryDataManager.findById(dto.repoId) ?: return false
analysisAsyncRunner.runAndRespond(
Expand Down Expand Up @@ -166,7 +166,7 @@ class RepositoryController(
/**
* Trigger download of files of the repo
*/
@GetMapping("/repositories/{id}/updateFiles")
@GetMapping("/repositories/{id}/files/update")
fun updateFilesOfRepo(@PathVariable id: Long): RepositoryFilesInfoDto? {
val repository = repositoryDataManager.findById(id)
return if (repository != null) {
Expand All @@ -186,7 +186,7 @@ class RepositoryController(
/**
* Trigger download of files of the repo
*/
@GetMapping("/repositories/{id}/updateFilesAsync")
@GetMapping("/repositories/{id}/files/update/detached")
fun updateFilesOfRepoAsync(@PathVariable id: Long): Boolean {
val repository = repositoryDataManager.findById(id)

Expand Down Expand Up @@ -215,7 +215,7 @@ class RepositoryController(
/**
* Get downloaded base files of the repo
*/
@GetMapping("/repositories/{id}/baseFiles")
@GetMapping("/repositories/{id}/bases")
fun getLocalBases(@PathVariable id: Long): List<BaseBranchInfoDto> {
val repo = repositoryDataManager.findById(id)
return if (repo != null) basesToDto(baseFileRecordRepository.findAllByRepo(repo)) else emptyList()
Expand All @@ -224,7 +224,7 @@ class RepositoryController(
/**
* Get downloaded base files of the repo
*/
@PostMapping("/repositories/{id}/baseFiles")
@PostMapping("/repositories/{id}/bases")
fun getLocalBases(@PathVariable id: Long, @RequestBody dto: LocalFileDto): List<BaseBranchInfoDto> {
val repo = repositoryDataManager.findById(id)
return if (repo != null) basesToDto(baseFileRecordRepository.findAllByRepo(repo)
Expand All @@ -237,7 +237,7 @@ class RepositoryController(
/**
* Get downloaded solution files of the repo
*/
@GetMapping("/repositories/{id}/solutionFiles")
@GetMapping("/repositories/{id}/solutions")
fun getLocalSolutions(@PathVariable id: Long): List<SolutionBranchInfoDto> {
val repo = repositoryDataManager.findById(id)
return if (repo != null) solutionsToDto(solutionFileRecordRepository.findAllByRepo(repo)) else emptyList()
Expand All @@ -246,7 +246,7 @@ class RepositoryController(
/**
* Get downloaded solution files of the repo
*/
@PostMapping("/repositories/{id}/solutionFiles")
@PostMapping("/repositories/{id}/solutions")
fun getLocalSolutions(@PathVariable id: Long, @RequestBody dto: LocalFileDto): List<SolutionBranchInfoDto> {
val repo = repositoryDataManager.findById(id)
return if (repo != null) solutionsToDto(solutionFileRecordRepository.findAllByRepo(repo)
Expand Down Expand Up @@ -288,4 +288,33 @@ class RepositoryController(
@GetMapping("/repositories/{id}/pulls")
fun getPullRequests(@PathVariable id: Long) =
pullRequestRepository.findAllByRepoId(id).map { PullRequestDto(it) }
}

@PostMapping("/repositories/{id}/bases/delete")
fun deleteBaseFiles(@PathVariable id: Long, @RequestBody ids: List<Long>) {
val repo = repositoryDataManager.findById(id) ?: return
val bases = baseFileRecordRepository.findAllById(ids)
bases.map { it.branch }.toSet().forEach { branchName ->
val branch = branchRepository.findByRepositoryAndName(repo, branchName)
if (branch != null) branchRepository.delete(branch)
}
bases.forEach {
sourceCodeStorage.deleteBaseFile(repo, it.branch, it.fileName)
}
baseFileRecordRepository.deleteAll(bases)
}

@PostMapping("/repositories/{id}/solutions/delete")
fun deleteSolutionFiles(@PathVariable id: Long, @RequestBody ids: List<Long>) {
val repo = repositoryDataManager.findById(id) ?: return
val solutions = solutionFileRecordRepository.findAllById(ids)
solutions.forEach {
sourceCodeStorage.deleteSolutionFile(
repo,
it.pullRequest.sourceBranchName,
it.pullRequest.creatorName,
it.fileName
)
}
solutionFileRecordRepository.deleteAll(solutions)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class GithubWebhookController(private val githubWebhookService: GithubPayloadPro
logger.info { "Webhook: got new $event" }
when (event) {
"pull_request" -> githubWebhookService.downloadSolutionsOfPullRequest(payload)
"push", "ping" -> githubWebhookService.downloadBasesOfRepository(payload)
"push" -> githubWebhookService.downloadBasesOfRepository(payload)
else -> logger.info { "Webhook: $event is not supported" }
}
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const App = () => {
<div>
<BrowserRouter>
<Route exact path="/" component={HomePage}/>
<Route exact path="/webhook" component={WebhookBanner}/>
<Route exact path="/webhook/:git(github|gitlab|bitbucket)" component={WebhookBanner}/>
<Route exact path="/repos" component={Repositories}/>
<Route exact path="/repos/new" component={NewRepo}/>
<Route exact path="/repos/:id(\d+)" component={Repository}/>
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/components/HomePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import {Link} from "react-router-dom";

export class HomePage extends React.Component {

constructor(props, context) {
super(props, context);
}

render() {
return <div className="jumbotron">
<h1 className="display-4">Gitplag</h1>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/NewRepo.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class NewRepo extends React.Component {
let dto = new RepoDto(this.state);
axios.post((PROP.serverUrl + "/api/repositories"), dto).then((response) => {
if (response.data.length !== 0) {
this.props.history.push("/webhook")
this.props.history.push("/webhook/" + this.state.git.toLowerCase())
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/Repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class Repository extends React.Component {
}

static startUpdateOfFiles(repoId) {
axios.get(PROP.serverUrl + "/api/repositories/" + repoId + "/updateFilesAsync")
axios.get(PROP.serverUrl + "/api/repositories/" + repoId + "/files/update/detached")
}

deleteAnalysis(repoId, analysisId) {
Expand Down
47 changes: 36 additions & 11 deletions frontend/src/components/RepositoryFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ export class RepositoryFiles extends React.Component {
this.fetchFiles();
this.handleChange = this.handleChange.bind(this);
this.fetchFiles = this.fetchFiles.bind(this);
this.deleteBaseFiles = this.deleteBaseFiles.bind(this);
this.deleteSolutionFiles = this.deleteSolutionFiles.bind(this);
}

static fileElementBase(base, file) {
return {id: file.id, branch: base.branch, updated: base.updated.toString().replace("T", " "), name: file.name};
}

static fileElementSolution(solution, student, file) {
return {
id: file.id,
branch: solution.sourceBranch,
student: student.student,
updated: student.updated.replace("T", " "),
name: file.name
};
}

static filterBase(regStr, base) {
Expand All @@ -46,19 +62,28 @@ export class RepositoryFiles extends React.Component {
return student.files.map((file) => RepositoryFiles.fileElementSolution(solution, student, file));
}

static fileElementBase(base, file) {
console.log(base);
console.log(file);
return {branch: base.branch, updated: base.updated.toString().replace("T", " "), name: file.name};
deleteBaseFiles() {
let selectedFiles = this.state.bases.filter(
(it) => RepositoryFiles.filterBase(this.state.sortedByName, it));
axios.post(PROP.serverUrl + "/api/repositories/" + this.state.repoId + "/bases/delete",
selectedFiles.map((file) => file.id)
);
this.setState({
bases: this.state.bases.filter((it) => selectedFiles.indexOf(it) < 0),
sortedByName: ""
});
}

static fileElementSolution(solution, student, file) {
return {
branch: solution.sourceBranch,
student: student.student,
updated: student.updated.replace("T", " "),
name: file.name
};
deleteSolutionFiles() {
let selectedFiles = this.state.solutions.filter(
(it) => RepositoryFiles.filterSolution(this.state.sortedByName, it));
axios.post(PROP.serverUrl + "/api/repositories/" + this.state.repoId + "/solutions/delete",
selectedFiles.map((file) => file.id)
);
this.setState({
bases: this.state.solutions.filter((it) => selectedFiles.indexOf(it) < 0),
sortedByName: ""
});
}

handleChange(event) {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/RunAnalysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class RunAnalysis extends React.Component {
}

handleSubmit() {
axios.post((PROP.serverUrl + "/api/repositories/" + this.state.repoId + "/analyzeWithNoResponse"), this.state).then(
axios.post((PROP.serverUrl + "/api/repositories/" + this.state.repoId + "/analyze/detached"), this.state).then(
() => this.props.history.push("/repos/" + this.state.repoId)
);
}
Expand Down
17 changes: 15 additions & 2 deletions frontend/src/components/WebhookBanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,31 @@ import {Link} from "react-router-dom";

export class WebhookBanner extends React.Component {

state = {
git: ""
};

constructor(props, context) {
super(props, context);
this.state.git = this.props.match.params.git;
}

render() {

return <div className="jumbotron vertical-center">
<div className="jumbotron my-auto mx-auto">
<div className="align-content-center text-center mb-2">
<h6>Add a webhook to the repo to automatically upload new files.</h6>
<h6>Add a webhook to the {this.state.git} repo settings to automatically upload new files.</h6>
</div>
<table className="table table-bordered table-hover">
<tbody>
<tr className="">
<td>URL</td>
<td><strong>{"{server_url}"}/webhook</strong></td>
<td><strong>{"{server_url}"}/webhook/{this.state.git}</strong></td>
</tr>
<tr className="">
<td>Content-type</td>
<td><strong>application/json</strong></td>
</tr>
<tr className="">
<td>Events</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ abstract class AbstractPayloadProcessor(
logger.info { "Webhook: received new pull request from repo ${repo.name}" }
cloneReceivedPullRequest(jsonObject, repo)
} else {
logger.info { "Webhook: received new repo $mainRepoFullName" }
cloneRepoAndAllPullRequests(mainRepoFullName, jsonObject.pullRequest.mainRepoId)
logger.info { "Webhook: ignored pull request to unknown repo $mainRepoFullName" }
}
}

Expand Down Expand Up @@ -75,8 +74,7 @@ abstract class AbstractPayloadProcessor(
Branch(updatedAt = updatedAt, repository = repo, name = branchName)
)
} else {
logger.info { "Webhook: received new repo ${jsonObject.pushRepoName}" }
cloneRepoAndAllPullRequests(jsonObject.pushRepoName, jsonObject.pushRepoId)
logger.info { "Webhook: ignored push to unknown repo ${jsonObject.pushRepoName}" }
}
}

Expand Down Expand Up @@ -108,18 +106,6 @@ abstract class AbstractPayloadProcessor(
}
}

private fun cloneRepoAndAllPullRequests(repoName: String?, repoId: String?) {
val repo = repositoryDataManager.save(
Repository(
name = requireNotNull(repoName),
gitService = git,
gitId = requireNotNull(repoId)
)
)
gitRestManager.cloneRepository(repo)
downloadAllPullRequestsOfRepository(repo)
}

override fun downloadAllPullRequestsOfRepository(repo: Repository) {
gitRestManager.findBranchesOfRepo(repo).forEach { branchName ->
val lastUpdated =
Expand Down
8 changes: 8 additions & 0 deletions model/src/main/kotlin/io/gitplag/model/dto/IdListDto.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.gitplag.model.dto

/**
* Dto for any list of ids
*/
class IdListDto(
val ids: Collection<Long>
)

0 comments on commit 889a589

Please sign in to comment.