Skip to content

Commit 65e34a8

Browse files
authored
deduplicate subjects before adding to statement (#180)
Signed-off-by: Brian DeHamer <[email protected]>
1 parent 4cd38b4 commit 65e34a8

File tree

5 files changed

+40
-5
lines changed

5 files changed

+40
-5
lines changed

__tests__/subject.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,31 @@ describe('subjectFromInputs', () => {
362362
})
363363
})
364364
})
365+
366+
describe('when duplicate subjects are supplied', () => {
367+
let otherDir = ''
368+
369+
// Add duplicate subject in alternate directory
370+
beforeEach(async () => {
371+
// Set-up temp directory
372+
const tmpDir = await fs.realpath(os.tmpdir())
373+
otherDir = await fs.mkdtemp(tmpDir + path.sep)
374+
375+
// Write file to temp directory
376+
await fs.writeFile(path.join(otherDir, filename), content)
377+
})
378+
379+
it('returns de-duplicated subjects', async () => {
380+
const inputs: SubjectInputs = {
381+
...blankInputs,
382+
subjectPath: `${path.join(dir, 'subject')}, ${path.join(otherDir, 'subject')} `
383+
}
384+
const subjects = await subjectFromInputs(inputs)
385+
386+
expect(subjects).toBeDefined()
387+
expect(subjects).toHaveLength(1)
388+
})
389+
})
365390
})
366391
})
367392

dist/index.js

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "actions/attest",
33
"description": "Generate signed attestations for workflow artifacts",
4-
"version": "2.0.0",
4+
"version": "2.0.1",
55
"author": "",
66
"private": true,
77
"homepage": "https://github.com/actions/attest",

src/subject.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,14 @@ const getSubjectFromPath = async (
8484
const name = subjectName || path.parse(file).base
8585
const digest = await digestFile(DIGEST_ALGORITHM, file)
8686

87-
digestedSubjects.push({ name, digest: { [DIGEST_ALGORITHM]: digest } })
87+
// Only add the subject if it is not already in the list
88+
if (
89+
!digestedSubjects.some(
90+
s => s.name === name && s.digest[DIGEST_ALGORITHM] === digest
91+
)
92+
) {
93+
digestedSubjects.push({ name, digest: { [DIGEST_ALGORITHM]: digest } })
94+
}
8895
}
8996

9097
if (digestedSubjects.length === 0) {

0 commit comments

Comments
 (0)