Skip to content
This repository has been archived by the owner on May 4, 2022. It is now read-only.

Commit

Permalink
Converge label descriptions
Browse files Browse the repository at this point in the history
[changelog:added]
  • Loading branch information
cdupuis committed Jun 17, 2020
1 parent 2224cec commit 1708cc8
Show file tree
Hide file tree
Showing 12 changed files with 829 additions and 617 deletions.
25 changes: 11 additions & 14 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
],
"globals": {
"Atomics": "readonly",
Expand All @@ -22,22 +23,18 @@
"@typescript-eslint"
],
"rules": {
"comma-dangle": [
"error",
"always-multiline"
],
"comma-spacing": "off",
"@typescript-eslint/comma-spacing": [
"error"
],
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-floating-promises": [
"error"
],
"semi": "off",
"@typescript-eslint/semi": [
"error"
],
"@typescript-eslint/no-use-before-define": "off"
"prettier/prettier": [
"error",
{
"arrowParens": "avoid",
"trailingComma": "all",
"tabWidth": 4,
"printWidth": 120
}
]
}
}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ node_modules/
/log/
git-info.json
/lib/typings/types.ts
/lib/typings/types.ts
/.atomist/
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
# `atomist/github-branch-deletion-skill`

<!---atomist-skill-description:start--->

Automatically delete pull request branches when a pull request is closed.

<!---atomist-skill-description:end--->

---

<!---atomist-skill-long_description:start--->

Automatically delete pull request branches when a pull request is closed.
No more dangling branches cluttering up your repositories, and no need to manually delete branches.
Use this skill to automatically delete branches when pull requests are closed or closed merged.

<!---atomist-skill-long_description:end--->

---

<!---atomist-skill-readme:start--->

# What it's useful for
Expand Down
80 changes: 0 additions & 80 deletions atomist.yaml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
subscription ConvergePullRequestBranchDeletionLabel{
subscription convergePullRequestBranchDeletionLabel{
PullRequest {
action
url
Expand Down
2 changes: 1 addition & 1 deletion graphql/subscription/deleteBranchOnPullRequest.graphql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
subscription DeleteBranchOnPullRequest {
subscription deleteBranchOnPullRequest {
PullRequest(actions: [closed]) {
url
merged
Expand Down
54 changes: 54 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright © 2020 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { gitHubResourceProvider, slackResourceProvider } from "@atomist/skill/lib/resource_providers";
import { ParameterType, repoFilter, skill } from "@atomist/skill/lib/skill";
import { DeleteBranchConfiguration } from "./lib/events/deleteBranchOnPullRequest";

export const Skill = skill<DeleteBranchConfiguration & { repos: any }>({
runtime: {
memory: 1024,
timeout: 540,
},

resourceProviders: {
github: gitHubResourceProvider({ minRequired: 1 }),
slack: slackResourceProvider({ minRequired: 0 }),
},

parameters: {
deleteOn: {
type: ParameterType.SingleChoice,
displayName: "Default auto-deletion policy",
description: "Branch deletion policy to apply when no explicit label is configured on a pull request",
options: [
{
text: "On pull request merge",
value: "on-merge",
},
{
text: "On pull request close or merge",
value: "on-close",
},
],
defaultValue: "on-merge",
required: false,
},
repos: repoFilter({ required: false }),
},

subscriptions: ["file://graphql/subscription/*.graphql"],
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@

import { EventHandler } from "@atomist/skill/lib/handler";
import { gitHubComRepository } from "@atomist/skill/lib/project";
import { gitHub } from "@atomist/skill/lib/project/github";
import { convergeLabel, gitHub } from "@atomist/skill/lib/project/github";
import { gitHubAppToken } from "@atomist/skill/lib/secrets";
import * as github from "@octokit/rest";
import { DeleteBranchConfiguration } from "./DeleteBranchOnPullRequest";
import {
ConvergePullRequestBranchDeletionLabelSubscription,
PullRequestAction,
} from "../typings/types";
import { DeleteBranchConfiguration } from "./deleteBranchOnPullRequest";
import { ConvergePullRequestBranchDeletionLabelSubscription, PullRequestAction } from "../typings/types";

export const handler: EventHandler<ConvergePullRequestBranchDeletionLabelSubscription, DeleteBranchConfiguration> = async ctx => {
export const handler: EventHandler<
ConvergePullRequestBranchDeletionLabelSubscription,
DeleteBranchConfiguration
> = async ctx => {
const pr = ctx.data.PullRequest[0];

if (pr.action !== PullRequestAction.Opened) {
await ctx.audit.log(`Pull request ${pr.repo.owner}/${pr.repo.name}#${pr.number} action not opened. Ignoring...`);
await ctx.audit.log(
`Pull request ${pr.repo.owner}/${pr.repo.name}#${pr.number} action not opened. Ignoring...`,
);

return {
visibility: "hidden",
Expand All @@ -44,51 +45,34 @@ export const handler: EventHandler<ConvergePullRequestBranchDeletionLabelSubscri

await ctx.audit.log(`Converging auto-branch deletion label`);

const api = gitHub(gitHubComRepository({ owner: repo.owner, repo: repo.name, credential }));
const id = gitHubComRepository({ owner: repo.owner, repo: repo.name, credential });

await addLabel("auto-branch-delete:on-close", "0F2630", owner, name, api);
await addLabel("auto-branch-delete:on-merge", "0F2630", owner, name, api);
await convergeLabel(id, "auto-branch-delete:on-close", "0F2630", "Delete branch when pull request gets closed");
await convergeLabel(id, "auto-branch-delete:on-merge", "0F2630", "Delete branch when pull request gets merged");

const labels = [];
if (!pr.labels.some(l => l.name.startsWith("auto-branch-delete:"))) {
labels.push(`auto-branch-delete:${ctx.configuration[0]?.parameters?.deleteOn || "on-merge"}`);
}

await ctx.audit.log(`Labelling pull request ${pr.repo.owner}/${pr.repo.name}#${pr.number} with configured auto-branch deletion method`);
await ctx.audit.log(
`Labelling pull request ${pr.repo.owner}/${pr.repo.name}#${pr.number} with configured auto-branch deletion method`,
);

// Add the default labels to the PR
await api.issues.addLabels({
issue_number: pr.number, // eslint-disable-line @typescript-eslint/camelcase
await gitHub(id).issues.addLabels({
issue_number: pr.number,
owner: repo.owner,
repo: repo.name,
labels,
});

await ctx.audit.log(`Pull request ${pr.repo.owner}/${pr.repo.name}#${pr.number} labelled with: ${labels.join(", ")}`);
await ctx.audit.log(
`Pull request ${pr.repo.owner}/${pr.repo.name}#${pr.number} labelled with: ${labels.join(", ")}`,
);

return {
code: 0,
reason: `Pull request [${pr.repo.owner}/${pr.repo.name}#${pr.number}](${pr.url}) labelled with auto-branch deletion label`,
};
};

async function addLabel(name: string,
color: string,
owner: string,
repo: string,
api: github.Octokit): Promise<void> {
try {
await api.issues.getLabel({
name,
repo,
owner,
});
} catch (err) {
await api.issues.createLabel({
owner,
repo,
name,
color,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export const handler: EventHandler<DeleteBranchOnPullRequestSubscription, Delete
const slug = `${owner}/${name}#${pr.number}`;
const link = `[${slug}](${pr.url})`;

await ctx.audit.log(`Starting auto-branch deletion for pull request ${slug} with labels: ${pr.labels.map(l => l.name).join(", ")}`);
await ctx.audit.log(
`Starting auto-branch deletion for pull request ${slug} with labels: ${pr.labels.map(l => l.name).join(", ")}`,
);

let deletePr = false;
if (!!pr.merged && pr.labels.some(l => l.name === "auto-branch-delete:on-merge")) {
Expand All @@ -44,7 +46,9 @@ export const handler: EventHandler<DeleteBranchOnPullRequestSubscription, Delete
}

if (deletePr) {
const credential = await ctx.credential.resolve(gitHubAppToken({ owner, repo: name, apiUrl: org.provider.apiUrl }));
const credential = await ctx.credential.resolve(
gitHubAppToken({ owner, repo: name, apiUrl: org.provider.apiUrl }),
);
if (credential) {
const api = gitHub(gitHubComRepository({ owner, repo: name, credential }));
try {
Expand Down
Loading

0 comments on commit 1708cc8

Please sign in to comment.