Skip to content

Commit

Permalink
feat: add policy output
Browse files Browse the repository at this point in the history
> Policy that was used to set labels on issue. Value is a stringified JSON object.
  • Loading branch information
jamacku committed Apr 18, 2024
1 parent e8b9bbe commit e4a460c
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 9 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,33 @@ List of labels that were set based on the provided policy. The output is a strin
> element: ${{ fromJSON(steps.<step-id>.outputs.labels)[0] }}
> ```

### policy

The representation of the policy that was used to set labels. The output is a stringified JSON object.

Example output in JSON format where:

* `template` - the name of the template that was used to parse the issue form
* `section` - key-value pairs where the key is the ID of the issue-form section and the value is an array of the applied labels for the given section

```json
{
"template": "bug.yml",
"section": {
"severity": [ "low" ],
"priority": [ "high" ]
}
}
```

> [!TIP]
>
> Use [`fromJSON`](https://docs.github.com/en/actions/learn-github-actions/expressions#fromjson) function to parse the output to get an object of policy.
>
> ```yml
> severity: ${{ fromJSON(steps.<step-id>.outputs.policy).section.severity }}
> ```

## Policy

It's possible to define a labeling policy to further customize the labeling process. The policy can be defined using `.github/advanced-issue-labeler.yml` configuration file. The structure needs to be as follows:
Expand Down
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ inputs:
outputs:
labels:
description: Labels that were set on issue. Value is a stringified array.
policy:
description: Policy that was used to set labels on issue. Value is a stringified JSON object.

runs:
using: node20
Expand Down
4 changes: 3 additions & 1 deletion dist/action.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/action.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions dist/labeler.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion dist/labeler.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/labeler.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions dist/schema/output.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions dist/schema/output.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/schema/output.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ async function action(octokit: CustomOctokit) {

const labels = labeler.gatherLabels();

setOutput('labels', JSON.stringify(labels ?? []));
setOutput('policy', JSON.stringify(labeler.outputPolicy));

// Check if there are some labels to be set
if (!labels || (Array.isArray(labels) && labels?.length < 1)) {
info('Nothing to do here. CY@');
return;
}

info(`Labels to be set: ${labels}`);
info(`Used policy: ${JSON.stringify(labeler.outputPolicy, null, 2)}`);

const response = await octokit.request(
'POST /repos/{owner}/{repo}/issues/{issue_number}/labels',
Expand All @@ -41,8 +45,6 @@ async function action(octokit: CustomOctokit) {
}
);

setOutput('labels', JSON.stringify(labels));

debug(`GitHub API response status: [${response.status}]`);
}

Expand Down
15 changes: 15 additions & 0 deletions src/labeler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Config } from './config';
import { IssueForm } from './issue-form';

import { BlockList, Section, Template } from './schema/input';
import { OutputPolicy } from './schema/output';

export class Labeler {
section: Section | undefined = undefined;
Expand All @@ -13,6 +14,11 @@ export class Labeler {
isConfig: boolean;
isInputs: boolean;

outputPolicy: OutputPolicy = {
template: '',
section: {},
};

constructor(
public issueForm: IssueForm,
public config: Config
Expand All @@ -31,6 +37,7 @@ export class Labeler {
}
// Config requires template as well
this.template = getInput('template');
this.outputPolicy.template = this.template;
}

gatherLabels() {
Expand Down Expand Up @@ -59,6 +66,8 @@ export class Labeler {
return;
}

this.outputPolicy.section[this.section] = keywords;

return keywords;
}

Expand Down Expand Up @@ -105,6 +114,12 @@ export class Labeler {
)
) {
labels.push(rule.name);

if (!this.outputPolicy.section.hasOwnProperty(singleID)) {
this.outputPolicy.section[singleID] = [];
}

this.outputPolicy.section[singleID]?.push(rule.name);
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/schema/output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { z } from 'zod';

const outputPolicySchema = z.object({
template: z.string(),
section: z.record(z.array(z.string())),
});

export type OutputPolicy = z.infer<typeof outputPolicySchema>;

0 comments on commit e4a460c

Please sign in to comment.