aboutsummaryrefslogtreecommitdiffstats
path: root/src/helpers/update-helpers.ts
blob: 3e548c69b96ee463163e552e8407faf0ca8ec188 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { Octokit } from '@octokit/core';
import { type IntlShape, defineMessages } from 'react-intl';

export function getFerdiumVersion(
  currentLocation: string,
  ferdiumVersion: string,
): string {
  const matches = currentLocation.match(/version=([^&]*)/);
  if (matches !== null) {
    return `v${matches[1]}`;
  }
  return `v${ferdiumVersion}`;
}

export function updateVersionParse(updateVersion: string): string {
  return updateVersion === '' ? '' : `?version=${updateVersion}`;
}

export function onAuthGoToReleaseNotes(
  currentLocation: string,
  updateVersionParsed: string = '',
): string {
  return currentLocation.includes('#/auth')
    ? `#/auth/releasenotes${updateVersionParsed}`
    : `#/releasenotes${updateVersionParsed}`;
}

const messages = defineMessages({
  connectionError: {
    id: 'settings.releasenotes.connectionError',
    defaultMessage:
      'An error occured when connecting to Github, please try again later.',
  },
  connectionErrorPageMissing: {
    id: 'settings.releasenotes.connectionErrorPageMissing',
    defaultMessage:
      'An error occured when connecting to Github, the page you are looking for is missing.',
  },
});

export async function getUpdateInfoFromGH(
  currentLocation: string,
  ferdiumVersion: string,
  intl: IntlShape,
): Promise<string> {
  const octokit = new Octokit();
  try {
    const response = await octokit.request(
      'GET /repos/{owner}/{repo}/releases/tags/{tag}',
      {
        owner: 'ferdium',
        repo: 'ferdium-app',
        tag: getFerdiumVersion(currentLocation, ferdiumVersion),
      },
    );

    if (response.status === 200) {
      const json = response.data.body;
      return json || `### ${intl.formatMessage(messages.connectionError)}`;
    }
    return `### ${intl.formatMessage(messages.connectionError)}`;
  } catch {
    return `### ${intl.formatMessage(messages.connectionErrorPageMissing)}`;
  }
}