Skip to content

Commit e4b9e8c

Browse files
committed
debug: add detailed logging for branch creation/update API responses
1 parent b8f91e1 commit e4b9e8c

File tree

2 files changed

+63
-20
lines changed

2 files changed

+63
-20
lines changed

dist/index.js

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10117,10 +10117,29 @@ function wrappy (fn, cb) {
1011710117
/***/ }),
1011810118

1011910119
/***/ 6719:
10120-
/***/ (function(__unused_webpack_module, exports) {
10120+
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
1012110121

1012210122
"use strict";
1012310123

10124+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
10125+
if (k2 === undefined) k2 = k;
10126+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
10127+
}) : (function(o, m, k, k2) {
10128+
if (k2 === undefined) k2 = k;
10129+
o[k2] = m[k];
10130+
}));
10131+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10132+
Object.defineProperty(o, "default", { enumerable: true, value: v });
10133+
}) : function(o, v) {
10134+
o["default"] = v;
10135+
});
10136+
var __importStar = (this && this.__importStar) || function (mod) {
10137+
if (mod && mod.__esModule) return mod;
10138+
var result = {};
10139+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
10140+
__setModuleDefault(result, mod);
10141+
return result;
10142+
};
1012410143
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
1012510144
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
1012610145
return new (P || (P = Promise))(function (resolve, reject) {
@@ -10132,29 +10151,43 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
1013210151
};
1013310152
Object.defineProperty(exports, "__esModule", ({ value: true }));
1013410153
exports.createBranch = void 0;
10154+
const core = __importStar(__nccwpck_require__(2186));
1013510155
function createBranch(getOctokit, context, branch, sha) {
10136-
var _a;
1013710156
return __awaiter(this, void 0, void 0, function* () {
1013810157
const toolkit = getOctokit(githubToken());
1013910158
// Sometimes branch might come in with refs/heads already
1014010159
branch = branch.replace('refs/heads/', '');
1014110160
const ref = `refs/heads/${branch}`;
10142-
// throws HttpError if branch already exists.
10161+
const refPath = `heads/${branch}`;
10162+
const targetSha = sha || context.sha;
10163+
core.debug(`Target ref: ${ref} (createRef), refPath: ${refPath} (getRef/updateRef), target SHA: ${targetSha}`);
10164+
// Check if branch already exists using git refs API (heads/<branch>)
1014310165
try {
10144-
yield toolkit.rest.repos.getBranch(Object.assign(Object.assign({}, context.repo), { branch }));
10166+
const refData = yield toolkit.rest.git.getRef(Object.assign({ ref: refPath }, context.repo));
10167+
core.debug(`Found ref via getRef: ${JSON.stringify(refData.data)}`);
10168+
// If ref exists, update it to target SHA
10169+
const resp = yield toolkit.rest.git.updateRef(Object.assign({ ref: refPath, sha: targetSha }, context.repo));
10170+
core.debug(`updateRef response: ${JSON.stringify(resp.data)}`);
10171+
return isValidRefResponse(resp, ref);
1014510172
}
1014610173
catch (error) {
10174+
// If the ref was not found, create it. Other errors bubble up.
1014710175
if (error.name === 'HttpError' && error.status === 404) {
10148-
const resp = yield toolkit.rest.git.createRef(Object.assign({ ref, sha: sha || context.sha }, context.repo));
10149-
return ((_a = resp === null || resp === void 0 ? void 0 : resp.data) === null || _a === void 0 ? void 0 : _a.ref) === ref;
10150-
}
10151-
else {
10152-
throw Error(error);
10176+
core.debug(`Ref not found via getRef, creating new branch`);
10177+
const resp = yield toolkit.rest.git.createRef(Object.assign({ ref, sha: targetSha }, context.repo));
10178+
core.debug(`createRef response: ${JSON.stringify(resp.data)}`);
10179+
return isValidRefResponse(resp, ref);
1015310180
}
10181+
core.debug(`Unexpected error while checking/creating ref: ${error.name} ${error.status} ${error.message}`);
10182+
throw Error(error);
1015410183
}
1015510184
});
1015610185
}
1015710186
exports.createBranch = createBranch;
10187+
function isValidRefResponse(resp, expectedRef) {
10188+
var _a;
10189+
return ((_a = resp === null || resp === void 0 ? void 0 : resp.data) === null || _a === void 0 ? void 0 : _a.ref) === expectedRef;
10190+
}
1015810191
function githubToken() {
1015910192
const token = process.env.GITHUB_TOKEN;
1016010193
if (!token)

src/create-branch.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,49 @@
11
import { Context } from '@actions/github/lib/context';
2+
import * as core from '@actions/core';
23

34
export async function createBranch(getOctokit: any, context: Context, branch: string, sha?: string) {
45
const toolkit = getOctokit(githubToken());
56
// Sometimes branch might come in with refs/heads already
67
branch = branch.replace('refs/heads/', '');
78
const ref = `refs/heads/${branch}`;
8-
9-
// Check if branch already exists
9+
const refPath = `heads/${branch}`;
10+
const targetSha = sha || context.sha;
11+
12+
core.debug(`Target ref: ${ref} (createRef), refPath: ${refPath} (getRef/updateRef), target SHA: ${targetSha}`);
13+
// Check if branch already exists using git refs API (heads/<branch>)
1014
try {
11-
await toolkit.rest.repos.getBranch({
15+
const refData = await toolkit.rest.git.getRef({
16+
ref: refPath,
1217
...context.repo,
13-
branch,
1418
});
1519

16-
// Branch exists, update it with the new SHA
20+
core.debug(`Found ref via getRef: ${JSON.stringify(refData.data)}`);
21+
22+
// If ref exists, update it to target SHA
1723
const resp = await toolkit.rest.git.updateRef({
18-
ref,
19-
sha: sha || context.sha,
24+
ref: refPath,
25+
sha: targetSha,
2026
...context.repo,
2127
});
2228

29+
core.debug(`updateRef response: ${JSON.stringify(resp.data)}`);
2330
return isValidRefResponse(resp, ref);
2431
} catch (error: any) {
32+
// If the ref was not found, create it. Other errors bubble up.
2533
if (error.name === 'HttpError' && error.status === 404) {
26-
// Branch doesn't exist, create it
34+
core.debug(`Ref not found via getRef, creating new branch`);
2735
const resp = await toolkit.rest.git.createRef({
2836
ref,
29-
sha: sha || context.sha,
37+
sha: targetSha,
3038
...context.repo,
3139
});
3240

41+
core.debug(`createRef response: ${JSON.stringify(resp.data)}`);
3342
return isValidRefResponse(resp, ref);
34-
} else {
35-
throw Error(error);
3643
}
44+
45+
core.debug(`Unexpected error while checking/creating ref: ${error.name} ${error.status} ${error.message}`);
46+
throw Error(error);
3747
}
3848
}
3949

0 commit comments

Comments
 (0)