|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +// changeIDFormat is the expected format for change IDs, included in error messages. |
| 10 | +const changeIDFormat = "{scheme}://{owner}/{repo}/{pr_number}/{head_commit_sha}" |
| 11 | + |
| 12 | +// ChangeID represents a parsed GitHub-family change identifier. |
| 13 | +// Covers GitHub.com, GitHub Enterprise (GHE), and GitHub Enterprise Server (GHES) |
| 14 | +// since they share the same pull request model. |
| 15 | +// Format: {scheme}://{owner}/{repo}/{pr_number}/{head_commit_sha} |
| 16 | +type ChangeID struct { |
| 17 | + // Scheme captures the source variant (e.g., "github", "ghe", "ghes"). |
| 18 | + Scheme string |
| 19 | + // Org is the organization or owner of the repository. |
| 20 | + Org string |
| 21 | + // Repo is the repository name. |
| 22 | + Repo string |
| 23 | + // PRNumber is the pull request number. |
| 24 | + PRNumber int |
| 25 | + // HeadCommitSHA is the head commit SHA at the time of request creation. |
| 26 | + HeadCommitSHA string |
| 27 | +} |
| 28 | + |
| 29 | +// ParseChangeID parses a raw change ID string into a ChangeID. |
| 30 | +// Expected format: {scheme}://{owner}/{repo}/{pr_number}/{head_commit_sha} |
| 31 | +// The parser works from the end: SHA (last), PR number (second-to-last), |
| 32 | +// and everything before is the repo path (split into owner and repo). |
| 33 | +func ParseChangeID(raw string) (ChangeID, error) { |
| 34 | + // Split on "://" to get scheme and path |
| 35 | + schemeSplit := strings.SplitN(raw, "://", 2) |
| 36 | + if len(schemeSplit) != 2 { |
| 37 | + return ChangeID{}, fmt.Errorf("invalid change ID %q: missing '://' separator (expected format: %s)", raw, changeIDFormat) |
| 38 | + } |
| 39 | + |
| 40 | + scheme := schemeSplit[0] |
| 41 | + if scheme == "" { |
| 42 | + return ChangeID{}, fmt.Errorf("invalid change ID %q: empty scheme (expected format: %s)", raw, changeIDFormat) |
| 43 | + } |
| 44 | + |
| 45 | + path := schemeSplit[1] |
| 46 | + |
| 47 | + // Split the path into segments and parse from the end. |
| 48 | + segments := strings.Split(path, "/") |
| 49 | + // Need at least 4 segments: {owner}/{repo}/{pr_number}/{sha} |
| 50 | + if len(segments) < 4 { |
| 51 | + return ChangeID{}, fmt.Errorf("invalid change ID %q: need at least owner/repo/pr/sha, got %d segments (expected format: %s)", raw, len(segments), changeIDFormat) |
| 52 | + } |
| 53 | + |
| 54 | + sha := segments[len(segments)-1] |
| 55 | + prStr := segments[len(segments)-2] |
| 56 | + repoSegments := segments[:len(segments)-2] |
| 57 | + |
| 58 | + if sha == "" { |
| 59 | + return ChangeID{}, fmt.Errorf("invalid change ID %q: empty head commit SHA (expected format: %s)", raw, changeIDFormat) |
| 60 | + } |
| 61 | + |
| 62 | + prNumber, err := strconv.Atoi(prStr) |
| 63 | + if err != nil { |
| 64 | + return ChangeID{}, fmt.Errorf("invalid change ID %q: PR number %q is not a valid integer (expected format: %s)", raw, prStr, changeIDFormat) |
| 65 | + } |
| 66 | + |
| 67 | + // Split repo path: last segment is repo name, everything before is the owner. |
| 68 | + if len(repoSegments) < 2 { |
| 69 | + return ChangeID{}, fmt.Errorf("invalid change ID %q: repo path must have at least owner/repo (expected format: %s)", raw, changeIDFormat) |
| 70 | + } |
| 71 | + |
| 72 | + repo := repoSegments[len(repoSegments)-1] |
| 73 | + org := strings.Join(repoSegments[:len(repoSegments)-1], "/") |
| 74 | + |
| 75 | + if org == "" { |
| 76 | + return ChangeID{}, fmt.Errorf("invalid change ID %q: empty owner (expected format: %s)", raw, changeIDFormat) |
| 77 | + } |
| 78 | + if repo == "" { |
| 79 | + return ChangeID{}, fmt.Errorf("invalid change ID %q: empty repo (expected format: %s)", raw, changeIDFormat) |
| 80 | + } |
| 81 | + |
| 82 | + return ChangeID{ |
| 83 | + Scheme: scheme, |
| 84 | + Org: org, |
| 85 | + Repo: repo, |
| 86 | + PRNumber: prNumber, |
| 87 | + HeadCommitSHA: sha, |
| 88 | + }, nil |
| 89 | +} |
| 90 | + |
| 91 | +// String returns the string representation of the change ID. |
| 92 | +func (c ChangeID) String() string { |
| 93 | + return fmt.Sprintf("%s://%s/%s/%d/%s", c.Scheme, c.Org, c.Repo, c.PRNumber, c.HeadCommitSHA) |
| 94 | +} |
| 95 | + |
| 96 | +// OwnerRepo returns the "{org}/{repo}" string. |
| 97 | +func (c ChangeID) OwnerRepo() string { |
| 98 | + return fmt.Sprintf("%s/%s", c.Org, c.Repo) |
| 99 | +} |
0 commit comments