-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathPeopleRepo.scala
83 lines (64 loc) · 2.68 KB
/
PeopleRepo.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
/*
* Copyright 2014 The Guardian
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package lib
import org.eclipse.jgit.api.{GitCommand, TransportCommand, Git}
import org.eclipse.jgit.transport.{CredentialsProvider, UsernamePasswordCredentialsProvider}
import com.madgag.git._
import scalax.io.Resource
import java.io.File
import org.eclipse.jgit.lib.Repository
import scalax.file.ImplicitConversions._
import org.eclipse.jgit.storage.file.FileRepositoryBuilder
import play.api.Logger
import collection.convert.wrapAll._
object PeopleRepo {
val UsernameRegex = """^([\p{Alnum}-]{2,}+)""".r
def getSponsoredUserLogins(dataDirectory: File, uri: String, credentials: Option[CredentialsProvider] = None, defaultBranch: String): Set[String] = {
def invoke[C <: GitCommand[_], R](command: TransportCommand[C, R]): R = {
command.setTimeout(5)
credentials.foreach(command.setCredentialsProvider)
command.call()
}
def getUpToDateRepo(): Repository = {
val gitdir = dataDirectory / "people.git"
Logger.info(s"Using repo with default branch name: $defaultBranch")
if (gitdir.exists) {
Logger.info("Updating Git repo with fetch...")
val repo = FileRepositoryBuilder.create(gitdir)
invoke(repo.git.fetch())
repo
} else {
gitdir.doCreateParents()
Logger.info("Cloning new Git repo...")
val cloneCommand =
Git.cloneRepository().setBare(true).setDirectory(gitdir).setURI(uri).setBranchesToClone(Seq(defaultBranch))
invoke(cloneCommand).getRepository
}
}
val repo = getUpToDateRepo()
implicit val reader = repo.newObjectReader()
Option(repo.resolve(s"${defaultBranch}^{tree}:users.txt")) match {
case Some(latestUserFileGitId) =>
val lines = Resource.fromInputStream(latestUserFileGitId.open.openStream()).lines()
val sponsoredUserNames = lines.collect { case UsernameRegex(username) => username }.toSet
Logger.info(s"Found ${sponsoredUserNames.size} sponsored usernames")
sponsoredUserNames
case None =>
Logger.warn(s"No users.txt found! Should be in the root folder of the 'people' repo")
Set.empty
}
}
}