|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + "strings" |
| 12 | + "time" |
| 13 | + |
| 14 | + "google.golang.org/protobuf/encoding/protojson" |
| 15 | + |
| 16 | + pb "github.com/ConductorOne/github-workflows/pb/artifacts/v1" |
| 17 | +) |
| 18 | + |
| 19 | +// RecordReleaseRequest is the JSON body sent to the registry API. |
| 20 | +type RecordReleaseRequest struct { |
| 21 | + Org string `json:"org"` |
| 22 | + Name string `json:"name"` |
| 23 | + Version string `json:"version"` |
| 24 | + RepositoryURL string `json:"repositoryUrl"` |
| 25 | + CommitSha string `json:"commitSha"` |
| 26 | + WorkflowRunID string `json:"workflowRunId"` |
| 27 | + Documentation string `json:"documentation,omitempty"` |
| 28 | + SignatureURL string `json:"signatureUrl,omitempty"` |
| 29 | + CertificateURL string `json:"certificateUrl,omitempty"` |
| 30 | + Assets map[string]*ReleaseAsset `json:"assets,omitempty"` |
| 31 | + Images map[string]*ReleaseImage `json:"images,omitempty"` |
| 32 | +} |
| 33 | + |
| 34 | +// ReleaseAsset is the transformed asset for the registry API. |
| 35 | +type ReleaseAsset struct { |
| 36 | + Platform string `json:"platform"` |
| 37 | + Filename string `json:"filename"` |
| 38 | + MediaType string `json:"mediaType"` |
| 39 | + SizeBytes int64 `json:"sizeBytes"` |
| 40 | + Sha256 string `json:"sha256"` |
| 41 | + DownloadURL string `json:"downloadUrl"` |
| 42 | + SignatureURL string `json:"signatureUrl,omitempty"` |
| 43 | + CertificateURL string `json:"certificateUrl,omitempty"` |
| 44 | + SbomURL string `json:"sbomUrl,omitempty"` |
| 45 | +} |
| 46 | + |
| 47 | +// ReleaseImage is the transformed image for the registry API. |
| 48 | +type ReleaseImage struct { |
| 49 | + Ref string `json:"ref"` |
| 50 | + Digest string `json:"digest"` |
| 51 | + Platform string `json:"platform"` |
| 52 | +} |
| 53 | + |
| 54 | +func main() { |
| 55 | + var ( |
| 56 | + manifestPath string |
| 57 | + docsPath string |
| 58 | + org string |
| 59 | + name string |
| 60 | + version string |
| 61 | + repositoryURL string |
| 62 | + commitSha string |
| 63 | + workflowRunID string |
| 64 | + registryURL string |
| 65 | + token string |
| 66 | + ) |
| 67 | + |
| 68 | + flag.StringVar(&manifestPath, "manifest", "", "Path to merged manifest.json file (required)") |
| 69 | + flag.StringVar(&docsPath, "docs", "", "Path to docs/connector.mdx file (optional)") |
| 70 | + flag.StringVar(&org, "org", "", "GitHub organization (required)") |
| 71 | + flag.StringVar(&name, "name", "", "Repository/connector name (required)") |
| 72 | + flag.StringVar(&version, "version", "", "Release version tag (required)") |
| 73 | + flag.StringVar(&repositoryURL, "repository-url", "", "Full repository URL (required)") |
| 74 | + flag.StringVar(&commitSha, "commit-sha", "", "Git commit SHA (required)") |
| 75 | + flag.StringVar(&workflowRunID, "workflow-run-id", "", "GitHub Actions workflow run ID (required)") |
| 76 | + flag.StringVar(®istryURL, "registry-url", "", "Registry API base URL (required)") |
| 77 | + flag.StringVar(&token, "token", "", "Bearer token (or set REGISTRY_API_TOKEN env var)") |
| 78 | + flag.Parse() |
| 79 | + |
| 80 | + // Validate required flags |
| 81 | + var missing []string |
| 82 | + if manifestPath == "" { |
| 83 | + missing = append(missing, "-manifest") |
| 84 | + } |
| 85 | + if org == "" { |
| 86 | + missing = append(missing, "-org") |
| 87 | + } |
| 88 | + if name == "" { |
| 89 | + missing = append(missing, "-name") |
| 90 | + } |
| 91 | + if version == "" { |
| 92 | + missing = append(missing, "-version") |
| 93 | + } |
| 94 | + if repositoryURL == "" { |
| 95 | + missing = append(missing, "-repository-url") |
| 96 | + } |
| 97 | + if commitSha == "" { |
| 98 | + missing = append(missing, "-commit-sha") |
| 99 | + } |
| 100 | + if workflowRunID == "" { |
| 101 | + missing = append(missing, "-workflow-run-id") |
| 102 | + } |
| 103 | + if registryURL == "" { |
| 104 | + missing = append(missing, "-registry-url") |
| 105 | + } |
| 106 | + if len(missing) > 0 { |
| 107 | + fmt.Fprintf(os.Stderr, "record-release: error: missing required flags: %s\n", strings.Join(missing, ", ")) |
| 108 | + flag.Usage() |
| 109 | + os.Exit(1) |
| 110 | + } |
| 111 | + |
| 112 | + // Resolve token: flag > env var |
| 113 | + if token == "" { |
| 114 | + token = os.Getenv("REGISTRY_API_TOKEN") |
| 115 | + } |
| 116 | + if token == "" { |
| 117 | + fmt.Fprintf(os.Stderr, "record-release: error: bearer token required (use -token flag or REGISTRY_API_TOKEN env var)\n") |
| 118 | + os.Exit(1) |
| 119 | + } |
| 120 | + |
| 121 | + // Read and parse manifest using protojson (same pattern as merge-manifests) |
| 122 | + manifestBytes, err := os.ReadFile(manifestPath) |
| 123 | + if err != nil { |
| 124 | + fmt.Fprintf(os.Stderr, "record-release: error: reading manifest: %v\n", err) |
| 125 | + os.Exit(1) |
| 126 | + } |
| 127 | + |
| 128 | + manifest := &pb.Manifest{} |
| 129 | + unmarshalOpts := protojson.UnmarshalOptions{ |
| 130 | + DiscardUnknown: true, |
| 131 | + } |
| 132 | + if err := unmarshalOpts.Unmarshal(manifestBytes, manifest); err != nil { |
| 133 | + fmt.Fprintf(os.Stderr, "record-release: error: parsing manifest: %v\n", err) |
| 134 | + os.Exit(1) |
| 135 | + } |
| 136 | + |
| 137 | + // Read optional documentation |
| 138 | + var documentation string |
| 139 | + if docsPath != "" { |
| 140 | + docsBytes, err := os.ReadFile(docsPath) |
| 141 | + if err != nil { |
| 142 | + // Not fatal -- docs are optional |
| 143 | + fmt.Fprintf(os.Stderr, "record-release: warning: could not read docs file: %v\n", err) |
| 144 | + } else { |
| 145 | + documentation = string(docsBytes) |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + // Transform manifest assets: href->downloadUrl, signatureHref->signatureUrl, etc. |
| 150 | + assets := make(map[string]*ReleaseAsset) |
| 151 | + for platform, asset := range manifest.GetAssets() { |
| 152 | + assets[platform] = &ReleaseAsset{ |
| 153 | + Platform: platform, |
| 154 | + Filename: asset.GetFilename(), |
| 155 | + MediaType: asset.GetMediaType(), |
| 156 | + SizeBytes: asset.GetSizeBytes(), |
| 157 | + Sha256: asset.GetSha256(), |
| 158 | + DownloadURL: asset.GetHref(), |
| 159 | + SignatureURL: asset.GetSignatureHref(), |
| 160 | + CertificateURL: asset.GetCertificateHref(), |
| 161 | + SbomURL: asset.GetSbomHref(), |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + // Transform manifest images: extract ref, digest, add platform from map key |
| 166 | + images := make(map[string]*ReleaseImage) |
| 167 | + for platform, image := range manifest.GetImages() { |
| 168 | + images[platform] = &ReleaseImage{ |
| 169 | + Ref: image.GetRef(), |
| 170 | + Digest: image.GetDigest(), |
| 171 | + Platform: platform, |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + // Build request body |
| 176 | + req := &RecordReleaseRequest{ |
| 177 | + Org: org, |
| 178 | + Name: name, |
| 179 | + Version: version, |
| 180 | + RepositoryURL: repositoryURL, |
| 181 | + CommitSha: commitSha, |
| 182 | + WorkflowRunID: workflowRunID, |
| 183 | + Documentation: documentation, |
| 184 | + SignatureURL: manifest.GetSignatureHref(), |
| 185 | + CertificateURL: manifest.GetCertificateHref(), |
| 186 | + Assets: assets, |
| 187 | + Images: images, |
| 188 | + } |
| 189 | + |
| 190 | + bodyBytes, err := json.Marshal(req) |
| 191 | + if err != nil { |
| 192 | + fmt.Fprintf(os.Stderr, "record-release: error: marshaling request body: %v\n", err) |
| 193 | + os.Exit(1) |
| 194 | + } |
| 195 | + |
| 196 | + // POST to registry API |
| 197 | + endpoint := strings.TrimRight(registryURL, "/") + "/api/v1/ingest/release" |
| 198 | + httpReq, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewReader(bodyBytes)) |
| 199 | + if err != nil { |
| 200 | + fmt.Fprintf(os.Stderr, "record-release: error: creating HTTP request: %v\n", err) |
| 201 | + os.Exit(1) |
| 202 | + } |
| 203 | + httpReq.Header.Set("Authorization", "Bearer "+token) |
| 204 | + httpReq.Header.Set("Content-Type", "application/json") |
| 205 | + |
| 206 | + client := &http.Client{ |
| 207 | + Timeout: 30 * time.Second, |
| 208 | + } |
| 209 | + resp, err := client.Do(httpReq) |
| 210 | + if err != nil { |
| 211 | + fmt.Fprintf(os.Stderr, "record-release: error: HTTP request failed: %v\n", err) |
| 212 | + os.Exit(1) |
| 213 | + } |
| 214 | + defer resp.Body.Close() |
| 215 | + |
| 216 | + respBody, err := io.ReadAll(resp.Body) |
| 217 | + if err != nil { |
| 218 | + fmt.Fprintf(os.Stderr, "record-release: error: reading response body: %v\n", err) |
| 219 | + os.Exit(1) |
| 220 | + } |
| 221 | + |
| 222 | + // Handle response codes |
| 223 | + switch resp.StatusCode { |
| 224 | + case http.StatusOK: |
| 225 | + result, _ := json.Marshal(map[string]interface{}{ |
| 226 | + "status": "success", |
| 227 | + "code": 200, |
| 228 | + "version": version, |
| 229 | + }) |
| 230 | + fmt.Println(string(result)) |
| 231 | + case http.StatusConflict: |
| 232 | + // 409 = already exists, not an error (expected during dual-write migration) |
| 233 | + result, _ := json.Marshal(map[string]interface{}{ |
| 234 | + "status": "already_exists", |
| 235 | + "code": 409, |
| 236 | + "version": version, |
| 237 | + }) |
| 238 | + fmt.Println(string(result)) |
| 239 | + default: |
| 240 | + fmt.Fprintf(os.Stderr, "::error::Registry API record failed: HTTP %d\n", resp.StatusCode) |
| 241 | + fmt.Fprintf(os.Stderr, "%s\n", string(respBody)) |
| 242 | + os.Exit(1) |
| 243 | + } |
| 244 | +} |
0 commit comments