diff --git a/Aptfile b/Aptfile new file mode 100644 index 0000000..054b0ae --- /dev/null +++ b/Aptfile @@ -0,0 +1,2 @@ +# Heroku-24 stack doesn't include git by default. +git-all \ No newline at end of file diff --git a/app.json b/app.json index 7ca7f21..09a781f 100644 --- a/app.json +++ b/app.json @@ -11,15 +11,15 @@ "required": true } }, - "formation": { - }, - "addons": [ - - ], + "formation": {}, + "addons": [], "buildpacks": [ + { + "url": "heroku-community/apt" + }, { "url": "heroku/nodejs" } ], - "stack": "heroku-22" + "stack": "heroku-24" } diff --git a/src/constants.ts b/src/constants.ts index bfc0e6f..c4fd313 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -27,3 +27,6 @@ export const BACKPORT_REQUESTED_LABEL = export const DEFAULT_BACKPORT_REVIEW_TEAM = process.env.DEFAULT_BACKPORT_REVIEW_TEAM; + +export const VALID_BACKPORT_CHECK_NAME = + process.env.BACKPORT_REQUESTED_LABEL || 'Valid Backport'; diff --git a/src/index.ts b/src/index.ts index 12bdbb9..916ba2a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,7 +11,12 @@ import { labelExistsOnPR, removeLabel, } from './utils/label-utils'; -import { CHECK_PREFIX, NO_BACKPORT_LABEL, SKIP_CHECK_LABEL } from './constants'; +import { + CHECK_PREFIX, + NO_BACKPORT_LABEL, + SKIP_CHECK_LABEL, + VALID_BACKPORT_CHECK_NAME, +} from './constants'; import { getEnvVar } from './utils/env-util'; import { PRChange, PRStatus, BackportPurpose, CheckRunStatus } from './enums'; import { Label } from '@octokit/webhooks-types'; @@ -28,15 +33,11 @@ import { updateBackportValidityCheck, } from './utils/checks-util'; import { register } from './utils/prom'; -import { - SimpleWebHookRepoContext, - WebHookIssueContext, - WebHookPR, - WebHookPRContext, -} from './types'; - -// Built in fetch doesn't support global-agent... -// @ts-ignore +import { SimpleWebHookRepoContext, WebHookPR, WebHookPRContext } from './types'; + +import { execSync } from 'child_process'; + +// @ts-ignore - builtin fetch doesn't support global-agent. delete globalThis.fetch; const probotHandler: ApplicationFunction = async (robot, { getRouter }) => { @@ -145,6 +146,12 @@ const probotHandler: ApplicationFunction = async (robot, { getRouter }) => { } }; + const gitExists = execSync('which git').toString().trim(); + if (/git not found/.test(gitExists)) { + robot.log('Git not found - unable to proceed with backporting'); + process.exit(1); + } + /** * Checks that a PR done to `main` contains the required * backport information, i.e.: at least a `no-backport` or @@ -154,8 +161,6 @@ const probotHandler: ApplicationFunction = async (robot, { getRouter }) => { * @returns */ - const VALID_BACKPORT_CHECK_NAME = 'Valid Backport'; - robot.on( [ 'pull_request.opened', @@ -423,7 +428,7 @@ const probotHandler: ApplicationFunction = async (robot, { getRouter }) => { ); // Backport pull requests to labeled targets when PR is merged. - robot.on('pull_request.closed', async (context: WebHookPRContext) => { + robot.on('pull_request.closed', async (context) => { const { pull_request: pr } = context.payload; const oldPRNumbers = getPRNumbersFromPRBody(pr, true); @@ -471,7 +476,7 @@ const probotHandler: ApplicationFunction = async (robot, { getRouter }) => { const TROP_COMMAND_PREFIX = '/trop '; // Manually trigger backporting process on trigger comment phrase. - robot.on('issue_comment.created', async (context: WebHookIssueContext) => { + robot.on('issue_comment.created', async (context) => { const { issue, comment } = context.payload; const isPullRequest = (i: { number: number; html_url: string }) => diff --git a/src/utils.ts b/src/utils.ts index cf205e2..18e021b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,5 +1,6 @@ import fetch from 'node-fetch'; import * as fs from 'fs-extra'; +import { execSync } from 'child_process'; import Queue from 'queue'; import simpleGit from 'simple-git'; @@ -29,7 +30,7 @@ import { WebHookPR, WebHookRepoContext, } from './types'; -import { Context, Probot } from 'probot'; +import { Probot } from 'probot'; const { parse: parseDiff } = require('what-the-diff'); @@ -475,6 +476,17 @@ export const backportImpl = async ( } } + const gitExists = execSync('which git').toString().trim(); + if (/git not found/.test(gitExists)) { + await context.octokit.issues.createComment( + context.repo({ + body: `Git not found - unable to proceed with backporting to ${targetBranch}`, + issue_number: pr.number, + }), + ); + return; + } + const base = pr.base; const slug = `${base.repo.owner.login}/${base.repo.name}`; const bp = `backport from PR #${pr.number} to "${targetBranch}"`;