@@ -12,6 +12,7 @@ import ch.epfl.scala.GithubDependencyGraphPlugin.autoImport._
12
12
import ch .epfl .scala .JsonProtocol ._
13
13
import ch .epfl .scala .githubapi .JsonProtocol ._
14
14
import ch .epfl .scala .githubapi ._
15
+ import gigahorse .FullResponse
15
16
import gigahorse .HttpClient
16
17
import gigahorse .support .apachehttp .Gigahorse
17
18
import sbt ._
@@ -69,35 +70,41 @@ object SubmitDependencyGraph {
69
70
70
71
private def submitInternal (state : State ): State = {
71
72
val snapshot = githubDependencySnapshot(state)
72
- val url = new URL ( s " ${githubApiUrl()}/repos/ ${githubRepository()}/dependency-graph/snapshots " )
73
+ val snapshotUrl = s " ${githubApiUrl()}/repos/ ${githubRepository()}/dependency-graph/snapshots "
73
74
74
75
val snapshotJson = CompactPrinter (Converter .toJsonUnsafe(snapshot))
75
76
val request = Gigahorse
76
- .url(url.toString )
77
+ .url(snapshotUrl )
77
78
.post(snapshotJson, StandardCharsets .UTF_8 )
78
79
.addHeaders(
79
80
" Content-Type" -> " application/json" ,
80
81
" Authorization" -> s " token ${githubToken()}"
81
82
)
82
83
83
- state.log.info(s " Submiting dependency snapshot to $url " )
84
+ state.log.info(s " Submiting dependency snapshot to $snapshotUrl " )
84
85
val result = for {
85
- httpResp <- Try (Await .result(http.run(request), Duration .Inf ))
86
- jsonResp <- JsonParser .parseFromByteBuffer(httpResp.bodyAsByteBuffer)
87
- response <- Converter .fromJson[SnapshotResponse ](jsonResp)
88
- } yield new URL (url, response.id.toString)
89
-
90
- result match {
91
- case scala.util.Success (result) =>
92
- state.log.info(s " Submitted successfully as $result" )
93
- state
94
- case scala.util.Failure (cause) =>
95
- throw new MessageOnlyException (
96
- s " Failed to submit the dependency snapshot because of ${cause.getClass.getName}: ${cause.getMessage}"
97
- )
86
+ httpResp <- Try (Await .result(http.processFull(request), Duration .Inf ))
87
+ snapshot <- getSnapshot(httpResp)
88
+ } yield {
89
+ state.log.info(s " Submitted successfully as $snapshotUrl/ ${snapshot.id}" )
90
+ state
98
91
}
92
+
93
+ result.get
99
94
}
100
95
96
+ private def getSnapshot (httpResp : FullResponse ): Try [SnapshotResponse ] =
97
+ httpResp.status match {
98
+ case status if status / 100 == 2 =>
99
+ JsonParser
100
+ .parseFromByteBuffer(httpResp.bodyAsByteBuffer)
101
+ .flatMap(Converter .fromJson[SnapshotResponse ])
102
+ case status =>
103
+ val message =
104
+ s " Unexpected status $status ${httpResp.statusText} with body: \n ${httpResp.bodyAsString}"
105
+ throw new MessageOnlyException (message)
106
+ }
107
+
101
108
private def githubDependencySnapshot (state : State ): DependencySnapshot = {
102
109
val detector = DetectorMetadata (
103
110
SbtGithubDependencySubmission .name,
0 commit comments