Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detecting w i p commits #32

Merged
merged 5 commits into from
Feb 12, 2018
Merged

Detecting w i p commits #32

merged 5 commits into from
Feb 12, 2018

Conversation

Raul6469
Copy link
Contributor

As asked in #10, this PR proposes to detect commits containing wip in the pull request, and sets the commit status to pending if so.

@Raul6469 Raul6469 changed the title Detecting wip commits Detecting w i p commits Feb 11, 2018
number: context.payload.pull_request.number
})

if (commits.data.some(element => { return /\b(wip)\b/i.test(element.commit.message) })) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to re-use isWip from above, if you extract it as a function and pass string argument to it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review @HeeL ! I pushed a commit that follows your suggestion

@@ -2,7 +2,18 @@ module.exports = handlePullRequestChange

async function handlePullRequestChange (robot, context) {
const title = context.payload.pull_request.title
const isWip = /\b(wip|do not merge)\b/i.test(title)
var isWip = /\b(wip|do not merge)\b/i.test(title)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use let :)

owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
number: context.payload.pull_request.number
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can simplify that a bit by using context.repo({number: context.payload.pull_request.number}) as below in the .createStatus call

@gr2m
Copy link
Collaborator

gr2m commented Feb 12, 2018

oops I'm sorry I made a reivew and forgot to submit it. I see you made a few changes since then, I'll have another look

Thanks for the review @HeeL 👍

@gr2m
Copy link
Collaborator

gr2m commented Feb 12, 2018

I apologise in advanced for being overly diligent. Usually I'd accept the pull request already because it's great, then take the discussion to a follow up pull request, because merging is fun 🎉

The wip-bot is special because me and others use it for learning purpose, too, so I want to make double sure that it's good code and easy to read.

I really like the idea by @HeeL. What I would like to avoid is to send a request for the commits with the title already contains a WIP.

I gave it some thought and came up with this

module.exports = handlePullRequestChange

async function handlePullRequestChange (robot, context) {
  const {title, html_url, head} = context.payload.pull_request
  const isWip = containsWIP(title) || await commitsContainWIP(context)
  const status = isWip ? 'pending' : 'success'

  console.log('Updating PR "%s" (%s): %s', title, html_url, status)

  context.github.repos.createStatus(context.repo({
    sha: head.sha,
    state: status,
    target_url: 'https://github.com/apps/wip',
    description: isWip ? 'work in progress – do not merge!' : 'ready for review',
    context: 'WIP'
  }))
}

async function commitsContainWIP ({github, payload, repo}) {
  const commits = await github.pullRequests.getCommits(repo({
    number: payload.pull_request.number
  })

  return commits.data.map(element => element.commit.message).some(containsWIP)
}

function containsWIP (string) {
  return /\b(wip|do not merge)\b/i.test(string)
}

It's only a base of discussion. I'd love to hear what you think. Maybe we can make it even simpler while keeping it readable? Fun exercise in group refactoring :)

@Raul6469
Copy link
Contributor Author

No problem, I understand. I don't have enough experience in Node to write the best code, so this is an occasion to improve myself :)

Your code is really understandable in my opinion, and I don't see ways to make it more concise.

I executed your code on my machine, and I get a TypeError: Cannot read property 'payload' of undefined in the commitsContainWIP function. However, it looks like payload is correctly defined in it. Do you have any idea why?

@gr2m
Copy link
Collaborator

gr2m commented Feb 12, 2018

Hmm can't tell from the code, I can give this a try myself.

For debugging, if you run this code locally, I recommend to use the Chrome Devtools Inspect. Put a debugger statement on top of the handlePullRequestChange function. Install the chrome extension, then start the probot app with

node --inspect probot run ./index.js

Open chrome and it should open the devtools automatically. Now the execution should stop when the code gets to the function and you have the full chrome devtools available for debugging :)

@Raul6469
Copy link
Contributor Author

Thanks for the tip!

I used VS Code and LocalTunnel instead to inspect variables, and looks like there is a problem with the strict mode

Here is what I get:
image

In detail what's in repo

TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them

@gr2m
Copy link
Collaborator

gr2m commented Feb 12, 2018

that's a weird error you get. What node version are you running the app in? Make sure it's Node 8 or 9

@Raul6469
Copy link
Contributor Author

Raul6469 commented Feb 12, 2018

I use 8.9.4 on Windows 10

@gr2m
Copy link
Collaborator

gr2m commented Feb 12, 2018

ah wait I know what this is, I run into it before. The context.repo method is not properly bound and is using the this keyword. It would be a nice PR to probot to fix that ;)

For this code to work we can't pass {github, payload, repo} but need to pass context instaed and then use context.repo etc. Could you give that a try please?

@Raul6469
Copy link
Contributor Author

Yep, and it works! 👌 Do I push that?

@gr2m
Copy link
Collaborator

gr2m commented Feb 12, 2018

yes please update the PR 👍

Copy link
Collaborator

@gr2m gr2m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fabulous, thank you :)

@gr2m gr2m merged commit 2d6181b into wip:master Feb 12, 2018
@Raul6469
Copy link
Contributor Author

You welcome, it was a pleasure working on it! 🎉

@gr2m
Copy link
Collaborator

gr2m commented Feb 12, 2018

I fear the deployment failed, I'm looking into it

@gr2m
Copy link
Collaborator

gr2m commented Feb 12, 2018

alright, all set, enjoy the new WIP :)

@Raul6469
Copy link
Contributor Author

Great! I was afraid I broke something 😅

@HeeL HeeL mentioned this pull request Feb 12, 2018
const status = isWip ? 'pending' : 'success'

console.log(`Updating PR "${title}" (${context.payload.pull_request.html_url}): ${status}`)
console.log('Updating PR "%s" (%s): %s', title, html_url, status)
Copy link
Contributor

@anshumanv anshumanv Feb 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason for switching from ${template literals} ?
This PR is great btw. 👍 😄

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that was more by accident 😁 At some point during refactoring I had this

console.log('Updating PR "%s" (%s): %s', 
  context.payload.pull_request.title, 
  context.payload.pull_request.html_url, 
  context.payload.pull_request.status)

Putting it all in one line made it less readable. Now that it's just console.log('Updating PR "%s" (%s): %s', title, html_url, status) we could as well have left it at template literals. Feel free to send a pull request if you like, I think template literals make it more readable :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure @gr2m thanks! 🎉
Sending one. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants