Skip to content

Commit

Permalink
Move GITHUB_TOKEN env var to github-token input
Browse files Browse the repository at this point in the history
  • Loading branch information
crazy-max committed Aug 20, 2020
1 parent cf49640 commit 1ff1894
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 53 deletions.
9 changes: 3 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,27 @@ jobs:
name: Labeler (test)
uses: ./
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
yaml_file: .res/labels.update.yml
skip_delete: true
dry_run: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
-
name: Labeler (exclude part 1)
uses: ./
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
yaml_file: .res/labels.exclude1.yml
dry_run: true
exclude: |
* d*
*enhancement
*fix
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
-
name: Labeler (exclude part 2)
uses: ./
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
yaml_file: .res/labels.exclude2.yml
dry_run: true
exclude: |
*fix
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
13 changes: 2 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ ___
* [Workflow](#workflow)
* [Customizing](#customizing)
* [inputs](#inputs)
* [environment variables](#environment-variables)
* [Keep up-to-date with GitHub Dependabot](#keep-up-to-date-with-github-dependabot)
* [How can I help?](#how-can-i-help)
* [License](#license)
Expand Down Expand Up @@ -71,14 +70,13 @@ jobs:
if: success()
uses: crazy-max/ghaction-github-labeler@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
yaml_file: .github/labels.yml
skip_delete: false
dry_run: false
exclude: |
help*
*issue
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```

With this workflow, the YAML configuration above on a [fresh repository](.res/samples/original.yml), this will:
Expand All @@ -101,19 +99,12 @@ Following inputs can be used as `step.with` keys

| Name | Type | Description |
|-----------------|----------|------------------------------------------------------------------------------------|
| `github-token` | String | [GitHub Token](https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token) as provided by `secrets` |
| `yaml_file` | String | Path to YAML file containing labels definitions (default `.github/labels.yml`) |
| `skip_delete` | Bool | If enabled, labels will not be deleted if not found in YAML file (default `false`) |
| `dry_run` | Bool | If enabled, changes will not be applied (default `false`) |
| `exclude` | String | Newline-delimited list of labels pattern(s)/matcher to exclude. |

### environment variables

Following environment variables can be used as `step.env` keys

| Name | Description |
|----------------|--------------------------------------|
| `GITHUB_TOKEN` | [GITHUB_TOKEN](https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token) as provided by `secrets` |

## Keep up-to-date with GitHub Dependabot

Since [Dependabot](https://docs.github.com/en/github/administering-a-repository/keeping-your-actions-up-to-date-with-github-dependabot)
Expand Down
90 changes: 72 additions & 18 deletions dist/index.js

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

19 changes: 19 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import fs from 'fs';
import path from 'path';
import * as core from '@actions/core';

export interface Inputs {
githubToken: string;
yamlFile: fs.PathLike;
skipDelete: boolean;
dryRun: boolean;
}

export async function getInputs(): Promise<Inputs> {
return {
githubToken: core.getInput('github-token'),
yamlFile: path.join(core.getInput('yaml_file') || '.github/labels.yml'),
skipDelete: /true/i.test(core.getInput('skip_delete')),
dryRun: /true/i.test(core.getInput('dry_run'))
};
}
35 changes: 17 additions & 18 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as fs from 'fs';
import * as path from 'path';
import * as yaml from 'js-yaml';
import matcher from 'matcher';
import * as core from '@actions/core';
import * as github from '@actions/github';
import {getInputs, Inputs} from './context';

const octokit = github.getOctokit(process.env['GITHUB_TOKEN'] || '');
let octokit;
let liveLabels: Array<Label>;
let fileLabels: Array<Label>;
let exclusions: Array<string>;
Expand All @@ -31,17 +31,16 @@ enum LabelStatus {

async function run() {
try {
const yaml_file: fs.PathLike = path.join(core.getInput('yaml_file') || '.github/labels.yml');
const skip_delete: boolean = /true/i.test(core.getInput('skip_delete'));
const dry_run: boolean = /true/i.test(core.getInput('dry_run'));
let inputs: Inputs = await getInputs();
octokit = github.getOctokit(inputs.githubToken);

if (!fs.existsSync(yaml_file)) {
core.setFailed(`Cannot find YAML file ${yaml_file}`);
if (!fs.existsSync(inputs.yamlFile)) {
core.setFailed(`Cannot find YAML file ${inputs.yamlFile}`);
return;
}

liveLabels = await getLiveLabels();
fileLabels = await getFileLabels(yaml_file);
fileLabels = await getFileLabels(inputs.yamlFile);
await displayLiveLabels();

core.info(`🏃 Running GitHub Labeler`);
Expand All @@ -50,11 +49,11 @@ async function run() {
for (const actionLabel of actionLabels) {
switch (actionLabel.ghaction_status) {
case LabelStatus.Exclude: {
core.info(`${dry_run ? '[dryrun] ' : ''}${actionLabel.ghaction_log}`);
core.info(`${inputs.dryRun ? '[dryrun] ' : ''}${actionLabel.ghaction_log}`);
break;
}
case LabelStatus.Create: {
if (dry_run) {
if (inputs.dryRun) {
core.info(`[dryrun] ${actionLabel.ghaction_log}`);
break;
}
Expand All @@ -77,7 +76,7 @@ async function run() {
break;
}
case LabelStatus.Update: {
if (dry_run) {
if (inputs.dryRun) {
core.info(`[dryrun] ${actionLabel.ghaction_log}`);
break;
}
Expand All @@ -101,7 +100,7 @@ async function run() {
break;
}
case LabelStatus.Rename: {
if (dry_run) {
if (inputs.dryRun) {
core.info(`[dryrun] ${actionLabel.ghaction_log}`);
break;
}
Expand All @@ -125,11 +124,11 @@ async function run() {
break;
}
case LabelStatus.Delete: {
if (skip_delete) {
core.info(`${dry_run ? '[dryrun] ' : ''}⛔️ Skipping delete for '${actionLabel.name}' (skip_delete on)`);
if (inputs.skipDelete) {
core.info(`${inputs.dryRun ? '[dryrun] ' : ''}⛔️ Skipping delete for '${actionLabel.name}' (inputs.skipDelete on)`);
break;
}
if (dry_run) {
if (inputs.dryRun) {
core.info(`[dryrun] ${actionLabel.ghaction_log}`);
break;
}
Expand All @@ -147,16 +146,16 @@ async function run() {
break;
}
case LabelStatus.Skip: {
core.info(`${dry_run ? '[dryrun] ' : ''}${actionLabel.ghaction_log}`);
core.info(`${inputs.dryRun ? '[dryrun] ' : ''}${actionLabel.ghaction_log}`);
break;
}
case LabelStatus.Error: {
core.error(`${dry_run ? '[dryrun] ' : ''}${actionLabel.ghaction_log}`);
core.error(`${inputs.dryRun ? '[dryrun] ' : ''}${actionLabel.ghaction_log}`);
hasError = true;
break;
}
default: {
core.error(`${dry_run ? '[dryrun] ' : ''}🚫 '${actionLabel.name}' not processed`);
core.error(`${inputs.dryRun ? '[dryrun] ' : ''}🚫 '${actionLabel.name}' not processed`);
hasError = true;
break;
}
Expand Down

0 comments on commit 1ff1894

Please sign in to comment.