octokit-js

How to find the user name who merged the pr to the base branch from the octokit library


Can anyone tell me that how to fetch the user name who merged the pr by the octokit.request api call.

I can fetch all the prs from octokit.request method with the query parameter state=all value. It is returning the array having all PRs (open and closed all). We can see that PRs with merged_at value is not equal to null are the pr which are merged.
Now how to find the user name who merged this pr.


Solution

  • Let me answer my question.
    These are the steps.

    1. Get the merged PR by retrieving closed PRs and check for merged_at!=null.
    2. Get the user login from response of step 1, i.e. by mergedPRs.user.login.
    3. Get All commits of the main branch.
    4. Filter and take those commits only whose commit.commit.commiter.name==='GitHub'. this will filter and take those commit only by which the PR is merged.
    5. now If step2===step4 then you got the answer. i.e. mergedPRs.user.login === filteredCommit.commit.author.login.
      You got the answer, it will tell you the pr is created and merged by same person or not.
         const allCommits : Array<any> = await getAllCommits(data);    
         const mergedPRs : Array<any> = await getMergedPRs(data);
         const MergeCommitsUserDetailsWithsha : Array<UserWithCommitSha> = []
         allCommits.forEach(commit => {
             if(commit.commit.committer.name==="GitHub"){
                 let objectToPush : UserWithCommitSha = {username:commit.author.login, commitsha:commit.sha};
                 MergeCommitsUserDetailsWithsha.push(objectToPush);
                }
             });
         const selfMergedPRs : Array<any> = mergedPRs.filter(pr=>{
             return MergeCommitsUserDetailsWithsha.some(element => element.username===pr.user.login && element.commitsha===pr.merge_commit_sha);
         });
        
         let result : Array<SelfMergedPRs>=[];
         selfMergedPRs.forEach(pr => {
                result.push({url:pr.html_url, userName:pr.user.login, commit:pr.merge_commit_sha, headBranch:pr.head.ref, baseBranch:pr.base.ref})
         });
            
         return result;