Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Use Spotless plugin to enforce and update copyright headers #701

Copy link
Copy link
@simonbasle

Description

@simonbasle
Issue body actions

See https://github.com/diffplug/spotless/tree/main/plugin-gradle#-spotless-plugin-for-gradle

This issue, unlike #700, is solely focused on using Spotless for copyright headers.

In snippets below, make sure to replace [[[ RELEVANT BRANCH ]]] and [[[ START DATE ]]] with the correct information.

spotless {
	if (project.hasProperty("spotlessFrom")) {
		if (project.spotlessFrom == "ALL") {
			println "[Spotless] Ratchet deactivated"
		}
		else {
			println "[Spotless] Ratchet from $project.spotlessFrom"
			ratchetFrom project.spotlessFrom
		}
	}
	else if (isCiServer) {
		println "[Spotless] CI detected without explicit branch, not enforcing check"
		enforceCheck false
	}
	else {
		String spotlessBranch = "origin/[[[ RELEVANT BRANCH ]]]"
		println "[Spotless] Local run detected, ratchet from $spotlessBranch"
		ratchetFrom spotlessBranch
	}
	java {
		target '**/*.java'
		licenseHeaderFile('codequality/spotless/licenseSlashstarStyle.txt')
		removeUnusedImports()
	}
}

It relies on the -PspotlessFrom to activate spotless on specific CI jobs, since spotless otherwise needs a full checkout of the project (in order to compare the commit with the relevant base branch).

We can update the checkout action of an existing job and add spotless as a separate step in order to make failures stand out. That would avoid an extra job, which counts towards CI limits.

Let's assume we have a check job that has checkout,java,gradle checks steps. We configure the checkout step to now checkout everything and add a spotless step configured to "ratchet from" the base branch of the PR right before the existing gradle step:

#ASSUMING THIS JOB EXISTS:
  check:
    name: other checks
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
#ADD:
      with:
        fetch-depth: 0
#ASSUMING NOW THERE'S A STEP TO CONFIGURE JAVA LIKE:
    - uses: actions/setup-java@v1
      with:
        java-version: 8
#ADD:
    - uses: eskatos/gradle-command-action@v1
      name: gradle
      with:
        arguments: spotlessCheck -PspotlessFrom=origin/${{ github.base_ref }}
        wrapper-cache-enabled: true
        dependencies-cache-enabled: true
        configuration-cache-enabled: true
#ASSUMING NOW THERE IS A check GRADLE STEP LIKE:
    - uses: eskatos/gradle-command-action@v1
      name: other tests
      with:
        arguments: check -x :reactor-core:test --no-daemon
        wrapper-cache-enabled: true
        dependencies-cache-enabled: true
        configuration-cache-enabled: true

The license file includes two line returns, which will enforce a single blank line by Spotless:

/*
 * Copyright (c) $YEAR VMware Inc. or its affiliates, All Rights Reserved.
 *
 * 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
 *
 *   https://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.
 */


Spotless interprets the $YEAR variable correctly in the face of ranges, updating the end year of the range but not the start year (2017-2018 -> 2017-2021). If a single past year is present, Spotless will add the current year as end of the range (2017 -> 2017-2021).

TODO:

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/all-projectsissues that are transverse to all projects, epicsissues that are transverse to all projects, epics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.