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

feat(config): support reading from local file if it exists #394

Merged
merged 9 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Various inputs are defined in [`action.yml`](action.yml) to let you configure th
| Name | Description | Default |
| - | - | - |
| `repo-token` | Token to use to authorize label changes. Typically the GITHUB_TOKEN secret, with `contents:read` and `pull-requests:write` access | `github.token` |
| `configuration-path` | The path to the label configuration file | `.github/labeler.yml` |
| `configuration-path` | The path to the label configuration file. If not found in the current working directory, will read from the source repository via the Github API. | `.github/labeler.yml` |
IvanZosimov marked this conversation as resolved.
Show resolved Hide resolved
IvanZosimov marked this conversation as resolved.
Show resolved Hide resolved
| `sync-labels` | Whether or not to remove labels when matching files are reverted or no longer changed by the PR | `false` |
| `dot` | Whether or not to auto-include paths starting with dot (e.g. `.github`) | `false` |

Expand Down
21 changes: 20 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ const core = __importStar(__nccwpck_require__(2186));
const github = __importStar(__nccwpck_require__(5438));
const yaml = __importStar(__nccwpck_require__(1917));
const minimatch_1 = __nccwpck_require__(2002);
const promises_1 = __nccwpck_require__(3292);
const warningPrefix = '[warning]';
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
Expand Down Expand Up @@ -115,7 +117,16 @@ function getChangedFiles(client, prNumber) {
}
function getLabelGlobs(client, configurationPath) {
return __awaiter(this, void 0, void 0, function* () {
const configurationContent = yield fetchContent(client, configurationPath);
let configurationContent;
try {
configurationContent = (yield (0, promises_1.readFile)(configurationPath, {
encoding: 'utf8'
})).toString();
}
catch (error) {
core.info(`${warningPrefix} configuration file (path: ${configurationPath}) not found locally (${error}), fetching via the api`);
configurationContent = yield fetchContent(client, configurationPath);
}
// loads (hopefully) a `{[label:string]: string | StringOrMatchConfig[]}`, but is `any`:
const configObject = yaml.load(configurationContent);
// transform `any` => `Map<string,StringOrMatchConfig[]>` or throw if yaml is malformed:
Expand Down Expand Up @@ -14179,6 +14190,14 @@ module.exports = require("fs");

/***/ }),

/***/ 3292:
/***/ ((module) => {

"use strict";
module.exports = require("fs/promises");

/***/ }),

/***/ 3685:
/***/ ((module) => {

Expand Down
22 changes: 18 additions & 4 deletions src/labeler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as core from '@actions/core';
import * as github from '@actions/github';
import * as yaml from 'js-yaml';
import {Minimatch} from 'minimatch';
import {readFile} from 'fs/promises';

interface MatchConfig {
all?: string[];
Expand All @@ -11,6 +12,8 @@ interface MatchConfig {
type StringOrMatchConfig = string | MatchConfig;
type ClientType = ReturnType<typeof github.getOctokit>;

const warningPrefix = '[warning]';

export async function run() {
try {
const token = core.getInput('repo-token');
Expand Down Expand Up @@ -97,10 +100,21 @@ async function getLabelGlobs(
client: ClientType,
configurationPath: string
): Promise<Map<string, StringOrMatchConfig[]>> {
const configurationContent: string = await fetchContent(
client,
configurationPath
);
let configurationContent: string;

try {
configurationContent = (
await readFile(configurationPath, {
lrstanley marked this conversation as resolved.
Show resolved Hide resolved
encoding: 'utf8'
})
).toString();
} catch (error) {
core.info(
`${warningPrefix} configuration file (path: ${configurationPath}) not found locally (${error}), fetching via the api`
IvanZosimov marked this conversation as resolved.
Show resolved Hide resolved
IvanZosimov marked this conversation as resolved.
Show resolved Hide resolved
);

configurationContent = await fetchContent(client, configurationPath);
}

// loads (hopefully) a `{[label:string]: string | StringOrMatchConfig[]}`, but is `any`:
const configObject: any = yaml.load(configurationContent);
Expand Down