writeFile commits the contents to the Git repo at the given filename & revision. If the Git repo does not yet have a file at this filename, it will create the file. Otherwise, it will simply update the file with the new contents.
(filename string, sha string, contents []byte)
| 131 | // If the Git repo does not yet have a file at this filename, it will create the file. |
| 132 | // Otherwise, it will simply update the file with the new contents. |
| 133 | func (gh *Storage) writeFile(filename string, sha string, contents []byte) error { |
| 134 | if err := gh.ensureClient(); err != nil { |
| 135 | return err |
| 136 | } |
| 137 | |
| 138 | var err error |
| 139 | var writeFunc func(context.Context, string, string, string, *github.RepositoryContentFileOptions) (*github.RepositoryContentResponse, *github.Response, error) |
| 140 | opts := &github.RepositoryContentFileOptions{ |
| 141 | Message: github.String(fmt.Sprintf("[checkup] store %s [ci skip]", gh.fullPathName(filename))), |
| 142 | Content: contents, |
| 143 | Committer: &github.CommitAuthor{ |
| 144 | Name: &gh.CommitterName, |
| 145 | Email: &gh.CommitterEmail, |
| 146 | }, |
| 147 | } |
| 148 | |
| 149 | if gh.Branch != "" { |
| 150 | opts.Branch = &gh.Branch |
| 151 | } |
| 152 | |
| 153 | // If no SHA specified, then create the file. |
| 154 | // Otherwise, update the file at the specified SHA. |
| 155 | if sha == "" { |
| 156 | writeFunc = gh.client.Repositories.CreateFile |
| 157 | log.Printf("github: creating %s on branch '%s'", gh.fullPathName(filename), gh.Branch) |
| 158 | } else { |
| 159 | opts.SHA = github.String(sha) |
| 160 | writeFunc = gh.client.Repositories.UpdateFile |
| 161 | log.Printf("github: updating %s on branch '%s'", gh.fullPathName(filename), gh.Branch) |
| 162 | } |
| 163 | |
| 164 | _, _, err = writeFunc( |
| 165 | context.Background(), |
| 166 | gh.RepositoryOwner, |
| 167 | gh.RepositoryName, |
| 168 | gh.fullPathName(filename), |
| 169 | opts, |
| 170 | ) |
| 171 | return err |
| 172 | } |
| 173 | |
| 174 | // deleteFile deletes a file from a Git tree and returns any applicable errors. |
| 175 | // If an empty SHA is passed as an argument, errFileNotFound is returned. |