Skip to content

Commit

Permalink
Add random gif (#11)
Browse files Browse the repository at this point in the history
* Add random gif

* Update dist

* use original size

* Change style

* Intentional fail test

* Revert failing test
  • Loading branch information
PerThomasHaga authored Nov 9, 2023
1 parent 2089b2b commit 76cd299
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
21 changes: 19 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119672,6 +119672,9 @@ __webpack_async_result__();
/* harmony import */ var _actions_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__nccwpck_require__.n(_actions_core__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _actions_github__WEBPACK_IMPORTED_MODULE_1__ = __nccwpck_require__(27133);
/* harmony import */ var _actions_github__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__nccwpck_require__.n(_actions_github__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var _actions_http_client__WEBPACK_IMPORTED_MODULE_2__ = __nccwpck_require__(99855);
/* harmony import */ var _actions_http_client__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__nccwpck_require__.n(_actions_http_client__WEBPACK_IMPORTED_MODULE_2__);




Expand All @@ -119689,10 +119692,12 @@ __webpack_async_result__();
const commentIdentifier = "junit-summary-action";
let comment;
if (result.numberOfFailedTests > 0) {
comment = `⚠️ ${result.numberOfFailedTests} tests failed.`;
const randomGif = await getRandomGif("fail");
comment = `<h2>⚠️ ${result.numberOfFailedTests} tests failed</h2>![fail](${randomGif})`;
}
else {
comment = `✅ All ${result.numberOfPassedTests} tests passed`;
const randomGif = await getRandomGif("success");
comment = `<h2>✅ All ${result.numberOfPassedTests} tests passed</h2>![success](${randomGif})`;
}
const commentBody = `**${comment}**\n\n[See summary](${getWorkflowRunSummaryUrl()})\n\n<sup>${commentIdentifier}</sup>`;
// Retrieve the list of comments on the pull request
Expand Down Expand Up @@ -119729,6 +119734,18 @@ __webpack_async_result__();
_actions_core__WEBPACK_IMPORTED_MODULE_0__.setFailed(`Action failed with error: ${error}`);
}
});
async function getRandomGif(search) {
const httpClient = new _actions_http_client__WEBPACK_IMPORTED_MODULE_2__.HttpClient();
const randomNumber = Math.floor(Math.random() * 101);
const res = await httpClient.get(`https://api.giphy.com/v1/gifs/search?api_key=PFZ64SqzXhfNwVVWo6iwe1UjZzUomr1j&q=${search}&limit=1&offset=${randomNumber}&rating=g&lang=en&bundle=messaging_non_clips`);
const body = await res.readBody();
const responseJson = JSON.parse(body);
if (responseJson.data.length > 0) {
const fixedWidthImage = responseJson.data[0].images.original;
return fixedWidthImage.url;
}
return undefined;
}
function getWorkflowRunSummaryUrl() {
const { owner, repo } = _actions_github__WEBPACK_IMPORTED_MODULE_1__.context.repo;
const runId = _actions_github__WEBPACK_IMPORTED_MODULE_1__.context.runId; // This is provided by the GitHub Actions environment
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@actions/exec": "^1.1.1",
"@actions/github": "^6.0.0",
"@actions/glob": "^0.4.0",
"@actions/http-client": "^2.2.0",
"cd": "^0.3.3",
"dotenv": "^16.3.1",
"eslint": "^8.52.0",
Expand Down
63 changes: 61 additions & 2 deletions src/utils/addCommentToPR.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as core from "@actions/core";
import * as github from "@actions/github";
import { context } from "@actions/github";
import * as http from "@actions/http-client";

export default async (result: WriteSummaryResult) => {
try {
Expand All @@ -19,9 +20,11 @@ export default async (result: WriteSummaryResult) => {

let comment: string;
if (result.numberOfFailedTests > 0) {
comment = `⚠️ ${result.numberOfFailedTests} tests failed.`;
const randomGif = await getRandomGif("fail");
comment = `<h2>⚠️ ${result.numberOfFailedTests} tests failed</h2>![fail](${randomGif})`;
} else {
comment = `✅ All ${result.numberOfPassedTests} tests passed`;
const randomGif = await getRandomGif("success");
comment = `<h2>✅ All ${result.numberOfPassedTests} tests passed</h2>![success](${randomGif})`;
}

const commentBody = `**${comment}**\n\n[See summary](${getWorkflowRunSummaryUrl()})\n\n<sup>${commentIdentifier}</sup>`;
Expand Down Expand Up @@ -67,8 +70,64 @@ export default async (result: WriteSummaryResult) => {
}
};

async function getRandomGif(search: string) {
const httpClient = new http.HttpClient();
const randomNumber = Math.floor(Math.random() * 101);
const res = await httpClient.get(
`https://api.giphy.com/v1/gifs/search?api_key=PFZ64SqzXhfNwVVWo6iwe1UjZzUomr1j&q=${search}&limit=1&offset=${randomNumber}&rating=g&lang=en&bundle=messaging_non_clips`
);

const body = await res.readBody();
const responseJson: GiphyResponse = JSON.parse(body);
if (responseJson.data.length > 0) {
const fixedWidthImage = responseJson.data[0].images.original;
return fixedWidthImage.url;
}
return undefined;
}

function getWorkflowRunSummaryUrl(): string {
const { owner, repo } = context.repo;
const runId = context.runId; // This is provided by the GitHub Actions environment
return `https://github.com/${owner}/${repo}/actions/runs/${runId}`;
}

// ChatGPT ftw
interface GiphyResponse {
data: GiphyData[];
pagination: Pagination;
meta: Meta;
}

interface GiphyData {
type: string;
id: string;
url: string;
images: GiphyImages;
}

interface GiphyImages {
original: ImageFormat;
fixed_height: ImageFormat;
fixed_height_downsampled: ImageFormat;
fixed_height_small: ImageFormat;
fixed_width: ImageFormat;
}

interface ImageFormat {
height: string;
width: string;
url: string;
}

interface Pagination {
total_count: number;
count: number;
offset: number;
}

interface Meta {
status: number;
msg: string;
response_id: string;
}

0 comments on commit 76cd299

Please sign in to comment.