getDescriptions loads OpenapiFiles for all the OpenAPI 3.0 description files in github/rest-api-description. This assumes that all directories in "descriptions/" contain OpenAPI 3.0 description files with the same name as the directory (plus the ".json" extension). For example, "descriptions/api.git
(ctx context.Context, client *github.Client, gitRef string)
| 95 | // - Directories are sorted by the pattern that matched in the same order they appear in dirPatterns. |
| 96 | // - Directories are then sorted by major and minor version in descending order. |
| 97 | func getDescriptions(ctx context.Context, client *github.Client, gitRef string) ([]*openapiFile, error) { |
| 98 | _, dir, resp, err := client.Repositories.GetContents( |
| 99 | ctx, |
| 100 | descriptionsOwnerName, |
| 101 | descriptionsRepoName, |
| 102 | descriptionsPath, |
| 103 | &github.RepositoryContentGetOptions{Ref: gitRef}, |
| 104 | ) |
| 105 | if err != nil { |
| 106 | return nil, err |
| 107 | } |
| 108 | if resp.StatusCode != 200 { |
| 109 | return nil, fmt.Errorf("unexpected status code: %v", resp.Status) |
| 110 | } |
| 111 | files := make([]*openapiFile, 0, len(dir)) |
| 112 | for _, d := range dir { |
| 113 | for i, pattern := range dirPatterns { |
| 114 | m := pattern.FindStringSubmatch(d.GetName()) |
| 115 | if m == nil { |
| 116 | continue |
| 117 | } |
| 118 | file := openapiFile{ |
| 119 | filename: fmt.Sprintf("descriptions/%v/%v.json", d.GetName(), d.GetName()), |
| 120 | plan: m[pattern.SubexpIndex("plan")], |
| 121 | planIdx: i, |
| 122 | } |
| 123 | rawMajor := m[pattern.SubexpIndex("major")] |
| 124 | if rawMajor != "" { |
| 125 | file.releaseMajor, err = strconv.Atoi(rawMajor) |
| 126 | if err != nil { |
| 127 | return nil, err |
| 128 | } |
| 129 | } |
| 130 | rawMinor := m[pattern.SubexpIndex("minor")] |
| 131 | if rawMinor != "" { |
| 132 | file.releaseMinor, err = strconv.Atoi(rawMinor) |
| 133 | if err != nil { |
| 134 | return nil, err |
| 135 | } |
| 136 | } |
| 137 | if file.plan == "ghes" && file.releaseMajor < 3 { |
| 138 | continue |
| 139 | } |
| 140 | files = append(files, &file) |
| 141 | break |
| 142 | } |
| 143 | } |
| 144 | slices.SortFunc(files, func(a, b *openapiFile) int { |
| 145 | // sort by the following rules: |
| 146 | // - planIdx ascending |
| 147 | // - releaseMajor descending |
| 148 | // - releaseMinor descending |
| 149 | return cmp.Or( |
| 150 | cmp.Compare(a.planIdx, b.planIdx), |
| 151 | cmp.Compare(b.releaseMajor, a.releaseMajor), |
| 152 | cmp.Compare(b.releaseMinor, a.releaseMinor), |
| 153 | ) |
| 154 | }) |
no test coverage detected
searching dependent graphs…