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 e4cfee1
Showing 1 changed file with 59 additions and 25 deletions.
84 changes: 59 additions & 25 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,74 @@ 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
}

/* autoquery name: UpdateDonationDonateTs :exec
} else {

UPDATE donations
SET donate_ts = UNIXEPOCH()
WHERE id = ?;
*/
_ = db.UpdateDonationDonateTs(ctx, row.ID)
/* autoquery name: UpdateDonationDonateTs :exec
UPDATE donations
SET donate_ts = UNIXEPOCH()
WHERE id = ?;
*/
_ = db.UpdateDonationDonateTs(ctx, row.ID)
}
}

return nil
Expand Down

0 comments on commit e4cfee1

Please sign in to comment.