@@ -23,22 +23,29 @@ import sjsonnew.support.scalajson.unsafe.{Parser => JsonParser, _}
23
23
24
24
object SubmitDependencyGraph {
25
25
val Submit = " githubSubmitDependencyGraph"
26
- val Generate = " generateDependencyGraph"
27
- val usage : String = s """ $Submit {"projects":[], "scalaVersions":[]} """
28
26
val brief = " Submit the dependency graph to Github Dependency API."
29
27
val detail = " Submit the dependency graph of a set of projects and scala versions to Github Dependency API"
28
+ val Generate = " generateDependencyGraph"
30
29
val briefGenerate = " Generate the dependency graph"
31
30
val detailGenerate = " Generate the dependency graph of a set of projects and scala versions"
31
+ val commands = new SubmitDependencyGraph (true , Generate , briefGenerate, detailGenerate).commands ++
32
+ new SubmitDependencyGraph (false , Submit , brief, detail).commands
33
+ }
34
+
35
+ class SubmitDependencyGraph (
36
+ val local : Boolean ,
37
+ val command : String ,
38
+ val brief : String ,
39
+ val detail : String
40
+ ) {
41
+ val usage : String = s """ $command {"projects":[], "scalaVersions":[]} """
32
42
33
- val SubmitInternal : String = s " ${Submit }Internal "
34
- val SubmitInternalLocal : String = s " ${Submit }InternalLocal "
43
+ val internalCommand = s " ${command}Internal "
35
44
val internalOnly = " internal usage only"
36
45
37
46
val commands : Seq [Command ] = Seq (
38
- Command (Submit , (usage, brief), detail)(inputParser)(submit(false )),
39
- Command (Generate , (usage, briefGenerate), detailGenerate)(inputParser)(submit(true )),
40
- Command .command(SubmitInternal , internalOnly, internalOnly)(submitInternal(false )),
41
- Command .command(SubmitInternalLocal , internalOnly, internalOnly)(submitInternal(true ))
47
+ Command (command, (usage, brief), detail)(inputParser)(submit),
48
+ Command .command(internalCommand, internalOnly, internalOnly)(submitInternal)
42
49
)
43
50
44
51
private lazy val http : HttpClient = Gigahorse .http(Gigahorse .config)
@@ -51,8 +58,8 @@ object SubmitDependencyGraph {
51
58
.get
52
59
}.failOnException
53
60
54
- private def submit (local : Boolean )( state : State , input : SubmitInput ): State = {
55
- checkGithubEnv(local ) // fail fast if the Github CI environment is incomplete
61
+ private def submit (state : State , input : SubmitInput ): State = {
62
+ checkGithubEnv() // fail fast if the Github CI environment is incomplete
56
63
val loadedBuild = state.setting(Keys .loadedBuild)
57
64
// all project refs that have a Scala version
58
65
val projectRefs = loadedBuild.allProjectRefs
@@ -64,7 +71,7 @@ object SubmitDependencyGraph {
64
71
.distinct
65
72
66
73
val root = Paths .get(loadedBuild.root).toAbsolutePath
67
- val workspace = Paths .get(githubWorkspace(local )).toAbsolutePath
74
+ val workspace = Paths .get(githubWorkspace()).toAbsolutePath
68
75
val buildFile =
69
76
if (root.startsWith(workspace)) workspace.relativize(root).resolve(" build.sbt" )
70
77
else root.resolve(" build.sbt" )
@@ -79,16 +86,13 @@ object SubmitDependencyGraph {
79
86
val storeAllManifests = scalaVersions.flatMap { scalaVersion =>
80
87
Seq (s " ++ $scalaVersion" , s " Global/ ${githubStoreDependencyManifests.key} $scalaVersion" )
81
88
}
82
- val commands = storeAllManifests :+ {
83
- if (local) { SubmitInternalLocal }
84
- else { SubmitInternal }
85
- }
89
+ val commands = storeAllManifests :+ internalCommand
86
90
commands.toList ::: initState
87
91
}
88
92
89
- private def submitInternal (local : Boolean )( state : State ): State = {
90
- val snapshot = githubDependencySnapshot(local)( state)
91
- val snapshotUrl = s " ${githubApiUrl(local )}/repos/ ${githubRepository(local )}/dependency-graph/snapshots "
93
+ private def submitInternal (state : State ): State = {
94
+ val snapshot = githubDependencySnapshot(state)
95
+ val snapshotUrl = s " ${githubApiUrl()}/repos/ ${githubRepository()}/dependency-graph/snapshots "
92
96
93
97
val snapshotJson = CompactPrinter (Converter .toJsonUnsafe(snapshot))
94
98
@@ -98,7 +102,8 @@ object SubmitDependencyGraph {
98
102
file
99
103
}
100
104
101
- if (! local) {
105
+ if (local) state
106
+ else {
102
107
103
108
val request = Gigahorse
104
109
.url(snapshotUrl)
@@ -107,6 +112,7 @@ object SubmitDependencyGraph {
107
112
" Content-Type" -> " application/json" ,
108
113
" Authorization" -> s " token ${githubToken()}"
109
114
)
115
+
110
116
state.log.info(s " Submiting dependency snapshot of job ${snapshot.job} to $snapshotUrl" )
111
117
val result = for {
112
118
httpResp <- Try (Await .result(http.processFull(request), Duration .Inf ))
@@ -120,17 +126,14 @@ object SubmitDependencyGraph {
120
126
)
121
127
state
122
128
}
129
+
123
130
result.get
124
- } else {
125
- state.log.info(s " Local mode: skipping submission " )
126
- state
127
131
}
128
-
129
132
}
130
133
131
134
// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter
132
135
private def setGithubOutputs (outputs : (String , String )* ): Unit = IO .writeLines(
133
- file(githubOutput( false ) ),
136
+ file(githubOutput),
134
137
outputs.toSeq.map { case (name, value) => s " ${name}= ${value}" },
135
138
append = true
136
139
)
@@ -147,7 +150,7 @@ object SubmitDependencyGraph {
147
150
throw new MessageOnlyException (message)
148
151
}
149
152
150
- private def githubDependencySnapshot (local : Boolean )( state : State ): DependencySnapshot = {
153
+ private def githubDependencySnapshot (state : State ): DependencySnapshot = {
151
154
val detector = DetectorMetadata (
152
155
SbtGithubDependencySubmission .name,
153
156
SbtGithubDependencySubmission .homepage.map(_.toString).getOrElse(" " ),
@@ -157,19 +160,19 @@ object SubmitDependencyGraph {
157
160
val manifests = state.get(githubManifestsKey).get
158
161
DependencySnapshot (
159
162
0 ,
160
- githubJob(local ),
161
- githubSha(local ),
162
- githubRef(local ),
163
+ githubJob(),
164
+ githubSha(),
165
+ githubRef(),
163
166
detector,
164
167
Map .empty[String , JValue ],
165
168
manifests,
166
169
scanned.toString
167
170
)
168
171
}
169
172
170
- private def githubJob (local : Boolean ): Job = {
171
- val correlator = s " ${githubWorkflow(local )}_ ${githubJobName(local )}_ ${githubAction(local )}"
172
- val id = githubRunId(local)
173
+ private def githubJob (): Job = {
174
+ val correlator = s " ${githubWorkflow()}_ ${githubJobName()}_ ${githubAction()}"
175
+ val id = githubRunId
173
176
val html_url =
174
177
for {
175
178
serverUrl <- Properties .envOrNone(" GITHUB_SERVER_URL" )
@@ -178,34 +181,33 @@ object SubmitDependencyGraph {
178
181
Job (correlator, id, html_url)
179
182
}
180
183
181
- private def checkGithubEnv (local : Boolean = false ): Unit = {
182
- githubWorkspace(local )
183
- githubWorkflow(local )
184
- githubJobName(local )
185
- githubAction(local )
186
- githubRunId(local )
187
- githubSha(local )
188
- githubRef(local )
189
- githubApiUrl(local )
190
- githubRepository(local )
191
- githubToken(local )
184
+ private def checkGithubEnv (): Unit = {
185
+ githubWorkspace()
186
+ githubWorkflow()
187
+ githubJobName()
188
+ githubAction()
189
+ githubRunId()
190
+ githubSha()
191
+ githubRef()
192
+ githubApiUrl()
193
+ githubRepository()
194
+ githubToken()
192
195
}
193
196
194
- private def githubWorkspace (local : Boolean = false ): String = githubCIEnv(" GITHUB_WORKSPACE" , local)
195
- private def githubWorkflow (local : Boolean = false ): String = githubCIEnv(" GITHUB_WORKFLOW" , local)
196
- private def githubJobName (local : Boolean = false ): String = githubCIEnv(" GITHUB_JOB" , local)
197
- private def githubAction (local : Boolean = false ): String = githubCIEnv(" GITHUB_ACTION" , local)
198
- private def githubRunId (local : Boolean = false ): String = githubCIEnv(" GITHUB_RUN_ID" , local)
199
- private def githubSha (local : Boolean = false ): String = githubCIEnv(" GITHUB_SHA" , local)
200
- private def githubRef (local : Boolean = false ): String = githubCIEnv(" GITHUB_REF" , local)
201
- private def githubApiUrl (local : Boolean = false ): String = githubCIEnv(" GITHUB_API_URL" , local)
202
- private def githubRepository (local : Boolean = false ): String = githubCIEnv(" GITHUB_REPOSITORY" , local)
203
- private def githubToken (local : Boolean = false ): String = githubCIEnv(" GITHUB_TOKEN" , local)
204
- private def githubOutput (local : Boolean = false ): String = githubCIEnv(" GITHUB_OUTPUT" , local)
205
-
206
- private def githubCIEnv (name : String , local : Boolean = false ): String =
207
- Properties .envOrNone(name).getOrElse {
208
- if (local) " "
209
- else throw new MessageOnlyException (s " Missing environment variable $name. This task must run in a Github Action. " )
197
+ private def githubWorkspace (): String = githubCIEnv(" GITHUB_WORKSPACE" )
198
+ private def githubWorkflow (): String = githubCIEnv(" GITHUB_WORKFLOW" )
199
+ private def githubJobName (): String = githubCIEnv(" GITHUB_JOB" )
200
+ private def githubAction (): String = githubCIEnv(" GITHUB_ACTION" )
201
+ private def githubRunId (): String = githubCIEnv(" GITHUB_RUN_ID" )
202
+ private def githubSha (): String = githubCIEnv(" GITHUB_SHA" )
203
+ private def githubRef (): String = githubCIEnv(" GITHUB_REF" )
204
+ private def githubApiUrl (): String = githubCIEnv(" GITHUB_API_URL" )
205
+ private def githubRepository (): String = githubCIEnv(" GITHUB_REPOSITORY" )
206
+ private def githubToken (): String = githubCIEnv(" GITHUB_TOKEN" )
207
+ private def githubOutput (): String = githubCIEnv(" GITHUB_OUTPUT" )
208
+
209
+ private def githubCIEnv (name : String ): String =
210
+ Properties .envOrNone(name).orElse(Some (" " ).find(_ => local)).getOrElse {
211
+ throw new MessageOnlyException (s " Missing environment variable $name. This task must run in a Github Action. " )
210
212
}
211
213
}
0 commit comments