From 5408529b11fa4e78e0f3e99e7a7efbbfab107793 Mon Sep 17 00:00:00 2001 From: HugoRCD Date: Tue, 3 Sep 2024 09:22:48 +0200 Subject: [PATCH] Add github workflow --- .github/workflows/build.yml | 22 +++++++ .github/workflows/label-pr.yml | 70 +++++++++++++++++++++ .github/workflows/semantic-pull-request.yml | 64 +++++++++++++++++++ 3 files changed, 156 insertions(+) create mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/label-pr.yml create mode 100644 .github/workflows/semantic-pull-request.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..6a2483c --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,22 @@ +name: Check if packages can be built + +on: + pull_request: + branches: + - '**' + +permissions: + contents: read + +jobs: + autofix: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: oven-sh/setup-bun@v2 + + - name: 📦 Install dependencies + run: bun install + + - name: 🛠️ Build + run: bun run build diff --git a/.github/workflows/label-pr.yml b/.github/workflows/label-pr.yml new file mode 100644 index 0000000..472b64a --- /dev/null +++ b/.github/workflows/label-pr.yml @@ -0,0 +1,70 @@ +name: Label PR + +on: + pull_request_target: + types: + - opened + branches: + - main + +jobs: + add-pr-labels: + name: Add PR labels + runs-on: ubuntu-latest + permissions: + pull-requests: write + steps: + - uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 + env: + PULL_REQUEST_TITLE: ${{ github.event.pull_request.title }} + with: + script: | + const labelsToAdd = [] + + const pullRequest = { + number: ${{ github.event.pull_request.number }}, + title: process.env.PULL_REQUEST_TITLE, + labelsNames: ${{ toJson(github.event.pull_request.labels.*.name) }} + } + + // Select label based on the type in PR title + const pullRequestTypeToLabelName = { + breaking: 'breaking', + feat: 'feature', + fix: 'bug', + build: 'build', + ci: 'ci', + docs: 'documentation', + enhancement: 'enhancement', + chore: 'dependencies', + perf: 'performance', + style: 'style', + test: 'test', + refactor: 'refactor', + revert: 'revert' + } + + for (const [pullRequestType, labelName] of Object.entries( + pullRequestTypeToLabelName + )) { + if ( + pullRequest.title.startsWith(pullRequestType) && + !pullRequest.labelsNames.includes( + pullRequestTypeToLabelName[pullRequestType] + ) + ) { + labelsToAdd.push(labelName) + + break + } + } + + // Add selected labels + if (labelsToAdd.length > 0) { + github.rest.issues.addLabels({ + issue_number: pullRequest.number, + owner: context.repo.owner, + repo: context.repo.repo, + labels: labelsToAdd + }) + } diff --git a/.github/workflows/semantic-pull-request.yml b/.github/workflows/semantic-pull-request.yml new file mode 100644 index 0000000..a96a3af --- /dev/null +++ b/.github/workflows/semantic-pull-request.yml @@ -0,0 +1,64 @@ +name: validate pr title + +on: + pull_request_target: + types: + - opened + - reopened + - edited + - synchronize + +permissions: + pull-requests: write + +jobs: + validate-pr: + name: Validate PR title + if: ${{ !contains(github.actor, 'renovate') }} + + runs-on: ubuntu-latest + steps: + - uses: amannn/action-semantic-pull-request@v5 + id: lint_pr_title + with: + types: | + breaking + feat + fix + build + ci + docs + enhancement + chore + performance + style + test + refactor + revert + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - uses: marocchino/sticky-pull-request-comment@v2 + # When the previous steps fail, the workflow would stop. By adding this + # condition you can continue the execution with the populated error message. + if: always() && (steps.lint_pr_title.outputs.error_message != null) + with: + header: pr-title-lint-error + message: | + Hey there and thank you for opening this pull request! 👋🏼 + + We require pull request titles to follow the [Conventional Commits specification](https://www.conventionalcommits.org/en/v1.0.0/) and it looks like your proposed title needs to be adjusted. + + Details: + + ``` + ${{ steps.lint_pr_title.outputs.error_message }} + ``` + + # Delete a previous comment when the issue has been resolved + - if: ${{ steps.lint_pr_title.outputs.error_message == null }} + uses: marocchino/sticky-pull-request-comment@v2 + with: + header: pr-title-lint-error + message: | + Thank you for following the naming conventions! 🙏