aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/datasource/metadata.ts
blob: 5a056283a9ab09295634bb6bd442eedf5043dbd6 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import URL from 'url';
import is from '@sindresorhus/is';
import parse from 'github-url-from-git';
import * as hostRules from '../util/host-rules';
import { ReleaseResult } from './common';

// Use this object to define changelog URLs for packages
// Only necessary when the changelog data cannot be found in the package's source repository
const manualChangelogUrls = {
  npm: {
    'babel-preset-react-app':
      'https://github.com/facebook/create-react-app/releases',
    firebase: 'https://firebase.google.com/support/release-notes/js',
    'flow-bin': 'https://github.com/facebook/flow/blob/master/Changelog.md',
    gatsby:
      'https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/CHANGELOG.md',
    'react-native':
      'https://github.com/react-native-community/react-native-releases/blob/master/CHANGELOG.md',
  },
  pypi: {
    'pytest-django':
      'https://pytest-django.readthedocs.io/en/latest/changelog.html#changelog',
    django: 'https://github.com/django/django/tree/master/docs/releases',
    djangorestframework:
      'https://www.django-rest-framework.org/community/release-notes/',
    flake8: 'http://flake8.pycqa.org/en/latest/release-notes/index.html',
    'django-storages':
      'https://github.com/jschneier/django-storages/blob/master/CHANGELOG.rst',
    phonenumbers:
      'https://github.com/daviddrysdale/python-phonenumbers/blob/dev/python/HISTORY.md',
    'psycopg2-binary': 'http://initd.org/psycopg/articles/tag/release/',
    'django-debug-toolbar':
      'https://django-debug-toolbar.readthedocs.io/en/latest/changes.html',
    'firebase-admin':
      'https://firebase.google.com/support/release-notes/admin/python',
    requests:
      'http://docs.python-requests.org/en/master/community/updates/#release-and-version-history',
    wagtail: 'https://github.com/wagtail/wagtail/tree/master/docs/releases',
  },
};

// Use this object to define manual source URLs for packages
// Only necessary if the datasource is unable to locate the source URL itself
const manualSourceUrls = {
  orb: {
    'cypress-io/cypress': 'https://github.com/cypress-io/circleci-orb',
    'hutson/library-release-workflows':
      'https://github.com/hyper-expanse/library-release-workflows',
  },
  docker: {
    node: 'https://github.com/nodejs/node',
  },
  kubernetes: {
    node: 'https://github.com/nodejs/node',
  },
  npm: {
    node: 'https://github.com/nodejs/node',
  },
  nvm: {
    node: 'https://github.com/nodejs/node',
  },
  pypi: {
    mkdocs: 'https://github.com/mkdocs/mkdocs',
  },
};

/* eslint-disable no-param-reassign */
export function addMetaData(
  dep?: ReleaseResult,
  datasource?: string,
  lookupName?: string
): void {
  if (!dep) {
    return;
  }
  const lookupNameLowercase = lookupName ? lookupName.toLowerCase() : null;
  if (
    manualChangelogUrls[datasource] &&
    manualChangelogUrls[datasource][lookupNameLowercase]
  ) {
    dep.changelogUrl = manualChangelogUrls[datasource][lookupNameLowercase];
  }
  if (
    manualSourceUrls[datasource] &&
    manualSourceUrls[datasource][lookupNameLowercase]
  ) {
    dep.sourceUrl = manualSourceUrls[datasource][lookupNameLowercase];
  }

  /**
   * @param {string} url
   */
  const massageGithubUrl = (url: string): string => {
    return url
      .replace('http:', 'https:')
      .replace(/^git:\/?\/?/, 'https://')
      .replace('www.github.com', 'github.com')
      .split('/')
      .slice(0, 5)
      .join('/');
  };
  /**
   * @param {string} url
   */
  const massageGitlabUrl = (url: string): string => {
    return url
      .replace('http:', 'https:')
      .replace(/^git:\/?\/?/, 'https://')
      .replace(/\/tree\/.*$/i, '')
      .replace(/\/$/i, '')
      .replace('.git', '');
  };

  if (
    dep.changelogUrl?.includes('github.com') && // lgtm [js/incomplete-url-substring-sanitization]
    !dep.sourceUrl
  ) {
    dep.sourceUrl = dep.changelogUrl;
  }
  // prettier-ignore
  if (dep.homepage?.includes('github.com')) { // lgtm [js/incomplete-url-substring-sanitization]
    if (!dep.sourceUrl) {
      dep.sourceUrl = dep.homepage;
    }
    delete dep.homepage;
  }
  const extraBaseUrls = [];
  // istanbul ignore next
  hostRules.hosts({ hostType: 'github' }).forEach((host) => {
    extraBaseUrls.push(host, `gist.${host}`);
  });
  extraBaseUrls.push('gitlab.com');
  if (dep.sourceUrl) {
    const parsedUrl = URL.parse(dep.sourceUrl);
    if (parsedUrl?.hostname) {
      let massagedUrl;
      if (parsedUrl.hostname.includes('gitlab')) {
        massagedUrl = massageGitlabUrl(dep.sourceUrl);
      } else {
        massagedUrl = massageGithubUrl(dep.sourceUrl);
      }
      // try massaging it
      dep.sourceUrl =
        parse(massagedUrl, {
          extraBaseUrls,
        }) || dep.sourceUrl;
    } else {
      delete dep.sourceUrl;
    }
  }

  // Clean up any empty urls
  const urls = ['homepage', 'sourceUrl', 'changelogUrl'];
  for (const url of urls) {
    if (is.nonEmptyString(dep[url])) {
      dep[url] = dep[url].trim();
      // istanbul ignore if
      if (!dep[url].match(/^https?:\/\//)) {
        delete dep[url];
      }
    } else {
      delete dep[url];
    }
  }
}