-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAuthors.scala
More file actions
165 lines (146 loc) · 5.78 KB
/
Copy pathAuthors.scala
File metadata and controls
165 lines (146 loc) · 5.78 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Copyright 2016 Martynas Mickevičius
*
* 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 lt.dvim.authors
import java.io.File
import scala.collection.JavaConverters._
import scala.concurrent.{ExecutionContext, Future}
import akka.NotUsed
import akka.actor.ActorSystem
import akka.event.{Logging, LoggingAdapter}
import akka.http.scaladsl.Http
import akka.http.scaladsl.marshalling.Marshal
import akka.http.scaladsl.marshalling.PredefinedToRequestMarshallers._
import akka.http.scaladsl.model.{HttpRequest, Uri}
import akka.stream.alpakka.json.scaladsl.JsonReader
import akka.stream.scaladsl.{Flow, Source}
import akka.util.ByteString
import com.madgag.git._
import org.eclipse.jgit.diff.DiffFormatter
import org.eclipse.jgit.internal.storage.file.FileRepository
import org.eclipse.jgit.storage.file.FileRepositoryBuilder
import org.eclipse.jgit.util.io.DisabledOutputStream
import org.mdedetrich.akka.stream.support.CirceStreamSupport
import lt.dvim.authors.Authors.{AuthorStats, Stats}
import lt.dvim.authors.GithubProtocol.{Commit, GitAuthor, GithubAuthor}
object Authors {
final val MaxAuthors = 1024
final val GithubApiUrl = "api.github.com"
final case class Stats(additions: Int, deletions: Int, commits: Int)
final case class AuthorStats(gitAuthor: GitAuthor, githubAuthor: Option[GithubAuthor], stats: Stats)
def summary(repo: Option[String], from: String, to: String, path: String): Future[String] = {
val cld = classOf[ActorSystem].getClassLoader
implicit val sys = ActorSystem("Authors", classLoader = Some(cld))
implicit val gitRepository = Authors.gitRepo(path)
implicit val log = Logging(sys, this.getClass)
import sys.dispatcher
def parsedRepo =
parseRepo(gitRepository.getConfig().getString("remote", "origin", "url"))
DiffSource(repo.getOrElse(parsedRepo), from, to)
.via(JsonReader.select("$.commits[*]"))
.via(CirceStreamSupport.decode[Commit])
.via(StatsAggregator())
.via(SortingMachine())
.via(MarkdownConverter())
.runFold("")(_ ++ "\n" ++ _)
.transformWith { res =>
for {
_ <- sys.terminate()
r <- Future.fromTry(res)
} yield r
}
}
def parseRepo(originUrl: String): String =
originUrl.split("github.com").tail.head.drop(1).split(".git").head
def gitRepo(path: String): FileRepository =
FileRepositoryBuilder
.create(new File(if (path.contains(".git")) path else path + "/.git"))
.asInstanceOf[FileRepository]
def shaToStats(sha: String)(implicit repo: FileRepository): Stats = {
implicit val (revWalk, reader) = repo.singleThreadedReaderTuple
val df = new DiffFormatter(DisabledOutputStream.INSTANCE)
df.setRepository(repo)
diff(abbrId(sha).asRevTree, abbrId(sha).asRevCommit.getParent(0).asRevTree)
.flatMap { d =>
df.toFileHeader(d).toEditList.asScala.map { edit =>
Stats(
additions = edit.getEndA - edit.getBeginA,
deletions = edit.getEndB - edit.getBeginB,
1
)
}
}
.fold(Stats(0, 0, 1))((sum, stats) => Stats(sum.additions + stats.additions, sum.deletions + stats.deletions, 1))
}
}
object DiffSource {
def apply(repo: String, from: String, to: String)(implicit
ec: ExecutionContext,
sys: ActorSystem
): Source[ByteString, NotUsed] =
Source
.future(
Marshal(Uri(s"/repos/$repo/compare/$from...$to"))
.to[HttpRequest]
)
.via(Http().outgoingConnectionHttps(Authors.GithubApiUrl))
.map(_.entity.dataBytes)
.flatMapConcat(identity)
}
object SortingMachine {
def apply(): Flow[AuthorStats, AuthorStats, NotUsed] =
Flow[AuthorStats]
.grouped(Authors.MaxAuthors)
.mapConcat(_.sortBy(s => (s.stats.commits, s.stats.additions, s.stats.deletions)).reverse)
}
object StatsAggregator {
def apply()(implicit repo: FileRepository, log: LoggingAdapter): Flow[Commit, AuthorStats, NotUsed] =
Flow[Commit]
.filterNot(_.commit.message.startsWith("Merge pull request"))
.groupBy(Authors.MaxAuthors, commit => commit.author.map(_.login).getOrElse(commit.commit.author.email))
.log("Commit")
.map(commit => AuthorStats(commit.commit.author, commit.author, Authors.shaToStats(commit.sha)))
.reduce((aggr, elem) =>
aggr.copy(
stats = Stats(
additions = aggr.stats.additions + elem.stats.additions,
deletions = aggr.stats.deletions + elem.stats.deletions,
aggr.stats.commits + elem.stats.commits
)
)
)
.mergeSubstreams
}
object MarkdownConverter {
def apply(): Flow[AuthorStats, String, NotUsed] =
Flow[AuthorStats]
.map { author =>
val authorId = author.githubAuthor.map { gh =>
// using html instead of markdown, because default
// avatars come from github not resized
s"""[<img width="20" alt="${gh.login}" src="${gh.avatarUrl}&s=40"/> **${gh.login}**](${gh.htmlUrl})"""
} getOrElse
author.gitAuthor.name
s"| $authorId | ${author.stats.commits} | ${author.stats.additions} | ${author.stats.deletions} |"
}
.prepend(
Source(
List(
"| Author | Commits | Lines added | Lines removed |",
"| ------ | ------- | ----------- | ------------- |"
)
)
)
}