Skip to content

Commit

Permalink
Merge pull request #318 from shoekstra/add_repository_file_resource
Browse files Browse the repository at this point in the history
Add repository file resource
  • Loading branch information
Jeremy Udit authored Feb 11, 2020
2 parents aab3459 + 0b1ad74 commit b838aed
Show file tree
Hide file tree
Showing 6 changed files with 866 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ In the organization you are using above, create the following test repositories:
* The website url should be `http://www.example.com`
* Create two topics within the repo named `test-topic` and `second-test-topic`
* In the repo settings, make sure all features and merge button options are enabled.
* Create a `test-branch` branch
* `test-repo-template`
* Configure the repository to be a [Template repository](https://help.github.com/en/github/creating-cloning-and-archiving-repositories/creating-a-template-repository)

Expand All @@ -103,3 +104,6 @@ Export your github username (the one you used to create the personal access toke
different github username as `GITHUB_TEST_COLLABORATOR`. Please note that these usernames cannot be the same as each other, and both of them
must be real github usernames. The collaborator user does not need to be added as a collaborator to your test repo or organization, but as
the acceptance tests do real things (and will trigger some notifications for this user), you should probably make sure the person you specify knows that you're doing this just to be nice.

Additionally the user exported as `GITHUB_TEST_USER` should have a public email address configured in their profile; this should be exported
as `GITHUB_TEST_USER_EMAIL` and the Github name exported as `GITHUB_TEST_USER_NAME` (this could be different to your GitHub login).
1 change: 1 addition & 0 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func Provider() terraform.ResourceProvider {
"github_project_column": resourceGithubProjectColumn(),
"github_repository_collaborator": resourceGithubRepositoryCollaborator(),
"github_repository_deploy_key": resourceGithubRepositoryDeployKey(),
"github_repository_file": resourceGithubRepositoryFile(),
"github_repository_project": resourceGithubRepositoryProject(),
"github_repository_webhook": resourceGithubRepositoryWebhook(),
"github_repository": resourceGithubRepository(),
Expand Down
350 changes: 350 additions & 0 deletions github/resource_github_repository_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,350 @@
package github

import (
"context"
"log"
"net/http"
"strings"

"fmt"

"github.com/google/go-github/v28/github"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceGithubRepositoryFile() *schema.Resource {
return &schema.Resource{
Create: resourceGithubRepositoryFileCreate,
Read: resourceGithubRepositoryFileRead,
Update: resourceGithubRepositoryFileUpdate,
Delete: resourceGithubRepositoryFileDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), ":")
branch := "master"

if len(parts) > 2 {
return nil, fmt.Errorf("Invalid ID specified. Supplied ID must be written as <repository>/<file path> (when branch is \"master\") or <repository>/<file path>:<branch>")
}

if len(parts) == 2 {
branch = parts[1]
}

client := meta.(*Organization).client
org := meta.(*Organization).name
repo, file := splitRepoFilePath(parts[0])
if err := checkRepositoryFileExists(client, org, repo, file, branch); err != nil {
return nil, err
}

d.SetId(fmt.Sprintf("%s/%s", repo, file))
d.Set("branch", branch)

return []*schema.ResourceData{d}, nil
},
},

Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The repository name",
},
"file": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The file path to manage",
},
"content": {
Type: schema.TypeString,
Required: true,
Description: "The file's content",
},
"branch": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The branch name, defaults to \"master\"",
Default: "master",
},
"commit_message": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The commit message when creating or updating the file",
},
"commit_author": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The commit author name, defaults to the authenticated user's name",
},
"commit_email": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The commit author email address, defaults to the authenticated user's email address",
},
"sha": {
Type: schema.TypeString,
Computed: true,
Description: "The blob SHA of the file",
},
},
}
}

func resourceGithubRepositoryFileOptions(d *schema.ResourceData) (*github.RepositoryContentFileOptions, error) {
opts := &github.RepositoryContentFileOptions{
Content: []byte(*github.String(d.Get("content").(string))),
Branch: github.String(d.Get("branch").(string)),
}

if commitMessage, hasCommitMessage := d.GetOk("commit_message"); hasCommitMessage {
opts.Message = new(string)
*opts.Message = commitMessage.(string)
}

if SHA, hasSHA := d.GetOk("sha"); hasSHA {
opts.SHA = new(string)
*opts.SHA = SHA.(string)
}

commitAuthor, hasCommitAuthor := d.GetOk("commit_author")
commitEmail, hasCommitEmail := d.GetOk("commit_email")

if hasCommitAuthor && !hasCommitEmail {
return nil, fmt.Errorf("Cannot set commit_author without setting commit_email")
}

if hasCommitEmail && !hasCommitAuthor {
return nil, fmt.Errorf("Cannot set commit_email without setting commit_author")
}

if hasCommitAuthor && hasCommitEmail {
name := commitAuthor.(string)
mail := commitEmail.(string)
opts.Author = &github.CommitAuthor{Name: &name, Email: &mail}
opts.Committer = &github.CommitAuthor{Name: &name, Email: &mail}
}

return opts, nil
}

func resourceGithubRepositoryFileCreate(d *schema.ResourceData, meta interface{}) error {
if err := checkOrganization(meta); err != nil {
return err
}

client := meta.(*Organization).client
org := meta.(*Organization).name
ctx := context.Background()

repo := d.Get("repository").(string)
file := d.Get("file").(string)
branch := d.Get("branch").(string)

if err := checkRepositoryBranchExists(client, org, repo, branch); err != nil {
return err
}

opts, err := resourceGithubRepositoryFileOptions(d)
if err != nil {
return err
}

if opts.Message == nil {
m := fmt.Sprintf("Add %s", file)
opts.Message = &m
}

log.Printf("[DEBUG] Creating repository file: %s/%s/%s in branch: %s", org, repo, file, branch)
_, _, err = client.Repositories.CreateFile(ctx, org, repo, file, opts)
if err != nil {
return err
}

d.SetId(fmt.Sprintf("%s/%s", repo, file))

return resourceGithubRepositoryFileRead(d, meta)
}

func resourceGithubRepositoryFileRead(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}

client := meta.(*Organization).client
org := meta.(*Organization).name
ctx := context.WithValue(context.Background(), ctxId, d.Id())

repo, file := splitRepoFilePath(d.Id())
branch := d.Get("branch").(string)

if err := checkRepositoryBranchExists(client, org, repo, branch); err != nil {
return err
}

log.Printf("[DEBUG] Reading repository file: %s/%s/%s, branch: %s", org, repo, file, branch)
opts := &github.RepositoryContentGetOptions{Ref: branch}
fc, _, _, _ := client.Repositories.GetContents(ctx, org, repo, file, opts)
if fc == nil {
log.Printf("[WARN] Removing repository path %s/%s/%s from state because it no longer exists in GitHub",
org, repo, file)
d.SetId("")
return nil
}

content, err := fc.GetContent()
if err != nil {
return err
}

d.Set("content", content)
d.Set("repository", repo)
d.Set("file", file)
d.Set("sha", fc.SHA)

log.Printf("[DEBUG] Fetching commit info for repository file: %s/%s/%s", org, repo, file)
commit, err := getFileCommit(client, org, repo, file, branch)
if err != nil {
return err
}

d.Set("commit_author", commit.Commit.Committer.GetName())
d.Set("commit_email", commit.Commit.Committer.GetEmail())
d.Set("commit_message", commit.Commit.GetMessage())

return nil
}

func resourceGithubRepositoryFileUpdate(d *schema.ResourceData, meta interface{}) error {
if err := checkOrganization(meta); err != nil {
return err
}

client := meta.(*Organization).client
org := meta.(*Organization).name
ctx := context.Background()

repo := d.Get("repository").(string)
file := d.Get("file").(string)
branch := d.Get("branch").(string)

if err := checkRepositoryBranchExists(client, org, repo, branch); err != nil {
return err
}

opts, err := resourceGithubRepositoryFileOptions(d)
if err != nil {
return err
}

if *opts.Message == fmt.Sprintf("Add %s", file) {
m := fmt.Sprintf("Update %s", file)
opts.Message = &m
}

log.Printf("[DEBUG] Updating content in repository file: %s/%s/%s", org, repo, file)
_, _, err = client.Repositories.CreateFile(ctx, org, repo, file, opts)
if err != nil {
return err
}

return resourceGithubRepositoryFileRead(d, meta)
}

func resourceGithubRepositoryFileDelete(d *schema.ResourceData, meta interface{}) error {
if err := checkOrganization(meta); err != nil {
return err
}

client := meta.(*Organization).client
org := meta.(*Organization).name
ctx := context.Background()

repo := d.Get("repository").(string)
file := d.Get("file").(string)
branch := d.Get("branch").(string)

message := fmt.Sprintf("Delete %s", file)
sha := d.Get("sha").(string)
opts := &github.RepositoryContentFileOptions{
Message: &message,
SHA: &sha,
Branch: &branch,
}

log.Printf("[DEBUG] Deleting repository file: %s/%s/%s", org, repo, file)
_, _, err := client.Repositories.DeleteFile(ctx, org, repo, file, opts)
if err != nil {
return nil
}

return nil
}

// checkRepositoryBranchExists tests if a branch exists in a repository.
func checkRepositoryBranchExists(client *github.Client, org, repo, branch string) error {
ctx := context.WithValue(context.Background(), ctxId, buildTwoPartID(&repo, &branch))
_, _, err := client.Repositories.GetBranch(ctx, org, repo, branch)
if err != nil {
if ghErr, ok := err.(*github.ErrorResponse); ok {
if ghErr.Response.StatusCode == http.StatusNotFound {
return fmt.Errorf("Branch %s not found in repository or repository is not readable", branch)
}
}
return err
}

return nil
}

// checkRepositoryFileExists tests if a file exists in a repository.
func checkRepositoryFileExists(client *github.Client, org, repo, file, branch string) error {
ctx := context.WithValue(context.Background(), ctxId, fmt.Sprintf("%s/%s", repo, file))
fc, _, _, err := client.Repositories.GetContents(ctx, org, repo, file, &github.RepositoryContentGetOptions{Ref: branch})
if err != nil {
return nil
}
if fc == nil {
return fmt.Errorf("File %s not a file in in repository %s/%s or repository is not readable", file, org, repo)
}

return nil
}

func getFileCommit(client *github.Client, org, repo, file, branch string) (*github.RepositoryCommit, error) {
ctx := context.WithValue(context.Background(), ctxId, fmt.Sprintf("%s/%s", repo, file))
commits, _, err := client.Repositories.ListCommits(ctx, org, repo, &github.CommitsListOptions{SHA: branch})
if err != nil {
return nil, err
}

for _, c := range commits {
sha := c.GetSHA()

// Skip merge commits
if strings.Contains(c.Commit.GetMessage(), "Merge branch") {
continue
}

rc, _, err := client.Repositories.GetCommit(ctx, org, repo, sha)
if err != nil {
return nil, err
}

for _, f := range rc.Files {
if f.GetFilename() == file && f.GetStatus() != "removed" {
log.Printf("[DEBUG] Found file: %s in commit: %s", file, sha)
return rc, nil
}
}
}

return nil, fmt.Errorf("Cannot find file %s in repo %s/%s", file, org, repo)
}
Loading

0 comments on commit b838aed

Please sign in to comment.