Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci(*): update dependabot.yml commit message type and pull-request.yml labeler condition #221

Merged
merged 2 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ updates:
- package-ecosystem: 'npm'
assignees:
- 'lumirlumir'
commit-message:
prefix: 'chore'
include: 'scope'
# Specify all directories from the current layer and below recursively, using globstar, for locations of manifest files.
directories:
- '**/*'
Expand Down Expand Up @@ -38,6 +41,9 @@ updates:
- package-ecosystem: 'github-actions'
assignees:
- 'lumirlumir'
commit-message:
prefix: 'chore'
include: 'scope'
# Workflow files stored in the default location of `.github/workflows`. (You don't need to specify `/.github/workflows` for `directory`. You can use `directory: "/"`.)
directory: '/'
reviewers:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,143 +142,143 @@
scope: ${{ steps.lint-pull-request-title.outputs.scope }}

labeler:
if: github.event.action == 'opened' || github.event.changes.title
if: github.event.action == 'opened' || github.event.changes.title || github.event.action == 'synchronize'

runs-on: ubuntu-latest

needs:
- lint-pull-request-title

steps:
- name: Labeler
uses: actions/github-script@v7
id: labeler
with:
result-encoding: string
script: |
// --------------------------------------------------------------------------------
// Constants
// --------------------------------------------------------------------------------

const isBreakingChange = context.payload.pull_request.title.includes(')!: ');

const owner = context.repo.owner;
const repo = context.repo.repo;
const issue_number = context.issue.number;

const emojiPrefix = ':label: ';
const breakingChangeLabelPrefix = `${emojiPrefix}BREAKING CHANGE`;
const typeLabelPrefix = `${emojiPrefix}type:`;
const scopeLabelPrefix = `${emojiPrefix}scope:`;

const newBreakingChangeLabel = breakingChangeLabelPrefix;
const newTypeLabel = `${typeLabelPrefix} ${{ needs.lint-pull-request-title.outputs.type }}`;
const newScopeLabel = `${scopeLabelPrefix} ${{ needs.lint-pull-request-title.outputs.scope }}`;

const newConventionalCommitsTypeLabels = isBreakingChange ? [newBreakingChangeLabel, newTypeLabel, newScopeLabel] : [newTypeLabel, newScopeLabel];

// --------------------------------------------------------------------------------
// Helpers
// --------------------------------------------------------------------------------

const formatLabels = labels => labels.map(label => `\`${label}\``).join(', ');

// --------------------------------------------------------------------------------
// List all current labels
// --------------------------------------------------------------------------------

// https://docs.github.com/en/rest/issues/labels?apiVersion=2022-11-28#list-labels-for-an-issue
const { data: labels } = await github.rest.issues.listLabelsOnIssue({
owner,
repo,
issue_number,
});

const oldConventionalCommitsTypeLabels = labels
.map(({ name }) => name)
.filter(label => label.startsWith(emojiPrefix));

// --------------------------------------------------------------------------------
// Choose the labels to add and remove
// --------------------------------------------------------------------------------

const labelsToAdd = newConventionalCommitsTypeLabels.filter(label => !oldConventionalCommitsTypeLabels.includes(label));
const labelsToRemove = oldConventionalCommitsTypeLabels.filter(label => !newConventionalCommitsTypeLabels.includes(label));

// --------------------------------------------------------------------------------
// Debug
// --------------------------------------------------------------------------------

console.log(`New Conventional Commits type labels: ${formatLabels(newConventionalCommitsTypeLabels)}`);
console.log(`Old Conventional Commits type labels: ${formatLabels(oldConventionalCommitsTypeLabels)}`);
console.log(`Labels to add: ${formatLabels(labelsToAdd)}`);
console.log(`Labels to remove: ${formatLabels(labelsToRemove)}`);

// --------------------------------------------------------------------------------
// Return early if there are no labels to add or remove
// --------------------------------------------------------------------------------

if (labelsToAdd.length === 0 && labelsToRemove.length === 0) {
console.log('No labels to add or remove: skipped');
return 'skipped';
}

// --------------------------------------------------------------------------------
// Remove all `BREAKING CHANGE`, `type:` and `scope:` labels synchronously
// --------------------------------------------------------------------------------

for (const label of labelsToRemove) {
try {
await github.rest.issues.removeLabel({
owner,
repo,
issue_number,
name: label,
});
console.log(`Removed label: \`${label}\``);
} catch (error) {
if (error.status !== 404) { // If the label doesn't exist, ignore the error
throw error;
}
}
}

// --------------------------------------------------------------------------------
// Add `BREAKING CHANGE`, `type:` and `scope:` labels
// --------------------------------------------------------------------------------

// https://docs.github.com/en/rest/issues/labels?apiVersion=2022-11-28#add-labels-to-an-issue
if (labelsToAdd.length !== 0) {
await github.rest.issues.addLabels({
owner,
repo,
issue_number,
labels: labelsToAdd,
})

for (const label of labelsToAdd) {
await github.rest.issues.updateLabel({
owner,
repo,
name: label,
color: '000000',
description: 'Auto-generated label based on Conventional Commits specification for GitHub release notes',
});
}
}

console.log(`Added labels: ${formatLabels(labelsToAdd)}`);

# Delete the previous comment if it exists.
- if: ${{ steps.labeler.outputs.result != 'skipped' }}
uses: marocchino/sticky-pull-request-comment@v2
with:
header: labeler
delete: true

- if: ${{ steps.labeler.outputs.result != 'skipped' }}
uses: marocchino/sticky-pull-request-comment@v2
with:
header: labeler
message: |
Labels have been automatically applied based on the [Conventional Commits specification](https://www.conventionalcommits.org/en/v1.0.0/).🏷️

Check warning

Code scanning / CodeQL

Workflow does not contain permissions Medium

Actions Job or Workflow does not set permissions
Loading