Skip to content

Commit

Permalink
Enhance github pr search based on sha (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
anbraten authored Nov 5, 2024
1 parent 97516de commit bacef55
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
dist/
.env
16 changes: 9 additions & 7 deletions src/forges/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,24 @@ export class GithubForge extends Forge {
repo: string;
commitHash: string;
}): Promise<PullRequest | undefined> {
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),
};
}

Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 4 additions & 2 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ export const defaultUserConfig: UserConfig = {
commentOnReleasedPullRequests: true,
};

export async function getConfig(): Promise<Config> {
export async function getConfig(basePath?: string): Promise<Config> {
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)
Expand Down

0 comments on commit bacef55

Please sign in to comment.