Skip to content

Commit

Permalink
feat: Add license utility to chectl to validate and add license heade…
Browse files Browse the repository at this point in the history
…r to code

Signed-off-by: Flavius Lacatusu <[email protected]>
  • Loading branch information
flacatus committed Jun 4, 2021
1 parent 82957d1 commit 59dafd4
Show file tree
Hide file tree
Showing 110 changed files with 837 additions and 368 deletions.
48 changes: 48 additions & 0 deletions .ci/check-license.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#
# Copyright (c) 2019-2021 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation

export ROOT_DIR=$(dirname $(dirname $(readlink -f "$0")));

# Validate a Eclipse Che license header
function validateChectlLicenseHeader() {
python "${ROOT_DIR}"/.ci/validate-license.py $(find "${ROOT_DIR}" -type d \( -path "${ROOT_DIR}"/node_modules -o -path "${ROOT_DIR}"/templates \) -prune -false -o -name '*.sh' -o -name '*.ts' -o -name '*.yml' -o -name '*.yaml' \
| grep -v installers/cert-manager.yml)
}

# Add a license to a file without license
function addLicensetoChectlCode() {
if ! command -v addlicense &> /dev/null
then
echo "Command addlicense not found locally. Please install it from https://github.com/google/addlicense."
exit 1
fi

addlicense -v -f "${ROOT_DIR}"/license_header.txt $(find . -type d \( -path "${ROOT_DIR}"/node_modules -o -path "${ROOT_DIR}"/templates \) -prune -false -o \( -name '*.sh' -o -name '*.ts' -o -name '*.yml' -o -name '*.yaml' \))
}

# catch first arguments with $1
case "$1" in
-c|--check-license)
echo -e "[INFO] Launching Eclipse Che license header check."
validateChectlLicenseHeader
;;
-a|--add-license)
echo -e "[INFO] Start adding Eclipse Che license headers to code."
addLicensetoChectlCode
;;
*)
# else
echo "Usage:
-c|--check-license: Check Eclipse license in codebase
-a|--add-license: Add a license to codebase. The file should not have any license if you execute this command.
"
;;
esac
3 changes: 1 addition & 2 deletions .ci/oci_chectl_olm.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/bin/bash
#
# Copyright (c) 2012-2020 Red Hat, Inc.
# Copyright (c) 2019-2021 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
Expand Down
3 changes: 1 addition & 2 deletions .ci/oci_chectl_operator.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/bin/bash
#
# Copyright (c) 2012-2020 Red Hat, Inc.
# Copyright (c) 2019-2021 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
Expand Down
68 changes: 68 additions & 0 deletions .ci/validate-license.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from __future__ import (
absolute_import, print_function, division, unicode_literals
)

import logging
import re
import sys
from datetime import datetime

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

CURRENT_YEAR = datetime.today().year

COPYRIGHT_RE=r'Copyright \(c\) 2019-'+str(CURRENT_YEAR) + r' Red Hat, Inc.'
PATTERN1=r'This program and the accompanying materials are made'
PATTERN2=r'available under the terms of the Eclipse Public License 2.0'
PATTERN3=r'which is available at https://www.eclipse.org/legal/epl-2.0/'
PATTERN4=r'SPDX-License-Identifier: EPL-2.0'
PATTERN5=r'Contributors:'
PATTERN6=r'Red Hat, Inc. - initial API and implementation'
ARRAY_OF_PATTERNS=[COPYRIGHT_RE, PATTERN6, PATTERN2, PATTERN3, PATTERN4, PATTERN5, PATTERN6]

def update_go_license(name, force=False):
with open(name) as f:
orig_lines = list(f)
lines = list(orig_lines)

for pattern in ARRAY_OF_PATTERNS:
try:
validated = license_lines_check(lines,pattern)
if validated is None:
raise ValueError('Exception: Found an invalid license, fileName=%s, pattern=%s, success=%s' % (name, pattern, False))
except ValueError as err:
print(err.args)
sys.exit(1)
print('Successfully validated license header', 'file_name=%s, success=%s' % (name, True))

def license_lines_check(lines, pattern):
for i, line in enumerate(lines[:20]):
found = False

m = re.compile(pattern, re.I).search(line)
if not m:
continue
found=True

return found

def main():
if len(sys.argv) == 1:
print('USAGE: %s FILE ...' % sys.argv[0])
sys.exit(1)

for name in sys.argv[1:]:
if name.endswith('.ts') or name.endswith('.sh') or name.endswith('.yaml') or name.endswith('.yml'):
try:
update_go_license(name)
except Exception as error:
logger.error('Failed to process file %s', name)
logger.exception(error)
raise error
else:
raise NotImplementedError('Unsupported file type: %s' % name)


if __name__ == "__main__":
main()
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
#
# Copyright (c) 2019-2021 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation

version: 2
updates:
- package-ecosystem: npm
Expand Down
14 changes: 14 additions & 0 deletions .github/workflows/automation-check.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
#
# Copyright (c) 2019-2021 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation

name: Chectl
on: [pull_request]

Expand All @@ -24,3 +35,6 @@ jobs:
echo "[ERROR] README.md it is not up to date. Please run 'yarn oclif-dev readme' to update and commit the changes."
exit 1
fi
- name: Check Eclipse license headers
run: |
/bin/bash .ci/check-license.sh --check-license
14 changes: 7 additions & 7 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#
# Copyright (c) 2012-2021 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
# Copyright (c) 2019-2021 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
# Contributors:
# Red Hat, Inc. - initial API and implementation

name: Code Coverage Report
on: [push]
Expand Down
15 changes: 8 additions & 7 deletions .github/workflows/e2e-minikube-devworkspace.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#
# Copyright (c) 2012-2020 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
# Copyright (c) 2019-2021 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
# Contributors:
# Red Hat, Inc. - initial API and implementation

name: Minikube E2E
on: pull_request
jobs:
Expand Down
15 changes: 8 additions & 7 deletions .github/workflows/e2e-minikube-helm.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#
# Copyright (c) 2012-2020 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
# Copyright (c) 2019-2021 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
# Contributors:
# Red Hat, Inc. - initial API and implementation

name: Minikube E2E
on: pull_request
jobs:
Expand Down
15 changes: 8 additions & 7 deletions .github/workflows/e2e-minikube-olm.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#
# Copyright (c) 2012-2020 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
# Copyright (c) 2019-2021 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
# Contributors:
# Red Hat, Inc. - initial API and implementation

name: Minikube E2E
on: pull_request
jobs:
Expand Down
15 changes: 8 additions & 7 deletions .github/workflows/e2e-minikube-operator-assembly.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#
# Copyright (c) 2012-2020 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
# Copyright (c) 2019-2021 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
# Contributors:
# Red Hat, Inc. - initial API and implementation

name: Minikube E2E
on: pull_request
jobs:
Expand Down
15 changes: 8 additions & 7 deletions .github/workflows/e2e-minikube-operator-update.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#
# Copyright (c) 2021 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
# Copyright (c) 2019-2021 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
# Contributors:
# Red Hat, Inc. - initial API and implementation

name: Minikube E2E
on: pull_request
jobs:
Expand Down
15 changes: 8 additions & 7 deletions .github/workflows/e2e-minikube-operator.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#
# Copyright (c) 2012-2020 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
# Copyright (c) 2019-2021 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
# Contributors:
# Red Hat, Inc. - initial API and implementation

name: Minikube E2E
on: pull_request
jobs:
Expand Down
15 changes: 8 additions & 7 deletions .github/workflows/e2e-minishift-operator.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#
# Copyright (c) 2012-2020 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
# Copyright (c) 2019-2021 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
# Contributors:
# Red Hat, Inc. - initial API and implementation

name: Minishift E2E
on: pull_request
jobs:
Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/happy-path.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#
# Copyright (c) 2012-2021 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
# Copyright (c) 2019-2021 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
# Contributors:
# Red Hat, Inc. - initial API and implementation

name: Happy Path
on:
Expand Down
15 changes: 8 additions & 7 deletions .github/workflows/release-announce.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#
# Copyright (c) 2012-2020 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
# Copyright (c) 2019-2021 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
# Contributors:
# Red Hat, Inc. - initial API and implementation

name: Release - announce in mattermost
on:
# Trigger the workflow on tag creation only
Expand Down
Loading

0 comments on commit 59dafd4

Please sign in to comment.