Skip to content

Commit

Permalink
Added workflow for auto-creating sonarcloud projects on merge + added… (
Browse files Browse the repository at this point in the history
  • Loading branch information
MatanHeledPort authored Jan 23, 2024
1 parent 4ef5711 commit b5ddaa5
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 26 deletions.
75 changes: 75 additions & 0 deletions .github/workflows/create-new-sonarcloud-project.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: SonarCloud Setup

on:
push:
branches:
- main
paths:
- integrations/**

jobs:
pre-run:
outputs:
changed_integrations: ${{ steps.create-matrix.outputs.INTEGRATIONS_MATRIX }}
count_changed_integrations: ${{steps.changed-integrations.outputs.all_modified_files_count }}
name: Build Sonarcloud scan run
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get all changed integrations
id: changed-integrations
uses: tj-actions/changed-files@v40
with:
json: true
dir_names: true
dir_names_max_depth: 2
files_ignore:
integrations/_infra/**
files: |
integrations/**
- name: Output changes integrations
id: create-matrix
run: |
echo ${{ steps.changed-integrations.outputs.all_changed_files }}
integrations_array=${{ steps.changed-integrations.outputs.all_changed_files }}
echo "INTEGRATIONS_MATRIX=$(jq -cn --argjson integArr $integrations_array '{integrations: $integArr}')" >> $GITHUB_OUTPUT
setup-sonarcloud:
if: ${{ needs.pre-run.outputs.count_changed_integrations > 0 }}
needs: pre-run
runs-on: ubuntu-latest
name: Create new project for integrations
strategy:
matrix: ${{ fromJson(needs.pre-run.outputs.changed_integrations) }}
steps:
- name: Create integration variable
run: |
unsplit_integ="${{ matrix.integrations }}"
integration=${unsplit_integ##*/}
echo "INTEG_NAME=${integration}" >> $GITHUB_ENV
echo "INTEG_PROJECT_ID=port-labs_ocean_${integration}" >> $GITHUB_ENV
integration=$(echo ${integration} | tr '-' '_') # replace '-'' with '_'
integration=$(echo ${integration} | awk '{print toupper($0)}') # Uppercase the string
echo "INTEG_SECRET_NAME=${integration}" >> $GITHUB_ENV
- name: Check if SonarCloud project exists
id: check_project
run: |
PROJECT_EXISTS=$(curl -u ${{ secrets.CREATE_SONARCLOUD_PROJECT_TOKEN }}: -X GET "https://sonarcloud.io/api/projects/search?projects=${{ env.INTEG_PROJECT_ID }}&organization=port-labs" | jq '.components | length > 0')
echo "PROJECT_EXISTS=$PROJECT_EXISTS" >> $GITHUB_ENV
- name: Create SonarCloud project and token
if: env.PROJECT_EXISTS == 'false'
run: |
# Create Project
curl -u ${{ secrets.CREATE_SONARCLOUD_PROJECT_TOKEN }}: -X POST "https://sonarcloud.io/api/projects/create?name=${{ env.INTEG_PROJECT_ID }}&project=Ocean-${{env.INTEG_NAME}}&visibility=public&organization=port-labs"
# Generate Token
SONAR_TOKEN=$(curl -u ${{ secrets.CREATE_SONARCLOUD_PROJECT_TOKEN }}: -X POST "https://sonarcloud.io/api/user_tokens/generate?name=${{env.INTEG_NAME}}-token" | jq -r '.token')
echo "SONAR_TOKEN=$SONAR_TOKEN" >> $GITHUB_ENV
- name: Create GitHub Repo Secret
if: env.SONAR_TOKEN != ''
uses: gliech/create-github-secret-action@v1
with:
name: ${{ env.INTEG_SECRET_NAME }}
value: ${{ env.SONAR_TOKEN }}
pa_token: ${{ secrets.PA_TOKEN_FOR_SECRET_CREATION }}
9 changes: 2 additions & 7 deletions .github/workflows/sonarcloud-framework.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@ on:
- main
paths:
- port_ocean/**
pull_request:
types:
- ready_for_review
- opened
- synchronize
paths:
- port_ocean/**
workflow_dispatch:

jobs:
sonarcloud:
name: Sonarcloud scan changed framework
Expand Down
9 changes: 2 additions & 7 deletions .github/workflows/sonarcloud-integrations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@ on:
- main
paths:
- integrations/**
pull_request:
types:
- ready_for_review
- opened
- synchronize
paths:
- integrations/**
workflow_dispatch:

jobs:
pre-run:
outputs:
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

<!-- towncrier release notes start -->

## 0.4.17 (2024-01-23)


### Features

- Added sonarcloud files for public integration scaffolding (PORT-6181)
- Replaced the `remove-docker` option from the `ocean new` cli with `private` & `public` flags (PORT-6181)


## 0.4.16 (2024-01-11)


Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ run: lint
$(ACTIVATE) && poetry run ocean sail ./integrations/example

new:
$(ACTIVATE) && poetry run ocean new ./integrations --remove-docker-files
$(ACTIVATE) && poetry run ocean new ./integrations --public

test: lint
$(ACTIVATE) && pytest
Expand Down
12 changes: 6 additions & 6 deletions port_ocean/cli/commands/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
@cli_start.command()
@click.argument("path", default=".", type=click.Path(exists=True))
@click.option(
"--remove-docker-files",
"remove_docker_files",
"--private/--public",
"is_private_integration",
is_flag=True,
default=False,
help="Remove Docker files from the generated project. (Used for contributing to Ocean)",
default=True,
help="Handle private integrations scaffolding. (Used for contributing to Ocean when set to public)",
)
def new(path: str, remove_docker_files: bool) -> None:
def new(path: str, is_private_integration: bool) -> None:
"""
Scaffold a new integration in the given PATH.
Expand All @@ -32,7 +32,7 @@ def new(path: str, remove_docker_files: bool) -> None:
f"{cli_root_path}/cookiecutter",
output_dir=path,
extra_context={
"remove_docker_files": remove_docker_files,
"is_private_integration": is_private_integration,
},
)
name = result.split("/")[-1]
Expand Down
2 changes: 1 addition & 1 deletion port_ocean/cli/cookiecutter/cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"full_name": "Your name",
"email": "Your address email <[email protected]>",
"release_date": "{% now 'local' %}",
"remove_docker_files": false,
"is_private_integration": true,
"_extensions": [
"jinja2_time.TimeExtension",
"extensions.VersionExtension"
Expand Down
8 changes: 5 additions & 3 deletions port_ocean/cli/cookiecutter/hooks/post_gen_project.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import os


def delete_docker_files():
if "{{ cookiecutter.remove_docker_files }}" == "True":
def handle_private_integration_flags():
if "{{ cookiecutter.is_private_integration }}" == "True":
os.remove("Dockerfile")
os.remove(".dockerignore")
if "{{ cookiecutter.is_private_integration }}" == "False":
os.remove("sonar-project.properties")


if __name__ == "__main__":
delete_docker_files()
handle_private_integration_flags()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sonar.projectKey=port-labs_ocean_{{ cookiecutter.integration_name.lower().replace(' ', '-') }}
sonar.organization=port-labs
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "port-ocean"
version = "0.4.16"
version = "0.4.17"
description = "Port Ocean is a CLI tool for managing your Port projects."
readme = "README.md"
homepage = "https://app.getport.io"
Expand Down

0 comments on commit b5ddaa5

Please sign in to comment.