# xml-generator

Fetches delivery metadata XML from internal OWS APIs. Supports two modes:

- **Single mode** — one release, result printed to stdout and written to disk
- **Batch mode** — multiple releases from a CSV, each written as a named file and zipped into a single artifact

Mode is determined automatically at runtime: if `input/input.csv` is present it runs in batch mode, otherwise it runs in single mode.

---

## Prerequisites

- Docker
- AWS credentials with access to Secrets Manager (for the M2M token) — use `awsume prod` or equivalent

---

## Running locally

### Setup

```bash
cp .env.shadow .env
# Fill in all values in .env
```

AWS credentials (`AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_SESSION_TOKEN`) are read from your shell environment and passed into the container — they do not go in `.env`.

### Single mode

Set `DMS_ID`, `UPC`, `FORMAT`, `TYPE`, `RINGTONE`, and `ENVIRONMENT` in `.env`. Make sure there is no `input/input.csv` file present, then run:

```bash
docker compose up dev
```

The XML is printed to stdout and written to `output/{UPC}.xml`.

### Batch mode

Create `input/input.csv` with a header row and one release per line:

```csv
DMS_ID,UPC,FORMAT
12345,123456789012,DDEX v. 4.3
67890,987654321098,iTunes Music
```

Set `TYPE`, `RINGTONE`, and `ENVIRONMENT` in `.env` (`DMS_ID`, `UPC`, `FORMAT` are ignored when the CSV is present), then run:

```bash
docker compose up dev
```

Results land in `output/`. A zip of all results is written to `output/results.zip`.

---

## Running in Jenkins

Jenkins uses the pre-built ECR image via the `deploy` service. AWS credentials are injected by the pipeline.

### Single mode

Pass `DMS_ID`, `UPC`, `FORMAT`, `TYPE`, `RINGTONE`, and `ENVIRONMENT` as job parameters.

```bash
docker compose up deploy
```

### Batch mode

Upload a CSV file to `input/input.csv` on the agent workspace. Pass `TYPE`, `RINGTONE`, and `ENVIRONMENT` as job parameters.

```bash
docker compose up deploy
```

Jenkins can publish `output/results.zip` as a build artifact.

---

## Output structure

```
output/
  {UPC}.xml                          # single mode
  {DMS_ID}/{TYPE}/{UPC}.xml          # batch mode (one per row)
  errors/{DMS_ID}_{TYPE}_{UPC}.txt   # failed rows (batch mode); contains the error message
  results.zip                        # batch mode only; contains all of the above
```

In batch mode, failed rows are written to `errors/` and processing continues — a partial result zip is still produced.

The `output/` directory is cleared at the start of each run.

---

## Supported FORMAT values

| Format | Notes |
|---|---|
| `DDEX v. 4.3` | Uses `ows-delivery-metadata` |
| `DDEX v. 3.8` | |
| `DDEX v. 3.7` | |
| `DDEX v. 3.4` | |
| `DDEX v. 3.2` | |
| `DDEX RDR 1.4` | |
| `iTunes Music` | |
| `iTunes Film` | |
| `Orchard 1.5` | |
| `Custom XML (Frmwk 2)` | |
| `Custom XML (Frmwk 3)` | |
| `Custom Film XML (Frmwk 3)` | |
| `Custom TV XML (Frmwk 3)` | |

`ENVIRONMENT` must be `qa` or `prod`.

---

## Environment variables

| Variable | Mode | Description |
|---|---|---|
| `DMS_ID` | Single only | |
| `UPC` | Single only | UPC barcode |
| `FORMAT` | Single only | Format string from the table above |
| `TYPE` | Both | `complete_album` or `metadata_update` |
| `RINGTONE` | Both | Set to `Yes` to include ringtone clip URL in request |
| `ENVIRONMENT` | Both | `qa` or `prod` |
| `AWS_ACCESS_KEY_ID` | Both | |
| `AWS_SECRET_ACCESS_KEY` | Both | |
| `AWS_SESSION_TOKEN` | Both | |
| `AWS_DEFAULT_REGION` | Both | |
