Skip to content

Commit

Permalink
extract labels to composite action
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuVeber committed Jun 11, 2024
1 parent fee1724 commit d6144bf
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 80 deletions.
61 changes: 61 additions & 0 deletions .github/actions/set-labels/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Set Labels
description: Issue or PR title must follow conventional commits.

inputs:
github-token:
description: The GitHub token to use.
required: true

runs:
using: "composite"
steps:
- name: Set Labels
uses: actions/github-script@v7
with:
github-token: ${{ inputs.github-token }}
script: |
const { NUMBER, OWNER, REPO, TITLE } = process.env
const regex = /^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(?:\((\w+)\))?(!)?: (.+)$/;
console.log(`Checking title "${TITLE}"`);
console.log("For more information: https://www.conventionalcommits.org/en/v1.0.0/");
if (!regex.test(TITLE)) {
throw new Error("Title must follow conventional commit format");
}
const labels = [];
const match = TITLE.match(regex);
switch (match[1]) {
case "feat":
labels.push("feature");
break;
default:
labels.push(match[1]);
break;
}
if (match[3]) {
labels.push("major");
}
else if (labels.includes("feature")) {
labels.push("minor");
}
else if (labels.includes("fix")) {
labels.push("patch");
}
console.log(`@${OWNER}/${REPO}#${NUMBER}: Setting labels to ${labels}`);
await github.rest.issues.setLabels({
owner: OWNER,
repo: REPO,
issue_number: NUMBER,
labels
});
env:
NUMBER: ${{ github.event.issue.number || github.event.pull_request.number }}
OWNER: ${{ github.repository_owner }}
REPO: ${{ github.event.repository.name }}
TITLE: ${{ github.event.issue.title || github.event.pull_request.title }}
41 changes: 1 addition & 40 deletions .github/workflows/issue.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,6 @@ jobs:
project-url: https://github.com/orgs/hedia-team/projects/2

- name: Set Labels
uses: actions/github-script@v7
uses: hedia-team/.github/.github/actions/set-labels@big-refactor
with:
github-token: ${{ secrets.HEDIA_BOT_GITHUB_PAT }}
script: |
const owner = "${{ github.repository_owner }}";
const repo = "${{ github.event.repository.name }}";
const issue_number = ${{ github.event.issue.number }};
const title = "${{ github.event.issue.title }}";
const regex = /^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(?:\((\w+)\))?(!)?: (.+)$/;
console.log(`Checking title "${title}"`);
console.log("For more information: https://www.conventionalcommits.org/en/v1.0.0/");
if (!regex.test(title)) {
throw new Error("Title must follow conventional commit format");
}
const labels = [];
const match = title.match(regex);
switch (match[1]) {
case "feat":
labels.push("feature");
break;
default:
labels.push(match[1]);
break;
}
if (match[3]) {
labels.push("major");
}
else if (labels.includes("feature")) {
labels.push("minor");
}
else {
labels.push("patch");
}
console.log(`@${owner}/${repo}#${issue_number}: Setting labels to ${labels}`);
await github.rest.issues.setLabels({ owner, repo, issue_number, labels });
41 changes: 1 addition & 40 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,45 +71,6 @@ jobs:
await github.rest.pulls.requestReviewers({ owner, repo, pull_number, reviewers, team_reviewers });
- name: Set Labels
uses: actions/github-script@v7
uses: hedia-team/.github/.github/actions/set-labels@big-refactor
with:
github-token: ${{ secrets.HEDIA_BOT_GITHUB_PAT }}
script: |
const owner = "${{ github.repository_owner }}";
const repo = "${{ github.event.repository.name }}";
const issue_number = ${{ github.event.pull_request.number }};
const title = "${{ github.event.pull_request.title }}";
const regex = /^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(?:\((\w+)\))?(!)?: (.+)$/;
console.log(`Checking title "${title}"`);
console.log("For more information: https://www.conventionalcommits.org/en/v1.0.0/");
if (!regex.test(title)) {
throw new Error("Title must follow conventional commit format");
}
const labels = [];
const match = title.match(regex);
switch (match[1]) {
case "feat":
labels.push("feature");
break;
default:
labels.push(match[1]);
break;
}
if (match[3]) {
labels.push("major");
}
else if (labels.includes("feature")) {
labels.push("minor");
}
else if (labels.includes("fix")) {
labels.push("patch");
}
console.log(`@${owner}/${repo}#${issue_number}: Setting labels to ${labels}`);
await github.rest.issues.setLabels({ owner, repo, issue_number, labels });

0 comments on commit d6144bf

Please sign in to comment.