Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@ node_modules
.idea
# Ignore built ts files
__tests__/runner/*
lib/**/*

8 changes: 5 additions & 3 deletions hack/run-e2e-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

set -euo pipefail

[[ -z $(command -v act) ]] && echo "act not found, install https://github.com/nektos/act" && exit 1
[[ -z ${GITHUB_TOKEN} ]] && echo "GITHUB_TOKEN env variable is required. obtail one from https://github.com/settings/tokens/new" && exit 1

yarn build
yarn package

act pull_request -e tests/act-pull-request.json \
-s GITHUB_TOKEN=${GITHUB_TOKEN} \
--env CODEBALL_API_HOST=http://host.docker.internal:8080
-s GITHUB_TOKEN=${GITHUB_TOKEN} \
--env CODEBALL_API_HOST=http://host.docker.internal:8080
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
"description": "",
"main": "lib/main.js",
"scripts": {
"build": "tsc",
"build": "yarn baller:build && yarn approver:build && yarn status:build && yarn labeler:build",
"format": "prettier --write '**/*.ts'",
"format-check": "prettier --check '**/*.ts'",
"lint": "eslint src/**/*.ts",
"package-baller": "ncc build lib/baller/main.js --out dist/baller --source-map --license licenses.txt",
"package-approver": "ncc build lib/approver/main.js --out dist/approver --source-map --license licenses.txt",
"package-status": "ncc build lib/status/main.js --out dist/status --source-map --license licenses.txt",
"package-labeler": "ncc build lib/labeler/main.js --out dist/labeler --source-map --license licenses.txt",
"package": "yarn package-baller && yarn package-approver && yarn package-status && yarn package-labeler",
"baller:build": "ncc build src/baller/main.ts --out dist/baller --source-map --license licenses.txt",
"approver:build": "ncc build src/approver/main.ts --out dist/approver --source-map --license licenses.txt",
"status:build": "ncc build src/status/main.ts --out dist/status --source-map --license licenses.txt",
"labeler:build": "ncc build src/labeler/main.ts --out dist/labeler --source-map --license licenses.txt",
"test": "jest",
"all": "yarn build && yarn format && yarn lint && yarn package"
"all": "yarn format && yarn lint && yarn build"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion src/approver/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
import {Octokit} from './octokit'
import {Octokit} from '../lib'

async function run(): Promise<void> {
try {
Expand Down
22 changes: 4 additions & 18 deletions src/baller/main.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import fetch from 'node-fetch'
import * as core from '@actions/core'
import * as github from '@actions/github'

type JobResponse = {
id: string
status: string
}
import {create} from '../lib'

async function run(): Promise<void> {
try {
const hostName = process.env.CODEBALL_API_HOST || 'https://api.codeball.ai'

const pullRequestURL = github.context.payload?.pull_request?.html_url
if (!pullRequestURL) {
core.setFailed('No pull request URL found')
Expand All @@ -25,20 +18,13 @@ async function run(): Promise<void> {

core.info(`Found contribution: ${pullRequestURL}`)

const data = {
const job = await create({
url: pullRequestURL,
access_token: githubToken
}

const response = await fetch(`${hostName}/jobs`, {
method: 'POST',
body: JSON.stringify(data)
})

const resData = (await response.json()) as JobResponse

core.info(`Job created: ${resData.id}`)
core.setOutput('codeball-job-id', resData.id)
core.info(`Job created: ${job.id}`)
core.setOutput('codeball-job-id', job.id)
} catch (error) {
if (error instanceof Error) core.setFailed(error.message)
}
Expand Down
2 changes: 1 addition & 1 deletion src/labeler/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
import {Octokit} from './octokit'
import {Octokit} from '../lib'

async function run(): Promise<void> {
try {
Expand Down
29 changes: 0 additions & 29 deletions src/labeler/octokit.ts

This file was deleted.

48 changes: 48 additions & 0 deletions src/lib/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import fetch, {Response} from 'node-fetch'

const BASE_URL = process.env.CODEBALL_API_HOST || 'https://api.codeball.ai'

export class BadRequestError extends Error {
constructor(message: string) {
super(message);
this.name = 'BadRequestError';
}
}

export class NotFoundError extends Error {
constructor() {
super('Not found');
this.name = 'NotFoundError';
}
}

const handleResponse = async (response: Response): Promise<any> => {
if (response.ok) {
return await response.json();
} else if (response.status === 404) {
throw new NotFoundError();
} else if (response.status === 400) {
throw new BadRequestError(await response.text());
} else {
throw new Error(await response.text());
}
};

export const get = async (path: string) =>
fetch(new URL(path, BASE_URL).toString(), {
headers: {
'User-Agent': 'github-actions',
},
redirect: 'follow',
}).then(handleResponse);

export const post = async (path: string, body: any) =>
fetch(new URL(path, BASE_URL).toString(), {
method: 'POST',
body: JSON.stringify(body),
headers: {
'User-Agent': 'github-actions',
'Content-Type': 'application/json'
},
redirect: 'follow',
}).then(handleResponse);
2 changes: 2 additions & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './jobs'
export * from './octokit'
6 changes: 6 additions & 0 deletions src/lib/jobs/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { get as apiGET, post } from '../api';
import type { Job } from './types';

export const get = (id: string): Promise<Job> => apiGET(`/jobs/${id}`);

export const create = ({ url, access_token }: { url: string, access_token: string }): Promise<Job> => post('/jobs', { url, access_token });
3 changes: 3 additions & 0 deletions src/lib/jobs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './types';
export * from './utils';
export * from './api';
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 3 additions & 11 deletions src/status/main.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import fetch from 'node-fetch'
import {Job} from './types'
import {isContributionJob, isFinalStatus} from './utils'
import {isContributionJob, isFinalStatus, get} from '../lib'
import * as core from '@actions/core'

async function getJob(id: string): Promise<Job> {
const res = await fetch(`https://api.codeball.ai/jobs/${id}`)
const data = (await res.json()) as Job
return data
}

async function run(): Promise<void> {
try {
const jobID = core.getInput('codeball-job-id')
Expand All @@ -18,7 +10,7 @@ async function run(): Promise<void> {

core.info(`Job ID: ${jobID}`)

let job = await getJob(jobID)
let job = await get(jobID)
let attempts = 0
const maxAttempts = 60
while (attempts < maxAttempts && !isFinalStatus(job.status)) {
Expand All @@ -27,7 +19,7 @@ async function run(): Promise<void> {
`Waiting for job ${jobID} to complete... (${attempts}/${maxAttempts})`
)
await new Promise(resolve => setTimeout(resolve, 5000))
job = await getJob(jobID)
job = await get(jobID)
}

if (!isFinalStatus(job.status)) {
Expand Down
48 changes: 0 additions & 48 deletions src/status/types.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/status/utils.ts

This file was deleted.

16 changes: 8 additions & 8 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"compilerOptions": {
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"outDir": "./lib", /* Redirect output structure to the directory. */
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"target": "es2015",
"moduleResolution": "node",
"module": "commonjs",
"rootDir": "./src",
"strict": true,
"noImplicitAny": true,
"esModuleInterop": true
},
"exclude": ["node_modules", "src/**/*.test.ts"]
}
}