Giter Site home page Giter Site logo

redhat-plumbers-in-action / advanced-commit-linter Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 1.0 13.54 MB

๐ŸŽซ ๐Ÿ” Check if your commit messages are in correct format based on policy

License: MIT License

TypeScript 100.00%
cherry-pick commit-message github-action policy-based tickets typescript

advanced-commit-linter's Introduction

Advanced Commit Linter

GitHub Marketplace Tests Lint Code Base CodeQL Check dist/ codecov

Advanced Commit Linter is a GitHub Action that lint commit messages of PR. It checks for issue trackers and upstream references. Results can be displayed as a status check or Pull Request comment when used together with issue-commentator GitHub Action.

How does it work

TBA

Features

  • Tracker references validator
  • Upstream references (cherry-pick) validator

Usage

To set up Advanced Commit Linter, we need three files:

  • Workflow that captures Pull Request metadata (number and commit metadata) and uploads this data as an artifact
  • Workflow that runs on workflow-run trigger, downloads artifact, and runs advanced-commit-linter GitHub Action
  • advanced-commit-linter.yml configuration

Note: Setup is complicated due to GitHub permissions on GITHUB_TOKEN. When used in workflow executed from fork it has read-only permissions. By using the workflow-run trigger we are able to safely overcome this limitation and it allows us to comment on Pull Requests.

policy:
  cherry-pick:
    upstream:
      - github: systemd/systemd
      - github: systemd/systemd-stable
    exception:
      note:
        - rhel-only
  tracker:
    - keyword:
        - 'Resolves: #'
        - 'Related: #'
      type: bugzilla
      issue-format:
        - '[0-9]+$'
      url: 'https://bugzilla.redhat.com/show_bug.cgi?id='
      exception:
        note:
          - github-only
    - keyword:
        - 'Resolves: '
        - 'Related: '
      type: jira
      issue-format:
        - 'JIRA-1234'
      url: 'https://issues.redhat.com/browse/'
      exception:
        note:
          - github-only
name: Gather Pull Request Metadata
on:
  pull_request:
    types: [ opened, reopened, synchronize ]
    branches: [ main ]

permissions:
  contents: read

jobs:
  gather-metadata:
    runs-on: ubuntu-latest

    steps:
      - name: Repository checkout
        uses: actions/checkout@v3

      - id: Metadata
        name: Gather Pull Request Metadata
        uses: redhat-plumbers-in-action/gather-pull-request-metadata@v1

      - name: Upload artifact with gathered metadata
        uses: actions/upload-artifact@v3
        with:
          name: pr-metadata
          path: ${{ steps.Metadata.outputs.metadata-file }}
name: Commit Linter
on:
  workflow_run:
    workflows: [ Gather Pull Request Metadata ]
    types:
      - completed

permissions:
  contents: read

jobs:
  download-metadata:
    if: >
      github.event.workflow_run.event == 'pull_request' &&
      github.event.workflow_run.conclusion == 'success'
    runs-on: ubuntu-latest

    outputs:
      pr-metadata: ${{ steps.Artifact.outputs.pr-metadata-json }}

    steps:
      - id: Artifact
        name: Download Artifact
        uses: redhat-plumbers-in-action/download-artifact@v1
        with:
          name: pr-metadata

  commit-linter:
    needs: [ download-metadata ]
    runs-on: ubuntu-latest

    outputs:
      validated-pr-metadata: ${{ steps.commit-linter.outputs.validated-pr-metadata }}

    permissions:
      # required for creation of checks
      checks: write

    steps:
      - id: commit-linter
        name: Lint Commits
        uses: redhat-plumbers-in-action/advanced-commit-linter@v1
        with:
          pr-metadata: ${{ needs.download-metadata.outputs.pr-metadata }}
          token: ${{ secrets.GITHUB_TOKEN }}

Configuration options

Action currently accepts the following options:

# ...

- uses: redhat-plumbers-in-action/advanced-commit-linter@v1
  with:
    pr-metadata:    <pr-metadata.json>
    config-path:    <path to config file>
    set-status:     <true or false>
    status-title:  <h3 title of status message>
    token:          <GitHub token or PAT>

# ...

pr-metadata

Stringified JSON Pull Request metadata provided by GitHub Action redhat-plumbers-in-action/gather-pull-request-metadata.

Pull Request metadata has the following format: metadata format

  • default value: undefined
  • requirements: required

config-path

Path to configuration file. Configuration file format is described in: Policy section.

  • default value: .github/advanced-commit-linter.yml
  • requirements: optional

set-status

Set status on Pull Request. If enabled, Action will create check-run with validation results.

  • default value: false
  • requirements: optional

status-title

Optional H3 title of status message.

  • default value: Commit validation
  • requirements: optional

token

GitHub token or PAT is used for setting status checks on Pull Request.

# required permission
permissions:
  checks: write
  pull-requests: write
  • default value: undefined
  • requirements: required
  • recomended value: secrets.GITHUB_TOKEN

Policy

Action is configured using special policy file: .github/advanced-commit-linter.yml. The structure needs to be as follows:

policy:
  cherry-pick:
    upstream:
      - github: systemd/systemd
      - github: systemd/systemd-stable
    exception:
      note:
        - rhel-only
  tracker:
    - keyword:
        - 'Resolves: #'
        - 'Related: #'
      type: bugzilla
      issue-format:
        - '[0-9]+$'
      url: 'https://bugzilla.redhat.com/show_bug.cgi?id='
      exception:
        note:
          - github-only
    - keyword:
        - 'Resolves: '
        - 'Related: '
      type: jira
      issue-format:
        - 'RHELPLAN-\d+$'
      url: 'https://issues.redhat.com/browse/'
      exception:
        note:
          - github-only

cherry-pick keyword

The section that specifies upstreams for which you frequently cherry-pick.

  • requirements: optional

cherry-pick.upstream keyword

An array of upstreams. Currently, the only supported upstream location is GitHub.

Supported keys:

  • github - GitHub repository in format <org>/<repo>
  • requirements: required

cherry-pick.exception keyword

Property that describes possible exceptions for referencing upstream commits in commit messages. Currently supported exceptions:

  • note - for example downstream-only or rhel-only

tracker keyword

The section specifies the form and type of required trackers.

tracker[].keyword keyword

Keyword that prefixes tracker identificator.

  • requirements: required
  • example: Fixes:

tracker[].type keyword

Type of tracker. Data can be used by postprocessing scripts/GitHub Actions.

Currently supproted types of trackers are: bugzilla, jira and unknown.

  • requirements: required

tracker[].issue-format keyword

Regex that describes identificator of given tracker.

  • requirements: required
  • example: [0-9]+$

tracker[].url keyword

Url to better display detected trackers in Pull Request comment as a link. Tracker ID will be appended at the end of url.

  • requirements: optional
  • example: https://issues.redhat.com/browse/

tracker[].exception keyword

Property that describes possible exceptions for referencing trackers in commit messages. Currently supported exceptions:

  • note - for example github-only or tests-only

Outputs

validated-pr-metadata

Example of validated metadata object
const metadata = {
  number: 15,
  labels: [
    {
      id: 5610751380,
      name: 'bug',
      description: 'Bug label',
    },
  ],
  milestone: {},
  commits: [
    {
      sha: 'b145cbd729d33cc50d299079a9a5c643531ad053',
      url: 'https://github.com/redhat-plumbers-in-action/advanced-commit-linter/commit/b145cbd729d33cc50d299079a9a5c643531ad053',
      message: {
        title: 'fix Typo in README.md',
        body: 'fix: typo\
\
rhel-only\
\
Related: RHELPLAN-1234',
        cherryPick: [],
      },
      validation: {
        status: 'success',
        message:
          '| https://github.com/redhat-plumbers-in-action/advanced-commit-linter/commit/b145cbd729d33cc50d299079a9a5c643531ad053 - _fix: typo_ | `rhel-only` |',
        tracker: {
          status: 'success',
          message:
            '[RHELPLAN-1234](https://issues.redhat.com/browse/RHELPLAN-1234)',
          data: [
            {
              data: {
                keyword: 'Related: ',
                id: 'RHELPLAN-1234',
                type: 'jira',
                url: 'https://issues.redhat.com/browse/RHELPLAN-1234',
              },
            },
          ],
        },
        upstream: {
          data: [],
          status: 'success',
          exception: 'rhel-only',
        },
      },
    },
  ],
  validation: {
    status: 'success',
    tracker: {
      message: 'Tracker found',
      type: 'unknown',
      id: 'RHELPLAN-1234',
      url: 'https://issues.redhat.com/browse/RHELPLAN-1234',
    },
    message:
      'Tracker - [RHELPLAN-1234](https://issues.redhat.com/browse/RHELPLAN-1234)\
\
#### The following commits meet all requirements\
\
| commit | upstream |\
|---|---|\
| https://github.com/redhat-plumbers-in-action/advanced-commit-linter/commit/b145cbd729d33cc50d299079a9a5c643531ad053 - _fix: typo_ | `rhel-only` |',
  },
};

commits[].validation.status keyword

Status of commit validation. Can be one of the following values:

  • success - commit meets all requirements
  • failure - commit does not meet all requirements

commits[].validation.message keyword

Message that describes commit validation status.

commits[].validation.tracker keyword

Object that describes all trackers detected in commit message and their validation status.

tracker: {
  status: 'success',
  message: '[RHELPLAN-1234](https://issues.redhat.com/browse/RHELPLAN-1234)',
  data: [{
    data: {
      keyword: 'Related: ',
      id: 'RHELPLAN-1234',
      type: 'jira',
      url: 'https://issues.redhat.com/browse/RHELPLAN-1234',
    },
  }],
}

commits[].validation.upstream keyword

Object that describes all upstreams detected in commit message and their validation status.

upstream: {
  data: [{
    sha: 'b145cbd729d33cc50d299079a9a5c643531ad053',
    repo: 'systemd/systemd',
    url: 'https://github.com/systemd/systemd/commit/b145cbd729d33cc50d299079a9a5c643531ad053',
  }],
  status: 'success',
  exception: 'rhel-only',
}

validation keyword

Object that describes overall validation status of all commits in Pull Request.

validation: {
  status: 'success',
  tracker: {
    message: 'Tracker found',
    type: 'unknown',
    id: 'RHELPLAN-1234',
    url: 'https://issues.redhat.com/browse/RHELPLAN-1234',
  },
  message:
    'Tracker - [RHELPLAN-1234](https://issues.redhat.com/browse/RHELPLAN-1234)\
\
#### The following commits meet all requirements\
\
| commit | upstream |\
|---|---|\
| https://github.com/redhat-plumbers-in-action/advanced-commit-linter/commit/b145cbd729d33cc50d299079a9a5c643531ad053 - _fix: typo_ | `rhel-only` |',
}

advanced-commit-linter's People

Contributors

dependabot[bot] avatar jamacku avatar renovate[bot] avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

jamacku

advanced-commit-linter's Issues

Summary comment overhauled, use tables instead plain text :bookmark_tabs:

Type of issue

Feature Request

Description

Let's make the summary more readable. Proposed design:


Tracker - Missing, needs inspection! โœ‹

The following commits meet all requirements

commit upstream
actions-private-playground/systemd-rhel9@fcbc7f6 - virt: Further improve detection of EC2 metal instances systemd/systemd@aab896e
systemd/systemd-stable@aab896e
actions-private-playground/systemd-rhel9@fe0df9c - docs: update unit name for sd-tmpfiles-setup systemd/systemd@7d33146
systemd/systemd-stable@7d33146
actions-private-playground/systemd-rhel9@e7e95c9 - doc: update README.md rhel-only

The following commits need an inspection

commit note
actions-private-playground/systemd-rhel9@5310d65 - Update README Missing upstream reference โ€ผ๏ธ
actions-private-playground/systemd-rhel9@5310d65 - Update README Missing issue tracker โœ‹
actions-private-playground/systemd-rhel9@5310d65 - Update README Missing upstream reference โ€ผ๏ธ
Missing issue tracker โœ‹

Describe the solution you'd like

No response

Make sure that all usecases works as expected :rescue_worker_helmet:

Type of issue

Bug Report

Description

Currently, the only properly tested use case is for systemd source-git workflow (full-config). Make sure that the following use cases work as expected and are covered by tests:

  • no configuration
  • only upstream configuration
  • only tracker configuration

Describe the solution you'd like

No response

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

This repository currently has no open or pending branches.

Detected dependencies

github-actions
.github/workflows/check-dist.yml
  • actions/checkout v4
  • actions/setup-node v4
  • actions/upload-artifact v4
.github/workflows/codeql-analysis.yml
  • actions/checkout v4
  • github/codeql-action v3
  • github/codeql-action v3
  • github/codeql-action v3
.github/workflows/issue-labeler.yml
  • actions/checkout v4
  • stefanbuck/github-issue-parser v2
.github/workflows/lint.yml
  • actions/checkout v4
  • actions/setup-node v4
.github/workflows/publish-release.yml
  • actions/checkout v4
  • Actions-R-Us/actions-tagger v2
.github/workflows/release-drafter.yml
  • release-drafter/release-drafter v6
.github/workflows/test.yml
  • actions/checkout v4
  • actions/setup-node v4
  • codecov/codecov-action v4
npm
package.json
  • @actions/core 1.10.1
  • @actions/github 6.0.0
  • @octokit/core 6.1.2
  • @octokit/plugin-throttling 9.3.0
  • @probot/octokit-plugin-config 3.0.2
  • zod 3.23.8
  • @octokit/types 13.5.0
  • @total-typescript/ts-reset 0.5.1
  • @types/node 20.12.12
  • @vercel/ncc 0.38.1
  • @vitest/coverage-v8 1.6.0
  • prettier 3.2.5
  • ts-node 10.9.2
  • typescript 5.4.5
  • vitest 1.6.0
  • yarn 4.2.2

  • Check this box to trigger a request for Renovate to run again on this repository

Show commits with missing tracker is section with commits that needs inspection

Type of issue

Feature Request

Description

In the current implementation, only commits with missing upstream references are listed in the section with commits that need an inspection. It would be helpful to show there are also commits that failed tracker validation.

Describe the solution you'd like

We might have to extend the validation schema ...

Show only the first upstream repo in which a commit is present

Type of issue

Feature Request

Description

Currently, if multiple upstream repos's are specified, the linter looks for a commit in all of them, with output like:

The following commits meet all requirements

commit upstream
redhat-plumbers/systemd-rhel8@36a944a - logind: simplify code systemd/systemd@e5c09aa
systemd/systemd-stable@e5c09aa

I don't see much value in doing that in general and it's completely pointless in this particular case, as every commit from systemd/systemd will be present in systemd/systemd-stable eventually.

Describe the solution you'd like

List just the first upstream repo that contains the commit. Example:

The following commits meet all requirements

commit upstream
redhat-plumbers/systemd-rhel8@36a944a - logind: simplify code systemd/systemd@e5c09aa

Use proper Check run instead of Commit status :statue_of_liberty:

Type of issue

Feature Request

Description

Check runs are harder to implement, but they provide greater flexibility which we could definitely take advantage of.

Resources:

Describe the solution you'd like

No response

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.