Skip to content

Commit

Permalink
Merge pull request #2144 from headlamp-k8s/remove-octokit
Browse files Browse the repository at this point in the history
frontend: Fix ReleaseNotes so it works again
  • Loading branch information
joaquimrocha authored Jul 18, 2024
2 parents 21c8d95 + 5755f36 commit 70803c7
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 462 deletions.
110 changes: 5 additions & 105 deletions frontend/package-lock.json

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

1 change: 0 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"@mui/styles": "^5.15.14",
"@mui/system": "^5.15.14",
"@mui/x-tree-view": "^6.17.0",
"@octokit/core": "^3.5.1",
"@reduxjs/toolkit": "^1.9.3",
"@testing-library/react": "^12.1.2",
"@types/glob": "^8.1.0",
Expand Down
155 changes: 96 additions & 59 deletions frontend/src/components/common/ReleaseNotes/ReleaseNotes.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Octokit } from '@octokit/core';
import React from 'react';
import semver from 'semver';
import helpers from '../../../helpers';
Expand All @@ -13,7 +12,7 @@ export default function ReleaseNotes() {
const [releaseFetchFailed, setReleaseFetchFailed] = React.useState<boolean>(false);
const [skipFetch, setSkipFetch] = React.useState(false);

// network controller this makes sure if the octockit request is still lying around we
// network controller this makes sure if the github request is still lying around we
// abort it on fetch release skip press button click
const controller = new AbortController();
const signal = controller.signal;
Expand All @@ -29,74 +28,112 @@ export default function ReleaseNotes() {
return;
}

const octokit = new Octokit({
timeout: 10000,
signal,
});

/**
* Fetches latest github release and sets the releaseNotes plus releaseDownloadURL.
*
* Also sets the following state whilst running:
* - fetchingRelease
* - releaseFetchFailed
* - skipFetch
*/
async function fetchRelease() {
// attach a timeout which checks after 5 second of fetching release
// attach a timeout which checks after 5 seconds of fetching release
// if the release request was not successful
const timeoutID = setTimeout(() => {
setFetchingRelease(true);
}, 5000);

const githubReleaseURL = `GET /repos/{owner}/{repo}/releases`;
// get me all the releases -> default decreasing order of releases
const response = await octokit.request(githubReleaseURL, {
owner: 'kinvolk',
repo: 'headlamp',
});

// Get the latest release that is not headlamp-plugin or headlamp-helm.
const latestRelease = response.data.find(
release => !release.name?.startsWith('headlamp-')
);
if (
latestRelease &&
semver.gt(latestRelease.name as string, currentBuildAppVersion) &&
!import.meta.env.FLATPAK_ID
) {
setReleaseDownloadURL(latestRelease.html_url);
}
const githubReleaseURL = `https://api.github.com/repos/kinvolk/headlamp/releases`;

try {
// get all the releases -> default decreasing order of releases
const response = await fetch(githubReleaseURL, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
signal,
});

if (!response.ok) {
throw new Error('Network response was not ok');
}

// check if there is already a version in store if it exists don't store the current version
// this check will help us later in determining whether we are on the latest release or not.
const storedAppVersion = helpers.getAppVersion();
let releaseNotes = '';
if (storedAppVersion && semver.lt(storedAppVersion as string, currentBuildAppVersion)) {
// get the release notes for the version with which the app was built with
const githubReleaseURL = `GET /repos/{owner}/{repo}/releases/tags/v${currentBuildAppVersion}`;
try {
const response = await octokit.request(githubReleaseURL, {
owner: 'kinvolk',
repo: 'headlamp',
});
const [notes] = response.data.body.split('<!-- end-release-notes -->');
if (!!notes) {
releaseNotes = notes;
type GithubRelease = {
name: string;
html_url: string;
body: string;
};

const releases: GithubRelease[] = await response.json();

// Get the latest release that is not headlamp-plugin or headlamp-helm.
const latestRelease = releases.find(
release => !release.name?.startsWith('headlamp-')
);

if (
latestRelease &&
semver.gt(latestRelease.name, currentBuildAppVersion) &&
!import.meta.env.FLATPAK_ID
) {
setReleaseDownloadURL(latestRelease.html_url);
}

// check if there is already a version in store, if it exists don't store the current version
// this check will help us later in determining whether we are on the latest release or not.
const storedAppVersion = helpers.getAppVersion();
let releaseNotes = '';

if (storedAppVersion && semver.lt(storedAppVersion, currentBuildAppVersion)) {
// get the release notes for the version with which the app was built
const tagReleaseURL = `https://api.github.com/repos/kinvolk/headlamp/releases/tags/v${currentBuildAppVersion}`;

try {
const tagResponse = await fetch(tagReleaseURL, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
signal,
});

if (!tagResponse.ok) {
throw new Error('Network response was not ok');
}

const tagData = await tagResponse.json();
const [notes] = tagData.body.split('<!-- end-release-notes -->');
if (notes) {
releaseNotes = notes;
}
} catch (err) {
setReleaseFetchFailed(true);
console.error(
`Error getting release notes for version ${currentBuildAppVersion}:`,
err
);
}
} catch (err) {
setReleaseFetchFailed(true);
console.error(
`Error getting release notes for version ${currentBuildAppVersion}:`,
err
);
}
}

// If all of the above was done before we need to warn the user, we don't need to warn them.
clearTimeout(timeoutID);
setFetchingRelease(false);
// If all of the above was done before we need to warn the user, we don't need to warn them.
clearTimeout(timeoutID);
setFetchingRelease(false);

// set the store version to the current so that we don't show release notes on
// every start of app
helpers.setAppVersion(currentBuildAppVersion);
// set the store version to the current so that we don't show release notes on
// every start of the app
helpers.setAppVersion(currentBuildAppVersion);

// Calling this after setting the version above, so the release notes have the right version
// set when they show it.
if (!!releaseNotes) {
setReleaseNotes(releaseNotes);
// Calling this after setting the version above, so the release notes have the right version
// set when they show it.
if (releaseNotes) {
setReleaseNotes(releaseNotes);
}
} catch (error) {
setReleaseFetchFailed(true);
console.error('Failed to fetch release:', error);
clearTimeout(timeoutID);
setFetchingRelease(false);
}
}

Expand All @@ -123,7 +160,7 @@ export default function ReleaseNotes() {
fetchingRelease={fetchingRelease}
releaseFetchFailed={releaseFetchFailed}
skipUpdateHandler={() => {
// abort the octockit request to fetch github release
// abort the github release fetch
controller.abort();
setSkipFetch(false);
setFetchingRelease(false);
Expand Down
Loading

0 comments on commit 70803c7

Please sign in to comment.