Tool to calculate the average time that takes for a GitHub pull request to get merged

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












I'm learning go, and this is my first attempt at a command line tool that uses the GitHub API to calculate the average time that takes for a pull request to get merged for a particular organisation or repository.



The algorithm follows a very simple approach:



  1. Get all the repositories for an organisation.

  2. For each repository, get all the closed pull requests.

  3. Calculate the average time using merged_at and created_at of each pull request.

For large organisations/projects, step 2 can take a long time, specially if it can't get all the pull requests from a single result page. So I want to execute that part concurrently.



Here's the part of the code that does that: GitHub



c := make(chan Result, len(repos))
var wg sync.WaitGroup
for _, repo := range repos
wg.Add(1)
go func(val Repository)
if cmd.Debug
fmt.Printf("Executing goroutine for value: %s", val.Name)

pullRequests, err := getPullRequests(ctx, client, val)
c <- ResultPullRequests: pullRequests, Err: err
wg.Done()
(repo)

wg.Wait()
close(c)

timeAccumulator := float64(0)
prAccumulator := int64(0)

var cmdErr error
for result := range c
pullRequests, err := result.PullRequests, result.Err
if err != nil
cmdErr = err
break

for _, pullRequest := range pullRequests
if pullRequest.GetMergedAt().IsZero()
continue

delta := pullRequest.GetMergedAt().Sub(pullRequest.GetCreatedAt()).Hours()
timeAccumulator += delta
prAccumulator++
if cmd.Debug
fmt.Printf("PR: %snCreated at: %vnMerged at:%vnDelta in hours: %fn", pullRequest.GetTitle(), pullRequest.GetCreatedAt(), pullRequest.GetMergedAt(), delta)





It works, but I'd like to get some feedback about the WaitGroup usage. It does feel that I don't need it here, because looping over range c would take results until the channel is closed, it's just not clear to me when I should really close the channel without a WaitGroup.



And of course any other feedback is really welcomed!







share|improve this question



























    up vote
    2
    down vote

    favorite












    I'm learning go, and this is my first attempt at a command line tool that uses the GitHub API to calculate the average time that takes for a pull request to get merged for a particular organisation or repository.



    The algorithm follows a very simple approach:



    1. Get all the repositories for an organisation.

    2. For each repository, get all the closed pull requests.

    3. Calculate the average time using merged_at and created_at of each pull request.

    For large organisations/projects, step 2 can take a long time, specially if it can't get all the pull requests from a single result page. So I want to execute that part concurrently.



    Here's the part of the code that does that: GitHub



    c := make(chan Result, len(repos))
    var wg sync.WaitGroup
    for _, repo := range repos
    wg.Add(1)
    go func(val Repository)
    if cmd.Debug
    fmt.Printf("Executing goroutine for value: %s", val.Name)

    pullRequests, err := getPullRequests(ctx, client, val)
    c <- ResultPullRequests: pullRequests, Err: err
    wg.Done()
    (repo)

    wg.Wait()
    close(c)

    timeAccumulator := float64(0)
    prAccumulator := int64(0)

    var cmdErr error
    for result := range c
    pullRequests, err := result.PullRequests, result.Err
    if err != nil
    cmdErr = err
    break

    for _, pullRequest := range pullRequests
    if pullRequest.GetMergedAt().IsZero()
    continue

    delta := pullRequest.GetMergedAt().Sub(pullRequest.GetCreatedAt()).Hours()
    timeAccumulator += delta
    prAccumulator++
    if cmd.Debug
    fmt.Printf("PR: %snCreated at: %vnMerged at:%vnDelta in hours: %fn", pullRequest.GetTitle(), pullRequest.GetCreatedAt(), pullRequest.GetMergedAt(), delta)





    It works, but I'd like to get some feedback about the WaitGroup usage. It does feel that I don't need it here, because looping over range c would take results until the channel is closed, it's just not clear to me when I should really close the channel without a WaitGroup.



    And of course any other feedback is really welcomed!







    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I'm learning go, and this is my first attempt at a command line tool that uses the GitHub API to calculate the average time that takes for a pull request to get merged for a particular organisation or repository.



      The algorithm follows a very simple approach:



      1. Get all the repositories for an organisation.

      2. For each repository, get all the closed pull requests.

      3. Calculate the average time using merged_at and created_at of each pull request.

      For large organisations/projects, step 2 can take a long time, specially if it can't get all the pull requests from a single result page. So I want to execute that part concurrently.



      Here's the part of the code that does that: GitHub



      c := make(chan Result, len(repos))
      var wg sync.WaitGroup
      for _, repo := range repos
      wg.Add(1)
      go func(val Repository)
      if cmd.Debug
      fmt.Printf("Executing goroutine for value: %s", val.Name)

      pullRequests, err := getPullRequests(ctx, client, val)
      c <- ResultPullRequests: pullRequests, Err: err
      wg.Done()
      (repo)

      wg.Wait()
      close(c)

      timeAccumulator := float64(0)
      prAccumulator := int64(0)

      var cmdErr error
      for result := range c
      pullRequests, err := result.PullRequests, result.Err
      if err != nil
      cmdErr = err
      break

      for _, pullRequest := range pullRequests
      if pullRequest.GetMergedAt().IsZero()
      continue

      delta := pullRequest.GetMergedAt().Sub(pullRequest.GetCreatedAt()).Hours()
      timeAccumulator += delta
      prAccumulator++
      if cmd.Debug
      fmt.Printf("PR: %snCreated at: %vnMerged at:%vnDelta in hours: %fn", pullRequest.GetTitle(), pullRequest.GetCreatedAt(), pullRequest.GetMergedAt(), delta)





      It works, but I'd like to get some feedback about the WaitGroup usage. It does feel that I don't need it here, because looping over range c would take results until the channel is closed, it's just not clear to me when I should really close the channel without a WaitGroup.



      And of course any other feedback is really welcomed!







      share|improve this question













      I'm learning go, and this is my first attempt at a command line tool that uses the GitHub API to calculate the average time that takes for a pull request to get merged for a particular organisation or repository.



      The algorithm follows a very simple approach:



      1. Get all the repositories for an organisation.

      2. For each repository, get all the closed pull requests.

      3. Calculate the average time using merged_at and created_at of each pull request.

      For large organisations/projects, step 2 can take a long time, specially if it can't get all the pull requests from a single result page. So I want to execute that part concurrently.



      Here's the part of the code that does that: GitHub



      c := make(chan Result, len(repos))
      var wg sync.WaitGroup
      for _, repo := range repos
      wg.Add(1)
      go func(val Repository)
      if cmd.Debug
      fmt.Printf("Executing goroutine for value: %s", val.Name)

      pullRequests, err := getPullRequests(ctx, client, val)
      c <- ResultPullRequests: pullRequests, Err: err
      wg.Done()
      (repo)

      wg.Wait()
      close(c)

      timeAccumulator := float64(0)
      prAccumulator := int64(0)

      var cmdErr error
      for result := range c
      pullRequests, err := result.PullRequests, result.Err
      if err != nil
      cmdErr = err
      break

      for _, pullRequest := range pullRequests
      if pullRequest.GetMergedAt().IsZero()
      continue

      delta := pullRequest.GetMergedAt().Sub(pullRequest.GetCreatedAt()).Hours()
      timeAccumulator += delta
      prAccumulator++
      if cmd.Debug
      fmt.Printf("PR: %snCreated at: %vnMerged at:%vnDelta in hours: %fn", pullRequest.GetTitle(), pullRequest.GetCreatedAt(), pullRequest.GetMergedAt(), delta)





      It works, but I'd like to get some feedback about the WaitGroup usage. It does feel that I don't need it here, because looping over range c would take results until the channel is closed, it's just not clear to me when I should really close the channel without a WaitGroup.



      And of course any other feedback is really welcomed!









      share|improve this question












      share|improve this question




      share|improve this question








      edited Feb 6 at 18:18









      200_success

      123k14143401




      123k14143401









      asked Feb 6 at 8:55









      Ruenzuo

      1113




      1113

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186896%2ftool-to-calculate-the-average-time-that-takes-for-a-github-pull-request-to-get-m%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186896%2ftool-to-calculate-the-average-time-that-takes-for-a-github-pull-request-to-get-m%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Greedy Best First Search implementation in Rust

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          C++11 CLH Lock Implementation