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