Skip to content

Commit

Permalink
$ Major Enhancements. (#19)
Browse files Browse the repository at this point in the history
Co-authored-by: Vedansh <[email protected]>
  • Loading branch information
TheHamsterBot and offensive-vk authored Dec 19, 2024
1 parent 359b617 commit a69eb66
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 37 deletions.
10 changes: 5 additions & 5 deletions .github/issues.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ issue:
feat:
- feature
- feat
- oi
- 'oi'

base:
- base
- root
- this
- testing
- 'base'
- 'root'
- 'this'
- 'testing'
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:
if: github.event_name == 'issues'
uses: ./
with:
debug: false
create-labels: true
github-token: ${{ secrets.MY_TOKEN }}
issue-config: '.github/issues.yml'
Expand All @@ -48,6 +49,7 @@ jobs:
if: github.event_name == 'pull_request'
uses: ./
with:
debug: true
create-labels: true
github-token: ${{ secrets.MY_TOKEN }}
pr-config: '.github/pr.yml'
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ WORKDIR /app
COPY package*.json ./

# Install dependencies using pnpm with only lockfile generation
RUN pnpm install
RUN pnpm i

# Copy the remaining project files into the container
COPY . .
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Configure inputs through the `with:` section of the Action. Below is the list of
| `issue-config` | `.github/issues.yml` | Path to the YAML configuration file for labeling issues. |
| `pr-config` | `.github/pr.yml` | Path to the YAML configuration file for labeling pull requests. |
| `create-labels`| `true` | Whether to create missing labels in the repository. |
| `debug` | `false` | Whether to enable debug mode or not. |

---

Expand Down Expand Up @@ -97,6 +98,7 @@ area/build:
---

## How It Works

1. The Action triggers based on GitHub events (`issues` or `pull_request`).
2. Matches keywords (for issues) or file patterns (for pull requests) against the configuration file.
3. Applies labels to issues or pull requests that match the criteria.
Expand Down
10 changes: 7 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ branding:
icon: mail
color: gray-dark
inputs:
debug:
required: false
description: "Whether to enable Debug Mode or not."
default: false
github-token:
description: "Your GitHub token (PAT or `github.token`). Required to authenticate API requests."
required: false
create-labels:
description: "Set to true if the labels does not exist in the repository. If set to false, the action will not create the labels."
required: false
default: 'true'
default: true
issue-config:
description: "Path to file where each label maps to an array of matching strings. File type: .yml, .yaml, .json."
required: false
Expand All @@ -20,7 +24,7 @@ inputs:
description: "Path to file for labeling pull request based on file changes. Like labeler.yml but more simpler."
required: false
default: '.github/pr.yml'

runs:
using: 'node20'
main: 'dist/index.js'
main: 'dist/index.js'
42 changes: 21 additions & 21 deletions dist/index.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/index.js.map

Large diffs are not rendered by default.

24 changes: 20 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ function parseConfigFile(filePath: string): Array<LabelConfig> {
throw new Error(`Parsed data from ${filePath} is not an object or is empty.`);
}
}
async function ensureLabelsExist(
octokit: any,
owner: string,
repo: string,
labels: Array<{ label: string; description?: string }>
) {
const tasks = labels.map(({ label, description }) =>
ensureLabelExists(octokit, owner, repo, label, description)
);
await Promise.all(tasks);
}

async function ensureLabelExists(octokit: any, owner: string, repo: string, label: string, description?: string) {
try {
Expand All @@ -69,7 +80,7 @@ async function ensureLabelExists(octokit: any, owner: string, repo: string, labe
});
core.info(`Label "${label}" created successfully.`);
} else {
throw error;
core.warning(error);
}
}
}
Expand Down Expand Up @@ -97,7 +108,8 @@ function getMatchedLabels<T extends LabelConfig>(content: Array<string>, labels:
try {
const token = core.getInput('github-token');
const octokit = github.getOctokit(token);

const debugMode = core.getInput('debug') || false;

const { owner: contextOwner, repo: contextRepo } = github.context.repo;
const owner = core.getInput('owner') || contextOwner;
const repo = core.getInput('repo') || contextRepo;
Expand All @@ -108,7 +120,11 @@ function getMatchedLabels<T extends LabelConfig>(content: Array<string>, labels:
const eventType = context.eventName;
const labelsToApply: string[] = [];
let targetNumber;


if (debugMode) {
core.debug('Debug mode enabled.');
}

if (eventType === 'pull_request' && context.payload.pull_request) {
const prNumber = context.payload.pull_request.number;
targetNumber = prNumber;
Expand All @@ -125,7 +141,7 @@ function getMatchedLabels<T extends LabelConfig>(content: Array<string>, labels:
if (matchedLabels) {
for (const { label, description } of matchedLabels) {
labelsToApply.push(label);
await ensureLabelExists(octokit, owner, repo, label, description);
await ensureLabelsExist(octokit, owner, repo, [{label: label, description: description}]);
}
} else {
core.warning('No labels matched the file changes in this pull request.');
Expand Down

0 comments on commit a69eb66

Please sign in to comment.