Skip to content

Commit

Permalink
Handle latest Octokit definitions (#78)
Browse files Browse the repository at this point in the history
Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max and crazy-max authored Jun 6, 2021
1 parent 3e1bfa4 commit f8607a6
Show file tree
Hide file tree
Showing 14 changed files with 1,267 additions and 3,906 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ jobs:
uses: crazy-max/ghaction-virustotal@v2
with:
vt_api_key: ${{ secrets.VT_API_KEY }}
github_token: ${{ github.token }}
files: |
.exe$
```
Expand Down
53 changes: 53 additions & 0 deletions __tests__/context.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import * as context from '../src/context';

describe('getInputList', () => {
it('handles single line correctly', async () => {
await setInput('foo', 'bar');
const res = await context.getInputList('foo');
console.log(res);
expect(res).toEqual(['bar']);
});

it('handles multiple lines correctly', async () => {
setInput('foo', 'bar\nbaz');
const res = await context.getInputList('foo');
console.log(res);
expect(res).toEqual(['bar', 'baz']);
});

it('handles comma correctly', async () => {
setInput('foo', 'bar,baz');
const res = await context.getInputList('foo');
console.log(res);
expect(res).toEqual(['bar', 'baz']);
});

it('handles different new lines correctly', async () => {
setInput('foo', 'bar\r\nbaz');
const res = await context.getInputList('foo');
console.log(res);
expect(res).toEqual(['bar', 'baz']);
});

it('handles different new lines and comma correctly', async () => {
setInput('foo', 'bar\r\nbaz,bat');
const res = await context.getInputList('foo');
console.log(res);
expect(res).toEqual(['bar', 'baz', 'bat']);
});
});

describe('resolvePaths', () => {
it('resolves files given a set of paths', async () => {
expect(context.resolvePaths(['tests/data/**/*'])).toEqual(['tests/data/foo/bar.txt']);
});
});

// See: https://github.com/actions/toolkit/blob/master/packages/core/src/core.ts#L67
function getInputName(name: string): string {
return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`;
}

function setInput(name: string, value: string): void {
process.env[getInputName(name)] = value;
}
25 changes: 25 additions & 0 deletions __tests__/fixtures/repo.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
GITHUB_ACTION=crazy-maxghaction-dump-context
GITHUB_ACTIONS=true
GITHUB_ACTION_PATH=/home/runner/work/_actions/crazy-max/ghaction-dump-context/v1
GITHUB_ACTION_REF=
GITHUB_ACTION_REPOSITORY=
GITHUB_ACTOR=crazy-max
GITHUB_API_URL=https://api.github.com
GITHUB_BASE_REF=
GITHUB_ENV=/home/runner/work/_temp/_runner_file_commands/set_env_a9bb29d9-c530-4234-a6bc-c19f2e166b64
GITHUB_EVENT_NAME=push
GITHUB_EVENT_PATH=./__tests__/fixtures/repo.json
GITHUB_GRAPHQL_URL=https://api.github.com/graphql
GITHUB_HEAD_REF=
GITHUB_JOB=dump
GITHUB_PATH=/home/runner/work/_temp/_runner_file_commands/add_path_a9bb29d9-c530-4234-a6bc-c19f2e166b64
GITHUB_REF=refs/heads/master
GITHUB_REPOSITORY=portapps/brave-portable
GITHUB_REPOSITORY_OWNER=portapps
GITHUB_RETENTION_DAYS=90
GITHUB_RUN_ID=911271838
GITHUB_RUN_NUMBER=1
GITHUB_SERVER_URL=https://github.com
GITHUB_SHA=411b28b73d17742f4afd55a46ef2c7483a760e5f
GITHUB_WORKFLOW=dump
GITHUB_WORKSPACE=/home/runner/work/brave-portable/brave-portable
191 changes: 191 additions & 0 deletions __tests__/fixtures/repo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
{
"after": "411b28b73d17742f4afd55a46ef2c7483a760e5f",
"base_ref": null,
"before": "8c7ad624f3de94e75410d30add4544a401e2d32a",
"commits": [
{
"author": {
"email": "crazy-max@users.noreply.github.com",
"name": "CrazyMax",
"username": "crazy-max"
},
"committer": {
"email": "crazy-max@users.noreply.github.com",
"name": "CrazyMax",
"username": "crazy-max"
},
"distinct": true,
"id": "411b28b73d17742f4afd55a46ef2c7483a760e5f",
"message": "Dump context",
"timestamp": "2021-06-06T10:29:21+02:00",
"tree_id": "67fc290b8438de3c56fad0ec5abd0ba9fb2be878",
"url": "https://github.com/portapps/brave-portable/commit/411b28b73d17742f4afd55a46ef2c7483a760e5f"
}
],
"compare": "https://github.com/portapps/brave-portable/compare/8c7ad624f3de...411b28b73d17",
"created": false,
"deleted": false,
"forced": false,
"head_commit": {
"author": {
"email": "crazy-max@users.noreply.github.com",
"name": "CrazyMax",
"username": "crazy-max"
},
"committer": {
"email": "crazy-max@users.noreply.github.com",
"name": "CrazyMax",
"username": "crazy-max"
},
"distinct": true,
"id": "411b28b73d17742f4afd55a46ef2c7483a760e5f",
"message": "Dump context",
"timestamp": "2021-06-06T10:29:21+02:00",
"tree_id": "67fc290b8438de3c56fad0ec5abd0ba9fb2be878",
"url": "https://github.com/portapps/brave-portable/commit/411b28b73d17742f4afd55a46ef2c7483a760e5f"
},
"organization": {
"avatar_url": "https://avatars.githubusercontent.com/u/33734063?v=4",
"description": "Collection of portable apps for Windows",
"events_url": "https://api.github.com/orgs/portapps/events",
"hooks_url": "https://api.github.com/orgs/portapps/hooks",
"id": 33734063,
"issues_url": "https://api.github.com/orgs/portapps/issues",
"login": "portapps",
"members_url": "https://api.github.com/orgs/portapps/members{/member}",
"node_id": "MDEyOk9yZ2FuaXphdGlvbjMzNzM0MDYz",
"public_members_url": "https://api.github.com/orgs/portapps/public_members{/member}",
"repos_url": "https://api.github.com/orgs/portapps/repos",
"url": "https://api.github.com/orgs/portapps"
},
"pusher": {
"email": "1951866+crazy-max@users.noreply.github.com",
"name": "crazy-max"
},
"ref": "refs/heads/master",
"repository": {
"archive_url": "https://api.github.com/repos/portapps/brave-portable/{archive_format}{/ref}",
"archived": false,
"assignees_url": "https://api.github.com/repos/portapps/brave-portable/assignees{/user}",
"blobs_url": "https://api.github.com/repos/portapps/brave-portable/git/blobs{/sha}",
"branches_url": "https://api.github.com/repos/portapps/brave-portable/branches{/branch}",
"clone_url": "https://github.com/portapps/brave-portable.git",
"collaborators_url": "https://api.github.com/repos/portapps/brave-portable/collaborators{/collaborator}",
"comments_url": "https://api.github.com/repos/portapps/brave-portable/comments{/number}",
"commits_url": "https://api.github.com/repos/portapps/brave-portable/commits{/sha}",
"compare_url": "https://api.github.com/repos/portapps/brave-portable/compare/{base}...{head}",
"contents_url": "https://api.github.com/repos/portapps/brave-portable/contents/{+path}",
"contributors_url": "https://api.github.com/repos/portapps/brave-portable/contributors",
"created_at": 1493828252,
"default_branch": "master",
"deployments_url": "https://api.github.com/repos/portapps/brave-portable/deployments",
"description": "🚀 Brave web browser portable for Windows",
"disabled": false,
"downloads_url": "https://api.github.com/repos/portapps/brave-portable/downloads",
"events_url": "https://api.github.com/repos/portapps/brave-portable/events",
"fork": false,
"forks": 14,
"forks_count": 14,
"forks_url": "https://api.github.com/repos/portapps/brave-portable/forks",
"full_name": "portapps/brave-portable",
"git_commits_url": "https://api.github.com/repos/portapps/brave-portable/git/commits{/sha}",
"git_refs_url": "https://api.github.com/repos/portapps/brave-portable/git/refs{/sha}",
"git_tags_url": "https://api.github.com/repos/portapps/brave-portable/git/tags{/sha}",
"git_url": "git://github.com/portapps/brave-portable.git",
"has_downloads": true,
"has_issues": true,
"has_pages": false,
"has_projects": false,
"has_wiki": false,
"homepage": "https://portapps.io/app/brave-portable/",
"hooks_url": "https://api.github.com/repos/portapps/brave-portable/hooks",
"html_url": "https://github.com/portapps/brave-portable",
"id": 90169309,
"issue_comment_url": "https://api.github.com/repos/portapps/brave-portable/issues/comments{/number}",
"issue_events_url": "https://api.github.com/repos/portapps/brave-portable/issues/events{/number}",
"issues_url": "https://api.github.com/repos/portapps/brave-portable/issues{/number}",
"keys_url": "https://api.github.com/repos/portapps/brave-portable/keys{/key_id}",
"labels_url": "https://api.github.com/repos/portapps/brave-portable/labels{/name}",
"language": "Go",
"languages_url": "https://api.github.com/repos/portapps/brave-portable/languages",
"license": {
"key": "mit",
"name": "MIT License",
"node_id": "MDc6TGljZW5zZTEz",
"spdx_id": "MIT",
"url": "https://api.github.com/licenses/mit"
},
"master_branch": "master",
"merges_url": "https://api.github.com/repos/portapps/brave-portable/merges",
"milestones_url": "https://api.github.com/repos/portapps/brave-portable/milestones{/number}",
"mirror_url": null,
"name": "brave-portable",
"node_id": "MDEwOlJlcG9zaXRvcnk5MDE2OTMwOQ==",
"notifications_url": "https://api.github.com/repos/portapps/brave-portable/notifications{?since,all,participating}",
"open_issues": 5,
"open_issues_count": 5,
"organization": "portapps",
"owner": {
"avatar_url": "https://avatars.githubusercontent.com/u/33734063?v=4",
"email": "contact@portapps.io",
"events_url": "https://api.github.com/users/portapps/events{/privacy}",
"followers_url": "https://api.github.com/users/portapps/followers",
"following_url": "https://api.github.com/users/portapps/following{/other_user}",
"gists_url": "https://api.github.com/users/portapps/gists{/gist_id}",
"gravatar_id": "",
"html_url": "https://github.com/portapps",
"id": 33734063,
"login": "portapps",
"name": "portapps",
"node_id": "MDEyOk9yZ2FuaXphdGlvbjMzNzM0MDYz",
"organizations_url": "https://api.github.com/users/portapps/orgs",
"received_events_url": "https://api.github.com/users/portapps/received_events",
"repos_url": "https://api.github.com/users/portapps/repos",
"site_admin": false,
"starred_url": "https://api.github.com/users/portapps/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/portapps/subscriptions",
"type": "Organization",
"url": "https://api.github.com/users/portapps"
},
"private": false,
"pulls_url": "https://api.github.com/repos/portapps/brave-portable/pulls{/number}",
"pushed_at": 1622968164,
"releases_url": "https://api.github.com/repos/portapps/brave-portable/releases{/id}",
"size": 617,
"ssh_url": "git@github.com:portapps/brave-portable.git",
"stargazers": 143,
"stargazers_count": 143,
"stargazers_url": "https://api.github.com/repos/portapps/brave-portable/stargazers",
"statuses_url": "https://api.github.com/repos/portapps/brave-portable/statuses/{sha}",
"subscribers_url": "https://api.github.com/repos/portapps/brave-portable/subscribers",
"subscription_url": "https://api.github.com/repos/portapps/brave-portable/subscription",
"svn_url": "https://github.com/portapps/brave-portable",
"tags_url": "https://api.github.com/repos/portapps/brave-portable/tags",
"teams_url": "https://api.github.com/repos/portapps/brave-portable/teams",
"trees_url": "https://api.github.com/repos/portapps/brave-portable/git/trees{/sha}",
"updated_at": "2021-06-03T19:30:35Z",
"url": "https://github.com/portapps/brave-portable",
"watchers": 143,
"watchers_count": 143
},
"sender": {
"avatar_url": "https://avatars.githubusercontent.com/u/1951866?v=4",
"events_url": "https://api.github.com/users/crazy-max/events{/privacy}",
"followers_url": "https://api.github.com/users/crazy-max/followers",
"following_url": "https://api.github.com/users/crazy-max/following{/other_user}",
"gists_url": "https://api.github.com/users/crazy-max/gists{/gist_id}",
"gravatar_id": "",
"html_url": "https://github.com/crazy-max",
"id": 1951866,
"login": "crazy-max",
"node_id": "MDQ6VXNlcjE5NTE4NjY=",
"organizations_url": "https://api.github.com/users/crazy-max/orgs",
"received_events_url": "https://api.github.com/users/crazy-max/received_events",
"repos_url": "https://api.github.com/users/crazy-max/repos",
"site_admin": false,
"starred_url": "https://api.github.com/users/crazy-max/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/crazy-max/subscriptions",
"type": "User",
"url": "https://api.github.com/users/crazy-max"
}
}
69 changes: 47 additions & 22 deletions __tests__/github.test.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,75 @@
import * as githubm from '../src/github';
import * as utilm from '../src/util';
import {asset} from '../src/virustotal';
import * as github from '@actions/github';
import * as fs from 'fs';
import * as path from 'path';
import * as dotenv from 'dotenv';
import * as context from '../src/context';
import * as github from '../src/github';
import {asset} from '../src/virustotal';
import {Context} from '@actions/github/lib/context';

let octokit;

jest.spyOn(context, 'tmpDir').mockImplementation((): string => {
const tmpDir = path.join('/tmp/.ghaction-virustotal-jest').split(path.sep).join(path.posix.sep);
if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir, {recursive: true});
}
return tmpDir;
});

const context: utilm.Context = {
github_owner: 'crazy-max',
github_repo: 'ghaction-virustotal-test',
github_ref: 'v1.0.0'
};
jest.spyOn(github, 'context').mockImplementation(
(): Context => {
return new Context();
}
);

const octokit = github.getOctokit(process.env.GITHUB_TOKEN || '');
beforeEach(() => {
Object.keys(process.env).forEach(function (key) {
if (key !== 'GITHUB_TOKEN' && key.startsWith('GITHUB_')) {
delete process.env[key];
}
});
const repoEnv = dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures', 'repo.env')));
for (const k in repoEnv) {
process.env[k] = repoEnv[k];
}
octokit = github.getOctokit(process.env.GITHUB_TOKEN || '', {
log: console
});
});

describe('github', () => {
let release: githubm.Release;
let assets: Array<githubm.ReleaseAsset>;
let release: github.Release;
let assets: Array<github.ReleaseAsset>;

it('returns a GitHub release', async () => {
release = await githubm.getRelease(octokit, context);
release = await github.getRelease(octokit, '1.20.110-71');
expect(release).not.toBeUndefined();
expect(release.id).toEqual(26057680);
expect(release.id).toEqual(38769571);
});

it('returns a GitHub release assets list', async () => {
if (!release) {
release = await githubm.getRelease(octokit, context);
release = await github.getRelease(octokit, '1.20.110-71');
}

assets = await githubm.getReleaseAssets(octokit, context, release, ['ghaction-virustotal-(win32|win64).exe']);
assets = await github.getReleaseAssets(octokit, release, ['brave-portable-(win32|win64).exe']);
expect(release).not.toBeUndefined();
expect(assets.length).toEqual(2);
expect(assets[0].name).toEqual('ghaction-virustotal-win32.exe');
expect(assets[1].name).toEqual('ghaction-virustotal-win64.exe');
expect(assets[0].name).toEqual('brave-portable-win32.exe');
expect(assets[1].name).toEqual('brave-portable-win64.exe');
});

it('download a GitHub release asset', async () => {
if (!release) {
release = await githubm.getRelease(octokit, context);
release = await github.getRelease(octokit, '1.20.110-71');
}
if (!assets) {
assets = await githubm.getReleaseAssets(octokit, context, release, ['ghaction-virustotal-(win32|win64).exe']);
assets = await github.getReleaseAssets(octokit, release, ['brave-portable-(win32|win64).exe']);
}

const releaseAsset = await githubm.downloadReleaseAsset(octokit, context, assets[0], path.join(utilm.tmpDir(), assets[0].name));
const releaseAsset = await github.downloadReleaseAsset(octokit, assets[0], path.join(context.tmpDir(), assets[0].name));
const {name, size, mime, file} = asset(releaseAsset);
expect(size).toEqual(1306112);
expect(size).toEqual(4391936);
expect(mime).toEqual('application/octet-stream');
});
});
Loading

0 comments on commit f8607a6

Please sign in to comment.