Skip to content

Commit 9b08219

Browse files
lsulakLadislav Sulak
andauthored
#22 add scalafmt to repo (#23)
* #22 adding scalafmt with a basic config targetting Scala 2.11 * #22 adding optional scalafmt CI check * #22 post-CQC changes in scalafmt conf we were discussing * #22 scalafmt config - moving closer to our codebase (without these, function definition formatting was very different) * #22 scalafmt will check only files that were changed in comparison to master branch * #22 post-review changes in CI file * #22 scalafmt: `origin/master` doesn't work in CI check, renaming to `master` * #22 scalafmt: `origin/master` and `master` doesn't work in CI check, trying `remotes/origin/master` * #22 scalafmt: downgrading scalafmt version, there might be a bug in the 2.4.6 See: scalameta/sbt-scalafmt#205 * #22 scalafmt: replicing master branch with the actual base branch * #22 scalafmt: tmp commit (I want to see how CI check behaves and what's in the log) * #22: checking out the branch, not the PR remote (head would be in a detached state) This is because for scalafmt we need to find the 'base' branch - which is master in this case, and compare the list of changed files against it, in order to check the formatting against such newly changed files only. Also, 'origin/' isn't needed because the 'base' branch will be 'origin/master', not just 'master' - see file build.sbt, line starting with `scalafmtFilter.withRank` and the expression starting with `git show-branch -a`. * #22 requested in CQC session, for preventing this to be evaluated when it's not needed * #22 post-review changes, improving readability Co-authored-by: Ladislav Sulak <[email protected]>
1 parent 7cb075c commit 9b08219

File tree

5 files changed

+81
-4
lines changed

5 files changed

+81
-4
lines changed

.github/workflows/format_check.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#
2+
# Copyright 2021 ABSA Group Limited
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
name: FormatCheck
18+
19+
on:
20+
pull_request:
21+
branches: [ master, develop ]
22+
types: [ assigned, opened, synchronize, reopened, labeled ]
23+
24+
jobs:
25+
scalafmt:
26+
runs-on: ubuntu-latest
27+
name: Scalafmt Check
28+
steps:
29+
- name: Checkout code
30+
uses: actions/checkout@v2
31+
with:
32+
fetch-depth: 0
33+
ref: ${{ github.event.pull_request.head.ref }}
34+
35+
- name: Setup Scala
36+
uses: olafurpg/setup-scala@v10
37+
with:
38+
java-version: "[email protected]"
39+
40+
- name: Run scalafmt And Print Diff
41+
continue-on-error: true
42+
run: sbt scalafmt scalafmtSbt && git diff --exit-code

.scalafmt.conf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
version = "3.5.3"
2+
runner.dialect = scala211
3+
4+
maxColumn = 120
5+
6+
align.preset = some
7+
align.multiline = false
8+
9+
indent.main = 2
10+
indent.defnSite = 2
11+
12+
lineEndings = unix
13+
14+
docstrings.blankFirstLine = yes
15+
docstrings.style = AsteriskSpace
16+
docstrings.wrap = no
17+
docstrings.removeEmpty = true
18+
19+
align.openParenDefnSite = true
20+
align.openParenCallSite = true

build.sbt

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
16+
import sys.process._
1717

1818
ThisBuild / name := "spark-data-standardization"
1919
ThisBuild / organization := "za.co.absa"
@@ -27,11 +27,11 @@ ThisBuild / scalaVersion := scala211
2727

2828
def sparkVersion(scalaVersion: String): String = if (scalaVersion==scala212) "3.2.1" else "2.4.7"
2929

30-
def sparkFastTestsVersion(scalaVersion: String): String = if (scalaVersion==scala212) "1.1.0" else "0.23.0"
30+
def sparkFastTestsVersion(scalaVersion: String): String = if (scalaVersion == scala212) "1.1.0" else "0.23.0"
3131

32-
libraryDependencies ++= List(
32+
libraryDependencies ++= List(
3333
"org.apache.spark" %% "spark-core" % sparkVersion(scalaVersion.value) % "provided",
34-
"org.apache.spark" %% "spark-sql" % sparkVersion(scalaVersion.value) % "provided",
34+
"org.apache.spark" %% "spark-sql" % sparkVersion(scalaVersion.value) % "provided",
3535
"za.co.absa" %% "spark-commons" % "0.2.0",
3636
"com.github.mrpowers" %% "spark-fast-tests" % sparkFastTestsVersion(scalaVersion.value) % Test,
3737
"org.scalatest" %% "scalatest" % "3.2.2" % Test,
@@ -47,6 +47,19 @@ ThisBuild / printSparkScalaVersion := {
4747

4848
Test / parallelExecution := false
4949

50+
// Only apply scalafmt to files that differ from master (i.e. files changed in the feature branch or so),
51+
// not on the whole repository.
52+
lazy val currBranchName = "git branch --show-current".!!.trim
53+
lazy val baseBranchName = (
54+
"git show-branch -a"
55+
#| raw"grep \*"
56+
#| s"grep -v $currBranchName"
57+
#| "head -n1"
58+
#| raw"sed s/.*\[\(.*\)\].*/\1/"
59+
).!!.trim
60+
61+
scalafmtFilter.withRank(KeyRanks.Invisible) := s"diff-ref=${baseBranchName}"
62+
5063
// licenceHeader check:
5164
ThisBuild / organizationName := "ABSA Group Limited"
5265
ThisBuild / startYear := Some(2021)

project/plugins.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.7.0")
2+
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.5")

src/main/scala/za/co/absa/standardization/Standardization.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import org.apache.spark.sql.functions._
2121
import org.apache.spark.sql.types.{StructType, _}
2222
import org.apache.spark.sql.{Column, DataFrame, SparkSession}
2323
import org.slf4j.{Logger, LoggerFactory}
24+
2425
import za.co.absa.spark.commons.implicits.StructTypeImplicits.StructTypeEnhancements
2526
import za.co.absa.standardization.config.{DefaultStandardizationConfig, StandardizationConfig}
2627
import za.co.absa.standardization.stages.{SchemaChecker, TypeParser}

0 commit comments

Comments
 (0)