-
Notifications
You must be signed in to change notification settings - Fork 5
WIP: Address various issues about milestones #143
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
a27115e
adc0fb5
b6e6295
3e20881
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const addIssueToCurrentMilestone= async (context, req) => { | ||
if (req.body.action !== 'closed') return "Nothing to do here: PR has not been closed" | ||
if (req.body.pull_request.merged !== 'true') return "Nothing to do here: PR has been closed, but not by merging" | ||
|
||
const owner = 'git-for-windows' | ||
const repo = 'git' | ||
const sender = req.body.sender.login | ||
|
||
const getToken = (() => { | ||
let token | ||
|
||
const get = async () => { | ||
const getInstallationIdForRepo = require('./get-installation-id-for-repo') | ||
const installationId = await getInstallationIdForRepo(context, owner, repo) | ||
const getInstallationAccessToken = require('./get-installation-access-token') | ||
return await getInstallationAccessToken(context, installationId) | ||
} | ||
|
||
return async () => token || (token = await get()) | ||
})() | ||
|
||
const isAllowed = async (login) => { | ||
if (login === 'gitforwindowshelper[bot]') return true | ||
const getCollaboratorPermissions = require('./get-collaborator-permissions') | ||
const token = await getToken() | ||
const permission = await getCollaboratorPermissions(context, token, owner, repo, login) | ||
return ['ADMIN', 'MAINTAIN', 'WRITE'].includes(permission.toString()) | ||
} | ||
|
||
if (!await isAllowed(sender)) throw new Error(`${sender} is not allowed to do that`) | ||
|
||
const githubApiRequest = require('./github-api-request') | ||
|
||
const candidates = req.body.pull_request.body.match(/(?:[Cc]loses|[Ff]ixes) (?:https:\/\/github\.com\/git-for-windows\/git\/issues\/|#)(\d+)/) | ||
|
||
if (candidates.length !== 1) throw new Error(`Expected 1 candidate issue, got ${candidates.length}`) | ||
Comment on lines
+34
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was looking for something more robust, and while there does not seem anything useful in GitHub's REST API, there is something in the GraphQL API. Try this, for example:
Over here, this returns this highly useful answer:
We already have a precedent of a GraphQL call when looking for the "collaborator permission", so we could something similar here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That does look immensely more helpful than the REST API |
||
|
||
const { getCurrentMilestone } = require('./GitForWindowsHelper/milestones') | ||
const current = await getCurrentMilestone(console, await getToken(), owner, repo) | ||
|
||
const issueNumber = candidates[0] | ||
const issue = await githubApiRequest(context, await getToken(), 'GET', `/repos/${owner}/${repo}/issues/${issueNumber}`) | ||
|
||
if (issue.labels.length>0){ | ||
for (const label of issue.labels) { | ||
if (label.name === "component-update"){ | ||
await githubApiRequest(context, await getToken(), 'PATCH', `/repos/${owner}/${repo}/issues/${issueNumber}`, { | ||
milestone: current.id | ||
}) | ||
|
||
return `Added issue ${issueNumber} to milestone "Next release"` | ||
} | ||
} | ||
} | ||
|
||
throw new Error(`Issue ${issueNumber} isn't a component update`) | ||
} | ||
|
||
|
||
module.exports = { | ||
addIssueToCurrentMilestone, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it is about high time to move this (duplicated) logic into a central place instead, and maybe now would be as good a time as any to do that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That thought has crossed my mind as well.