Skip to content

Commit

Permalink
Merge pull request #92 from rerost/rerost/use-discussion-close
Browse files Browse the repository at this point in the history
Rerost/use discussion close
  • Loading branch information
rerost authored Jun 11, 2023
2 parents 2674b29 + d2568a7 commit 963f943
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 89 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ e.g.
- Template Issue: https://github.com/rerost/issue-creator/issues/1
- Template Manifest: WIP

## Breaking Change
v0.1.x -> v0.2.x: The behavior has changed from moving to the Archive Category when closing a GitHub Discussion to simply closing the Discussion


## Flow
![image](https://user-images.githubusercontent.com/5201588/63219703-a4848b00-c1b2-11e9-90a7-aa2a4920d47b.png)

Expand Down
15 changes: 8 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/google/go-github/v37 v37.0.0
github.com/google/wire v0.3.0
github.com/pkg/errors v0.8.1
github.com/shurcooL/githubv4 v0.0.0-20210725200734-83ba7b4c9228
github.com/shurcooL/githubv4 v0.0.0-20230424031643-6cea62ecd5a9
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.3
github.com/spf13/viper v1.4.0
Expand All @@ -34,19 +34,20 @@ require (
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/pelletier/go-toml v1.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a // indirect
github.com/shurcooL/graphql v0.0.0-20220606043923-3cf50f8a0a29 // indirect
github.com/spf13/afero v1.1.2 // indirect
github.com/spf13/cast v1.3.0 // indirect
github.com/spf13/jwalterweatherman v1.0.0 // indirect
github.com/stretchr/testify v1.3.0 // indirect
go.uber.org/atomic v1.4.0 // indirect
go.uber.org/multierr v1.1.0 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/sys v0.0.0-20221006211917-84dc82d7e875 // indirect
golang.org/x/text v0.3.8 // indirect
golang.org/x/tools v0.1.12 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/tools v0.6.0 // indirect
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
google.golang.org/appengine v1.5.0 // indirect
gopkg.in/yaml.v2 v2.2.2 // indirect
)
29 changes: 15 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7z
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/shurcooL/githubv4 v0.0.0-20210725200734-83ba7b4c9228 h1:N5B+JgvM/DVYIxreItPJMM3yWrNO/GB2q4nESrtBisM=
github.com/shurcooL/githubv4 v0.0.0-20210725200734-83ba7b4c9228/go.mod h1:hAF0iLZy4td2EX+/8Tw+4nodhlMrwN3HupfaXj3zkGo=
github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a h1:KikTa6HtAK8cS1qjvUvvq4QO21QnwC+EfvB+OAuZ/ZU=
github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a/go.mod h1:AuYgA5Kyo4c7HfUmvRGs/6rGlMMV/6B1bVnB9JxJEEg=
github.com/shurcooL/githubv4 v0.0.0-20230424031643-6cea62ecd5a9 h1:nCBaIs5/R0HFP5+aPW/SzFUF8z0oKuCXmuDmHWaxzjY=
github.com/shurcooL/githubv4 v0.0.0-20230424031643-6cea62ecd5a9/go.mod h1:hAF0iLZy4td2EX+/8Tw+4nodhlMrwN3HupfaXj3zkGo=
github.com/shurcooL/graphql v0.0.0-20220606043923-3cf50f8a0a29 h1:B1PEwpArrNp4dkQrfxh/abbBAOZBVp0ds+fBEOUOqOc=
github.com/shurcooL/graphql v0.0.0-20220606043923-3cf50f8a0a29/go.mod h1:AuYgA5Kyo4c7HfUmvRGs/6rGlMMV/6B1bVnB9JxJEEg=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
Expand Down Expand Up @@ -158,42 +158,43 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5U
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20221006211917-84dc82d7e875 h1:AzgQNqF+FKwyQ5LbVrVqOcuuFB67N47F9+htZYH0wFM=
golang.org/x/sys v0.0.0-20221006211917-84dc82d7e875/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190422233926-fe54fb35175b/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
Expand Down
27 changes: 5 additions & 22 deletions repo/discussion.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,30 +277,16 @@ func (r *discussionRepositoryImpl) CloseByURL(ctx context.Context, issueURL stri
return errors.WithStack(err)
}

var archiveCategoryId githubv4.ID
for _, category := range q.Repository.DiscussionCategory.Nodes {
if category.Name == "Archive" {
archiveCategoryId = category.Id
break
}
}
if archiveCategoryId == nil {
return errors.New("Archive Category Not Found")
}

var m struct {
UpdateDiscussion struct {
CloseDiscussion struct {
Discussion struct {
Discussion
}
} `graphql:"updateDiscussion(input: $input)"`
} `graphql:"closeDiscussion(input: $input)"`
}

input := githubv4.UpdateDiscussionInput{
input := githubv4.CloseDiscussionInput{
DiscussionID: q.Repository.Discussion.Id,
Title: &q.Repository.Discussion.Title,
Body: &q.Repository.Discussion.Body,
CategoryID: &archiveCategoryId,
}

err = r.ghc.Mutate(ctx, &m, input, nil)
Expand All @@ -312,9 +298,6 @@ func (r *discussionRepositoryImpl) CloseByURL(ctx context.Context, issueURL stri
}

func (r *discussionRepositoryImpl) IsValidTemplateIssue(i types.Issue) bool {
if i.Meta == nil {
return false
}
_, ok := (*i.Meta)[categoryKey]
return ok
// TODO Check label
return true
}
130 changes: 84 additions & 46 deletions repo/discussion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,52 @@ func ToPtr[T any](v T) *T {
}

func NewTestDiscussionRepository(ctx context.Context) repo.IssueRepository {
client := NewGithubClient(ctx)
return repo.NewDisscussionRepository(client)
}

func NewGithubClient(ctx context.Context) *githubv4.Client {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("TEST_TOKEN")},
)
tc := oauth2.NewClient(ctx, ts)
return repo.NewDisscussionRepository(githubv4.NewClient(tc))

return githubv4.NewClient(tc)
}

func URLToID(ctx context.Context, githubClient *githubv4.Client, url string) (githubv4.ID, error) {
discussionData, err := repo.ParseIssueURL(url)
if err != nil {
return "", err
}

var q struct {
Repository struct {
Id githubv4.String
DiscussionCategory struct {
Nodes []struct {
Id githubv4.ID
Name githubv4.String
}
} `graphql:"discussionCategories(first: 100)"`
Discussion struct {
Id githubv4.ID
} `graphql:"discussion(number: $number)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}

variables := map[string]interface{}{
"number": githubv4.Int(discussionData.IssueNumber),
"owner": githubv4.String(discussionData.Owner),
"name": githubv4.String(discussionData.Repository),
}

err = githubClient.Query(ctx, &q, variables)
if err != nil {
return "", err
}

return q.Repository.Discussion.Id, nil
}

func TestCreate(t *testing.T) {
Expand Down Expand Up @@ -154,65 +194,65 @@ func TestFindLastIssue(t *testing.T) {
}
}

func ArchiveToGeneral(t *testing.T, ctx context.Context, url string) {
func Reopen(t *testing.T, ctx context.Context, url string) {
t.Helper()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("TEST_TOKEN")},
)
tc := oauth2.NewClient(ctx, ts)
githubClient := githubv4.NewClient(tc)
githubClient := NewGithubClient(ctx)

discussionData, err := repo.ParseIssueURL(url)
id, err := URLToID(ctx, githubClient, url)
if err != nil {
t.Error(err)
return
}

var q struct {
Repository struct {
Id githubv4.String
DiscussionCategory struct {
Nodes []struct {
Id githubv4.ID
Name githubv4.String
}
} `graphql:"discussionCategories(first: 100)"`
var m struct {
ReopenDiscussion struct {
Discussion struct {
Id githubv4.ID
} `graphql:"discussion(number: $number)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
} `graphql:"reopenDiscussion(input: $input)"`
}

variables := map[string]interface{}{
"number": githubv4.Int(discussionData.IssueNumber),
"owner": githubv4.String(discussionData.Owner),
"name": githubv4.String(discussionData.Repository),
input := githubv4.ReopenDiscussionInput{
DiscussionID: id,
}

err = githubClient.Query(ctx, &q, variables)
err = githubClient.Mutate(ctx, &m, input, nil)
if err != nil {
t.Error(err)
return
}
}

var m struct {
UpdateDiscussion struct {
func IsClosed(ctx context.Context, url string) (bool, error) {
githubClient := NewGithubClient(ctx)

discussionData, err := repo.ParseIssueURL(url)
if err != nil {
return false, err
}

var q struct {
Repository struct {
Id githubv4.String
Discussion struct {
Id githubv4.ID
}
} `graphql:"updateDiscussion(input: $input)"`
Id githubv4.ID
Closed *githubv4.Boolean
} `graphql:"discussion(number: $number)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}

input := githubv4.UpdateDiscussionInput{
DiscussionID: q.Repository.Discussion.Id,
CategoryID: githubv4.NewID("DIC_kwDOJt6V-s4CXH0p"), /* General Category */
variables := map[string]interface{}{
"number": githubv4.Int(discussionData.IssueNumber),
"owner": githubv4.String(discussionData.Owner),
"name": githubv4.String(discussionData.Repository),
}

err = githubClient.Mutate(ctx, &m, input, nil)
err = githubClient.Query(ctx, &q, variables)
if err != nil {
t.Error(err)
return
return false, err
}

return (bool)(*q.Repository.Discussion.Closed), nil
}

// WARNING: https://github.com/rerost/issue-creator-for-test の状態が変わるので、並列でこのテストが走ると問題になる
Expand All @@ -235,32 +275,30 @@ func TestCloseByURL(t *testing.T) {
t.Parallel()

discussionRepo := NewTestDiscussionRepository(ctx)
oldState, err := discussionRepo.FindByURL(ctx, test.in)
// validate
isClosed, err := IsClosed(ctx, test.in)
if err != nil {
t.Error(err)
return
}
// validate
if (*oldState.Meta)["categoryId"] != "DIC_kwDOJt6V-s4CXH0p" /* General Category */ {
t.Errorf("%v is not valid state", test.in)
return
if isClosed {
t.Errorf("%v is already closed", test.in)
}

err = discussionRepo.CloseByURL(ctx, test.in)
if err != nil {
t.Error(err)
return
}
defer ArchiveToGeneral(t, ctx, test.in)
defer Reopen(t, ctx, test.in)

newState, err := discussionRepo.FindByURL(ctx, test.in)
isClosed, err = IsClosed(ctx, test.in)
if err != nil {
t.Error(err)
return
}

if (*newState.Meta)["categoryId"] != "DIC_kwDOJt6V-s4CXH0u" /* Archive Category */ {
t.Errorf("%v is not close(archived)", test.in)
if !isClosed {
t.Errorf("%v is not close", test.in)
}
})
}
Expand All @@ -277,7 +315,7 @@ func TestIsValidTemplateIssue(t *testing.T) {
in: types.Issue{
Meta: nil,
},
out: false,
out: true,
},
{
in: types.Issue{
Expand Down

0 comments on commit 963f943

Please sign in to comment.