Skip to content

Commit

Permalink
feat: update conventional label and now won't delete and add same lab…
Browse files Browse the repository at this point in the history
…el twice
  • Loading branch information
sirily11 committed Feb 15, 2022
1 parent e5d12cf commit ce62dc2
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 58 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Lint PR
on:
pull_request:
branches: [ master ]

jobs:
label:
runs-on: ubuntu-latest
name: A job to say hello
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'yarn'
- run: yarn
- run: yarn build
- name: label
uses: ./
with:
access_token: ${{ secrets.GITHUB_TOKEN }}
- name: Get the output
run: echo "The labels were ${{ steps.label.outputs.labels }}"
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: 'Conventional Labeler'
description: 'A Pull request labeler using conventional commit standard'
inputs:
access_token:
description: 'Github Access Token to access and modify your pr's label'
description: Github Access Token to access and modify your pr's label
required: true
outputs:
labels:
Expand Down
98 changes: 65 additions & 33 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,25 @@ exports.ConventionalCommit = ConventionalCommit;

"use strict";

var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
Expand All @@ -111,12 +130,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.GithubClient = void 0;
const github_1 = __importDefault(__nccwpck_require__(5438));
const github = __importStar(__nccwpck_require__(5438));
/**
* Github client is a wrapper for the Github API.
*/
Expand All @@ -126,11 +142,11 @@ class GithubClient {
}
addLabel(pr, labels) {
return __awaiter(this, void 0, void 0, function* () {
const client = github_1.default.getOctokit(this.token);
const client = github.getOctokit(this.token);
try {
yield client.rest.issues.addLabels({
owner: github_1.default.context.repo.owner,
repo: github_1.default.context.repo.repo,
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: pr,
labels: labels,
});
Expand All @@ -149,11 +165,11 @@ class GithubClient {
*/
removeLabels(pr, labels) {
return __awaiter(this, void 0, void 0, function* () {
const client = github_1.default.getOctokit(this.token);
const client = github.getOctokit(this.token);
try {
yield Promise.all(labels.map((label) => client.rest.issues.removeLabel({
owner: github_1.default.context.repo.owner,
repo: github_1.default.context.repo.repo,
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: pr,
name: label,
})));
Expand All @@ -171,10 +187,10 @@ class GithubClient {
getLabels(pr) {
return __awaiter(this, void 0, void 0, function* () {
try {
const client = github_1.default.getOctokit(this.token);
const client = github.getOctokit(this.token);
const labels = yield client.rest.issues.listLabelsOnIssue({
owner: github_1.default.context.repo.owner,
repo: github_1.default.context.repo.repo,
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: pr,
});
return {
Expand All @@ -193,7 +209,7 @@ class GithubClient {
*/
getTitle() {
// use github api to get pr's title
const pullRequest = github_1.default.context.payload.pull_request;
const pullRequest = github.context.payload.pull_request;
return pullRequest === null || pullRequest === void 0 ? void 0 : pullRequest.title;
}
/**
Expand All @@ -202,7 +218,7 @@ class GithubClient {
*/
getPr() {
var _a;
return (_a = github_1.default.context.payload.pull_request) === null || _a === void 0 ? void 0 : _a.number;
return (_a = github.context.payload.pull_request) === null || _a === void 0 ? void 0 : _a.number;
}
}
exports.GithubClient = GithubClient;
Expand Down Expand Up @@ -239,6 +255,25 @@ const labeler_1 = __nccwpck_require__(8389);

"use strict";

var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
Expand All @@ -248,17 +283,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.ConventionalLabeler = void 0;
const conventional_commit_1 = __nccwpck_require__(6382);
const github_1 = __nccwpck_require__(3412);
const core_1 = __importDefault(__nccwpck_require__(2186));
const core = __importStar(__nccwpck_require__(2186));
class ConventionalLabeler {
constructor() {
const token = core_1.default.getInput("access_token", { required: true });
const token = core.getInput("access_token", { required: true });
this.githubClient = new github_1.GithubClient(token);
this.conventionalCommit = new conventional_commit_1.ConventionalCommit();
}
Expand All @@ -269,50 +301,50 @@ class ConventionalLabeler {
var _a;
return __awaiter(this, void 0, void 0, function* () {
// get pr's number
core_1.default.info("Getting PR number");
core.info("Getting PR number");
const pr = this.githubClient.getPr();
if (!pr) {
core_1.default.error("No pull request found");
core.error("No pull request found");
return;
}
// get pr's existing labels
core_1.default.info("Getting PR labels");
core.info("Getting PR labels");
const labels = yield this.githubClient.getLabels(pr);
if (labels.error) {
core_1.default.error(labels.error);
core.error(labels.error);
return;
}
// get list of preset labels
core_1.default.info("Getting preset labels");
core.info("Getting preset labels");
const presetLabels = this.conventionalCommit.getValidLabels((_a = labels.labels) !== null && _a !== void 0 ? _a : []);
// remove them
core_1.default.info("Removing preset labels");
core.info("Removing preset labels");
const removeError = yield this.githubClient.removeLabels(pr, presetLabels);
if (removeError) {
core_1.default.error(removeError);
core.error(removeError);
return;
}
// get the pr title
const title = this.githubClient.getTitle();
if (!title || title.length === 0) {
core_1.default.error("Failed to get the pr title");
core.error("Failed to get the pr title");
return;
}
// get the label
core_1.default.info("Getting conventional label");
core.info("Getting conventional label");
const label = this.conventionalCommit.getLabel(title);
if (label.error) {
core_1.default.error(label.error);
core.error(label.error);
return;
}
// add the label
core_1.default.info("Adding label to PR");
core.info("Adding label to PR");
const error = yield this.githubClient.addLabel(pr, [label.label]);
if (error) {
core_1.default.error(error);
core.error(error);
return;
}
core_1.default.setOutput("labels", labels.labels);
core.setOutput("labels", labels.labels);
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ Conventional labeler will label your PR based on your PR's feature if it follows

## Required input

- access_token: can be set by using ${{ secrets.GITHUB_TOKEN }}
- access_token: can be set by using `${{ secrets.GITHUB_TOKEN }}`
13 changes: 13 additions & 0 deletions src/client/conventional_commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,17 @@ export class ConventionalCommit {
}
return validLabels;
}

/**
* Given two labels array labels1 and labels2, return the labels that are not in labels2
*/
getDiffLabels(labels: string[], labels2: string[]): string[] {
const diffLabels = [];
for (const label of labels) {
if (!labels2.includes(label)) {
diffLabels.push(label);
}
}
return diffLabels;
}
}
2 changes: 1 addition & 1 deletion src/client/github.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import github from "@actions/github";
import * as github from "@actions/github";

/**
* Github client is a wrapper for the Github API.
Expand Down
51 changes: 30 additions & 21 deletions src/labeler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ConventionalCommit } from "./client/conventional_commit";
import { GithubClient } from "./client/github";
import core from "@actions/core";
import * as core from "@actions/core";

export class ConventionalLabeler {
private githubClient: GithubClient;
Expand Down Expand Up @@ -32,19 +32,6 @@ export class ConventionalLabeler {
return;
}

// get list of preset labels
core.info("Getting preset labels");
const presetLabels = this.conventionalCommit.getValidLabels(
labels.labels ?? []
);
// remove them
core.info("Removing preset labels");
const removeError = await this.githubClient.removeLabels(pr, presetLabels);
if (removeError) {
core.error(removeError);
return;
}

// get the pr title
const title = this.githubClient.getTitle();
if (!title || title.length === 0) {
Expand All @@ -53,21 +40,43 @@ export class ConventionalLabeler {
}

// get the label
core.info("Getting conventional label");
const label = this.conventionalCommit.getLabel(title);
if (label.error) {
core.error(label.error);
core.info(`Getting conventional label`);
const generatedLabel = this.conventionalCommit.getLabel(title);
if (generatedLabel.error) {
core.error(generatedLabel.error);
return;
}

// get list of preset labels
core.info("Getting preset labels");
const presetLabels = this.conventionalCommit.getValidLabels(
labels.labels ?? []
);

const differentLabels = this.conventionalCommit.getDiffLabels(
presetLabels,
[generatedLabel.label!]
);

// remove them
core.info("Removing different label");
const removeError = await this.githubClient.removeLabels(
pr,
differentLabels
);
if (removeError) {
core.error(removeError);
return;
}

// add the label
core.info("Adding label to PR");
const error = await this.githubClient.addLabel(pr, [label.label!]);
core.info(`Adding label ${generatedLabel.label} to PR`);
const error = await this.githubClient.addLabel(pr, [generatedLabel.label!]);
if (error) {
core.error(error);
return;
}

core.setOutput("labels", labels.labels);
core.setOutput("labels", generatedLabel.label);
}
}
32 changes: 32 additions & 0 deletions src/tests/covnentioanl_commit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,36 @@ describe("Given a conventional commit client", () => {
const labels = client.getValidLabels([]);
expect(labels.length).toBe(0);
});

it("Should return different labels", () => {
const diff = client.getDiffLabels(
["enhancement", "bugfix"],
["enhancement", "bugfix"]
);
expect(diff.length).toBe(0);
});

it("Should return different labels", () => {
const diff = client.getDiffLabels(
["enhancement", "bug"],
["enhancement", "bugfix"]
);
expect(diff).toEqual(["bug"]);
});

it("Should return different labels", () => {
const diff = client.getDiffLabels(
["enhancement"],
["enhancement", "bugfix"]
);
expect(diff.length).toEqual(0);
});

it("Should return different labels", () => {
const diff = client.getDiffLabels(
["enhancement", "bug", "bugfix"],
["enhancement", "bugfix"]
);
expect(diff).toEqual(["bug"]);
});
});
Loading

0 comments on commit ce62dc2

Please sign in to comment.