Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional auto merge job #40

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/main/scala/sbtghactions/GenerativeKeys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ trait GenerativeKeys {

lazy val githubWorkflowPublishPreamble = settingKey[Seq[WorkflowStep]]("A list of steps to insert after base setup but before publishing (default: [])")
lazy val githubWorkflowPublishPostamble = settingKey[Seq[WorkflowStep]]("A list of steps to insert after publication but before the end of the publish job (default: [])")
lazy val githubWorkflowPublish = settingKey[Seq[WorkflowStep]]("A sequence workflow steps which publishe the project (default: [Sbt(List(\"+publish\"))])")
lazy val githubWorkflowPublish = settingKey[Seq[WorkflowStep]]("A sequence workflow steps which publish the project (default: [Sbt(List(\"+publish\"))])")
lazy val githubWorkflowPublishTargetBranches = settingKey[Seq[RefPredicate]]("A set of branch predicates which will be applied to determine whether the current branch gets a publication stage; if empty, publish will be skipped entirely (default: [== master])")
lazy val githubWorkflowPublishCond = settingKey[Option[String]]("A set of conditionals to apply to the publish job to further restrict its run (default: [])")

Expand All @@ -58,8 +58,12 @@ trait GenerativeKeys {
lazy val githubWorkflowArtifactUpload = settingKey[Boolean]("Controls whether or not to upload target directories in the event that multiple jobs are running sequentially. Can be set on a per-project basis (default: true)")
lazy val githubWorkflowJobSetup = settingKey[Seq[WorkflowStep]]("The automatically-generated checkout, setup, and cache steps which are common to all jobs which touch the build (default: autogenerated)")

lazy val githubWorkflowEnv = settingKey[Map[String, String]](s"A map of static environment variable assignemnts global to the workflow (default: { GITHUB_TOKEN: $${{ secrets.GITHUB_TOKEN }} })")
lazy val githubWorkflowEnv = settingKey[Map[String, String]]("A map of static environment variable assignments global to the workflow (default: { GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} })")
lazy val githubWorkflowAddedJobs = settingKey[Seq[WorkflowJob]]("A list of additional jobs to add to the CI workflow (default: [])")

lazy val githubWorkflowAutoMerge = settingKey[Boolean]("Whether or not to enable auto merging of bot PRs, e.g. by scala-steward (default: false)")
lazy val githubWorkflowAutoMergeAuthor = settingKey[String]("The user name of a bot whose PRs will be merged automatically (default: scala-steward)")
lazy val githubWorkflowAutoMergeMethod = settingKey[MergeMethod]("The merge method to use for auto merging bot PRs (default: Squash)")
}

object GenerativeKeys extends GenerativeKeys
29 changes: 27 additions & 2 deletions src/main/scala/sbtghactions/GenerativePlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,12 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}"""
githubWorkflowTargetTags := Seq(),

githubWorkflowEnv := Map("GITHUB_TOKEN" -> s"$${{ secrets.GITHUB_TOKEN }}"),
githubWorkflowAddedJobs := Seq())
githubWorkflowAddedJobs := Seq(),

githubWorkflowAutoMerge := false,
githubWorkflowAutoMergeAuthor := "scala-steward",
githubWorkflowAutoMergeMethod := MergeMethod.Squash
)

private lazy val internalTargetAggregation = settingKey[Seq[File]]("Aggregates target directories from all subprojects")

Expand Down Expand Up @@ -543,6 +548,25 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}"""
javas = List(githubWorkflowJavaVersions.value.head),
needs = List("build"))).filter(_ => !githubWorkflowPublishTargetBranches.value.isEmpty)

val autoMergeOpt = if (githubWorkflowAutoMerge.value)
WorkflowJob(
id = "merge",
name = "Merge automatically",
steps = WorkflowStep.Use(
owner = "ridedott",
repo = "merge-me-action",
ref = "v1",
params = Map(
"GITHUB_LOGIN" -> githubWorkflowAutoMergeAuthor.value,
"GITHUB_TOKEN" -> "${{ secrets.GITHUB_TOKEN }}",
"MERGE_METHOD" -> githubWorkflowAutoMergeMethod.value.value
)
) :: Nil,
needs = List("build")
) :: Nil
else
Nil

Seq(
WorkflowJob(
"build",
Expand All @@ -562,7 +586,8 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}"""
matrixAdds = githubWorkflowBuildMatrixAdditions.value,
matrixIncs = githubWorkflowBuildMatrixInclusions.value.toList,
matrixExcs = githubWorkflowBuildMatrixExclusions.value.toList,
runsOnExtraLabels = githubWorkflowBuildRunsOnExtraLabels.value.toList )) ++ publishJobOpt ++ githubWorkflowAddedJobs.value
runsOnExtraLabels = githubWorkflowBuildRunsOnExtraLabels.value.toList )
) ++ autoMergeOpt ++ publishJobOpt ++ githubWorkflowAddedJobs.value
})

private val generateCiContents = Def task {
Expand Down
27 changes: 27 additions & 0 deletions src/main/scala/sbtghactions/MergeMethod.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2020 Daniel Spiewak
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package sbtghactions

sealed trait MergeMethod {
final def value: String = toString.toUpperCase
}

object MergeMethod {
case object Merge extends MergeMethod
case object Squash extends MergeMethod
case object Rebase extends MergeMethod
}
1 change: 1 addition & 0 deletions src/sbt-test/sbtghactions/check-and-regenerate/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ ThisBuild / githubWorkflowBuildMatrixExclusions +=

ThisBuild / githubWorkflowBuild += WorkflowStep.Run(List("echo yo"))
ThisBuild / githubWorkflowPublish += WorkflowStep.Run(List("echo sup"))
ThisBuild / githubWorkflowAutoMerge := true
16 changes: 16 additions & 0 deletions src/sbt-test/sbtghactions/check-and-regenerate/expected-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ jobs:
name: target-${{ matrix.os }}-${{ matrix.scala }}-${{ matrix.java }}
path: targets.tar

merge:
name: Merge automatically
needs: [build]
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.13.1]
java: [[email protected]]
runs-on: ${{ matrix.os }}
steps:
- uses: ridedott/merge-me-action@v1
with:
GITHUB_LOGIN: scala-steward
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
MERGE_METHOD: SQUASH

publish:
name: Publish Artifacts
needs: [build]
Expand Down