Skip to content

Commit

Permalink
Update ci split off release bits (#266)
Browse files Browse the repository at this point in the history
* Update ci split off release bits

- Release CI split-off from validation bit
- Release only on pushing a release tag
- No snapshot releases

* Review feedback

* Simplify Ci by using an OS matrix

* Add line ending preservation to scalafmt

- Should resolve `scalafmtCheckAll` issue on Windows

* Debug

* Formatting...

* Debig

* ensuring the cmt.conf file is created from scratch with default values during testing

* removed the now-unnecessary printlns and added comment to the configuration spec fix (so it doesn't get removed accidentally in future)

---------

Co-authored-by: thinkmorestupidless <[email protected]>
  • Loading branch information
eloots and thinkmorestupidless authored Jul 27, 2023
1 parent a8a84e1 commit 409f0a0
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 108 deletions.
51 changes: 0 additions & 51 deletions .github/workflows/ci.yml → .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,59 +10,8 @@ permissions:
contents: write

jobs:
run-tests:
runs-on: ubuntu-latest

steps:

- name: Checkout Course Management Tools Repo
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: 11
distribution: adopt-hotspot
cache: 'sbt'

- name: Setup Coursier
run: |
curl -fLo coursier https://git.io/coursier-cli &&
chmod +x coursier &&
./coursier
- name: Check code formatting
run: sbt "scalafmtCheckAll ; scalafmtSbtCheck"

- name: Run tests
run: sbt test

run-tests-windows:
runs-on: windows-latest

steps:

- name: Checkout Course Management Tools Repo
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: 11
distribution: adopt-hotspot
cache: 'sbt'

- name: Run tests
shell: bash
run: sbt test

create-release:
runs-on: ubuntu-latest
needs: [run-tests, run-tests-windows]
if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags/')

steps:
Expand Down
36 changes: 36 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: CI
on:
push:
branches: [ main ]

pull_request:

permissions:
contents: write

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ 'ubuntu-latest', 'windows-latest' ]

steps:
- name: Checkout Course Management Tools Repo
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: 11
distribution: adopt-hotspot
cache: 'sbt'

- name: Check code formatting
run: sbt "scalafmtCheckAll ; scalafmtSbtCheck"

- name: Run tests
shell: bash
run: sbt test
1 change: 1 addition & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ optIn.breakChainOnFirstMethodDot = false
optIn.configStyleArguments = false
danglingParentheses.defnSite = false
danglingParentheses.callSite = false
lineEndings = preserve
rewrite.neverInfix.excludeFilters = [
and
min
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ final class SetCurrentCourseSpec
expectedDirectory.mkdir()
val cmtConfigFolder = expectedDirectory / ".cmt"
cmtConfigFolder.mkdir()

dumpStringToFile(`cmt-config`, cmtConfigFolder / ".cmt-config")
dumpStringToFile(`cmt-code-size-checksums`, cmtConfigFolder / ".cmt-code-size-checksums")
dumpStringToFile(`cmt-test-size-checksums`, cmtConfigFolder / ".cmt-test-size-checksums")
Expand Down
31 changes: 4 additions & 27 deletions core/src/main/scala/com/lunatech/cmt/client/Configuration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@ import dev.dirs.ProjectDirectories
import sbt.io.IO.*
import sbt.io.syntax.*

import java.nio.charset.StandardCharsets
import java.util.Base64
import scala.jdk.CollectionConverters.*
import scala.util.{Failure, Success, Try}

final case class Configuration(
homeDirectory: CmtHome,
coursesDirectory: CoursesDirectory,
currentCourse: CurrentCourse,
githubApiToken: GithubApiToken) {
currentCourse: CurrentCourse) {
def flush(): Either[CmtError, Unit] = {
val configFile = globalConfigFile(homeDirectory)

Expand All @@ -28,10 +25,7 @@ final case class Configuration(
_.replaceAll(CoursesDirectoryToken, s""""${adaptToNixSeparatorChar(coursesDirectory.value.getAbsolutePath)}"""")
.replaceAll(
CurrentCourseToken,
s""""${adaptToNixSeparatorChar(currentCourse.value.value.getAbsolutePath)}"""")
.replaceAll(
GithubApiTokenToken,
s""""${GithubApiToken.fromBase64EncodedString(DefaultGithubApiToken).value}""""))) match {
s""""${adaptToNixSeparatorChar(currentCourse.value.value.getAbsolutePath)}""""))) match {
case Success(_) => Right(())
case Failure(exception) => Left(FailedToWriteGlobalConfiguration(exception))
}
Expand All @@ -49,31 +43,17 @@ object Configuration:
ConfigFactory.parseFile(value)
}
final case class CmtCoursesHome(value: File)
final case class GithubApiToken(value: String)
object GithubApiToken {
// If github detects an API token in a commit it deactivates the token so we'll Base64 encode the token in config
def fromBase64EncodedString(base64EncodedString: String): GithubApiToken = {
val encodedBytes = base64EncodedString.getBytes(StandardCharsets.UTF_8)
val decodedBytes = Base64.getDecoder().decode(encodedBytes)
val decodedString = new String(decodedBytes, StandardCharsets.UTF_8)
GithubApiToken(decodedString)
}
}

private val projectDirectories = ProjectDirectories.from("com", "lunatech", "cmt")
val UserConfigDir = projectDirectories.configDir
val CmtGlobalConfigName = "com.lunatech.cmt.conf"
val CoursesDirectoryToken = "COURSES_DIRECTORY"
val CurrentCourseToken = "CURRENT_COURSE"
val GithubApiTokenToken = "GITHUB_API_TOKEN"
val CmtHomeEnvKey = "CMT_HOME"

val DefaultCmtCoursesHome = s"${projectDirectories.cacheDir}/Courses"
val CmtCoursesHomeEnvKey = "CMT_COURSES_HOME"

val DefaultGithubApiToken =
"Z2l0aHViX3BhdF8xMUFIS0w3Q1kwRVdtUVpPcnV3aG5LX1RpVGk4OVdPaHlDTkdqNmFibXdxN3dBWmV4b1k0NFRkb3E4UFpWOWRoQjVLQ1pPQ1dQR1J2WEFsdFBn"

private def globalConfigFile(cmtHome: CmtHome): CmtGlobalConfigFile =
CmtGlobalConfigFile(cmtHome.value / CmtGlobalConfigName)

Expand All @@ -82,7 +62,6 @@ object Configuration:
|cmtc {
| courses-directory = $CoursesDirectoryToken
| current-course = $CurrentCourseToken
| github-api-token = $GithubApiTokenToken
|}
|""".stripMargin

Expand Down Expand Up @@ -115,8 +94,7 @@ object Configuration:
val coursesDirectory = CoursesDirectory(file(adaptToOSSeparatorChar(config.getString("cmtc.courses-directory"))))
val currentCourse = CurrentCourse(
StudentifiedRepo(file(adaptToOSSeparatorChar(config.getString("cmtc.current-course")))))
val githubApiToken = GithubApiToken(config.getString("cmtc.github-api-token"))
Configuration(cmtHome, coursesDirectory, currentCourse, githubApiToken)
Configuration(cmtHome, coursesDirectory, currentCourse)
}

private def createIfNotExists(cmtHome: CmtHome, cmtCoursesHome: CmtCoursesHome): Unit =
Expand All @@ -143,8 +121,7 @@ object Configuration:
printMessage(
s"global configuration file is missing from '${configFile.value.getAbsolutePath}' creating it with default values")
val currentCourse = CurrentCourse(StudentifiedRepo(cmtCoursesHome.value))
val githubApiToken = GithubApiToken(DefaultGithubApiToken)
val configuration = Configuration(cmtHome, CoursesDirectory(cmtCoursesHome.value), currentCourse, githubApiToken)
val configuration = Configuration(cmtHome, CoursesDirectory(cmtCoursesHome.value), currentCourse)
val _ = configuration.flush()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.lunatech.cmt.client

import com.lunatech.cmt.Domain.StudentifiedRepo
import com.lunatech.cmt.client.Configuration.{CmtHome, GithubApiToken}
import com.lunatech.cmt.client.Configuration.CmtHome
import com.lunatech.cmt.support.EitherSupport
import dev.dirs.ProjectDirectories
import org.scalatest.BeforeAndAfterEach
Expand All @@ -10,42 +10,24 @@ import org.scalatest.wordspec.AnyWordSpecLike
import sbt.io.IO as sbtio
import sbt.io.syntax.*

import java.nio.charset.StandardCharsets
import scala.compiletime.uninitialized

final class ConfigurationSpec extends AnyWordSpecLike with Matchers with BeforeAndAfterEach with EitherSupport {

var tempDirectory: File = uninitialized
val configFile = file(Configuration.UserConfigDir) / Configuration.CmtGlobalConfigName
var savedCmtConfig: Option[String] =
if (configFile.isFile)
Some(sbtio.readLines(configFile, StandardCharsets.UTF_8).mkString("\n"))
else None

override def beforeEach(): Unit = {
super.beforeEach()
tempDirectory = sbtio.createTemporaryDirectory
sbt.io.IO.delete(configFile)
}

override def afterEach(): Unit = {
savedCmtConfig.foreach { config =>
com.lunatech.cmt.Helpers.dumpStringToFile(config, configFile)
}
sbtio.delete(tempDirectory)
super.afterEach()
}

"load" should {
"create the default configuration in the appropriate home directory" in {
val projectDirectories = ProjectDirectories.from("com", "lunatech", "cmt")
val configDir = file(projectDirectories.configDir)

val cacheDir = file(projectDirectories.cacheDir)
val expectedConfiguration = Configuration(
CmtHome(configDir),
CoursesDirectory(cacheDir / "Courses"),
CurrentCourse(StudentifiedRepo(cacheDir / "Courses")),
GithubApiToken.fromBase64EncodedString(Configuration.DefaultGithubApiToken))
CurrentCourse(StudentifiedRepo(cacheDir / "Courses")))

// this spec is mimic'ing the first time the tool is run, so it expects no config file to exist.
// other specs _may_ have run before this one and already created the config file
// so that's why we ensure that the cmt config file is removed before we execute
// otherwise, if the file exists, it will likely not contain default values and the assertion below will fail
sbtio.delete(configDir / "com.lunatech.cmt.conf")

val receivedConfiguration = assertRight(Configuration.load())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import com.lunatech.cmt.admin.cli.SharedOptions
import com.lunatech.cmt.admin.command
import com.lunatech.cmt.Domain.StudentifiedRepo
import com.lunatech.cmt.client.{Configuration, CoursesDirectory, CurrentCourse}
import com.lunatech.cmt.client.Configuration.{CmtHome, GithubApiToken}
import com.lunatech.cmt.client.Configuration.CmtHome
import com.lunatech.cmt.client.Domain.{ExerciseId, ForceMoveToExercise, TemplatePath}
import com.lunatech.cmt.client.command.{
GotoExercise,
Expand Down Expand Up @@ -219,5 +219,4 @@ private def createConfiguration(studentifiedRepoDirectory: File): Configuration
Configuration(
CmtHome(file(".")),
CoursesDirectory(file(".")),
CurrentCourse(StudentifiedRepo(studentifiedRepoDirectory)),
GithubApiToken(Configuration.DefaultGithubApiToken))
CurrentCourse(StudentifiedRepo(studentifiedRepoDirectory)))

0 comments on commit 409f0a0

Please sign in to comment.