Skip to content

Commit 16e9e0c

Browse files
THWisemanAlexITC
authored andcommitted
[WX-1395] Scalafmt GitHub Action (#7337)
1 parent ffc0a43 commit 16e9e0c

File tree

4 files changed

+106
-22
lines changed

4 files changed

+106
-22
lines changed

.github/workflows/scalafmt-check.yml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: 'Scalafmt'
2+
3+
# This GitHub Action runs the ScalaFmt linting tool on the entire codebase.
4+
# It fails if any files are not formatted properly.
5+
# If it is triggered by someone commenting 'scalafmt' on a PR, it will first format, commit, and push formatted code
6+
# to the branch.
7+
8+
run-name: ${{ format('ScalaFmt Check on {0}', github.ref_name) }}
9+
10+
on:
11+
workflow_dispatch:
12+
push:
13+
14+
permissions:
15+
contents: read
16+
17+
jobs:
18+
run-scalafmt-check:
19+
runs-on: ubuntu-latest
20+
timeout-minutes: 20
21+
steps:
22+
- uses: actions/checkout@v3
23+
with:
24+
ref: ${{ inputs.target-branch }}
25+
- uses: ./.github/set_up_cromwell_action
26+
with:
27+
cromwell_repo_token: ${{ secrets.BROADBOT_GITHUB_TOKEN }}
28+
- name: Run ScalaFmt
29+
run: |
30+
sbt scalafmtCheckAll
31+
working-directory: ${{ github.workspace }}

.github/workflows/scalafmt-fix.yml

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: 'Scalafmt'
2+
3+
# This GitHub Action runs the ScalaFmt linting tool on the entire codebase.
4+
# It will fix, commit, and push linted code.
5+
# It will only run when someone comments "scalafmt" on a PR.
6+
7+
run-name: ${{ format('ScalaFmt Fix on {0}', github.ref_name) }}
8+
9+
on:
10+
workflow_dispatch:
11+
issue_comment:
12+
types:
13+
- created
14+
15+
jobs:
16+
run-scalafmt-fix:
17+
runs-on: ubuntu-latest
18+
timeout-minutes: 20
19+
steps:
20+
- uses: actions/checkout@v3
21+
with:
22+
ref: ${{ inputs.target-branch }}
23+
- uses: ./.github/set_up_cromwell_action
24+
with:
25+
cromwell_repo_token: ${{ secrets.BROADBOT_GITHUB_TOKEN }}
26+
- name: Check for ScalaFmt Comment
27+
id: check-comment
28+
run: |
29+
if [[ "${{ github.event_name }}" == "issue_comment" && "${{ github.event.comment.body }}" == *"scalafmt"* ]]; then
30+
echo "::set-output name=comment-triggered::true"
31+
echo "::set-output name=comment-author-email::${{ github.event.comment.user.login }}@users.noreply.github.com"
32+
echo "::set-output name=comment-author-name::${{ github.event.comment.user.login }}"
33+
else
34+
echo "::set-output name=comment-triggered::false"
35+
fi
36+
shell: bash
37+
- name: Run ScalaFmt
38+
run: |
39+
if [[ ${{ steps.check-comment.outputs.comment-triggered }} == true ]]; then
40+
echo "PR Comment Detected. Formatting, committing, and pushing formatted scala code."
41+
sbt scalaFmtAll
42+
git config --global user.email "${{ steps.check-comment.outputs.comment-author-email }}"
43+
git config --global user.name "${{ steps.check-comment.outputs.comment-author-name }}"
44+
git add .
45+
git commit -m "Auto-format code with ScalaFmt"
46+
git push origin ${{ github.ref }}
47+
fi
48+
working-directory: ${{ github.workspace }}

project/ContinuousIntegration.scala

+26-22
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object ContinuousIntegration {
99
lazy val ciSettings: Seq[Setting[_]] = List(
1010
srcCiResources := sourceDirectory.value / "ci" / "resources",
1111
targetCiResources := target.value / "ci" / "resources",
12-
envFile := srcCiResources.value / "env.temp", //generated by resources/acquire_b2c_token.sh
12+
envFile := srcCiResources.value / "env.temp", // generated by resources/acquire_b2c_token.sh
1313

1414
vaultToken := userHome / ".vault-token",
1515
copyCiResources := {
@@ -31,29 +31,32 @@ object ContinuousIntegration {
3131

3232
// Only include the local file argument if the file exists (local development w/ acquire_b2c_token.sh)
3333
// Don't include it otherwise (CI/CD and other development)
34-
val localEnvFileArgs = if(envFile.value.exists()) List("-e", s"ENV_FILE=${envFile.value}") else List()
34+
val localEnvFileArgs = if (envFile.value.exists()) List("-e", s"ENV_FILE=${envFile.value}") else List()
3535

36-
val cmd: List[String] = List.concat(List(
37-
"docker",
38-
"run",
39-
"--rm",
40-
"-v",
41-
s"${vaultToken.value}:/root/.vault-token",
42-
"-v",
43-
s"${srcCiResources.value}:${srcCiResources.value}",
44-
"-v",
45-
s"${targetCiResources.value}:${targetCiResources.value}"),
36+
val cmd: List[String] = List.concat(
37+
List(
38+
"docker",
39+
"run",
40+
"--rm",
41+
"-v",
42+
s"${vaultToken.value}:/root/.vault-token",
43+
"-v",
44+
s"${srcCiResources.value}:${srcCiResources.value}",
45+
"-v",
46+
s"${targetCiResources.value}:${targetCiResources.value}"
47+
),
4648
localEnvFileArgs,
4749
List(
48-
"-e",
49-
"ENVIRONMENT=not_used",
50-
"-e",
51-
s"INPUT_PATH=${srcCiResources.value}",
52-
"-e",
53-
s"OUT_PATH=${targetCiResources.value}",
54-
"broadinstitute/dsde-toolbox:dev",
55-
"render-templates.sh"
56-
))
50+
"-e",
51+
"ENVIRONMENT=not_used",
52+
"-e",
53+
s"INPUT_PATH=${srcCiResources.value}",
54+
"-e",
55+
s"OUT_PATH=${targetCiResources.value}",
56+
"broadinstitute/dsde-toolbox:dev",
57+
"render-templates.sh"
58+
)
59+
)
5760
val result = cmd ! log
5861
if (result != 0) {
5962
sys.error(
@@ -79,7 +82,8 @@ object ContinuousIntegration {
7982
private val srcCiResources: SettingKey[File] = settingKey[File]("Source directory for CI resources")
8083
private val targetCiResources: SettingKey[File] = settingKey[File]("Target directory for CI resources")
8184
private val vaultToken: SettingKey[File] = settingKey[File]("File with the vault token")
82-
private val envFile: SettingKey[File] = settingKey[File]("File with the environment variables needed to render CI resources.")
85+
private val envFile: SettingKey[File] =
86+
settingKey[File]("File with the environment variables needed to render CI resources.")
8387

8488
/**
8589
* For "reasons" these projects are excluded from the root aggregation in build.sbt.

project/plugins.sbt

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "1.1.1")
33
addSbtPlugin("com.github.sbt" % "sbt-git" % "2.0.0")
44
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "2.0.4")
55
addSbtPlugin("com.github.cb372" % "sbt-explicit-dependencies" % "0.2.16")
6+
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2")
67
addDependencyTreePlugin

0 commit comments

Comments
 (0)