forked from del-systems/check-if-version-bumped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
38 lines (32 loc) · 1.37 KB
/
index.js
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
const core = require('@actions/core');
const github = require('@actions/github');
const fetch = require('node-fetch');
const semver = require('semver');
try {
if (github.context.eventName !== 'pull_request') {
core.info('Skipping as it is not pull request');
return;
}
const token = core.getInput('token');
const nodePath = core.getInput('node-path');
const headers = {};
if (token) {
core.info('Using specified token');
headers.Authorization = `token ${token}`;
}
const baseSha = github.context.payload.pull_request.base.sha;
const headSha = github.context.payload.pull_request.head.sha;
core.info(`Comparing ${ headSha } to ${ baseSha }`);
const baseUrl = `https://raw.githubusercontent.com/${ github.context.repo.owner }/${ github.context.repo.repo }/${ baseSha }/${nodePath}package.json`
fetch(baseUrl, { headers })
.then(res => res.json())
.then(res => res.version)
.then(version => {
const localVersion = require(`${ process.env.GITHUB_WORKSPACE }/${nodePath}package.json`).version;
if (!semver.valid(localVersion)) core.setFailed(`Current version '${ localVersion }' detected as invalid one`);
if (!semver.gt(localVersion, version)) core.setFailed(`Version '${ localVersion }' wasn't detected as greater than '${ version }'`);
})
.catch(core.setFailed);
} catch (error) {
core.setFailed(error.message);
}