-
Notifications
You must be signed in to change notification settings - Fork 56
/
AuditDef.scala
87 lines (68 loc) · 2.4 KB
/
AuditDef.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
/*
* 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 java.net.URL
import com.github.nscala_time.time.Imports._
import com.squareup.okhttp
import okhttp.{OkHttpClient, OkUrlFactory}
import lib.Implicits._
import org.joda.time.DateTime
import org.kohsuke.github.{GitHub, HttpConnector}
import play.api.Logger
import scala.collection.convert.wrapAsScala._
import scalax.file.ImplicitConversions._
import scalax.file.Path
object AuditDef {
val parentWorkDir = Path.fromString("/tmp") / "working-dir"
def safelyCreateFor(orgName: String, apiKey: String) = {
val org = GitHub.connectUsingOAuth(apiKey).getOrganization(orgName)
AuditDef(org.getLogin, apiKey)
}
}
case class AuditDef(orgLogin: String, apiKey: String) {
val workingDir = AuditDef.parentWorkDir / orgLogin.toLowerCase
workingDir.mkdirs()
lazy val okHttpClient = {
val client = new OkHttpClient
val responseCacheDir = workingDir / "http-cache"
responseCacheDir.mkdirs()
if (responseCacheDir.exists) {
client.setCache(new okhttp.Cache(responseCacheDir, 5 * 1024 * 1024))
} else {
Logger.warn(s"Couldn't create HttpResponseCache dir ${responseCacheDir.path}")
}
client
}
def conn() = {
val gh = GitHub.connectUsingOAuth(apiKey)
gh.setConnector(new HttpConnector {
def connect(url: URL) = new OkUrlFactory(okHttpClient).open(url)
})
gh
}
lazy val (org, bot) = {
val c = conn()
val org = c.getOrganization(orgLogin)
val bot = c.getMyself
require(bot.isMemberOf(org))
(org, bot)
}
def ensureSeemsLegit() = {
require(bot.isMemberOf(org), s"Supplied bot account ${bot.atLogin} must be a member of ${org.atLogin}")
require(org.listPublicMembers.exists(_.createdAt < DateTime.now - 3.months),
s"Organisation ${org.atLogin} must have at least one *public* member whose account is over 3 months old")
}
}