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
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"@typescript-eslint/semi": ["error", "never"],
"@typescript-eslint/type-annotation-spacing": "error",
"@typescript-eslint/unbound-method": "error",
"no-console": "off"
"no-console": "off",
"import/no-commonjs": "off"
},
"env": {
"node": true,
Expand Down
32 changes: 20 additions & 12 deletions src/approver/main.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import fetch from 'node-fetch'
import {Job} from './types'
import { isContributionJob, isFinalStatus } from "./utils";
import {isContributionJob, isFinalStatus} from './utils'
import * as core from '@actions/core'
import {Octokit} from '@octokit/action'

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

const pullRequestURL = github.context.payload?.pull_request?.html_url
if (!pullRequestURL) {
throw new Error('No pull request URL found')
Expand Down Expand Up @@ -61,6 +63,7 @@ async function run(): Promise<void> {
const approved = job.contribution?.result === 'approved'

const octokit = new Octokit()

if (approved) {
core.info(`Job ${jobID} is approved, approving the PR now!`)

Expand All @@ -81,9 +84,7 @@ async function run(): Promise<void> {
}

if (!haveLabel) {
core.info(
`Label "${labelName}" does not exist, creating it now`
)
core.info(`Label "${labelName}" does not exist, creating it now`)
await octokit.issues.createLabel({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
Expand All @@ -92,9 +93,7 @@ async function run(): Promise<void> {
description: 'Codeball approved this pull request'
})
} else {
core.debug(
`Label "${labelName}" already exists, will not create it`
)
core.debug(`Label "${labelName}" already exists, will not create it`)
}

await octokit.issues.addLabels({
Expand Down Expand Up @@ -132,7 +131,16 @@ async function run(): Promise<void> {
.addLink('View on web', `https://codeball.ai/prediction/${jobID}`)
.write()
} catch (error) {
if (error instanceof Error) core.setFailed(error.message)
if (error instanceof Error) {
if (error.message === 'Resource not accessible by integration') {
core.error(
'Codeball Approver failed to access GitHub. Check the "GITHUB_TOKEN Permissions" of this job and make sure that the job has WRITE permissions to Pull Requests.'
)
core.error(error)
} else {
core.setFailed(error.message)
}
}
}
}

Expand Down