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
31 changes: 28 additions & 3 deletions .github/workflows/codeball.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,32 @@ name: Codeball
jobs:
codeball_job:
runs-on: ubuntu-latest
name: Run Codeball
name: Codeball
steps:
- name: Codeball AI Actions
uses: sturdy-dev/codeball-action@v1
- name: Checkout source code so we can install the action locally
if: ${{ env.ACT }}
uses: actions/checkout@v2

- name: Baller (local)
if: ${{ env.ACT }}
id: codeball_baller_local
uses: ./baller

- name: Approver (local)
if: ${{ env.ACT }}
id: codeball_approver_local
uses: ./approver
with:
codeball-job-id: ${{ steps.codeball_baller_local.outputs.codeball-job-id }}

- name: Baller (v1)
if: ${{ !env.ACT }}
id: codeball_baller_v1
uses: sturdy-dev/codeball-action/baller@v1

- name: Approver (v1)
if: ${{ !env.ACT }}
id: codeball_approver_v1
uses: sturdy-dev/codeball-action/approver@v1
with:
codeball-job-id: ${{ steps.codeball_baller_v1.outputs.codeball-job-id }}
6 changes: 3 additions & 3 deletions hack/build-beta.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ git add lib dist || true
git commit -m "Build action" --allow-empty

# Use @beta version of the action
ls **/action.yml | xargs -n1 gsed -i 's/@v1/@beta/'
git add **/action.yml || true
find . -type f -name "action.yml" -exec gsed -i 's/@v1/@beta/' {} \;
find . -type f -name "action.yml" -exec git add {} \;
git commit -m "Use beta version" --allow-empty

# Push
git push -u origin beta:beta
git push -u origin beta:beta --force
10 changes: 10 additions & 0 deletions hack/run-e2e-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

set -euo pipefail

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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@
"@actions/github": "^5.0.3",
"@octokit/action": "^3.18.1",
"@octokit/core": "^3.6.0",
"@octokit/plugin-rest-endpoint-methods": "^5.13.0",
"node-fetch": "^3.2.4"
},
"devDependencies": {
"@types/jest": "^27.5.1",
"@types/node": "^16.10.5",
"@typescript-eslint/parser": "^5.26.0",
"@vercel/ncc": "^0.33.4",
"@vercel/ncc": "^0.34.0",
"eslint": "^8.16.0",
"eslint-plugin-github": "^4.3.2",
"eslint-plugin-jest": "^26.2.0",
"eslint-plugin-jest": "^26.4.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "^28.1.0",
"js-yaml": "^4.1.0",
Expand Down
90 changes: 58 additions & 32 deletions src/approver/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ import fetch from 'node-fetch'
import {Job} from './types'
import {isContributionJob, isFinalStatus} from './utils'
import * as core from '@actions/core'
import {Octokit} from '@octokit/action'
import * as github from '@actions/github'
import {Octokit} from './octokit'

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
}

// The import of @actions/github must be a require to work correctly on GitHub Runners
// import * as github from '@actions/github'
const github = require('@actions/github')

async function run(): Promise<void> {
try {
const pullRequestURL = github.context.payload?.pull_request?.html_url
Expand All @@ -31,14 +28,34 @@ async function run(): Promise<void> {
throw new Error('No commit ID found')
}

const repoOwner = github.context.payload.repository?.owner.login
if (!repoOwner) {
throw new Error('No repo owner found')
}

const repoName = github.context.payload.repository?.name
if (!repoName) {
throw new Error('No repo name found')
}

const jobID = core.getInput('codeball-job-id')
if (!jobID) {
throw new Error('No job ID found')
}

const githubToken = core.getInput('GITHUB_TOKEN')
if (!githubToken) {
core.setFailed('No GITHUB_TOKEN found')
return
}

const doApprove = core.getInput('do-approve') === 'true'
const doLabel = core.getInput('do-label') === 'true'
const labelName = core.getInput('label-name')

core.info(`Job ID: ${jobID}`)
core.info(`Do approve: ${doApprove}`)
core.info(`Do label: ${doLabel} with value: ${labelName}`)
core.info(`Configuration: Do approve: ${doApprove}`)
core.info(`Configuration: Do label: ${doLabel} with value: ${labelName}`)

let job = await getJob(jobID)
let attempts = 0
Expand All @@ -62,7 +79,7 @@ async function run(): Promise<void> {

const approved = job.contribution?.result === 'approved'

const octokit = new Octokit()
const octokit = new Octokit({auth: githubToken})

if (approved) {
core.info(`Job ${jobID} is approved, approving the PR now!`)
Expand All @@ -71,8 +88,8 @@ async function run(): Promise<void> {
core.debug(`Adding label "${labelName}" to PR ${pullRequestURL}`)

const existingLabels = await octokit.issues.listLabelsForRepo({
owner: github.context.repo.owner,
repo: github.context.repo.repo
owner: repoOwner,
repo: repoName
})

let haveLabel = false
Expand All @@ -85,30 +102,36 @@ async function run(): Promise<void> {

if (!haveLabel) {
core.info(`Label "${labelName}" does not exist, creating it now`)
await octokit.issues.createLabel({
owner: github.context.repo.owner,
repo: github.context.repo.repo,

const createLabelParams = {
owner: repoOwner,
repo: repoName,
name: labelName,
color: '008E43',
description: 'Codeball approved this pull request'
})
}

core.debug(`Create label: ${JSON.stringify(createLabelParams)}`)
await octokit.issues.createLabel(createLabelParams)
} else {
core.debug(`Label "${labelName}" already exists, will not create it`)
}

await octokit.issues.addLabels({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
const addLabelParams = {
owner: repoOwner,
repo: repoName,
issue_number: pullRequestNumber,
labels: [labelName]
})
}

core.debug(`Add label: ${JSON.stringify(addLabelParams)}`)
await octokit.issues.addLabels(addLabelParams)
}

if (doApprove) {
core.debug(`Approving PR ${pullRequestURL}`)
await octokit.pulls.createReview({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
owner: repoOwner,
repo: repoName,
pull_number: pullRequestNumber,
commit_id: commitId,
body: 'Codeball: LGTM! :+1:',
Expand All @@ -119,17 +142,20 @@ async function run(): Promise<void> {
core.info(`Job ${jobID} is not approved, will not approve the PR`)
}

await core.summary
.addHeading('Codeball')
.addTable([
[
{data: 'Pull Request', header: true},
{data: 'Result', header: true}
],
[`#${pullRequestNumber}`, approved ? 'Approved ✅' : 'Not approved']
])
.addLink('View on web', `https://codeball.ai/prediction/${jobID}`)
.write()
// Create summary if available
if (process.env.GITHUB_STEP_SUMMARY) {
await core.summary
.addHeading('Codeball')
.addTable([
[
{data: 'Pull Request', header: true},
{data: 'Result', header: true}
],
[`#${pullRequestNumber}`, approved ? 'Approved ✅' : 'Not approved']
])
.addLink('View on web', `https://codeball.ai/prediction/${jobID}`)
.write()
}
} catch (error) {
if (error instanceof Error) {
if (error.message === 'Resource not accessible by integration') {
Expand Down
31 changes: 31 additions & 0 deletions src/approver/octokit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {Octokit as Core} from '@octokit/core'
import {paginateRest} from '@octokit/plugin-paginate-rest'
import {legacyRestEndpointMethods} from '@octokit/plugin-rest-endpoint-methods'
// import { OctokitOptions } from "@octokit/core/dist-types/types";
import ProxyAgent from 'proxy-agent'
// export {RestEndpointMethodTypes} from '@octokit/plugin-rest-endpoint-methods'

const DEFAULTS = {
baseUrl: getApiBaseUrl()
}

export const Octokit = Core.plugin(
paginateRest,
legacyRestEndpointMethods
).defaults(function buildDefaults(options: any): any {
return {
...DEFAULTS,
...options,
request: {
agent: new ProxyAgent(),
...options.request
}
}
})

// export type OC = InstanceType<typeof Octokit>

function getApiBaseUrl(): string {
/* istanbul ignore next */
return process.env['GITHUB_API_URL'] || 'https://api.github.com'
}
18 changes: 12 additions & 6 deletions src/baller/main.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
import fetch from 'node-fetch'
import * as core from '@actions/core'
import * as github from '@actions/github'

type JobResponse = {
id: string
status: string
}

async function run(): Promise<void> {
const core = require('@actions/core')

try {
const github = require('@actions/github')
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')
return
}

const githubToken = core.getInput('GITHUB_TOKEN')
if (!githubToken) {
core.setFailed('No GITHUB_TOKEN found')
return
}

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

const data = {
url: pullRequestURL,
access_token: core.getInput('GITHUB_TOKEN')
access_token: githubToken
}

const response = await fetch('https://api.codeball.ai/jobs', {
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}`)
Expand Down
19 changes: 19 additions & 0 deletions tests/act-pull-request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"repository": {
"owner": {
"login": "sturdy-dev"
},
"name": "test-codeball"
},
"pull_request": {
"html_url": "https://github.com/sturdy-dev/test-codeball/pull/12",
"number": 12,
"head": {
"ref": "sample-head-ref",
"sha": "5babb53e3087d4058acefbcd59f307f4cd59cea2"
},
"base": {
"ref": "sample-base-ref"
}
}
}
Loading