Skip to content

Commit

Permalink
refactor: update file formats (#24)
Browse files Browse the repository at this point in the history
* refactor: update format

* refactor: decrease tab width to 2

* chore: prettier config for yml

* refactor: format
  • Loading branch information
shioyang authored Jan 6, 2023
1 parent 97f03ff commit 0ead670
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 44 deletions.
30 changes: 15 additions & 15 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
on:
push:
branches:
- master
push:
branches:
- master

jobs:
check_pr_labels_job:
runs-on: ubuntu-latest
name: A job to check the PR labels contain given labels
steps:
- name: Check PR labels action step
id: check_pr_labels
uses: shioyang/[email protected]
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
labels: '["enhancement"]'
- name: See result
run: echo "${{ steps.check_pr_labels.outputs.result }}"
check_pr_labels_job:
runs-on: ubuntu-latest
name: A job to check the PR labels contain given labels
steps:
- name: Check PR labels action step
id: check_pr_labels
uses: shioyang/[email protected]
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
labels: '["enhancement"]'
- name: See result
run: echo "${{ steps.check_pr_labels.outputs.result }}"
10 changes: 10 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"overrides": [
{
"files": "*.yml",
"options": {
"singleQuote": true
}
}
]
}
6 changes: 3 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: 'Check PR Labels on Push'
description: 'This action check if given labels have be applied to the PR when pushed.'
inputs:
github-token: # id of input
github-token: # id of input
description: 'The repository token, i.e. secrets.GITHUB_TOKEN'
required: true
labels: # id of input
description: "The array of label name, e.g. '[\"label-1\", \"label-2\"]'"
labels: # id of input
description: 'The array of label name, e.g. ''["label-1", "label-2"]'''
required: true
outputs:
result: # id of output
Expand Down
52 changes: 26 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,42 @@ import * as github from "@actions/github";
import { GitHub } from "@actions/github/lib/utils";

async function run() {
const token = core.getInput("github-token", { required: true });
const octokit = github.getOctokit(token);
const token = core.getInput("github-token", { required: true });
const octokit = github.getOctokit(token);

const labelNames = await getPullRequestLabelNames(octokit);
const labelNames = await getPullRequestLabelNames(octokit);

const labels = getInputLabels();
const result = labels.every(
(label) => labelNames.findIndex((value) => label === value) >= 0
);
core.setOutput("result", result);
const labels = getInputLabels();
const result = labels.every(
(label) => labelNames.findIndex((value) => label === value) >= 0
);
core.setOutput("result", result);
}

async function getPullRequestLabelNames(
octokit: InstanceType<typeof GitHub>
octokit: InstanceType<typeof GitHub>
): Promise<string[]> {
const owner = github.context.repo.owner;
const repo = github.context.repo.repo;
const commit_sha = github.context.sha;

const response =
await octokit.rest.repos.listPullRequestsAssociatedWithCommit({
owner,
repo,
commit_sha,
});

const pr = response.data.length > 0 && response.data[0];
return pr ? pr.labels.map((label) => label.name || "") : [];
const owner = github.context.repo.owner;
const repo = github.context.repo.repo;
const commit_sha = github.context.sha;

const response =
await octokit.rest.repos.listPullRequestsAssociatedWithCommit({
owner,
repo,
commit_sha,
});

const pr = response.data.length > 0 && response.data[0];
return pr ? pr.labels.map((label) => label.name || "") : [];
}

function getInputLabels(): string[] {
const raw = core.getInput("labels", { required: true });
const json = JSON.parse(raw);
return Array.isArray(json) ? json : [];
const raw = core.getInput("labels", { required: true });
const json = JSON.parse(raw);
return Array.isArray(json) ? json : [];
}

run().catch((err) => {
core.setFailed(err.message);
core.setFailed(err.message);
});

0 comments on commit 0ead670

Please sign in to comment.