Skip to content

Commit 2eedb18

Browse files
authored
Merge pull request #11 from matlab-actions/alert
Add alert to manually download YAML file instead
2 parents 52852a3 + dcea854 commit 2eedb18

File tree

4 files changed

+80
-1
lines changed

4 files changed

+80
-1
lines changed

public/index.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@
2121
your browser.
2222
</div>
2323
</noscript>
24+
<div
25+
id="download-alert"
26+
class="alert alert-warning alert-dismissible fade show d-none"
27+
role="alert"
28+
tabindex="-1">
29+
<strong>Having trouble?</strong>
30+
If the generated workflow does not automatically appear in your workflow
31+
editor, manually download
32+
<a href="#" id="download-alert-link">matlab.yml</a>
33+
and save it to the
34+
<code>.github/workflows</code>
35+
folder of your repository.
36+
<button
37+
type="button"
38+
class="btn-close"
39+
data-bs-dismiss="alert"
40+
aria-label="Close"></button>
41+
</div>
2442
<main class="container py-5">
2543
<div class="text-center mt-4 mb-4">
2644
<img

public/scripts/main.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,36 @@ function handleFormSubmit(e) {
4141
url += `/${repoInfo.owner}/${repoInfo.repo}/new/main?filename=${filePath}&value=${encoded}`;
4242

4343
window.navigateTo(url);
44+
45+
showDownloadAlert();
46+
}
47+
48+
function showDownloadAlert() {
49+
const alert = document.getElementById("download-alert");
50+
alert.classList.remove("d-none");
51+
alert.focus();
52+
}
53+
54+
function handleDownloadClick(e) {
55+
e.preventDefault();
56+
57+
const workflow = generateWorkflowWithFormInputs();
58+
59+
const blob = new Blob([workflow], { type: "text/yaml" });
60+
const url = URL.createObjectURL(blob);
61+
const a = document.createElement("a");
62+
a.href = url;
63+
a.download = "matlab.yml";
64+
a.click();
65+
URL.revokeObjectURL(url);
4466
}
4567

4668
document
4769
.getElementById("generate-form")
4870
.addEventListener("submit", handleFormSubmit);
71+
document
72+
.getElementById("download-alert-link")
73+
.addEventListener("click", handleDownloadClick);
4974

5075
document.querySelectorAll('[data-bs-toggle="tooltip"]').forEach((el) => {
5176
new bootstrap.Tooltip(el);

public/style.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,12 @@ p.lead.text-muted {
118118
.bi-question-circle {
119119
color: #0076a8;
120120
}
121+
122+
#download-alert {
123+
text-align: center;
124+
position: fixed;
125+
top: 5px;
126+
left: 2%;
127+
width: 96%;
128+
z-index: 2000;
129+
}

tests/main.test.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ beforeEach(async () => {
1717
<input type="checkbox" id="build-across-platforms" />
1818
<button type="submit" id="generate-button"></button>
1919
</form>
20-
<a id="downloadButton"></a>
20+
<a id="download-alert-link"></a>
21+
<div id="download-alert" class="d-none"></div>
2122
`;
2223

2324
await import("../public/scripts/main.js");
@@ -98,3 +99,29 @@ test("advanced options are passed to generateWorkflow", async () => {
9899
jsyaml: window.jsyaml,
99100
});
100101
});
102+
103+
test("download link triggers file download", () => {
104+
const repoInput = document.getElementById("repo");
105+
repoInput.value = "owner/repo";
106+
107+
window.jsyaml = { dump: () => "yaml-content" };
108+
109+
const mockCreateObjectURL = jest.fn(() => "blob:url");
110+
const mockRevokeObjectURL = jest.fn();
111+
global.URL.createObjectURL = mockCreateObjectURL;
112+
global.URL.revokeObjectURL = mockRevokeObjectURL;
113+
114+
const a = document.createElement("a");
115+
document.body.appendChild(a);
116+
jest.spyOn(document, "createElement").mockImplementation((tag) => {
117+
if (tag === "a") return a;
118+
return document.createElement(tag);
119+
});
120+
const clickSpy = jest.spyOn(a, "click");
121+
122+
document.getElementById("download-alert-link").click();
123+
124+
expect(mockCreateObjectURL).toHaveBeenCalled();
125+
expect(clickSpy).toHaveBeenCalled();
126+
expect(mockRevokeObjectURL).toHaveBeenCalled();
127+
});

0 commit comments

Comments
 (0)