Skip to content

Commit 6b57ccf

Browse files
authored
Improve Action Reliability (#1) (#2)
* refactor: improve input handling, typing, and error safety in branch create action - Add explicit TypeScript types for all inputs - Trim all received inputs to avoid whitespace-related failures - Strengthen error handling with unknown type guard - Add JSDoc annotation for main function - Replace direct run() call with void run() for explicit async invocation - Maintain existing GitHub API request logic while improving reliability * build: update compiled artifact in dist/index.js - Regenerate dist output to reflect recent refactor and typing improvements - Align compiled bundle with updated input validation and enhanced error handling
1 parent 52b82b5 commit 6b57ccf

File tree

2 files changed

+36
-34
lines changed

2 files changed

+36
-34
lines changed

dist/index.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29963,17 +29963,18 @@ var __importStar = (this && this.__importStar) || (function () {
2996329963
Object.defineProperty(exports, "__esModule", ({ value: true }));
2996429964
const core = __importStar(__nccwpck_require__(7484));
2996529965
const github = __importStar(__nccwpck_require__(3228));
29966+
/**
29967+
* Main asynchronous function.
29968+
* @returns {Promise<void>}
29969+
*/
2996629970
async function run() {
2996729971
try {
29968-
// 1. Obter os inputs
29969-
const owner = core.getInput('owner', { required: true });
29970-
const repo = core.getInput('repo', { required: true });
29971-
const baseBranch = core.getInput('base-branch', { required: true });
29972-
const newBranch = core.getInput('new-branch', { required: true });
29973-
const githubToken = core.getInput('github-token', { required: true });
29974-
// 2. Inicializar o cliente Octokit (para interagir com a API do GitHub)
29972+
const githubToken = core.getInput('github-token', { required: true }).trim();
29973+
const owner = core.getInput('owner', { required: true }).trim();
29974+
const repo = core.getInput('repo', { required: true }).trim();
29975+
const baseBranch = core.getInput('base-branch', { required: true }).trim();
29976+
const newBranch = core.getInput('new-branch', { required: true }).trim();
2997529977
const octokit = github.getOctokit(githubToken);
29976-
// 3. Obter o SHA do commit da base-branch
2997729978
core.info(`-> Trying to get the SHA for the base branch: ${baseBranch}`);
2997829979
const { data: baseBranchData } = await octokit.rest.git.getRef({
2997929980
owner,
@@ -29982,26 +29983,26 @@ async function run() {
2998229983
});
2998329984
const sha = baseBranchData.object.sha;
2998429985
core.info(`<- SHA of the base branch (${baseBranch}) successfully obtained: ${sha}`);
29985-
// 4. Criar a nova branch (referência)
2998629986
core.info(`-> Creating the new branch: ${newBranch} with SHA: ${sha}`);
2998729987
await octokit.rest.git.createRef({
2998829988
owner,
2998929989
repo,
2999029990
ref: `refs/heads/${newBranch}`, // 'refs/heads/' é necessário para criar novas branches
2999129991
sha: sha,
2999229992
});
29993-
// 5. Sucesso
2999429993
core.setOutput('success-message', `Branch '${newBranch}' created successfully in the repository '${owner}/${repo}' from '${baseBranch}'.`);
2999529994
core.info('Operation completed successfully!');
2999629995
}
2999729996
catch (error) {
29998-
// 6. Falha
29999-
core.setFailed(error.message);
30000-
core.error(`Failed to create branch: ${error.message}`);
29997+
if (error instanceof Error) {
29998+
core.setFailed(`Action failed due to error: ${error.message}`);
29999+
}
30000+
else {
30001+
core.setFailed('Action failed with an unknown error.');
30002+
}
3000130003
}
3000230004
}
30003-
// Executar a função principal
30004-
run();
30005+
void run();
3000530006

3000630007

3000730008
/***/ }),

src/index.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
import * as core from '@actions/core';
22
import * as github from '@actions/github';
33

4-
async function run() {
4+
/**
5+
* Main asynchronous function.
6+
* @returns {Promise<void>}
7+
*/
8+
async function run(): Promise<void> {
59
try {
6-
// 1. Obter os inputs
7-
const owner = core.getInput('owner', { required: true });
8-
const repo = core.getInput('repo', { required: true });
9-
const baseBranch = core.getInput('base-branch', { required: true });
10-
const newBranch = core.getInput('new-branch', { required: true });
11-
const githubToken = core.getInput('github-token', { required: true });
12-
13-
// 2. Inicializar o cliente Octokit (para interagir com a API do GitHub)
10+
const githubToken: string = core.getInput('github-token', { required: true }).trim();
11+
const owner: string = core.getInput('owner', { required: true }).trim();
12+
const repo: string = core.getInput('repo', { required: true }).trim();
13+
const baseBranch: string = core.getInput('base-branch', { required: true }).trim();
14+
const newBranch: string = core.getInput('new-branch', { required: true }).trim();
15+
1416
const octokit = github.getOctokit(githubToken);
1517

16-
// 3. Obter o SHA do commit da base-branch
1718
core.info(`-> Trying to get the SHA for the base branch: ${baseBranch}`);
19+
1820
const { data: baseBranchData } = await octokit.rest.git.getRef({
1921
owner,
2022
repo,
2123
ref: `heads/${baseBranch}`, // 'heads/' é necessário para referências de branches
2224
});
2325

2426
const sha = baseBranchData.object.sha;
27+
2528
core.info(`<- SHA of the base branch (${baseBranch}) successfully obtained: ${sha}`);
26-
27-
// 4. Criar a nova branch (referência)
2829
core.info(`-> Creating the new branch: ${newBranch} with SHA: ${sha}`);
2930

3031
await octokit.rest.git.createRef({
@@ -34,16 +35,16 @@ async function run() {
3435
sha: sha,
3536
});
3637

37-
// 5. Sucesso
3838
core.setOutput('success-message', `Branch '${newBranch}' created successfully in the repository '${owner}/${repo}' from '${baseBranch}'.`);
3939
core.info('Operation completed successfully!');
4040

41-
} catch (error: any) {
42-
// 6. Falha
43-
core.setFailed(error.message);
44-
core.error(`Failed to create branch: ${error.message}`);
41+
} catch (error: unknown) {
42+
if (error instanceof Error) {
43+
core.setFailed(`Action failed due to error: ${error.message}`);
44+
} else {
45+
core.setFailed('Action failed with an unknown error.');
46+
}
4547
}
4648
}
4749

48-
// Executar a função principal
49-
run();
50+
void run();

0 commit comments

Comments
 (0)