From bacef551512d68f8af44c18872908e05e7c96cd0 Mon Sep 17 00:00:00 2001 From: Anbraten <6918444+anbraten@users.noreply.github.com> Date: Tue, 5 Nov 2024 14:29:52 +0100 Subject: [PATCH] Enhance github pr search based on sha (#225) --- .gitignore | 1 + src/forges/github.ts | 16 +++++++++------- src/index.ts | 7 ++++++- src/run.ts | 6 ++++-- src/utils/config.ts | 4 ++-- 5 files changed, 22 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index b947077..deed335 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ dist/ +.env diff --git a/src/forges/github.ts b/src/forges/github.ts index 4ca8986..26c31ee 100644 --- a/src/forges/github.ts +++ b/src/forges/github.ts @@ -91,22 +91,24 @@ export class GithubForge extends Forge { repo: string; commitHash: string; }): Promise { - const pr = await this.octokit.repos.listPullRequestsAssociatedWithCommit({ + const response = await this.octokit.repos.listPullRequestsAssociatedWithCommit({ owner: options.owner, repo: options.repo, commit_sha: options.commitHash, }); - if (pr.data.length === 0) { + const prs = response.data.filter((pr) => pr.merge_commit_sha === options.commitHash && pr.state === 'closed'); + + if (prs.length === 0) { return undefined; } return { - number: pr.data[0].number, - title: pr.data[0].title, - description: pr.data[0].body || '', - author: pr.data[0].user?.login || '', - labels: pr.data[0].labels.map((label) => label.name), + number: prs[0].number, + title: prs[0].title, + description: prs[0].body || '', + author: prs[0].user?.login || '', + labels: prs[0].labels.map((label) => label.name), }; } diff --git a/src/index.ts b/src/index.ts index 497b4b4..3e5612b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -51,7 +51,12 @@ export async function run({ git, forge, config }: { git: SimpleGit; forge: Forge const { releaseBranch } = config.ci; - await git.fetch(['--unshallow', '--tags']); + try { + await git.fetch(['--unshallow', '--tags']); + } catch (error) { + console.error(c.yellow('# Error doing unshallow fetch'), error); + await git.fetch(['--tags']); + } await git.checkout(releaseBranch); await git.branch(['--set-upstream-to', `origin/${releaseBranch}`]); await git.pull(); diff --git a/src/run.ts b/src/run.ts index 17a225c..a3aefa1 100644 --- a/src/run.ts +++ b/src/run.ts @@ -5,10 +5,12 @@ import { getForge } from './forges'; import simpleGit from 'simple-git'; async function main() { + const basePath = process.env.BASE; // Can be used for testing + try { - const config = await getConfig(); + const config = await getConfig(basePath); const forge = await getForge(config); - const git = simpleGit(); + const git = simpleGit(basePath); await run({ git, forge, config }); } catch (_error) { diff --git a/src/utils/config.ts b/src/utils/config.ts index da48b2f..85d0208 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -75,10 +75,10 @@ export const defaultUserConfig: UserConfig = { commentOnReleasedPullRequests: true, }; -export async function getConfig(): Promise { +export async function getConfig(basePath?: string): Promise { const userConfig: UserConfig = {}; - const configFilePath = ciConfig.configFile || path.join(process.cwd(), 'release-config.ts'); + const configFilePath = ciConfig.configFile || path.resolve(basePath ?? process.cwd(), 'release-config.ts'); if ( await fs .stat(configFilePath)