Skip to content

Commit

Permalink
add vercheck (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
rotemtam authored Oct 26, 2022
1 parent 4342b9e commit 86a011b
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 7 deletions.
4 changes: 2 additions & 2 deletions __tests__/atlas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ describe('run with "latest" flag', () => {
Host: 'test',
Path: '',
RawPath: '',
OmitHost: false,
ForceQuery: false,
RawQuery: 'mode=memory&cache=shared&_fk=1',
Fragment: '',
RawFragment: '',
DSN: 'file:test?mode=memory&cache=shared&_fk=1',
Schema: 'main'
},
// This value is random and changes on every run.
Expand Down Expand Up @@ -187,11 +187,11 @@ describe('run with "latest" flag', () => {
Host: 'test',
Path: '',
RawPath: '',
OmitHost: false,
ForceQuery: false,
RawQuery: 'mode=memory&cache=shared&_fk=1',
Fragment: '',
RawFragment: '',
DSN: 'file:test?mode=memory&cache=shared&_fk=1',
Schema: 'main'
},
Dir: '__tests__/testdata/sqlite-wrong-sum'
Expand Down
7 changes: 4 additions & 3 deletions __tests__/input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Options, OptionsFromEnv, PullReqFromContext } from '../src/input'
import { expect } from '@jest/globals'
import { defaultEnv, defaultVersion } from './env'
import { atlasArgs } from '../src/atlas'
import { Context } from '@actions/github/lib/context'
import * as fs from 'fs/promises'
import path from 'path'

Expand All @@ -25,7 +24,8 @@ describe('input', () => {
'INPUT_ARIGA-TOKEN': 'ariga-token',
'INPUT_ARIGA-URL': 'ariga-url',
INPUT_LATEST: '3',
'INPUT_PROJECT-ENV': 'env'
'INPUT_PROJECT-ENV': 'env',
'INPUT_SKIP-CHECK-FOR-UPDATE': 'true'
})
let expected: Options = {
atlasVersion: 'v0.1.2',
Expand All @@ -36,7 +36,8 @@ describe('input', () => {
arigaToken: 'ariga-token',
arigaURL: 'ariga-url',
latest: 3,
projectEnv: 'env'
projectEnv: 'env',
skipCheckForUpdate: true
}
expect(options).toEqual(expected)
})
Expand Down
32 changes: 32 additions & 0 deletions __tests__/vercheck.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { vercheck, Vercheck } from '../src/vercheck'
import nock from 'nock'
import * as http from '@actions/http-client'
import { expect } from '@jest/globals'

describe('vercheck', function () {
afterEach(() => {
nock.cleanAll()
})
const testCase = async (v: string, payload: string, exp: Vercheck) => {
test(v, async () => {
const scope = nock('https://vercheck.ariga.io')
.get(`/atlas-action/${v}`)
.reply(http.HttpCodes.OK, payload)

const actual = await vercheck(v)
scope.done()
expect(actual).toEqual(exp)
})
}

testCase('v0', '{}', {})
testCase(
'v0.1.0',
'{"latest":{"Version":"v0.2.0","Summary":"","Link":"https://github.com/ariga/atlas-action/releases/tag/v0.2.0"},"advisory":null}',
{
Link: 'https://github.com/ariga/atlas-action/releases/tag/v0.2.0',
Version: 'v0.2.0',
Summary: ''
}
)
})
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ inputs:
token:
description: "GitHub token passed to the action"
default: ${{ github.token }}
skip-check-for-update:
description: 'Set true to skip check for update'
default: false
runs:
using: 'node16'
main: 'dist/index.js'
62 changes: 62 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,9 @@ function OptionsFromEnv(env) {
if (input('token')) {
opts.token = input('token');
}
if (input('skip-check-for-update') == 'true') {
opts.skipCheckForUpdate = true;
}
return opts;
}
exports.OptionsFromEnv = OptionsFromEnv;
Expand Down Expand Up @@ -593,11 +596,27 @@ const github_2 = __nccwpck_require__(5438);
const cloud_1 = __nccwpck_require__(217);
const input_1 = __nccwpck_require__(1044);
const rest_1 = __nccwpck_require__(5375);
const vercheck_1 = __nccwpck_require__(3495);
const commentFooter = 'Migrations automatically reviewed by <a href="https://atlasgo.io/integrations/github-actions">Atlas</a>';
// Entry point for GitHub Action runner.
function run(input) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const ref = process.env.GITHUB_ACTION_REF;
if (!input.opts.skipCheckForUpdate && (ref === null || ref === void 0 ? void 0 : ref.startsWith('v'))) {
try {
const v = yield (0, vercheck_1.vercheck)(ref);
if (v.Version) {
(0, core_1.info)(`Update available for atlas-action: ${JSON.stringify(v)}`);
}
if (v.Advisory) {
(0, core_1.warning)(`Security advisory from atlas-action: ${v.Advisory}`);
}
}
catch (err) {
console.warn(err);
}
}
try {
const bin = yield (0, atlas_1.installAtlas)(input.opts.atlasVersion);
const res = yield (0, atlas_1.runAtlas)(bin, input.opts);
Expand Down Expand Up @@ -645,6 +664,49 @@ run({

/***/ }),

/***/ 3495:
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {

"use strict";

var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.vercheck = void 0;
const http_client_1 = __nccwpck_require__(6255);
function vercheck(cur) {
return __awaiter(this, void 0, void 0, function* () {
const c = new http_client_1.HttpClient('atlas-action');
const resp = yield c.get(`https://vercheck.ariga.io/atlas-action/${cur}`);
if (resp.message.statusCode != 200) {
throw new Error(`vercheck failed: ${resp.message.statusMessage}`);
}
const payload = yield resp.readBody();
const parsed = JSON.parse(payload);
const output = {};
if (parsed.latest != null) {
output.Version = parsed.latest.Version;
output.Link = parsed.latest.Link;
output.Summary = parsed.latest.Summary;
}
if (parsed.advisory != null) {
output.Advisory = parsed.advisory.Text;
}
return output;
});
}
exports.vercheck = vercheck;
//# sourceMappingURL=vercheck.js.map

/***/ }),

/***/ 7351:
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {

Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"devDependencies": {
"@types/jest": "^27.4.1",
"@types/node": "^17.0.23",
"@types/semver": "^7.3.12",
"@typescript-eslint/eslint-plugin": "^5.31.0",
"@typescript-eslint/parser": "^5.31.0",
"@vercel/ncc": "^0.33.3",
Expand Down
4 changes: 4 additions & 0 deletions src/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type Options = {
projectEnv?: string
schemaInsights: boolean
token?: string
skipCheckForUpdate?: boolean
}

export interface PullRequest {
Expand Down Expand Up @@ -88,5 +89,8 @@ export function OptionsFromEnv(env: Dict<string>): Options {
if (input('token')) {
opts.token = input('token')
}
if (input('skip-check-for-update') == 'true') {
opts.skipCheckForUpdate = true
}
return opts
}
17 changes: 16 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import { AtlasResult, ExitCodes, installAtlas, runAtlas } from './atlas'
import { info, setFailed, summary } from '@actions/core'
import { info, warning, setFailed, summary } from '@actions/core'
import { comment, report, summarize } from './github'
import { context } from '@actions/github'
import { reportToCloud } from './cloud'
import { OptionsFromEnv, PullReqFromContext, RunInput } from './input'
import { Octokit } from '@octokit/rest'
import { vercheck } from './vercheck'

const commentFooter =
'Migrations automatically reviewed by <a href="https://atlasgo.io/integrations/github-actions">Atlas</a>'

// Entry point for GitHub Action runner.
export async function run(input: RunInput): Promise<AtlasResult | void> {
const ref = process.env.GITHUB_ACTION_REF
if (!input.opts.skipCheckForUpdate && ref?.startsWith('v')) {
try {
const v = await vercheck(ref)
if (v.Version) {
info(`Update available for atlas-action: ${JSON.stringify(v)}`)
}
if (v.Advisory) {
warning(`Security advisory from atlas-action: ${v.Advisory}`)
}
} catch (err) {
console.warn(err)
}
}
try {
const bin = await installAtlas(input.opts.atlasVersion)
const res = await runAtlas(bin, input.opts)
Expand Down
28 changes: 28 additions & 0 deletions src/vercheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { HttpClient } from '@actions/http-client'

export interface Vercheck {
Version?: string
Summary?: string
Link?: string
Advisory?: string
}

export async function vercheck(cur: string): Promise<Vercheck> {
const c = new HttpClient('atlas-action')
const resp = await c.get(`https://vercheck.ariga.io/atlas-action/${cur}`)
if (resp.message.statusCode != 200) {
throw new Error(`vercheck failed: ${resp.message.statusMessage}`)
}
const payload = await resp.readBody()
const parsed = JSON.parse(payload)
const output: Vercheck = {}
if (parsed.latest != null) {
output.Version = parsed.latest.Version
output.Link = parsed.latest.Link
output.Summary = parsed.latest.Summary
}
if (parsed.advisory != null) {
output.Advisory = parsed.advisory.Text
}
return output
}

0 comments on commit 86a011b

Please sign in to comment.