Skip to content

Commit

Permalink
feat: reset old files
Browse files Browse the repository at this point in the history
  • Loading branch information
anasmuhmd committed Jul 29, 2024
1 parent 4bc18f7 commit 976a51f
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 13 deletions.
1 change: 1 addition & 0 deletions internal/github/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type IssuesClient interface {

type GistClient interface {
Create(ctx context.Context, gist *gh.Gist) (*gh.Gist, *gh.Response, error)
Get(ctx context.Context, id string) (*gh.Gist, *gh.Response, error)
Edit(ctx context.Context, id string, gist *gh.Gist) (*gh.Gist, *gh.Response, error)
}

Expand Down
19 changes: 18 additions & 1 deletion internal/github/publish_github_gist.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,24 @@ func publishGithubGist(loader ClientLoaderFn) plugin.PublishFunc {
}
slog.InfoContext(ctx, "created gist", "url", *gist.HTMLURL)
} else {
gist, _, err := client.Gists().Edit(ctx, gistId.AsString(), payload)

gist, _, err := client.Gists().Get(ctx, gistId.AsString())
if err != nil {
return diagnostics.Diag{{
Severity: hcl.DiagError,
Summary: "Failed to retreive gist",
Detail: err.Error(),
}}
}
// changing filename or output format will create a new file instead of updating the existing one.
// following logic will remove the old files and add new files.
for _, file := range gist.Files {
_, exists := payload.Files[gh.GistFilename(*file.Filename)]
if !exists {
payload.Files[gh.GistFilename(*file.Filename)] = gh.GistFile{}
}
}
gist, _, err = client.Gists().Edit(ctx, gistId.AsString(), payload)
if err != nil {
return diagnostics.Diag{{
Severity: hcl.DiagError,
Expand Down
62 changes: 62 additions & 0 deletions internal/github/publish_github_gist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,65 @@ func (s *GithubPublishGistTestSuite) TestFilenameOptional() {
})
s.Require().Nil(diags)
}

func (s *GithubPublishGistTestSuite) TestResetOldFiles() {
s.cli.On("Gists").Return(s.gistCli)
s.gistCli.On("Get", mock.Anything, "gistid").Return(&gh.Gist{
Files: map[gh.GistFilename]gh.GistFile{
"oldfile.md": {
Filename: gh.String("oldfile.md"),
Content: gh.String("old content"),
},
},
}, &gh.Response{}, nil)
s.gistCli.On("Edit", mock.Anything, "gistid", &gh.Gist{
Description: gh.String("test description"),
Public: gh.Bool(false),
Files: map[gh.GistFilename]gh.GistFile{
"oldfile.md": {},
"test_document.md": {
Content: gh.String("# Header 1\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit."),
Filename: gh.String("test_document.md"),
},
},
}).Return(&gh.Gist{HTMLURL: gh.String("http://gist.github.com/mock")}, &gh.Response{}, nil)
ctx := context.Background()
titleMeta := plugin.MapData{
"provider": plugin.StringData("title"),
"plugin": plugin.StringData("blackstork/builtin"),
}
diags := s.plugin.Publish(ctx, "github_gist", &plugin.PublishParams{
Config: cty.ObjectVal(map[string]cty.Value{
"github_token": cty.StringVal("testtoken"),
}),
Args: cty.ObjectVal(map[string]cty.Value{
"description": cty.StringVal("test description"),
"filename": cty.StringVal(""),
"make_public": cty.NullVal(cty.Bool),
"gist_id": cty.StringVal("gistid"),
}),
Format: plugin.OutputFormatMD,
DataContext: plugin.MapData{
"document": plugin.MapData{
"meta": plugin.MapData{
"name": plugin.StringData("test_document"),
},
"content": plugin.MapData{
"type": plugin.StringData("section"),
"children": plugin.ListData{
plugin.MapData{
"type": plugin.StringData("element"),
"markdown": plugin.StringData("# Header 1"),
"meta": titleMeta,
},
plugin.MapData{
"type": plugin.StringData("element"),
"markdown": plugin.StringData("Lorem ipsum dolor sit amet, consectetur adipiscing elit."),
},
},
},
},
},
})
s.Require().Nil(diags)
}
68 changes: 68 additions & 0 deletions mocks/internalpkg/github/gist_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 14 additions & 12 deletions plugin/pluginapi/v1/cty.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 976a51f

Please sign in to comment.