diff --git a/src/main/resources/update/1_4.sql b/src/main/resources/update/1_4.sql index 749e9aa..c1b3022 100644 --- a/src/main/resources/update/1_4.sql +++ b/src/main/resources/update/1_4.sql @@ -1,13 +1,13 @@ CREATE TABLE PULL_REQUEST( - PULL_REQUEST_ID INT AUTO_INCREMENT, USER_NAME VARCHAR(100) NOT NULL, REPOSITORY_NAME VARCHAR(100) NOT NULL, ISSUE_ID INT NOT NULL, + ORIGIN_BRANCH VARCHAR(100) NOT NULL, REQUEST_USER_NAME VARCHAR(100) NOT NULL, REQUEST_REPOSITORY_NAME VARCHAR(100) NOT NULL, REQUEST_COMMIT_ID VARCHAR(40) NOT NULL ); -ALTER TABLE PULL_REQUEST ADD CONSTRAINT IDX_PULL_REQUEST_PK PRIMARY KEY (PULL_REQUEST_ID); +ALTER TABLE PULL_REQUEST ADD CONSTRAINT IDX_PULL_REQUEST_PK PRIMARY KEY (USER_NAME, REPOSITORY_NAME, ISSUE_ID); ALTER TABLE PULL_REQUEST ADD CONSTRAINT IDX_PULL_REQUEST_FK0 FOREIGN KEY (USER_NAME, REPOSITORY_NAME, ISSUE_ID) REFERENCES ISSUE (USER_NAME, REPOSITORY_NAME, ISSUE_ID); ALTER TABLE PULL_REQUEST ADD CONSTRAINT IDX_PULL_REQUEST_FK1 FOREIGN KEY (REQUEST_USER_NAME, REQUEST_REPOSITORY_NAME) REFERENCES REPOSITORY (USER_NAME, REPOSITORY_NAME); diff --git a/src/main/scala/app/CreateRepositoryController.scala b/src/main/scala/app/CreateRepositoryController.scala index d89d306..748504f 100644 --- a/src/main/scala/app/CreateRepositoryController.scala +++ b/src/main/scala/app/CreateRepositoryController.scala @@ -80,7 +80,9 @@ val git = Git.open(tmpdir) git.add.addFilepattern("README.md").call - git.commit.setMessage("Initial commit").call + git.commit + .setCommitter(new PersonIdent(loginUserName, loginAccount.mailAddress)) + .setMessage("Initial commit").call git.push.call } finally { diff --git a/src/main/scala/app/PullRequestsController.scala b/src/main/scala/app/PullRequestsController.scala index 6fb1656..d3fb03d 100644 --- a/src/main/scala/app/PullRequestsController.scala +++ b/src/main/scala/app/PullRequestsController.scala @@ -1,26 +1,42 @@ package app -import util.{FileUtil, JGitUtil, ReferrerAuthenticator} +import util.{CollaboratorsAuthenticator, FileUtil, JGitUtil, ReferrerAuthenticator} import util.Directory._ import service._ import org.eclipse.jgit.treewalk.CanonicalTreeParser import util.JGitUtil.{DiffInfo, CommitInfo} import scala.collection.mutable.ArrayBuffer import org.eclipse.jgit.api.Git -import org.eclipse.jgit.lib.ObjectId +import jp.sf.amateras.scalatra.forms._ +import util.JGitUtil.DiffInfo +import scala.Some +import util.JGitUtil.CommitInfo class PullRequestsController extends PullRequestsControllerBase - with RepositoryService with AccountService with ReferrerAuthenticator + with RepositoryService with AccountService with IssuesService with PullRequestService + with ReferrerAuthenticator with CollaboratorsAuthenticator trait PullRequestsControllerBase extends ControllerBase { - self: ReferrerAuthenticator with RepositoryService => + self: ReferrerAuthenticator with RepositoryService with IssuesService + with PullRequestService with CollaboratorsAuthenticator => + + val form = mapping( + "title" -> trim(label("Title" , text(required, maxlength(100)))), + "content" -> trim(label("Content", optional(text()))), + "branch" -> trim(text(required, maxlength(100))), + "requestUserName" -> trim(text(required, maxlength(100))), + "requestCommitId" -> trim(text(required, maxlength(40))) + )(PullRequestForm.apply) + + case class PullRequestForm(title: String, content: Option[String], branch: String, + requestUserName: String, requestCommitId: String) get("/:owner/:repository/pulls")(referrersOnly { repository => pulls.html.list(repository) }) // TODO Replace correct authenticator - get("/:owner/:repository/pulls/compare")(referrersOnly { newRepo => + get("/:owner/:repository/pulls/compare")(collaboratorsOnly { newRepo => (newRepo.repository.originUserName, newRepo.repository.originRepositoryName) match { case (None,_)|(_, None) => NotFound // TODO BadRequest? case (Some(originUserName), Some(originRepositoryName)) => { @@ -40,7 +56,7 @@ }) // TODO Replace correct authenticator - get("/:owner/:repository/pulls/compare/*:*...*")(referrersOnly { repository => + get("/:owner/:repository/pulls/compare/*:*...*")(collaboratorsOnly { repository => if(repository.repository.originUserName.isEmpty || repository.repository.originRepositoryName.isEmpty){ NotFound // TODO BadRequest? } else { @@ -94,6 +110,29 @@ } }) + post("/:owner/:repository/pulls/new", form)(referrersOnly { (form, repository) => + val loginUserName = context.loginAccount.get.userName + + val issueId = createIssue( + repository.owner, + repository.name, + loginUserName, + form.title, + form.content, + None, None) + + createPullRequest( + repository.owner, + repository.name, + issueId, + form.branch, + form.requestUserName, + repository.name, + form.requestCommitId) + + redirect(s"/${repository.owner}/${repository.name}/pulls/${issueId}") + }) + private def withGit[T](oldDir: java.io.File, newDir: java.io.File)(action: (Git, Git) => T): T = { val oldGit = Git.open(oldDir) val newGit = Git.open(newDir) diff --git a/src/main/scala/model/PullRequest.scala b/src/main/scala/model/PullRequest.scala index 950ebbf..5645ae7 100644 --- a/src/main/scala/model/PullRequest.scala +++ b/src/main/scala/model/PullRequest.scala @@ -3,21 +3,21 @@ import scala.slick.driver.H2Driver.simple._ object PullRequests extends Table[PullRequest]("PULL_REQUEST") with IssueTemplate { - def pullRequestId = column[Int]("PULL_REQUEST_ID") def requestUserName = column[String]("REQUEST_USER_NAME") def requestRepositoryName = column[String]("REQUEST_REPOSITORY_NAME") def requestCommitId = column[String]("REQUEST_COMMIT_ID") - def * = pullRequestId ~ userName ~ repositoryName ~ issueId ~ requestUserName ~ requestRepositoryName ~ requestCommitId <> (PullRequest, PullRequest.unapply _) + def originBranch = column[String]("ORIGIN_BRANCH") + def * = userName ~ repositoryName ~ issueId ~ originBranch ~ requestUserName ~ requestRepositoryName ~ requestCommitId <> (PullRequest, PullRequest.unapply _) - def autoinc = userName ~ repositoryName ~ issueId ~ requestUserName ~ requestRepositoryName ~ requestCommitId returning pullRequestId - def byPrimaryKey(pullRequestId: Int) = this.pullRequestId is pullRequestId.bind + def byPrimaryKey(userName: String, repositoryName: String, issueId: Int) = byIssue(userName, repositoryName, issueId) + def byPrimaryKey(userName: Column[String], repositoryName: Column[String], issueId: Column[Int]) = byIssue(userName, repositoryName, issueId) } case class PullRequest( - pullRequestId: Int, userName: String, repositoryName: String, issueId: Int, + originBranch: String, requestUserName: String, requestRepositoryName: String, requestCommitId: String) \ No newline at end of file diff --git a/src/main/scala/service/PullRequestService.scala b/src/main/scala/service/PullRequestService.scala new file mode 100644 index 0000000..a52fe11 --- /dev/null +++ b/src/main/scala/service/PullRequestService.scala @@ -0,0 +1,25 @@ +package service + +import scala.slick.driver.H2Driver.simple._ +import Database.threadLocalSession + +import model._ + +//import scala.slick.jdbc.{StaticQuery => Q} +//import Q.interpolation + + +trait PullRequestService { + + def createPullRequest(originUserName: String, originRepositoryName: String, issueId: Int, + originBranch: String, requestUserName: String, requestRepositoryName: String, requestCommitId: String): Unit = + PullRequests insert (PullRequest( + originUserName, + originRepositoryName, + issueId, + originBranch, + requestUserName, + requestRepositoryName, + requestCommitId)) + +} diff --git a/src/main/scala/servlet/AutoUpdateListener.scala b/src/main/scala/servlet/AutoUpdateListener.scala index 4614230..4fe7397 100644 --- a/src/main/scala/servlet/AutoUpdateListener.scala +++ b/src/main/scala/servlet/AutoUpdateListener.scala @@ -49,6 +49,7 @@ * The history of versions. A head of this sequence is the current BitBucket version. */ val versions = Seq( + Version(1, 4), new Version(1, 3){ override def update(conn: Connection): Unit = { super.update(conn) diff --git a/src/main/twirl/pulls/compare.scala.html b/src/main/twirl/pulls/compare.scala.html index 9ae11a4..4edd2d6 100644 --- a/src/main/twirl/pulls/compare.scala.html +++ b/src/main/twirl/pulls/compare.scala.html @@ -12,7 +12,7 @@ Edit @origin:@originId ... @repository.owner:@forkedId -
- -@date(day.head.time) | -||
---|---|---|
- @avatar(commit.committer, 20) - @commit.committer - | -@commit.shortMessage | -- @commit.id.substring(0, 7) - | -