Skip to content

Commit

Permalink
Extract service name from metadata (#538)
Browse files Browse the repository at this point in the history
Relates to
#537
  • Loading branch information
sethvargo authored Aug 21, 2024
1 parent 3355306 commit c422674
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export async function run(): Promise<void> {
try {
// Get action inputs
const image = getInput('image'); // Image ie gcr.io/...
const service = getInput('service'); // Service name
let service = getInput('service'); // Service name
const job = getInput('job'); // Job name
const metadata = getInput('metadata'); // YAML file
const projectId = getInput('project_id');
Expand Down Expand Up @@ -156,6 +156,19 @@ export async function run(): Promise<void> {
const contents = await readFile(metadata, 'utf8');
const parsed = parseYAML(contents);

// Extract service name from metadata template
const name = parsed?.metadata?.name;
if (!name) {
throw new Error(`${metadata} is missing 'metadata.name'`);
}
if (service && service != name) {
throw new Error(
`service name in ${metadata} ("${name}") does not match GitHub ` +
`Actions service input ("${service}")`,
);
}
service = name;

const kind = parsed?.kind;
if (kind === 'Service') {
deployCmd = ['run', 'services', 'replace', metadata];
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,31 @@ test('#run', { concurrency: true }, async (suite) => {
assertMembers(args, ['services', 'replace']);
});

await suite.test('errors if metadata is given and the service names do not match', async (t) => {
defaultMocks(t.mock, {
metadata: 'tests/fixtures/service.yaml',
service: 'not-a-match',
});

await assert.rejects(
async () => {
await run();
},
{ message: /does not match/ },
);
});

await suite.test('does not error if metadata is given and the service names match', async (t) => {
defaultMocks(t.mock, {
metadata: 'tests/fixtures/service.yaml',
service: 'run-full-yaml',
});

await assert.doesNotReject(async () => {
await run();
});
});

await suite.test('sets job metadata if given', async (t) => {
const mocks = defaultMocks(t.mock, {
metadata: 'tests/fixtures/job.yaml',
Expand Down

0 comments on commit c422674

Please sign in to comment.