Skip to content

Commit

Permalink
use sponsorId instead of sponsorLogin for donate cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
nehzata committed Jan 8, 2024
1 parent c941d9b commit e029274
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 32 deletions.
84 changes: 58 additions & 26 deletions commands/donate/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ func (c *CmdDonate) Run(
amount := githubv4.Int(c.Amount)
isRecurring := githubv4.Boolean(c.IsRecurring)
privacyLevel := githubv4.SponsorshipPrivacy(githubv4.SponsorshipPrivacyPublic)
receiveEmails := githubv4.Boolean(false)

sponsorIds := map[string]string{}

// For each recipient create a GH sponsorship that is:
// - $1
Expand All @@ -72,43 +75,72 @@ func (c *CmdDonate) Run(
row := row
logger.Infof("donating %s:%s", row.SponsorID, row.RecipientID)

var m struct {
CreateSponsorship struct {
ClientMutationID string
} `graphql:"createSponsorship(input:$input)"`
failed := false

sid, ok := sponsorIds[row.SponsorID]
if !ok {
var q struct {
RepositoryOwner struct {
ID string
} `graphql:"repositoryOwner(login: $login)"`
}
var vars map[string]any = map[string]any{
"login": githubv4.String(row.SponsorID),
}

err := client.Query(ctx, &q, vars)
if err != nil {
logger.WithError(err).Error("failed to get sponsor id")
failed = true
} else {
sid = q.RepositoryOwner.ID
sponsorIds[row.SponsorID] = sid
}
}
id := githubv4.String(fmt.Sprintf("%s:%s", row.SponsorID, row.RecipientID))
sponsorLogin := githubv4.String(row.SponsorID)
sponsorableLogin := githubv4.String(row.RecipientID)
var input githubv4.Input = githubv4.CreateSponsorshipInput{
ClientMutationID: &id,
IsRecurring: &isRecurring,
Amount: &amount,
SponsorLogin: &sponsorLogin,
SponsorableLogin: &sponsorableLogin,
PrivacyLevel: &privacyLevel,

if !failed {
var m struct {
CreateSponsorship struct {
ClientMutationID string
} `graphql:"createSponsorship(input:$input)"`
}
id := githubv4.String(fmt.Sprintf("%s:%s", row.SponsorID, row.RecipientID))
sponsorId := githubv4.ID(sid)
sponsorableLogin := githubv4.String(row.RecipientID)
var input githubv4.Input = githubv4.CreateSponsorshipInput{
ClientMutationID: &id,
IsRecurring: &isRecurring,
Amount: &amount,
SponsorID: &sponsorId,
SponsorableLogin: &sponsorableLogin,
PrivacyLevel: &privacyLevel,
ReceiveEmails: &receiveEmails,
}

err := client.Mutate(ctx, &m, input, nil)
if err != nil {
logger.WithError(err).Errorf("failed to create sponsorship for %s", row.RecipientID)
failed = true
}
}

err := client.Mutate(ctx, &m, input, nil)
if err != nil {
logger.WithError(err).Error("failed to create sponsorship")
if failed {
/* autoquery name: UpdateDonationDonateAttemptTs :exec
UPDATE donations
SET donate_attempt_ts = UNIXEPOCH()
WHERE id = ?;
*/
_ = db.UpdateDonationDonateAttemptTs(ctx, row.ID)
continue
}
} else {
/* autoquery name: UpdateDonationDonateTs :exec
/* autoquery name: UpdateDonationDonateTs :exec
UPDATE donations
SET donate_ts = UNIXEPOCH()
WHERE id = ?;
*/
_ = db.UpdateDonationDonateTs(ctx, row.ID)
UPDATE donations
SET donate_ts = UNIXEPOCH()
WHERE id = ?;
*/
_ = db.UpdateDonationDonateTs(ctx, row.ID)
}
}

return nil
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ require (
github.com/alecthomas/kong v0.8.0
github.com/google/go-github/v55 v55.0.0
github.com/google/uuid v1.3.0
github.com/jpillora/backoff v1.0.0
github.com/mattn/go-sqlite3 v1.14.17
github.com/pressly/goose/v3 v3.15.0
github.com/shurcooL/githubv4 v0.0.0-20230704064427-599ae7bbf278
github.com/sirupsen/logrus v1.9.3
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
golang.org/x/oauth2 v0.12.0
golang.org/x/sync v0.3.0
golang.org/x/tools v0.13.0
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
Expand Down Expand Up @@ -57,8 +55,6 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck=
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
Expand Down

0 comments on commit e029274

Please sign in to comment.