Skip to content

Commit 76cd299

Browse files
Add random gif (#11)
* Add random gif * Update dist * use original size * Change style * Intentional fail test * Revert failing test
1 parent 2089b2b commit 76cd299

File tree

4 files changed

+82
-4
lines changed

4 files changed

+82
-4
lines changed

dist/index.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119672,6 +119672,9 @@ __webpack_async_result__();
119672119672
/* harmony import */ var _actions_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__nccwpck_require__.n(_actions_core__WEBPACK_IMPORTED_MODULE_0__);
119673119673
/* harmony import */ var _actions_github__WEBPACK_IMPORTED_MODULE_1__ = __nccwpck_require__(27133);
119674119674
/* harmony import */ var _actions_github__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__nccwpck_require__.n(_actions_github__WEBPACK_IMPORTED_MODULE_1__);
119675+
/* harmony import */ var _actions_http_client__WEBPACK_IMPORTED_MODULE_2__ = __nccwpck_require__(99855);
119676+
/* harmony import */ var _actions_http_client__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__nccwpck_require__.n(_actions_http_client__WEBPACK_IMPORTED_MODULE_2__);
119677+
119675119678

119676119679

119677119680

@@ -119689,10 +119692,12 @@ __webpack_async_result__();
119689119692
const commentIdentifier = "junit-summary-action";
119690119693
let comment;
119691119694
if (result.numberOfFailedTests > 0) {
119692-
comment = `⚠️ ${result.numberOfFailedTests} tests failed.`;
119695+
const randomGif = await getRandomGif("fail");
119696+
comment = `<h2>⚠️ ${result.numberOfFailedTests} tests failed</h2>![fail](${randomGif})`;
119693119697
}
119694119698
else {
119695-
comment = `✅ All ${result.numberOfPassedTests} tests passed`;
119699+
const randomGif = await getRandomGif("success");
119700+
comment = `<h2>✅ All ${result.numberOfPassedTests} tests passed</h2>![success](${randomGif})`;
119696119701
}
119697119702
const commentBody = `**${comment}**\n\n[See summary](${getWorkflowRunSummaryUrl()})\n\n<sup>${commentIdentifier}</sup>`;
119698119703
// Retrieve the list of comments on the pull request
@@ -119729,6 +119734,18 @@ __webpack_async_result__();
119729119734
_actions_core__WEBPACK_IMPORTED_MODULE_0__.setFailed(`Action failed with error: ${error}`);
119730119735
}
119731119736
});
119737+
async function getRandomGif(search) {
119738+
const httpClient = new _actions_http_client__WEBPACK_IMPORTED_MODULE_2__.HttpClient();
119739+
const randomNumber = Math.floor(Math.random() * 101);
119740+
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`);
119741+
const body = await res.readBody();
119742+
const responseJson = JSON.parse(body);
119743+
if (responseJson.data.length > 0) {
119744+
const fixedWidthImage = responseJson.data[0].images.original;
119745+
return fixedWidthImage.url;
119746+
}
119747+
return undefined;
119748+
}
119732119749
function getWorkflowRunSummaryUrl() {
119733119750
const { owner, repo } = _actions_github__WEBPACK_IMPORTED_MODULE_1__.context.repo;
119734119751
const runId = _actions_github__WEBPACK_IMPORTED_MODULE_1__.context.runId; // This is provided by the GitHub Actions environment

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"@actions/exec": "^1.1.1",
3232
"@actions/github": "^6.0.0",
3333
"@actions/glob": "^0.4.0",
34+
"@actions/http-client": "^2.2.0",
3435
"cd": "^0.3.3",
3536
"dotenv": "^16.3.1",
3637
"eslint": "^8.52.0",

src/utils/addCommentToPR.ts

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as core from "@actions/core";
22
import * as github from "@actions/github";
33
import { context } from "@actions/github";
4+
import * as http from "@actions/http-client";
45

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

2021
let comment: string;
2122
if (result.numberOfFailedTests > 0) {
22-
comment = `⚠️ ${result.numberOfFailedTests} tests failed.`;
23+
const randomGif = await getRandomGif("fail");
24+
comment = `<h2>⚠️ ${result.numberOfFailedTests} tests failed</h2>![fail](${randomGif})`;
2325
} else {
24-
comment = `✅ All ${result.numberOfPassedTests} tests passed`;
26+
const randomGif = await getRandomGif("success");
27+
comment = `<h2>✅ All ${result.numberOfPassedTests} tests passed</h2>![success](${randomGif})`;
2528
}
2629

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

73+
async function getRandomGif(search: string) {
74+
const httpClient = new http.HttpClient();
75+
const randomNumber = Math.floor(Math.random() * 101);
76+
const res = await httpClient.get(
77+
`https://api.giphy.com/v1/gifs/search?api_key=PFZ64SqzXhfNwVVWo6iwe1UjZzUomr1j&q=${search}&limit=1&offset=${randomNumber}&rating=g&lang=en&bundle=messaging_non_clips`
78+
);
79+
80+
const body = await res.readBody();
81+
const responseJson: GiphyResponse = JSON.parse(body);
82+
if (responseJson.data.length > 0) {
83+
const fixedWidthImage = responseJson.data[0].images.original;
84+
return fixedWidthImage.url;
85+
}
86+
return undefined;
87+
}
88+
7089
function getWorkflowRunSummaryUrl(): string {
7190
const { owner, repo } = context.repo;
7291
const runId = context.runId; // This is provided by the GitHub Actions environment
7392
return `https://github.com/${owner}/${repo}/actions/runs/${runId}`;
7493
}
94+
95+
// ChatGPT ftw
96+
interface GiphyResponse {
97+
data: GiphyData[];
98+
pagination: Pagination;
99+
meta: Meta;
100+
}
101+
102+
interface GiphyData {
103+
type: string;
104+
id: string;
105+
url: string;
106+
images: GiphyImages;
107+
}
108+
109+
interface GiphyImages {
110+
original: ImageFormat;
111+
fixed_height: ImageFormat;
112+
fixed_height_downsampled: ImageFormat;
113+
fixed_height_small: ImageFormat;
114+
fixed_width: ImageFormat;
115+
}
116+
117+
interface ImageFormat {
118+
height: string;
119+
width: string;
120+
url: string;
121+
}
122+
123+
interface Pagination {
124+
total_count: number;
125+
count: number;
126+
offset: number;
127+
}
128+
129+
interface Meta {
130+
status: number;
131+
msg: string;
132+
response_id: string;
133+
}

0 commit comments

Comments
 (0)