@@ -23,22 +23,29 @@ import sjsonnew.support.scalajson.unsafe.{Parser => JsonParser, _}
2323
2424object SubmitDependencyGraph {
2525 val Submit = " githubSubmitDependencyGraph"
26- val Generate = " generateDependencyGraph"
27- val usage : String = s """ $Submit {"projects":[], "scalaVersions":[]} """
2826 val brief = " Submit the dependency graph to Github Dependency API."
2927 val detail = " Submit the dependency graph of a set of projects and scala versions to Github Dependency API"
28+ val Generate = " generateDependencyGraph"
3029 val briefGenerate = " Generate the dependency graph"
3130 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":[]} """
3242
33- val SubmitInternal : String = s " ${Submit }Internal "
34- val SubmitInternalLocal : String = s " ${Submit }InternalLocal "
43+ val internalCommand = s " ${command}Internal "
3544 val internalOnly = " internal usage only"
3645
3746 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)
4249 )
4350
4451 private lazy val http : HttpClient = Gigahorse .http(Gigahorse .config)
@@ -51,8 +58,8 @@ object SubmitDependencyGraph {
5158 .get
5259 }.failOnException
5360
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
5663 val loadedBuild = state.setting(Keys .loadedBuild)
5764 // all project refs that have a Scala version
5865 val projectRefs = loadedBuild.allProjectRefs
@@ -64,7 +71,7 @@ object SubmitDependencyGraph {
6471 .distinct
6572
6673 val root = Paths .get(loadedBuild.root).toAbsolutePath
67- val workspace = Paths .get(githubWorkspace(local )).toAbsolutePath
74+ val workspace = Paths .get(githubWorkspace()).toAbsolutePath
6875 val buildFile =
6976 if (root.startsWith(workspace)) workspace.relativize(root).resolve(" build.sbt" )
7077 else root.resolve(" build.sbt" )
@@ -79,16 +86,13 @@ object SubmitDependencyGraph {
7986 val storeAllManifests = scalaVersions.flatMap { scalaVersion =>
8087 Seq (s " ++ $scalaVersion" , s " Global/ ${githubStoreDependencyManifests.key} $scalaVersion" )
8188 }
82- val commands = storeAllManifests :+ {
83- if (local) { SubmitInternalLocal }
84- else { SubmitInternal }
85- }
89+ val commands = storeAllManifests :+ internalCommand
8690 commands.toList ::: initState
8791 }
8892
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 "
9296
9397 val snapshotJson = CompactPrinter (Converter .toJsonUnsafe(snapshot))
9498
@@ -98,7 +102,8 @@ object SubmitDependencyGraph {
98102 file
99103 }
100104
101- if (! local) {
105+ if (local) state
106+ else {
102107
103108 val request = Gigahorse
104109 .url(snapshotUrl)
@@ -107,6 +112,7 @@ object SubmitDependencyGraph {
107112 " Content-Type" -> " application/json" ,
108113 " Authorization" -> s " token ${githubToken()}"
109114 )
115+
110116 state.log.info(s " Submiting dependency snapshot of job ${snapshot.job} to $snapshotUrl" )
111117 val result = for {
112118 httpResp <- Try (Await .result(http.processFull(request), Duration .Inf ))
@@ -120,17 +126,14 @@ object SubmitDependencyGraph {
120126 )
121127 state
122128 }
129+
123130 result.get
124- } else {
125- state.log.info(s " Local mode: skipping submission " )
126- state
127131 }
128-
129132 }
130133
131134 // https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter
132135 private def setGithubOutputs (outputs : (String , String )* ): Unit = IO .writeLines(
133- file(githubOutput( false ) ),
136+ file(githubOutput),
134137 outputs.toSeq.map { case (name, value) => s " ${name}= ${value}" },
135138 append = true
136139 )
@@ -147,7 +150,7 @@ object SubmitDependencyGraph {
147150 throw new MessageOnlyException (message)
148151 }
149152
150- private def githubDependencySnapshot (local : Boolean )( state : State ): DependencySnapshot = {
153+ private def githubDependencySnapshot (state : State ): DependencySnapshot = {
151154 val detector = DetectorMetadata (
152155 SbtGithubDependencySubmission .name,
153156 SbtGithubDependencySubmission .homepage.map(_.toString).getOrElse(" " ),
@@ -157,19 +160,19 @@ object SubmitDependencyGraph {
157160 val manifests = state.get(githubManifestsKey).get
158161 DependencySnapshot (
159162 0 ,
160- githubJob(local ),
161- githubSha(local ),
162- githubRef(local ),
163+ githubJob(),
164+ githubSha(),
165+ githubRef(),
163166 detector,
164167 Map .empty[String , JValue ],
165168 manifests,
166169 scanned.toString
167170 )
168171 }
169172
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
173176 val html_url =
174177 for {
175178 serverUrl <- Properties .envOrNone(" GITHUB_SERVER_URL" )
@@ -178,34 +181,33 @@ object SubmitDependencyGraph {
178181 Job (correlator, id, html_url)
179182 }
180183
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()
192195 }
193196
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. " )
210212 }
211213}
0 commit comments