From 542f33226cf7c23d40ec682ba8aa7205aa633be9 Mon Sep 17 00:00:00 2001 From: "atomist[bot]" <22779605+atomist[bot]@users.noreply.github.com> Date: Fri, 17 Jul 2020 14:49:29 +0000 Subject: [PATCH] ESLint fixes (#14) * ESLint fixes [atomist:generated] [atomist-skill:atomist/eslint-skill] Pull request auto merged: * 1 approved review by @cdupuis * 2 successful checks --- lib/comment.ts | 106 ++--- lib/configuration.ts | 2 +- .../convergePullRequestAutoRebaseLabels.ts | 58 +-- lib/events/rebaseOnPullRequestComment.ts | 204 +++++---- lib/events/rebaseOnPush.ts | 391 ++++++++++-------- lib/util.ts | 62 +-- skill.ts | 84 ++-- 7 files changed, 509 insertions(+), 398 deletions(-) diff --git a/lib/comment.ts b/lib/comment.ts index 6cb7ee8..d8fe60d 100644 --- a/lib/comment.ts +++ b/lib/comment.ts @@ -18,63 +18,77 @@ import { EventContext, github, repository, secret } from "@atomist/skill"; import { PullRequest } from "./typings/types"; export interface GitHubCommentDetails { - apiUrl: string; - owner: string; - repo: string; - number: number; - id: number; + apiUrl: string; + owner: string; + repo: string; + number: number; + id: number; } export type PullRequestCommentCreator = ( - ctx: EventContext, - pr: PullRequest, - credential: secret.GitHubAppCredential | secret.GitHubCredential, - body: string, + ctx: EventContext, + pr: PullRequest, + credential: secret.GitHubAppCredential | secret.GitHubCredential, + body: string, ) => Promise; export type PullRequestCommentUpdater = ( - ctx: EventContext, - comment: T, - credential: secret.GitHubAppCredential | secret.GitHubCredential, - body: string, + ctx: EventContext, + comment: T, + credential: secret.GitHubAppCredential | secret.GitHubCredential, + body: string, ) => Promise; export const gitHubPullRequestCommentCreator: PullRequestCommentCreator = async ( - ctx, - pr, - credential, - body, + ctx, + pr, + credential, + body, ) => { - const result = ( - await github - .api(repository.gitHub({ owner: pr.repo.owner, repo: pr.repo.name, credential })) - .issues.createComment({ - owner: pr.repo.owner, - repo: pr.repo.name, - issue_number: pr.number, - body, - }) - ).data; - await ctx.audit.log(body); - return { - apiUrl: pr.repo.org.provider.apiUrl, - owner: pr.repo.owner, - repo: pr.repo.name, - number: pr.number, - id: result.id, - }; + const result = ( + await github + .api( + repository.gitHub({ + owner: pr.repo.owner, + repo: pr.repo.name, + credential, + }), + ) + .issues.createComment({ + owner: pr.repo.owner, + repo: pr.repo.name, + issue_number: pr.number, + body, + }) + ).data; + await ctx.audit.log(body); + return { + apiUrl: pr.repo.org.provider.apiUrl, + owner: pr.repo.owner, + repo: pr.repo.name, + number: pr.number, + id: result.id, + }; }; export const gitHubPullRequestCommentUpdater: PullRequestCommentUpdater = async ( - ctx, - comment, - credential, - body, + ctx, + comment, + credential, + body, ) => { - await github.api(repository.gitHub({ owner: comment.owner, repo: comment.repo, credential })).issues.updateComment({ - owner: comment.owner, - repo: comment.repo, - comment_id: comment.id, - body, - }); - await ctx.audit.log(body); + await github + .api( + repository.gitHub({ + owner: comment.owner, + repo: comment.repo, + credential, + }), + ) + .issues.updateComment({ + owner: comment.owner, + repo: comment.repo, + comment_id: comment.id, + body, + }); + await ctx.audit.log(body); }; diff --git a/lib/configuration.ts b/lib/configuration.ts index a8b30f4..387813f 100644 --- a/lib/configuration.ts +++ b/lib/configuration.ts @@ -15,5 +15,5 @@ */ export interface RebaseConfiguration { - strategy?: "ours" | "theirs"; + strategy?: "ours" | "theirs"; } diff --git a/lib/events/convergePullRequestAutoRebaseLabels.ts b/lib/events/convergePullRequestAutoRebaseLabels.ts index bb226e6..f617650 100644 --- a/lib/events/convergePullRequestAutoRebaseLabels.ts +++ b/lib/events/convergePullRequestAutoRebaseLabels.ts @@ -15,35 +15,47 @@ */ import { EventHandler, secret, repository, github } from "@atomist/skill"; -import { ConvergePullRequestAutoRebaseLabelsSubscription, PullRequestAction } from "../typings/types"; +import { + ConvergePullRequestAutoRebaseLabelsSubscription, + PullRequestAction, +} from "../typings/types"; export const AutoRebaseOnPushLabel = "auto-rebase:on-push"; export const handler: EventHandler = async ctx => { - const pr = ctx.data.PullRequest[0]; + 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...`, - ); + if (pr.action !== PullRequestAction.Opened) { + await ctx.audit.log( + `Pull request ${pr.repo.owner}/${pr.repo.name}#${pr.number} action not opened. Ignoring...`, + ); - return { - visibility: "hidden", - code: 0, - reason: `Pull request [${pr.repo.owner}/${pr.repo.name}#${pr.number}](${pr.url}) action not opened. Ignoring...`, - }; - } + return { + visibility: "hidden", + code: 0, + reason: `Pull request [${pr.repo.owner}/${pr.repo.name}#${pr.number}](${pr.url}) action not opened. Ignoring...`, + }; + } - const repo = ctx.data.PullRequest[0].repo; - const { owner, name } = repo; - const credential = await ctx.credential.resolve(secret.gitHubAppToken({ owner, repo: name })); + const repo = ctx.data.PullRequest[0].repo; + const { owner, name } = repo; + const credential = await ctx.credential.resolve( + secret.gitHubAppToken({ owner, repo: name }), + ); - const id = repository.gitHub({ owner, repo: name, credential }); - await ctx.audit.log(`Converging auto-rebase label '${AutoRebaseOnPushLabel}'`); - await github.convergeLabel(id, AutoRebaseOnPushLabel, "0E8A16", "Auto-rebase pull request branch"); - await ctx.audit.log(`Converged auto-rebase label 'AutoRebaseOnPushLabel'`); - return { - code: 0, - reason: `Converged auto-rebase label for [${repo.owner}/${repo.name}](${repo.url})`, - }; + const id = repository.gitHub({ owner, repo: name, credential }); + await ctx.audit.log( + `Converging auto-rebase label '${AutoRebaseOnPushLabel}'`, + ); + await github.convergeLabel( + id, + AutoRebaseOnPushLabel, + "0E8A16", + "Auto-rebase pull request branch", + ); + await ctx.audit.log(`Converged auto-rebase label 'AutoRebaseOnPushLabel'`); + return { + code: 0, + reason: `Converged auto-rebase label for [${repo.owner}/${repo.name}](${repo.url})`, + }; }; diff --git a/lib/events/rebaseOnPullRequestComment.ts b/lib/events/rebaseOnPullRequestComment.ts index 2f9ef89..8964db0 100644 --- a/lib/events/rebaseOnPullRequestComment.ts +++ b/lib/events/rebaseOnPullRequestComment.ts @@ -14,107 +14,143 @@ * limitations under the License. */ -import { EventHandler, git, secret, repository, log, github } from "@atomist/skill"; +import { + EventHandler, + git, + secret, + repository, + log, + github, +} from "@atomist/skill"; import { codeLine } from "@atomist/slack-messages"; -import { gitHubPullRequestCommentCreator, gitHubPullRequestCommentUpdater } from "../comment"; +import { + gitHubPullRequestCommentCreator, + gitHubPullRequestCommentUpdater, +} from "../comment"; import { RebaseConfiguration } from "../configuration"; import { RebaseOnPullRequestCommentSubscription } from "../typings/types"; -export const handler: EventHandler = async ctx => { - const pr = ctx.data.Comment[0].pullRequest; - const repo = pr.repo; +export const handler: EventHandler< + RebaseOnPullRequestCommentSubscription, + RebaseConfiguration +> = async ctx => { + const pr = ctx.data.Comment[0].pullRequest; + const repo = pr.repo; - const credential = await ctx.credential.resolve( - secret.gitHubAppToken({ owner: repo.owner, repo: repo.name, apiUrl: repo.org.provider.apiUrl }), - ); + const credential = await ctx.credential.resolve( + secret.gitHubAppToken({ + owner: repo.owner, + repo: repo.name, + apiUrl: repo.org.provider.apiUrl, + }), + ); - const comment = await gitHubPullRequestCommentCreator( - ctx, - pr, - credential, - `Pull request rebase is in progress + const comment = await gitHubPullRequestCommentCreator( + ctx, + pr, + credential, + `Pull request rebase is in progress ${github.formatMarkers(ctx)}`, - ); + ); - const project = await ctx.project.clone( - repository.gitHub({ - owner: repo.owner, - repo: repo.name, - credential, - branch: pr.branchName, - }), - { alwaysDeep: true, detachHead: false }, - ); + const project = await ctx.project.clone( + repository.gitHub({ + owner: repo.owner, + repo: repo.name, + credential, + branch: pr.branchName, + }), + { alwaysDeep: true, detachHead: false }, + ); - try { - await git.checkout(project, pr.branchName); - } catch (e) { - log.warn("Failed to checkout PR branch: %s", e.message); - await gitHubPullRequestCommentUpdater( - ctx, - comment, - credential, - `Pull request rebase failed because branch **${pr.branchName}** couldn't be checked out + try { + await git.checkout(project, pr.branchName); + } catch (e) { + log.warn("Failed to checkout PR branch: %s", e.message); + await gitHubPullRequestCommentUpdater( + ctx, + comment, + credential, + `Pull request rebase failed because branch **${ + pr.branchName + }** couldn't be checked out ${github.formatMarkers(ctx)}`, - ); - return { - code: 0, - reason: `Pull request [${pr.repo.owner}/${pr.repo.name}#${pr.number}](${pr.url}) rebase failed because branch ${pr.branchName} couldn't be checked out`, - }; - } - try { - const args = []; - if (ctx.configuration[0]?.parameters?.strategy) { - args.push("-X", ctx.configuration[0].parameters.strategy); - } - await project.exec("git", ["rebase", ...args, `origin/${pr.baseBranchName}`]); - } catch (e) { - log.warn("Failed to rebase PR branch: %s", e.message); + ); + return { + code: 0, + reason: `Pull request [${pr.repo.owner}/${pr.repo.name}#${pr.number}](${pr.url}) rebase failed because branch ${pr.branchName} couldn't be checked out`, + }; + } + try { + const args = []; + if (ctx.configuration[0]?.parameters?.strategy) { + args.push("-X", ctx.configuration[0].parameters.strategy); + } + await project.exec("git", [ + "rebase", + ...args, + `origin/${pr.baseBranchName}`, + ]); + } catch (e) { + log.warn("Failed to rebase PR branch: %s", e.message); - const result = await project.exec("git", ["diff", "--name-only", "--diff-filter=U"]); - const conflicts = result.stdout.trim().split("\n"); + const result = await project.exec("git", [ + "diff", + "--name-only", + "--diff-filter=U", + ]); + const conflicts = result.stdout.trim().split("\n"); - await gitHubPullRequestCommentUpdater( - ctx, - comment, - credential, - `Pull request rebase failed because of following conflicting ${conflicts.length === 1 ? "file" : "files"}: + await gitHubPullRequestCommentUpdater( + ctx, + comment, + credential, + `Pull request rebase failed because of following conflicting ${ + conflicts.length === 1 ? "file" : "files" + }: ${conflicts.map(c => `- ${codeLine(c)}`).join("\n")} ${github.formatMarkers(ctx)}`, - ); - return { - code: 0, - reason: `Pull request [${pr.repo.owner}/${pr.repo.name}#${pr.number}](${pr.url}) rebase failed because of conflicts`, - }; - } + ); + return { + code: 0, + reason: `Pull request [${pr.repo.owner}/${pr.repo.name}#${pr.number}](${pr.url}) rebase failed because of conflicts`, + }; + } - try { - await project.exec("git", ["push", "origin", pr.branchName, "--force-with-lease"]); - } catch (e) { - log.warn("Failed to force push PR branch: %s", e.message); + try { + await project.exec("git", [ + "push", + "origin", + pr.branchName, + "--force-with-lease", + ]); + } catch (e) { + log.warn("Failed to force push PR branch: %s", e.message); - await gitHubPullRequestCommentUpdater( - ctx, - comment, - credential, - `Pull request rebase failed because force push to **${pr.branchName}** errored + await gitHubPullRequestCommentUpdater( + ctx, + comment, + credential, + `Pull request rebase failed because force push to **${ + pr.branchName + }** errored ${github.formatMarkers(ctx)}`, - ); - return { - code: 0, - reason: `Pull request [${pr.repo.owner}/${pr.repo.name}#${pr.number}](${pr.url}) rebase failed because force push errored`, - }; - } + ); + return { + code: 0, + reason: `Pull request [${pr.repo.owner}/${pr.repo.name}#${pr.number}](${pr.url}) rebase failed because force push errored`, + }; + } - await gitHubPullRequestCommentUpdater( - ctx, - comment, - credential, - `Pull request was successfully rebased + await gitHubPullRequestCommentUpdater( + ctx, + comment, + credential, + `Pull request was successfully rebased ${github.formatMarkers(ctx)}`, - ); - return { - code: 0, - reason: `Pull request [${pr.repo.owner}/${pr.repo.name}#${pr.number}](${pr.url}) was successfully rebased`, - }; + ); + return { + code: 0, + reason: `Pull request [${pr.repo.owner}/${pr.repo.name}#${pr.number}](${pr.url}) was successfully rebased`, + }; }; diff --git a/lib/events/rebaseOnPush.ts b/lib/events/rebaseOnPush.ts index 7b4a3bc..2e9adf2 100644 --- a/lib/events/rebaseOnPush.ts +++ b/lib/events/rebaseOnPush.ts @@ -14,201 +14,242 @@ * limitations under the License. */ -import { EventHandler, git, github, log, repository, secret } from "@atomist/skill"; +import { + EventHandler, + git, + github, + log, + repository, + secret, +} from "@atomist/skill"; import { codeLine } from "@atomist/slack-messages"; import * as _ from "lodash"; -import { gitHubPullRequestCommentCreator, gitHubPullRequestCommentUpdater } from "../comment"; +import { + gitHubPullRequestCommentCreator, + gitHubPullRequestCommentUpdater, +} from "../comment"; import { RebaseConfiguration } from "../configuration"; import { AutoRebaseOnPushLabel } from "./convergePullRequestAutoRebaseLabels"; import { - PullRequestByRepoAndBranchQuery, - PullRequestByRepoAndBranchQueryVariables, - RebaseOnPushSubscription, + PullRequestByRepoAndBranchQuery, + PullRequestByRepoAndBranchQueryVariables, + RebaseOnPushSubscription, } from "../typings/types"; import { truncateCommitMessage } from "../util"; -export const handler: EventHandler = async ctx => { - const push = ctx.data.Push[0]; - - // Check if there is an open PR against the branch this push is on - const prs = await ctx.graphql.query( - "pullRequestByRepoAndBranch.graphql", - { - owner: push.repo.owner, - repo: push.repo.name, - branch: push.branch, - }, - ); - - const results = []; - - if (!!prs?.PullRequest && prs.PullRequest.length > 0) { - const commits = push.commits - .map(c => `- ${c.sha.slice(0, 7)} _${truncateCommitMessage(c.message)}_`) - .join("\n"); - - for (const pr of prs.PullRequest) { - if (!isRebaseRequested(pr, push)) { - continue; - } - - const { repo } = pr; - const credential = await ctx.credential.resolve( - secret.gitHubAppToken({ owner: repo.owner, repo: repo.name, apiUrl: repo.org.provider.apiUrl }), - ); - - const comment = await gitHubPullRequestCommentCreator( - ctx, - pr as any, - credential, - `Pull request rebase is in progress because @${push.after.author.login} pushed ${push.commits.length} ${ - push.commits.length === 1 ? "commit" : "commits" - } to **${push.branch}**: +export const handler: EventHandler< + RebaseOnPushSubscription, + RebaseConfiguration +> = async ctx => { + const push = ctx.data.Push[0]; + + // Check if there is an open PR against the branch this push is on + const prs = await ctx.graphql.query< + PullRequestByRepoAndBranchQuery, + PullRequestByRepoAndBranchQueryVariables + >("pullRequestByRepoAndBranch.graphql", { + owner: push.repo.owner, + repo: push.repo.name, + branch: push.branch, + }); + + const results = []; + + if (!!prs?.PullRequest && prs.PullRequest.length > 0) { + const commits = push.commits + .map(c => `- ${c.sha.slice(0, 7)} _${truncateCommitMessage(c.message)}_`) + .join("\n"); + + for (const pr of prs.PullRequest) { + if (!isRebaseRequested(pr, push)) { + continue; + } + + const { repo } = pr; + const credential = await ctx.credential.resolve( + secret.gitHubAppToken({ + owner: repo.owner, + repo: repo.name, + apiUrl: repo.org.provider.apiUrl, + }), + ); + + const comment = await gitHubPullRequestCommentCreator( + ctx, + pr as any, + credential, + `Pull request rebase is in progress because @${ + push.after.author.login + } pushed ${push.commits.length} ${ + push.commits.length === 1 ? "commit" : "commits" + } to **${push.branch}**: ${commits} ${github.formatMarkers(ctx)}`, - ); - - const project = await ctx.project.clone( - repository.gitHub({ - owner: repo.owner, - repo: repo.name, - credential, - branch: pr.branchName, - }), - { alwaysDeep: true, detachHead: false }, - ); - - try { - await git.checkout(project, pr.branchName); - } catch (e) { - log.warn("Failed to checkout PR branch: %s", e.message); - await gitHubPullRequestCommentUpdater( - ctx, - comment, - credential, - `Pull request rebase failed because branch **${pr.branchName}** couldn't be checked out + ); + + const project = await ctx.project.clone( + repository.gitHub({ + owner: repo.owner, + repo: repo.name, + credential, + branch: pr.branchName, + }), + { alwaysDeep: true, detachHead: false }, + ); + + try { + await git.checkout(project, pr.branchName); + } catch (e) { + log.warn("Failed to checkout PR branch: %s", e.message); + await gitHubPullRequestCommentUpdater( + ctx, + comment, + credential, + `Pull request rebase failed because branch **${ + pr.branchName + }** couldn't be checked out ${github.formatMarkers(ctx)}`, - ); - results.push({ - code: 0, - reason: `Pull request [${pr.repo.owner}/${pr.repo.name}#${pr.number}](${pr.url}) rebase failed because branch ${pr.branchName} couldn't be checked out`, - }); - continue; - } - try { - const args = []; - if (ctx.configuration[0]?.parameters?.strategy) { - args.push("-X", ctx.configuration[0].parameters.strategy); - } - await project.exec("git", ["rebase", ...args, `origin/${pr.baseBranchName}`]); - } catch (e) { - log.warn("Failed to rebase PR branch: %s", e.message); - - const result = await project.exec("git", ["diff", "--name-only", "--diff-filter=U"]); - const conflicts = result.stdout.trim().split("\n"); - - await gitHubPullRequestCommentUpdater( - ctx, - comment, - credential, - `Pull request rebase to ${push.after.sha.slice(0, 7)} by @${ - push.after.author.login - } failed because of following conflicting ${conflicts.length === 1 ? "file" : "files"}: + ); + results.push({ + code: 0, + reason: `Pull request [${pr.repo.owner}/${pr.repo.name}#${pr.number}](${pr.url}) rebase failed because branch ${pr.branchName} couldn't be checked out`, + }); + continue; + } + try { + const args = []; + if (ctx.configuration[0]?.parameters?.strategy) { + args.push("-X", ctx.configuration[0].parameters.strategy); + } + await project.exec("git", [ + "rebase", + ...args, + `origin/${pr.baseBranchName}`, + ]); + } catch (e) { + log.warn("Failed to rebase PR branch: %s", e.message); + + const result = await project.exec("git", [ + "diff", + "--name-only", + "--diff-filter=U", + ]); + const conflicts = result.stdout.trim().split("\n"); + + await gitHubPullRequestCommentUpdater( + ctx, + comment, + credential, + `Pull request rebase to ${push.after.sha.slice(0, 7)} by @${ + push.after.author.login + } failed because of following conflicting ${ + conflicts.length === 1 ? "file" : "files" + }: ${conflicts.map(c => `- ${codeLine(c)}`).join("\n")} ${github.formatMarkers(ctx)}`, - ); - results.push({ - code: 0, - reason: `Pull request [${pr.repo.owner}/${pr.repo.name}#${pr.number}](${pr.url}) rebase failed because of conflicts`, - }); - continue; - } - - try { - await project.exec("git", ["push", "origin", pr.branchName, "--force-with-lease"]); - } catch (e) { - log.warn("Failed to force push PR branch: %s", e.message); - - await gitHubPullRequestCommentUpdater( - ctx, - comment, - credential, - `Pull request rebase failed because force push to **${pr.branchName}** errored + ); + results.push({ + code: 0, + reason: `Pull request [${pr.repo.owner}/${pr.repo.name}#${pr.number}](${pr.url}) rebase failed because of conflicts`, + }); + continue; + } + + try { + await project.exec("git", [ + "push", + "origin", + pr.branchName, + "--force-with-lease", + ]); + } catch (e) { + log.warn("Failed to force push PR branch: %s", e.message); + + await gitHubPullRequestCommentUpdater( + ctx, + comment, + credential, + `Pull request rebase failed because force push to **${ + pr.branchName + }** errored ${github.formatMarkers(ctx)}`, - ); - results.push({ - code: 0, - reason: `Pull request [${pr.repo.owner}/${pr.repo.name}#${pr.number}](${pr.url}) rebase failed because force push errored`, - }); - continue; - } - - await gitHubPullRequestCommentUpdater( - ctx, - comment, - credential, - `Pull request was successfully rebased onto ${push.after.sha.slice(0, 7)} by @${push.after.author.login} + ); + results.push({ + code: 0, + reason: `Pull request [${pr.repo.owner}/${pr.repo.name}#${pr.number}](${pr.url}) rebase failed because force push errored`, + }); + continue; + } + + await gitHubPullRequestCommentUpdater( + ctx, + comment, + credential, + `Pull request was successfully rebased onto ${push.after.sha.slice( + 0, + 7, + )} by @${push.after.author.login} ${github.formatMarkers(ctx)}`, - ); - results.push({ - code: 0, - reason: `Pull request [${pr.repo.owner}/${pr.repo.name}#${pr.number}](${ - pr.url - }) was successfully rebased onto [${push.after.sha.slice(0, 7)}](${push.after.url}) by @${ - push.after.author.login - }`, - }); - } - } - - if (results.length > 0) { - return { - code: _.max(results.map(r => r.code)), - reason: results.map(r => r.reason).join("\n"), - }; - } else { - return { - visibility: "hidden", - code: 0, - reason: `No open pull request that needs rebasing against branch ${push.branch}`, - }; - } + ); + results.push({ + code: 0, + reason: `Pull request [${pr.repo.owner}/${pr.repo.name}#${pr.number}](${ + pr.url + }) was successfully rebased onto [${push.after.sha.slice(0, 7)}](${ + push.after.url + }) by @${push.after.author.login}`, + }); + } + } + + if (results.length > 0) { + return { + code: _.max(results.map(r => r.code)), + reason: results.map(r => r.reason).join("\n"), + }; + } else { + return { + visibility: "hidden", + code: 0, + reason: `No open pull request that needs rebasing against branch ${push.branch}`, + }; + } }; function isRebaseRequested( - pr: PullRequestByRepoAndBranchQuery["PullRequest"][0], - push: RebaseOnPushSubscription["Push"][0], - label: string = AutoRebaseOnPushLabel, - tag = `[${AutoRebaseOnPushLabel}]`, + pr: PullRequestByRepoAndBranchQuery["PullRequest"][0], + push: RebaseOnPushSubscription["Push"][0], + label: string = AutoRebaseOnPushLabel, + tag = `[${AutoRebaseOnPushLabel}]`, ): boolean { - // 0. check labels - if (pr?.labels?.some(l => l.name === label)) { - return true; - } - - // 1. check body and title for auto rebase marker - if (isTagged(pr?.title, tag) || isTagged(pr?.body, tag)) { - return true; - } - - // 2. PR comment that contains the rebase marker - if (pr?.comments?.some(c => isTagged(c.body, tag))) { - return true; - } - - // 3. Commit message containing the auto rebase marker - if (pr?.commits?.some(c => isTagged(c.message, tag))) { - return true; - } - - // 4. Commit push message containing the auto rebase marker - if (push?.commits?.some(c => isTagged(c.message, tag))) { - return true; - } - - return false; + // 0. check labels + if (pr?.labels?.some(l => l.name === label)) { + return true; + } + + // 1. check body and title for auto rebase marker + if (isTagged(pr?.title, tag) || isTagged(pr?.body, tag)) { + return true; + } + + // 2. PR comment that contains the rebase marker + if (pr?.comments?.some(c => isTagged(c.body, tag))) { + return true; + } + + // 3. Commit message containing the auto rebase marker + if (pr?.commits?.some(c => isTagged(c.message, tag))) { + return true; + } + + // 4. Commit push message containing the auto rebase marker + if (push?.commits?.some(c => isTagged(c.message, tag))) { + return true; + } + + return false; } function isTagged(msg: string, tag: string): boolean { - return msg && msg.indexOf(tag) >= 0; + return msg && msg.indexOf(tag) >= 0; } diff --git a/lib/util.ts b/lib/util.ts index b1f79ec..663ad57 100644 --- a/lib/util.ts +++ b/lib/util.ts @@ -17,37 +17,37 @@ import { escape } from "@atomist/slack-messages"; export function truncateCommitMessage(message: string): string { - const title = (message || "").split("\n")[0]; - const escapedTitle = escape(title); + const title = (message || "").split("\n")[0]; + const escapedTitle = escape(title); - if (escapedTitle.length <= 50) { - return escapedTitle; - } + if (escapedTitle.length <= 50) { + return escapedTitle; + } - const splitRegExp = /(&(?:[gl]t|amp);|<.*?\||>)/; - const titleParts = escapedTitle.split(splitRegExp); - let truncatedTitle = ""; - let addNext = 1; - let i; - for (i = 0; i < titleParts.length; i++) { - let newTitle = truncatedTitle; - if (i % 2 === 0) { - newTitle += titleParts[i]; - } else if (/^&(?:[gl]t|amp);$/.test(titleParts[i])) { - newTitle += "&"; - } else if (/^<.*\|$/.test(titleParts[i])) { - addNext = 2; - continue; - } else if (titleParts[i] === ">") { - addNext = 1; - continue; - } - if (newTitle.length > 50) { - const l = 50 - newTitle.length; - titleParts[i] = titleParts[i].slice(0, l) + "..."; - break; - } - truncatedTitle = newTitle; - } - return titleParts.slice(0, i + addNext).join(""); + const splitRegExp = /(&(?:[gl]t|amp);|<.*?\||>)/; + const titleParts = escapedTitle.split(splitRegExp); + let truncatedTitle = ""; + let addNext = 1; + let i; + for (i = 0; i < titleParts.length; i++) { + let newTitle = truncatedTitle; + if (i % 2 === 0) { + newTitle += titleParts[i]; + } else if (/^&(?:[gl]t|amp);$/.test(titleParts[i])) { + newTitle += "&"; + } else if (/^<.*\|$/.test(titleParts[i])) { + addNext = 2; + continue; + } else if (titleParts[i] === ">") { + addNext = 1; + continue; + } + if (newTitle.length > 50) { + const l = 50 - newTitle.length; + titleParts[i] = titleParts[i].slice(0, l) + "..."; + break; + } + truncatedTitle = newTitle; + } + return titleParts.slice(0, i + addNext).join(""); } diff --git a/skill.ts b/skill.ts index 99c829e..5a3f58e 100644 --- a/skill.ts +++ b/skill.ts @@ -14,49 +14,57 @@ * limitations under the License. */ -import { Category, parameter, ParameterType, resourceProvider, skill } from "@atomist/skill"; +import { + Category, + parameter, + ParameterType, + resourceProvider, + skill, +} from "@atomist/skill"; import { RebaseConfiguration } from "./lib/configuration"; export const Skill = skill({ - name: "github-auto-rebase-skill", - namespace: "atomist", - displayName: "Auto-Rebase Pull Requests", - author: "Atomist", - categories: [Category.CodeReview, Category.DevEx], - homepageUrl: "https://github.com/atomist-skills/github-auto-rebase-skill", - repositoryUrl: "https://github.com/atomist-skills/github-auto-rebase-skill.git", - iconUrl: "file://docs/images/icon.svg", - license: "Apache-2.0", + name: "github-auto-rebase-skill", + namespace: "atomist", + displayName: "Auto-Rebase Pull Requests", + author: "Atomist", + categories: [Category.CodeReview, Category.DevEx], + homepageUrl: "https://github.com/atomist-skills/github-auto-rebase-skill", + repositoryUrl: + "https://github.com/atomist-skills/github-auto-rebase-skill.git", + iconUrl: "file://docs/images/icon.svg", + license: "Apache-2.0", - runtime: { - memory: 1024, - timeout: 540, - }, + runtime: { + memory: 1024, + timeout: 540, + }, - resourceProviders: { - github: resourceProvider.gitHub({ minRequired: 1 }), - slack: resourceProvider.chat({ minRequired: 0 }), - }, + resourceProviders: { + github: resourceProvider.gitHub({ minRequired: 1 }), + slack: resourceProvider.chat({ minRequired: 0 }), + }, - parameters: { - strategy: { - type: ParameterType.SingleChoice, - displayName: "Default merge strategy", - description: "Select which merge strategy to use when rebasing pull request branches", - options: [ - { - text: "Ours", - value: "ours", - }, - { - text: "Theirs", - value: "theirs", - }, - ], - required: false, - }, - repos: parameter.repoFilter({ required: false }), - }, + parameters: { + strategy: { + type: ParameterType.SingleChoice, + displayName: "Default merge strategy", + description: + "Select which merge strategy to use when rebasing pull request branches", + options: [ + { + text: "Ours", + value: "ours", + }, + { + text: "Theirs", + value: "theirs", + }, + ], + required: false, + }, + repos: parameter.repoFilter({ required: false }), + }, - subscriptions: ["file://graphql/subscription/*.graphql"], + subscriptions: ["file://graphql/subscription/*.graphql"], });