From c5926f1df3bd2aa01811855151fff61a3fbe74f9 Mon Sep 17 00:00:00 2001 From: Simon Schneegans Date: Sun, 9 Oct 2022 06:27:21 +0200 Subject: [PATCH] :memo: Add some comments --- .clang-format | 1 + index.js | 136 +++++++++++++++++++++++++++----------------------- 2 files changed, 75 insertions(+), 62 deletions(-) diff --git a/.clang-format b/.clang-format index 6978ceb..879a1a0 100644 --- a/.clang-format +++ b/.clang-format @@ -1,3 +1,4 @@ BasedOnStyle: Google +ColumnLimit: 90 AlignConsecutiveAssignments: true KeepEmptyLinesAtTheStartOfBlocks: true \ No newline at end of file diff --git a/index.js b/index.js index 8e15a02..0cbfc22 100644 --- a/index.js +++ b/index.js @@ -1,63 +1,72 @@ +////////////////////////////////////////////////////////////////////////////////////////// +// This file is part of the Dynamic Badges Action // +// It may be used under the terms of the MIT license. See the LICENSE file for details. // +// Copyright: (c) 2020 Simon Schneegans // +////////////////////////////////////////////////////////////////////////////////////////// + const core = require('@actions/core'); const http = require('https'); +// Performs an HTTP request and returns a Promise accordingly. See docs of +// http.request() for the available options. +function doRequest(options, data) { + return new Promise((resolve, reject) => { + const req = http.request(options, res => { + res.setEncoding('utf8'); + let responseBody = ''; + + res.on('data', (chunk) => { + responseBody += chunk; + }); + + res.on('end', () => { + const {statusCode, statusMessage} = res; + resolve({statusCode, statusMessage, body: JSON.parse(responseBody)}); + }); + }); + + req.on('error', (err) => { + reject(err); + }); + + req.write(data) + req.end(); + }); +} + +// This uses the method above to update a gist with the given data. The user agent is +// required as defined in https://developer.github.com/v3/#user-agent-required +function updateGist(data) { + const updateGistOptions = { + host: 'api.github.com', + path: '/gists/' + core.getInput('gistID'), + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Content-Length': data.length, + 'User-Agent': 'Schneegans', + 'Authorization': 'token ' + core.getInput('auth'), + } + }; + + doRequest(updateGistOptions, data).then(res => { + if (res.statusCode < 200 || res.statusCode >= 400) { + core.setFailed( + 'Failed to create gist, response status code: ' + res.statusCode + + ', status message: ' + res.statusMessage); + } else { + console.log('Success!'); + } + }); +} + +// We wrap the entire action in a try / catch block so we can set it to "failed" if +// something goes wrong. try { - function updateGist(data) { - // Perform the actual request. The user agent is required as defined in - // https://developer.github.com/v3/#user-agent-required - const updateGistOptions = { - host: 'api.github.com', - path: '/gists/' + core.getInput('gistID'), - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Content-Length': data.length, - 'User-Agent': 'Schneegans', - 'Authorization': 'token ' + core.getInput('auth'), - } - }; - - doRequest(updateGistOptions, data).then(res => { - if (res.statusCode < 200 || res.statusCode >= 400) { - core.setFailed( - 'Failed to create gist, response status code: ' + res.statusCode + - ', status message: ' + res.statusMessage); - } else { - console.log('Success!'); - } - }); - } - - function doRequest(options, data) { - return new Promise((resolve, reject) => { - const req = http.request(options, res => { - res.setEncoding('utf8'); - let responseBody = ''; - - res.on('data', (chunk) => { - responseBody += chunk; - }); - - res.on('end', () => { - const {statusCode, statusMessage} = res; - resolve({statusCode, statusMessage, body: JSON.parse(responseBody)}); - }); - }); - - req.on('error', (err) => { - reject(err); - }); - - req.write(data) - req.end(); - }); - } - - // This object will be stringified and uploaded to the gist. The - // schemaVersion, label and message attributes are always required. All others - // are optional and added to the content object only if they are given to the - // action. + // This object will be stringified and uploaded to the gist. The schemaVersion, label + // and message attributes are always required. All others are optional and added to the + // content object only if they are given to the action. let content = { schemaVersion: 1, label: core.getInput('label'), @@ -158,9 +167,14 @@ try { const request = JSON.stringify({files: {[filename]: {content: JSON.stringify(content)}}}); + // If "forceUpdate" is set to true, we can simply update the gist. If not, we have to + // get the gist data and compare it to the new value before. if (core.getBooleanInput('forceUpdate')) { updateGist(request); + } else { + + // Get the old gist. const getGistOptions = { host: 'api.github.com', path: '/gists/' + core.getInput('gistID'), @@ -174,7 +188,7 @@ try { doRequest(getGistOptions, JSON.stringify({})).then(oldGist => { if (oldGist.statusCode < 200 || oldGist.statusCode >= 400) { - // print the error, but don't fail the action + // print the error, but don't fail the action. console.log( 'Failed to get gist, response status code: ' + oldGist.statusCode + ', status message: ' + oldGist.statusMessage); @@ -182,22 +196,20 @@ try { let shouldUpdate = true; - if (oldGist && oldGist.body && oldGist.body.files && - oldGist.body.files[filename]) { + if (oldGist && oldGist.body && oldGist.body.files && oldGist.body.files[filename]) { const oldContent = oldGist.body.files[filename].content; if (oldContent === JSON.stringify(content)) { - console.log( - `Content did not change, not updating gist at ${filename}`); + console.log(`Content did not change, not updating gist at ${filename}.`); shouldUpdate = false; } } if (shouldUpdate) { if (oldGist.body.files[filename]) { - console.log(`Content changed, updating gist at ${filename}`); + console.log(`Content changed, updating gist at ${filename}.`); } else { - console.log(`Content didn't exist, creating gist at ${filename}`); + console.log(`Content didn't exist, creating gist at ${filename}.`); } updateGist(request);