Skip to content

Commit 7d5ae5f

Browse files
committed
better bits of logs
1 parent 278f316 commit 7d5ae5f

File tree

5 files changed

+84
-24
lines changed

5 files changed

+84
-24
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ jest.config.js
66
.github
77
.vscode
88
dummy
9+
.env
10+
.env*

github.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const fs = require("fs");
22
const yargs = require("yargs");
33
const CURRENT_VERSION = require("./package.json").version;
4+
const { info, error, debug } = require("./src/log");
45

56
const {
67
init,
@@ -18,7 +19,7 @@ const appendLineToFile = (filename, line) => {
1819
try {
1920
fs.appendFileSync(filename, `${line}\n`);
2021
} catch (e) {
21-
console.error(`Error appending line to file ${filename}: ${e.message}`);
22+
error(`Error appending line to file ${filename}: ${e.message}`);
2223
throw e;
2324
}
2425
};
@@ -96,7 +97,7 @@ yargs
9697
commitMessage,
9798
commitDescription,
9899
} = argv;
99-
100+
debug("Passed args:", JSON.stringify(argv, null, 2));
100101
createCommitOnBranch(
101102
owner,
102103
repo,
@@ -107,7 +108,7 @@ yargs
107108
commitDescription
108109
)
109110
.then((response) => {
110-
console.log(`Commit created: ${response.commitUrl}`);
111+
info(`Commit created: ${response.commitUrl}`);
111112
writeResultToGithubOutputFile([
112113
{
113114
label: "command",
@@ -119,8 +120,8 @@ yargs
119120
},
120121
]);
121122
})
122-
.catch((error) => {
123-
console.error("Failed to create commit:", error.message);
123+
.catch((err) => {
124+
error("Failed to create commit:", err.message);
124125
process.exit(1);
125126
});
126127
}
@@ -151,10 +152,11 @@ yargs
151152
},
152153
(argv) => {
153154
const { owner, repo, branch } = argv;
155+
debug("Passed args:", JSON.stringify(argv, null, 2));
154156
checkIfBranchExists(owner, repo, branch)
155157
.then((response) => {
156158
const n = response ? "a" : "no";
157-
console.log(
159+
info(
158160
`Repository ${owner}/${repo} has ${n} branch named '${branch}'`
159161
);
160162
writeResultToGithubOutputFile([
@@ -168,8 +170,8 @@ yargs
168170
},
169171
]);
170172
})
171-
.catch((error) => {
172-
console.error("Failed to check if branch exists:", error.message);
173+
.catch((err) => {
174+
error("Failed to check if branch exists:", err.message);
173175
process.exit(1);
174176
});
175177
}

index.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const {
44
cacheExchange,
55
fetchExchange
66
} = require("@urql/core");
7+
const { info, error, debug } = require("./src/log");
78

89
let GITHUB_GRAPHQL_URL = null;
910
let githubToken = null;
@@ -16,7 +17,7 @@ let client = null;
1617
* @param {string} apiUrl GitHub GraphQL API URL
1718
*/
1819
function init(token, apiUrl) {
19-
githubToken = token || process.env.GITHUB_TOKEN;
20+
const githubToken = token || process.env.GITHUB_TOKEN;
2021
if (!githubToken) {
2122
throw new Error("Error: token argument missing or GITHUB_TOKEN env var not set.");
2223
}
@@ -87,10 +88,7 @@ function extractChangedOrDeletedFiles(changedFiles, deletedFiles) {
8788

8889
function checkErrorResponse(response) {
8990
if (!!response.errors || !!response.error || !response.data) {
90-
console.error(
91-
"Error response from API:",
92-
JSON.stringify(response, null, 2)
93-
);
91+
error("Error response from API:", JSON.stringify(response, null, 2));
9492
throw new Error("Received error response from API");
9593
}
9694
}
@@ -119,11 +117,11 @@ async function fetchBranchData(repoOwner, repoName, branchName) {
119117
const response = await client.query(query, variables);
120118
checkErrorResponse(response);
121119
return response;
122-
} catch (error) {
123-
console.error(
124-
`Error while trying to fetch data from API: ${error.message}`
120+
} catch (err) {
121+
error(
122+
`Error while trying to fetch data from API: ${err.message}`
125123
);
126-
throw error;
124+
throw err;
127125
}
128126
}
129127

@@ -179,8 +177,8 @@ async function createCommitOnBranch(
179177
changedFiles,
180178
deletedFiles
181179
));
182-
console.log("Changed files:", JSON.stringify(changedFiles, null, 2));
183-
console.log("Deleted files:", JSON.stringify(deletedFiles, null, 2));
180+
info("Changed files:", JSON.stringify(changedFiles, null, 2));
181+
info("Deleted files:", JSON.stringify(deletedFiles, null, 2));
184182

185183
if (!commitMessage) {
186184
throw new Error("No commit message provided. Aborting.");
@@ -216,11 +214,11 @@ async function createCommitOnBranch(
216214
data: response,
217215
commitUrl: response?.data?.createCommitOnBranch?.commit?.url || null
218216
};
219-
} catch (error) {
220-
console.error(
221-
`Error while performing commit action via GraphQL API: ${error.message}`
217+
} catch (err) {
218+
error(
219+
`Error while performing commit action via GraphQL API: ${err.message}`
222220
);
223-
throw error;
221+
throw err;
224222
}
225223
}
226224

index.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe("createCommitOnBranch", () => {
2525
"this is a commit msg",
2626
"the description of the commit"
2727
);
28-
console.log(JSON.stringify(result, null, 2));
28+
//console.log(JSON.stringify(result, null, 2));
2929
const commitUrl = result?.commitUrl || "";
3030
expect(commitUrl).toMatch(
3131
new RegExp(

src/log.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
// Description: This file contains the log utility functions.
3+
// The log utility functions are used to log messages to the console.
4+
// IMPORTANT: to avoid circular dependencies, the log utility functions
5+
// should not import any other files.
6+
//
7+
8+
const isRunningInCI = () => {
9+
return (
10+
!!process.env.GITHUB_ACTIONS ||
11+
!!process.env.TRAVIS ||
12+
!!process.env.CIRCLECI ||
13+
!!process.env.GITLAB_CI ||
14+
!!process.env.APPVEYOR
15+
);
16+
};
17+
18+
// flag to determine if we are running in CI
19+
const _isCI = isRunningInCI();
20+
21+
const pad = (str, length, separator) => {
22+
if (str.length === 0 || str.length >= length) return str;
23+
const d = length - str.length;
24+
const m = d % 2;
25+
const p = (d - m) / 2;
26+
return str.padStart(str.length + p, separator).padEnd(length, separator);
27+
};
28+
29+
const getPrefix = (level) => {
30+
// only print timestamp if not running in CI, CI have their own timestamps
31+
const timestamp = _isCI ? "" : `[${new Date().toISOString()}] `;
32+
// second argument is max possible length of log level string
33+
level = pad(level, 5, " ");
34+
return `${timestamp}[${level}] :`
35+
};
36+
37+
const info = (...args) => {
38+
const prefix = getPrefix("INFO");
39+
console.log(prefix, ...args);
40+
};
41+
42+
const error = (...args) => {
43+
const prefix = getPrefix("ERROR");
44+
console.error(prefix, ...args);
45+
};
46+
47+
const debug = (...args) => {
48+
if (!!process.env.DEBUG) {
49+
const prefix = getPrefix("DEBUG");
50+
console.log(prefix, ...args);
51+
}
52+
};
53+
54+
module.exports = {
55+
info,
56+
error,
57+
debug,
58+
};

0 commit comments

Comments
 (0)