Skip to content

Commit 00b0dcd

Browse files
Merge pull request #2 from fern-api/branch-name
Chore: Stopped duplicate PRs for fern api update changes
2 parents b914188 + 9a622b2 commit 00b0dcd

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

dist/index.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36996,9 +36996,6 @@ async function run() {
3699636996
const autoMerge = core.getBooleanInput('auto_merge') || false;
3699736997
const addTimestamp = core.getBooleanInput('add_timestamp') || true;
3699836998
const updateFromSource = core.getBooleanInput('update_from_source') || false;
36999-
if (addTimestamp) {
37000-
branch = `${branch}-${new Date().toISOString().replace(/[:.]/g, '-')}`;
37001-
}
3700236999
if (!token) {
3700337000
throw new Error('GitHub token is required. Please provide a token with appropriate permissions.');
3700437001
}
@@ -37028,7 +37025,9 @@ async function updateFromSourceSpec(token, branch, autoMerge) {
3702837025
await exec.exec('git', ['config', 'user.name', 'github-actions']);
3702937026
await exec.exec('git', ['config', 'user.email', 'github-actions@github.com']);
3703037027
core.info(`Creating and checking out branch: ${branch}`);
37031-
await exec.exec('git', ['checkout', '-b', branch]);
37028+
const octokit = github.getOctokit(token);
37029+
const doesBranchExist = await branchExists(owner, repo, branch, octokit);
37030+
await setupBranch(branch, doesBranchExist);
3703237031
await runFernApiUpdate();
3703337032
const diff = await exec.getExecOutput('git', ['status', '--porcelain'], { silent: true });
3703437033
if (!diff.stdout.trim()) {
@@ -37038,7 +37037,7 @@ async function updateFromSourceSpec(token, branch, autoMerge) {
3703837037
await exec.exec('git', ['add', '.'], { silent: true });
3703937038
await exec.exec('git', ['commit', '-m', 'Update API specifications with fern api update'], { silent: true });
3704037039
core.info(`Pushing changes to branch: ${branch}`);
37041-
await exec.exec('git', ['push', '--verbose', 'origin', branch], { silent: false });
37040+
await exec.exec('git', ['push', '--force', '--verbose', 'origin', branch], { silent: false });
3704237041
if (!autoMerge) {
3704337042
const octokit = github.getOctokit(token);
3704437043
await createPR(octokit, owner, repo, branch, github.context.ref.replace('refs/heads/', ''), true);
@@ -37197,6 +37196,7 @@ async function branchExists(owner, repo, branchName, octokit) {
3719737196
async function setupBranch(branchName, exists) {
3719837197
try {
3719937198
if (exists) {
37199+
await exec.exec('git', ['fetch', 'origin']);
3720037200
core.info(`Branch ${branchName} exists. Checking it out.`);
3720137201
await exec.exec('git', ['checkout', branchName]);
3720237202
await exec.exec('git', ['pull', 'origin', branchName], { silent: true });
@@ -37314,9 +37314,10 @@ async function updatePR(octokit, owner, repo, prNumber) {
3731437314
// Create a new PR
3731537315
async function createPR(octokit, owner, repo, branchName, targetBranch, isFromFern) {
3731637316
core.info(`Creating new PR from ${branchName} to ${targetBranch}`);
37317+
const date = new Date().toISOString().replace(/[:.]/g, '-');
3731737318
let prTitle = isFromFern ?
37318-
'Update API specifications with fern api update' :
37319-
'Update OpenAPI specifications';
37319+
`chore: Update API specifications with fern api update (${date})` :
37320+
`chore: Update OpenAPI specifications (${date})`;
3732037321
let prBody = isFromFern ?
3732137322
'Update API specifications by running fern api update.' :
3732237323
'Update OpenAPI specifications based on changes in the source repository.';

src/sync.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ export async function run(): Promise<void> {
3131
const addTimestamp = core.getBooleanInput('add_timestamp') || true;
3232
const updateFromSource = core.getBooleanInput('update_from_source') || false;
3333

34-
if (addTimestamp) {
35-
branch = `${branch}-${new Date().toISOString().replace(/[:.]/g, '-')}`;
36-
}
37-
3834
if (!token) {
3935
throw new Error('GitHub token is required. Please provide a token with appropriate permissions.');
4036
}
@@ -66,7 +62,10 @@ async function updateFromSourceSpec(token: string, branch: string, autoMerge: bo
6662
await exec.exec('git', ['config', 'user.email', 'github-actions@github.com']);
6763

6864
core.info(`Creating and checking out branch: ${branch}`);
69-
await exec.exec('git', ['checkout', '-b', branch]);
65+
66+
const octokit = github.getOctokit(token);
67+
const doesBranchExist = await branchExists(owner, repo, branch, octokit);
68+
await setupBranch(branch, doesBranchExist);
7069

7170
await runFernApiUpdate();
7271

@@ -82,7 +81,7 @@ async function updateFromSourceSpec(token: string, branch: string, autoMerge: bo
8281

8382
core.info(`Pushing changes to branch: ${branch}`);
8483

85-
await exec.exec('git', ['push', '--verbose', 'origin', branch], { silent: false });
84+
await exec.exec('git', ['push', '--force', '--verbose', 'origin', branch], { silent: false });
8685

8786
if (!autoMerge) {
8887
const octokit = github.getOctokit(token);
@@ -259,6 +258,7 @@ async function branchExists(owner: string, repo: string, branchName: string, oct
259258
async function setupBranch(branchName: string, exists: boolean): Promise<void> {
260259
try {
261260
if (exists) {
261+
await exec.exec('git', ['fetch', 'origin']);
262262
core.info(`Branch ${branchName} exists. Checking it out.`);
263263
await exec.exec('git', ['checkout', branchName]);
264264
await exec.exec('git', ['pull', 'origin', branchName], { silent: true });
@@ -396,10 +396,12 @@ async function updatePR(octokit: any, owner: string, repo: string, prNumber: num
396396
// Create a new PR
397397
async function createPR(octokit: any, owner: string, repo: string, branchName: string, targetBranch: string, isFromFern: boolean): Promise<any> {
398398
core.info(`Creating new PR from ${branchName} to ${targetBranch}`);
399+
const date = new Date().toISOString().replace(/[:.]/g, '-');
400+
399401

400402
let prTitle = isFromFern ?
401-
'Update API specifications with fern api update' :
402-
'Update OpenAPI specifications';
403+
`chore: Update API specifications with fern api update (${date})` :
404+
`chore: Update OpenAPI specifications (${date})`;
403405

404406
let prBody = isFromFern ?
405407
'Update API specifications by running fern api update.' :

0 commit comments

Comments
 (0)