-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCICommitHook.scala
102 lines (95 loc) · 4.52 KB
/
CICommitHook.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package io.github.gitbucket.ci.hook
import gitbucket.core.plugin.ReceiveHook
import gitbucket.core.model.Profile._
import gitbucket.core.service._
import gitbucket.core.util.Directory.getRepositoryDir
import gitbucket.core.util.JGitUtil
import io.github.gitbucket.ci.model.CIConfig
import io.github.gitbucket.ci.service.CIService
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.transport.{ReceiveCommand, ReceivePack}
import profile.blockingApi._
import scala.util.Using
class CICommitHook extends ReceiveHook
with CIService with RepositoryService with AccountService with CommitStatusService with SystemSettingsService {
override def postReceive(owner: String, repository: String, receivePack: ReceivePack,
command: ReceiveCommand, pusher: String, mergePullRequest: Boolean)(implicit session: Session): Unit = {
val branch = command.getRefName.stripPrefix("refs/heads/")
if(branch != command.getRefName && command.getType != ReceiveCommand.Type.DELETE){
getRepository(owner, repository).foreach { repositoryInfo =>
Using.resource(Git.open(getRepositoryDir(owner, repository))) { git =>
val sha = command.getNewId.name
val revCommit = JGitUtil.getRevCommitFromId(git, command.getNewId)
loadCIConfig(owner, repository).foreach { buildConfig =>
if(
buildConfig.skipWordsSeq.find(revCommit.getFullMessage.contains).isEmpty &&
buildConfig.skipWordsSeq.find(branch.contains).isEmpty
){
runBuild(
userName = owner,
repositoryName = repository,
buildUserName = owner,
buildRepositoryName = repository,
buildBranch = branch,
sha = sha,
commitMessage = revCommit.getShortMessage,
commitUserName = revCommit.getCommitterIdent.getName,
commitMailAddress = revCommit.getCommitterIdent.getEmailAddress,
pullRequestId = None,
pusher = pusher,
config = buildConfig
)
}
}
for {
(pullreq, issue) <- PullRequests
.join(Issues).on { (t1, t2) => t1.byPrimaryKey(t2.userName, t2.repositoryName, t2.issueId) }
.filter { case (t1, t2) =>
(t1.requestUserName === owner.bind) && (t1.requestRepositoryName === repository.bind) &&
(t1.requestBranch === branch.bind) && (t2.closed === false.bind)
}
.list
buildConfig <- loadCIConfig(pullreq.userName, pullreq.repositoryName)
} yield {
if(buildConfig.skipWordsSeq.find(revCommit.getFullMessage.contains).isEmpty){
runBuild(
userName = pullreq.userName,
repositoryName = pullreq.repositoryName,
buildUserName = owner,
buildRepositoryName = repository,
buildBranch = branch,
sha = sha,
commitMessage = revCommit.getShortMessage,
commitUserName = revCommit.getCommitterIdent.getName,
commitMailAddress = revCommit.getCommitterIdent.getEmailAddress,
pullRequestId = Some(pullreq.issueId),
pusher = pusher,
config = buildConfig
)
}
}
}
}
}
}
private def runBuild(userName: String, repositoryName: String, buildUserName: String, buildRepositoryName: String,
buildBranch: String, sha: String, commitMessage: String, commitUserName: String, commitMailAddress: String,
pullRequestId: Option[Int], pusher: String, config: CIConfig)(implicit session: Session): Unit = {
getAccountByUserName(pusher).foreach { pusherAccount =>
runBuild(
userName = userName,
repositoryName = repositoryName,
buildUserName = buildUserName,
buildRepositoryName = buildRepositoryName,
buildBranch = buildBranch,
sha = sha,
commitMessage = commitMessage,
commitUserName = commitUserName,
commitMailAddress = commitMailAddress,
pullRequestId = pullRequestId,
buildAuthor = pusherAccount,
config = config
)
}
}
}