Skip to content

Commit 0e8fe8a

Browse files
committedApr 24, 2024··
retry request on failure to save attestation
Signed-off-by: Brian DeHamer <bdehamer@github.com>
1 parent 29885a8 commit 0e8fe8a

File tree

5 files changed

+280
-54
lines changed

5 files changed

+280
-54
lines changed
 

‎packages/attest/RELEASES.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# @actions/attest Releases
22

3+
### 1.2.1
4+
5+
- Retry request on attestation persistence failure
6+
37
### 1.2.0
48

59
- Generate attestations using the v0.3 Sigstore bundle format.

‎packages/attest/__tests__/store.test.ts

+33-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,39 @@ describe('writeAttestation', () => {
5252
})
5353

5454
it('throws an error', async () => {
55-
await expect(writeAttestation(attestation, token)).rejects.toThrow(/oops/)
55+
await expect(
56+
writeAttestation(attestation, token, {retry: 0})
57+
).rejects.toThrow(/oops/)
58+
})
59+
})
60+
61+
describe('when the api call fails but succeeds on retry', () => {
62+
beforeEach(() => {
63+
const pool = mockAgent.get('https://api.github.com')
64+
65+
pool
66+
.intercept({
67+
path: '/repos/foo/bar/attestations',
68+
method: 'POST',
69+
headers: {authorization: `token ${token}`},
70+
body: JSON.stringify({bundle: attestation})
71+
})
72+
.reply(500, 'oops')
73+
.times(1)
74+
75+
pool
76+
.intercept({
77+
path: '/repos/foo/bar/attestations',
78+
method: 'POST',
79+
headers: {authorization: `token ${token}`},
80+
body: JSON.stringify({bundle: attestation})
81+
})
82+
.reply(201, {id: '123'})
83+
.times(1)
84+
})
85+
86+
it('persists the attestation', async () => {
87+
await expect(writeAttestation(attestation, token)).resolves.toEqual('123')
5688
})
5789
})
5890
})

‎packages/attest/package-lock.json

+227-50
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/attest/package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@actions/attest",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "Actions attestation lib",
55
"keywords": [
66
"github",
@@ -46,9 +46,15 @@
4646
"@actions/core": "^1.10.1",
4747
"@actions/github": "^6.0.0",
4848
"@actions/http-client": "^2.2.1",
49+
"@octokit/plugin-retry": "^6.0.1",
4950
"@sigstore/bundle": "^2.3.0",
5051
"@sigstore/sign": "^2.3.0",
5152
"jsonwebtoken": "^9.0.2",
5253
"jwks-rsa": "^3.1.0"
54+
},
55+
"overrides": {
56+
"@octokit/plugin-retry": {
57+
"@octokit/core": "^5.2.0"
58+
}
5359
}
5460
}

‎packages/attest/src/store.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import * as github from '@actions/github'
2+
import {retry} from '@octokit/plugin-retry'
23

34
const CREATE_ATTESTATION_REQUEST = 'POST /repos/{owner}/{repo}/attestations'
5+
const DEFAULT_RETRY_COUNT = 5
46

7+
export type WriteOptions = {
8+
retry?: number
9+
}
510
/**
611
* Writes an attestation to the repository's attestations endpoint.
712
* @param attestation - The attestation to write.
@@ -11,9 +16,11 @@ const CREATE_ATTESTATION_REQUEST = 'POST /repos/{owner}/{repo}/attestations'
1116
*/
1217
export const writeAttestation = async (
1318
attestation: unknown,
14-
token: string
19+
token: string,
20+
options: WriteOptions = {}
1521
): Promise<string> => {
16-
const octokit = github.getOctokit(token)
22+
const retries = options.retry ?? DEFAULT_RETRY_COUNT
23+
const octokit = github.getOctokit(token, {retry: {retries}}, retry)
1724

1825
try {
1926
const response = await octokit.request(CREATE_ATTESTATION_REQUEST, {

0 commit comments

Comments
 (0)
Please sign in to comment.