Skip to content

Commit

Permalink
Merge pull request #22 from banyan/fix-labelable-ids
Browse files Browse the repository at this point in the history
Fix labelIds handling
  • Loading branch information
banyan authored Feb 2, 2020
2 parents 86c0a05 + 8bd5473 commit 5d4fc35
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
14 changes: 8 additions & 6 deletions dist/entrypoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async function run() {
}, {});
util_1.logger.debug('allLabels', allLabels);
const currentLabelNames = new Set(result.repository.pullRequest.labels.edges.map((edge) => edge.node.name));
util_1.logger.debug('currentLabelNames', currentLabelNames);
util_1.logger.debug('currentLabelNames', Array.from(currentLabelNames));
const { headRefOid, baseRefOid } = result.repository.pullRequest;
const { stdout } = await exec(`git fetch && git merge-base --is-ancestor ${baseRefOid} ${headRefOid} && git diff --name-only ${baseRefOid} || git diff --name-only $(git merge-base ${baseRefOid} ${headRefOid})`);
const diffFiles = stdout.trim().split('\n');
Expand All @@ -82,11 +82,13 @@ async function run() {
});
return acc;
}, []));
util_1.logger.debug('newLabelNames', newLabelNames);
const ruledLabelNames = new Set(Object.keys(config.rules));
const labelNamesToAdd = new Set([...newLabelNames].filter(labelName => !currentLabelNames.has(labelName)));
const labelNamesToRemove = new Set([...currentLabelNames].filter((labelName) => !newLabelNames.has(labelName) && ruledLabelNames.has(labelName)));
util_1.logger.debug('labelNamesToAdd', labelNamesToAdd);
util_1.logger.debug('labelNamesToRemove', labelNamesToRemove);
util_1.logger.debug('ruledLabelNames', Array.from(ruledLabelNames));
util_1.logger.debug('labelNamesToAdd', Array.from(labelNamesToAdd));
util_1.logger.debug('labelNamesToRemove', Array.from(labelNamesToRemove));
const labelableId = result.repository.pullRequest.id;
util_1.logger.debug('labelableId', labelableId);
if (labelNamesToAdd.size > 0) {
Expand Down Expand Up @@ -132,7 +134,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core = tslib_1.__importStar(require("@actions/core"));
const lodash_pick_1 = tslib_1.__importDefault(require("lodash.pick"));
exports.getLabelIds = (allLabels, labelNames) => JSON.stringify(Object.values(lodash_pick_1.default(allLabels, labelNames)));
exports.getLabelIds = (allLabels, labelNames) => Object.values(lodash_pick_1.default(allLabels, labelNames));
exports.logger = {
debug: (message, object) => {
return core.debug(`${message}: ${JSON.stringify(object)}`);
Expand Down Expand Up @@ -190,7 +192,7 @@ exports.getPullRequestAndLabels = (graphqlWithAuth, { owner, repo, number, }) =>
};
exports.addLabelsToLabelable = (graphqlWithAuth, { labelIds, labelableId, }) => {
const query = `
mutation addLabelsToLabelable($labelIds: String!, $labelableId: String!) {
mutation addLabelsToLabelable($labelIds: [ID!]!, $labelableId: ID!) {
addLabelsToLabelable(input: {labelIds:$labelIds, labelableId:$labelableId}) {
clientMutationId
}
Expand All @@ -203,7 +205,7 @@ exports.addLabelsToLabelable = (graphqlWithAuth, { labelIds, labelableId, }) =>
};
exports.removeLabelsFromLabelable = (graphqlWithAuth, { labelIds, labelableId, }) => {
const query = `
mutation removeLabelsFromLabelable($labelIds: String!, $labelableId: String!) {
mutation removeLabelsFromLabelable($labelIds: [ID!]!, $labelableId: ID!) {
removeLabelsFromLabelable(input: {labelIds:$labelIds, labelableId:$labelableId}) {
clientMutationId
}
Expand Down
9 changes: 6 additions & 3 deletions src/entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async function run() {
),
);

logger.debug('currentLabelNames', currentLabelNames);
logger.debug('currentLabelNames', Array.from(currentLabelNames));

const { headRefOid, baseRefOid } = result.repository.pullRequest;

Expand All @@ -118,6 +118,8 @@ async function run() {
}, []),
);

logger.debug('newLabelNames', newLabelNames);

const ruledLabelNames = new Set(Object.keys(config.rules));

const labelNamesToAdd = new Set(
Expand All @@ -133,8 +135,9 @@ async function run() {
),
);

logger.debug('labelNamesToAdd', labelNamesToAdd);
logger.debug('labelNamesToRemove', labelNamesToRemove);
logger.debug('ruledLabelNames', Array.from(ruledLabelNames));
logger.debug('labelNamesToAdd', Array.from(labelNamesToAdd));
logger.debug('labelNamesToRemove', Array.from(labelNamesToRemove));

const labelableId = result.repository.pullRequest.id;

Expand Down
9 changes: 5 additions & 4 deletions src/query.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Label } from './interface';
import { graphql } from '@octokit/graphql';
import * as OctokitTypes from '@octokit/types';

Expand Down Expand Up @@ -66,12 +67,12 @@ export const addLabelsToLabelable = (
labelIds,
labelableId,
}: {
labelIds: string;
labelIds: (Label | undefined)[];
labelableId: string;
},
) => {
const query = `
mutation addLabelsToLabelable($labelIds: String!, $labelableId: String!) {
mutation addLabelsToLabelable($labelIds: [ID!]!, $labelableId: ID!) {
addLabelsToLabelable(input: {labelIds:$labelIds, labelableId:$labelableId}) {
clientMutationId
}
Expand All @@ -90,12 +91,12 @@ export const removeLabelsFromLabelable = (
labelIds,
labelableId,
}: {
labelIds: string;
labelIds: (Label | undefined)[];
labelableId: string;
},
) => {
const query = `
mutation removeLabelsFromLabelable($labelIds: String!, $labelableId: String!) {
mutation removeLabelsFromLabelable($labelIds: [ID!]!, $labelableId: ID!) {
removeLabelsFromLabelable(input: {labelIds:$labelIds, labelableId:$labelableId}) {
clientMutationId
}
Expand Down
6 changes: 2 additions & 4 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import * as core from '@actions/core';
import pick from 'lodash.pick';

import { Label, LabelName } from './interface';
export const getLabelIds = (
allLabels: Label[],
labelNames: LabelName[],
): string => JSON.stringify(Object.values(pick(allLabels, labelNames)));
export const getLabelIds = (allLabels: Label[], labelNames: LabelName[]) =>
Object.values(pick(allLabels, labelNames));

export const logger = {
debug: (message: string, object: {} | null | undefined) => {
Expand Down

0 comments on commit 5d4fc35

Please sign in to comment.