Skip to content

Commit 82984b0

Browse files
committed
Merge branch 'main' into v2
2 parents 0887d5f + 44f2f31 commit 82984b0

File tree

11 files changed

+244
-196
lines changed

11 files changed

+244
-196
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,54 @@ jobs:
181181
</details>
182182
183183
184+
### Example:
185+
186+
<details>
187+
<summary>▶️ codeball-fail-not-approved.yml</summary>
188+
189+
```yaml
190+
on: [pull_request]
191+
192+
permissions:
193+
contents: read
194+
issues: write
195+
pull-requests: write
196+
197+
jobs:
198+
codeball:
199+
runs-on: ubuntu-latest
200+
name: Codeball
201+
steps:
202+
# Start a new Codeball review job
203+
# This step is asynchronous and will return a job id
204+
- name: Trigger Codeball
205+
id: codeball_baller
206+
uses: sturdy-dev/codeball-action/baller@v2
207+
208+
# Wait for Codeball to return the status
209+
- name: Get Status
210+
id: codeball_status
211+
uses: sturdy-dev/codeball-action/status@v2
212+
with:
213+
codeball-job-id: ${{ steps.codeball_baller.outputs.codeball-job-id }}
214+
215+
# If Codeball approved the contribution, approve the PR
216+
- name: Approve PR
217+
uses: sturdy-dev/codeball-action/approver@v2
218+
if: ${{ steps.codeball_status.outputs.approved == 'true' }}
219+
with:
220+
message: 'Codeball: LGTM! :+1:'
221+
222+
# If Codeball didn't approve the contribution, fail the job.
223+
- name: Fail Job
224+
if: ${{ steps.codeball_status.outputs.approved == 'fail' }}
225+
run: |
226+
echo "Not approved"
227+
exit 1
228+
```
229+
</details>
230+
231+
184232
## Building Blocks
185233
186234
The Codeball sub-actions are:
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
on: [pull_request]
2+
3+
permissions:
4+
contents: read
5+
issues: write
6+
pull-requests: write
7+
8+
jobs:
9+
codeball:
10+
runs-on: ubuntu-latest
11+
name: Codeball
12+
steps:
13+
# Start a new Codeball review job
14+
# This step is asynchronous and will return a job id
15+
- name: Trigger Codeball
16+
id: codeball_baller
17+
uses: sturdy-dev/codeball-action/baller@v2
18+
19+
# Wait for Codeball to return the status
20+
- name: Get Status
21+
id: codeball_status
22+
uses: sturdy-dev/codeball-action/status@v2
23+
with:
24+
codeball-job-id: ${{ steps.codeball_baller.outputs.codeball-job-id }}
25+
26+
# If Codeball approved the contribution, approve the PR
27+
- name: Approve PR
28+
uses: sturdy-dev/codeball-action/approver@v2
29+
if: ${{ steps.codeball_status.outputs.approved == 'true' }}
30+
31+
# If Codeball didn't approve the contribution, fail the job.
32+
- name: Fail Job
33+
if: ${{ steps.codeball_status.outputs.approved == 'fail' }}
34+
run: |
35+
echo "Not approved"
36+
exit 1

hack/build-beta.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ git branch -D beta || true
88

99
git checkout -b beta
1010

11+
rm -rf lib dist
12+
1113
# Build
1214
yarn build
13-
yarn package
14-
git add lib dist || true
15+
git add dist || true
1516
git commit -m "Build action" --allow-empty
1617

1718
# Use @beta version of the action
18-
find . -type f -name "action.yml" -exec gsed -i 's/@v1/@beta/' {} \;
19+
find . -type f -name "action.yml" -exec gsed -i 's/@v2/@beta/' {} \;
1920
find . -type f -name "action.yml" -exec git add {} \;
2021
git commit -m "Use beta version" --allow-empty
2122

src/approver/main.ts

Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,51 @@
11
import * as core from '@actions/core'
22
import * as github from '@actions/github'
3-
import {Octokit} from '../lib'
3+
import {Octokit, optional, required} from '../lib'
44
import {track} from '../lib/track/track'
55

6+
const jobID = optional('codeball-job-id')
7+
68
async function run(): Promise<void> {
7-
try {
8-
const pullRequestURL = github.context.payload?.pull_request?.html_url
9-
if (!pullRequestURL) {
10-
throw new Error('No pull request URL found')
11-
}
9+
const pullRequestURL = github.context.payload?.pull_request?.html_url
10+
if (!pullRequestURL) throw new Error('No pull request URL found')
1211

13-
const pullRequestNumber = github.context.payload?.pull_request?.number
14-
if (!pullRequestNumber) {
15-
throw new Error('No pull request number found')
16-
}
12+
const pullRequestNumber = github.context.payload?.pull_request?.number
13+
if (!pullRequestNumber) throw new Error('No pull request number found')
1714

18-
const commitId = github.context.payload.pull_request?.head.sha
19-
if (!commitId) {
20-
throw new Error('No commit ID found')
21-
}
15+
const commitId = github.context.payload.pull_request?.head.sha
16+
if (!commitId) throw new Error('No commit ID found')
2217

23-
const repoOwner = github.context.payload.repository?.owner.login
24-
if (!repoOwner) {
25-
throw new Error('No repo owner found')
26-
}
18+
const repoOwner = github.context.payload.repository?.owner.login
19+
if (!repoOwner) throw new Error('No repo owner found')
2720

28-
const repoName = github.context.payload.repository?.name
29-
if (!repoName) {
30-
throw new Error('No repo name found')
31-
}
21+
const repoName = github.context.payload.repository?.name
22+
if (!repoName) throw new Error('No repo name found')
3223

33-
const githubToken = core.getInput('GITHUB_TOKEN')
34-
if (!githubToken) {
35-
core.setFailed('No GITHUB_TOKEN found')
36-
return
37-
}
24+
const githubToken = required('GITHUB_TOKEN')
3825

39-
const jobID = core.getInput('codeball-job-id') // jobID is not required
26+
const octokit = new Octokit({auth: githubToken})
4027

41-
const octokit = new Octokit({auth: githubToken})
42-
43-
await octokit.pulls.createReview({
44-
owner: repoOwner,
45-
repo: repoName,
46-
pull_number: pullRequestNumber,
47-
commit_id: commitId,
48-
body: 'Codeball: LGTM! :+1:',
49-
event: 'APPROVE'
50-
})
28+
await octokit.pulls.createReview({
29+
owner: repoOwner,
30+
repo: repoName,
31+
pull_number: pullRequestNumber,
32+
commit_id: commitId,
33+
body: 'Codeball: LGTM! :+1:',
34+
event: 'APPROVE'
35+
})
36+
}
5137

52-
await track(jobID, 'approver')
53-
} catch (error) {
38+
run()
39+
.then(() => track({jobID, actionName: 'approver'}))
40+
.catch(error => {
5441
if (error instanceof Error) {
42+
track({jobID, actionName: 'approver', error: error.message})
5543
if (error.message === 'Resource not accessible by integration') {
56-
core.error(
44+
core.setFailed(
5745
'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.'
5846
)
59-
core.error(error)
6047
} else {
6148
core.setFailed(error.message)
6249
}
6350
}
64-
}
65-
}
66-
67-
run()
51+
})

src/baller/main.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,32 @@ import * as github from '@actions/github'
33
import {create} from '../lib'
44
import {track} from '../lib/track/track'
55

6-
async function run(): Promise<void> {
7-
try {
8-
const pullRequestURL = github.context.payload?.pull_request?.html_url
9-
if (!pullRequestURL) {
10-
core.setFailed('No pull request URL found')
11-
return
12-
}
13-
14-
const githubToken = core.getInput('GITHUB_TOKEN')
15-
if (!githubToken) {
16-
core.setFailed('No GITHUB_TOKEN found')
17-
return
18-
}
6+
async function run(): Promise<{jobId: string}> {
7+
const pullRequestURL = github.context.payload?.pull_request?.html_url
8+
if (!pullRequestURL) throw new Error('No pull request URL found')
199

20-
core.info(`Found contribution: ${pullRequestURL}`)
10+
const githubToken = core.getInput('GITHUB_TOKEN')
11+
if (!githubToken) throw new Error('No GitHub token found')
2112

22-
const job = await create({
23-
url: pullRequestURL,
24-
access_token: githubToken
25-
})
13+
core.info(`Found contribution: ${pullRequestURL}`)
2614

27-
core.info(`Job created: ${job.id}`)
28-
core.setOutput('codeball-job-id', job.id)
15+
const job = await create({
16+
url: pullRequestURL,
17+
access_token: githubToken
18+
})
2919

30-
await track(job.id, 'baller')
31-
} catch (error) {
32-
if (error instanceof Error) core.setFailed(error.message)
33-
}
20+
core.info(`Job created: ${job.id}`)
21+
return {jobId: job.id}
3422
}
3523

3624
run()
25+
.then(({jobId}) => {
26+
track({jobID: jobId, actionName: 'baller'})
27+
core.setOutput('codeball-job-id', jobId)
28+
})
29+
.catch(error => {
30+
track({actionName: 'baller', error: error.message})
31+
if (error instanceof Error) {
32+
core.setFailed(error.message)
33+
}
34+
})

0 commit comments

Comments
 (0)