Files
react-native/.github/workflows/verifyVersion.js
T
Olga Zinoveva 26b2bb5343 Add automatic RN version checking workflow (#36075)
Summary:
Adding automatic RN version checking github workflow, which will verify the version of RN listed on all new issues filed in the repository.
Additionally, this change refactors the existing labeler workflow to make it re-usable by the version check workflow. The change also creates a logical place to add future automatic detection checks, like auto-verification of repro, template, etc.
This is technically not new functionality, as the react-native-bot does this _sometimes_, but this should be a lot more reliable.
The logic for valid release checking follows what is listed in the documentation - valid versions are current and N-2 minors, with the highest available patches.

## Changelog

[INTERNAL] [FIXED] - Made the automated RN version checking workflow more reliable

Pull Request resolved: https://github.com/facebook/react-native/pull/36075

Test Plan:
I have verified a variety of different versions on issues here: https://github.com/SlyCaptainFlint/react-native/issues
I have also re-verified all the tags that were previously handled by the labeler workflow, since I have refactored it. Please take a look at both the open and closed issues in the linked repo for examples.

Reviewed By: cortinico

Differential Revision: D43089150

Pulled By: SlyCaptainFlint

fbshipit-source-id: 7da67f5cb2a4875f22e1f9e46d7ca07d43f3e135
2023-02-22 21:26:36 -08:00

118 lines
3.8 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
module.exports = async (github, context) => {
const issue = context.payload.issue;
// Ignore issues using upgrade template (they use a special label)
if (issue.labels.find(label => label.name == 'Type: Upgrade Issue')) {
return;
}
const issueVersionUnparsed =
getReactNativeVersionFromIssueBodyIfExists(issue);
const issueVersion = parseVersionFromString(issueVersionUnparsed);
// Nightly versions are always supported
if (reportedVersionIsNightly(issueVersionUnparsed, issueVersion)) return;
if (!issueVersion) {
return 'Needs: Version Info';
}
// Ensure the version matches one we support
const recentReleases = (
await github.rest.repos.listReleases({
owner: context.repo.owner,
repo: context.repo.repo,
})
).data.map(release => release.name);
const latestRelease = (
await github.rest.repos.getLatestRelease({
owner: context.repo.owner,
repo: context.repo.repo,
})
).data;
const latestVersion = parseVersionFromString(latestRelease.name);
if (!isVersionSupported(issueVersion, latestVersion)) {
return 'Type: Unsupported Version';
}
// We want to encourage users to repro the issue on the highest available patch for the given minor.
const latestPatchForVersion = getLatestPatchForVersion(
issueVersion,
recentReleases,
);
if (latestPatchForVersion > issueVersion.patch) {
return 'Needs: Verify on Latest Version';
}
};
// We support N-2 minor versions, and the latest major.
function isVersionSupported(actualVersion, latestVersion) {
return (
actualVersion.major >= latestVersion.major &&
actualVersion.minor >= latestVersion.minor - 2
);
}
// Assumes that releases are sorted in the order of recency (i.e. most recent releases are earlier in the list)
// This enables us to stop looking as soon as we find the first release with a matching major/minor version, since
// we know it's the most recent release, therefore the highest patch available.
function getLatestPatchForVersion(version, releases) {
for (releaseName of releases) {
const release = parseVersionFromString(releaseName);
if (
release &&
release.major == version.major &&
release.minor == version.minor
) {
return release.patch;
}
}
}
function getReactNativeVersionFromIssueBodyIfExists(issue) {
if (!issue || !issue.body) return;
const rnVersionRegex = /React Native Version[\r\n]+(?<version>.+)[\r\n]*/;
const rnVersionMatch = issue.body.match(rnVersionRegex);
if (!rnVersionMatch || !rnVersionMatch.groups.version) return;
return rnVersionMatch.groups.version;
}
function reportedVersionIsNightly(unparsedVersionString, version) {
if (!unparsedVersionString && !version) return false;
const nightlyRegex = /nightly/i;
const nightlyMatch = unparsedVersionString.match(nightlyRegex);
const versionIsNightly = nightlyMatch && nightlyMatch[0];
const versionIsZero =
version && version.major == 0 && version.minor == 0 && version.patch == 0;
return versionIsZero || versionIsNightly;
}
function parseVersionFromString(version) {
if (!version) return;
// This will match the standard x.x.x semver format, as well as the non-standard prerelease x.x.x-rc.x
const semverRegex =
/(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)(-[rR]{1}[cC]{1}\.(?<prerelease>\d+))?/;
const versionMatch = version.match(semverRegex);
if (!versionMatch) return;
const {major, minor, patch, prerelease} = versionMatch.groups;
return {
major: parseInt(major),
minor: parseInt(minor),
patch: parseInt(patch),
prerelease: parseInt(prerelease),
};
}