Skip to content

Commit

Permalink
deduplicate subjects before adding to statement (#180)
Browse files Browse the repository at this point in the history
Signed-off-by: Brian DeHamer <bdehamer@github.com>
  • Loading branch information
bdehamer authored Dec 6, 2024
1 parent 4cd38b4 commit 65e34a8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
25 changes: 25 additions & 0 deletions __tests__/subject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,31 @@ describe('subjectFromInputs', () => {
})
})
})

describe('when duplicate subjects are supplied', () => {
let otherDir = ''

// Add duplicate subject in alternate directory
beforeEach(async () => {
// Set-up temp directory
const tmpDir = await fs.realpath(os.tmpdir())
otherDir = await fs.mkdtemp(tmpDir + path.sep)

// Write file to temp directory
await fs.writeFile(path.join(otherDir, filename), content)
})

it('returns de-duplicated subjects', async () => {
const inputs: SubjectInputs = {
...blankInputs,
subjectPath: `${path.join(dir, 'subject')}, ${path.join(otherDir, 'subject')} `
}
const subjects = await subjectFromInputs(inputs)

expect(subjects).toBeDefined()
expect(subjects).toHaveLength(1)
})
})
})
})

Expand Down
5 changes: 4 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "actions/attest",
"description": "Generate signed attestations for workflow artifacts",
"version": "2.0.0",
"version": "2.0.1",
"author": "",
"private": true,
"homepage": "https://github.com/actions/attest",
Expand Down
9 changes: 8 additions & 1 deletion src/subject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,14 @@ const getSubjectFromPath = async (
const name = subjectName || path.parse(file).base
const digest = await digestFile(DIGEST_ALGORITHM, file)

digestedSubjects.push({ name, digest: { [DIGEST_ALGORITHM]: digest } })
// Only add the subject if it is not already in the list
if (
!digestedSubjects.some(
s => s.name === name && s.digest[DIGEST_ALGORITHM] === digest
)
) {
digestedSubjects.push({ name, digest: { [DIGEST_ALGORITHM]: digest } })
}
}

if (digestedSubjects.length === 0) {
Expand Down

0 comments on commit 65e34a8

Please sign in to comment.