-
Notifications
You must be signed in to change notification settings - Fork 17
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
chore(ci): authenticate release issue by approval comment #316
Changes from all commits
fb6392c
21ce8c0
fd73907
436b77e
1c66be4
097026e
3239ac8
6794008
4a8971e
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import path from 'path'; | ||
|
||
import { Octokit } from '@octokit/rest'; | ||
|
||
import clientsConfig from '../../config/clients.config.json'; | ||
import config from '../../config/release.config.json'; | ||
import { getGitHubUrl, run } from '../common'; | ||
|
@@ -8,6 +10,7 @@ export const RELEASED_TAG = config.releasedTag; | |
export const MAIN_BRANCH = config.mainBranch; | ||
export const OWNER = config.owner; | ||
export const REPO = config.repo; | ||
export const TEAM_SLUG = config.teamSlug; | ||
export const MAIN_PACKAGE = Object.keys(clientsConfig).reduce( | ||
(mainPackage: { [lang: string]: string }, lang: string) => { | ||
return { | ||
|
@@ -18,6 +21,12 @@ export const MAIN_PACKAGE = Object.keys(clientsConfig).reduce( | |
{} | ||
); | ||
|
||
export function getOctokit(githubToken: string): Octokit { | ||
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. Unless we have different GH tokens, we can have a variable for it 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. ah bah what Pierre said 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. Environment variables travel through process-release.yml -> process-release.ts, and I don't think it's good to have code accessing environment variables directly inside |
||
return new Octokit({ | ||
auth: `token ${githubToken}`, | ||
}); | ||
} | ||
|
||
export function getTargetBranch(language: string): string { | ||
return config.targetBranch[language] || config.defaultTargetBranch; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,9 +23,11 @@ import { | |
RELEASED_TAG, | ||
OWNER, | ||
REPO, | ||
TEAM_SLUG, | ||
getMarkdownSection, | ||
configureGitHubAuthor, | ||
cloneRepository, | ||
getOctokit, | ||
} from './common'; | ||
import TEXT from './text'; | ||
import type { | ||
|
@@ -50,14 +52,22 @@ const BEFORE_CLIENT_COMMIT: { [lang: string]: BeforeClientCommitCommand } = { | |
}, | ||
}; | ||
|
||
function getIssueBody(): string { | ||
return JSON.parse( | ||
execa.sync('curl', [ | ||
'-H', | ||
`Authorization: token ${process.env.GITHUB_TOKEN}`, | ||
`https://api.github.com/repos/${OWNER}/${REPO}/issues/${process.env.EVENT_NUMBER}`, | ||
]).stdout | ||
).body; | ||
async function getIssueBody(): Promise<string> { | ||
const octokit = getOctokit(process.env.GITHUB_TOKEN!); | ||
const { | ||
data: { body }, | ||
} = await octokit.rest.issues.get({ | ||
owner: OWNER, | ||
repo: REPO, | ||
issue_number: Number(process.env.EVENT_NUMBER), | ||
}); | ||
|
||
if (!body) { | ||
throw new Error( | ||
`Unexpected \`body\` of the release issue: ${JSON.stringify(body)}` | ||
); | ||
} | ||
return body; | ||
} | ||
|
||
function getDateStamp(): string { | ||
|
@@ -154,6 +164,26 @@ async function updateChangelog({ | |
); | ||
} | ||
|
||
async function isAuthorizedRelease(): Promise<boolean> { | ||
const octokit = getOctokit(process.env.GITHUB_TOKEN!); | ||
const { data: members } = await octokit.rest.teams.listMembersInOrg({ | ||
org: OWNER, | ||
team_slug: TEAM_SLUG, | ||
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. Although I get the idea, I wonder if it's good to check if it's a team member. The end goal of this project is to let anyone from the company contribute to our clients, which would not match this condition. Wdyt? 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 think it's still okay. Anyone can open pull-requests and contribute to the code base, but only us can trigger releases. What do you think? 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. Maybe anyone at algolia should be able to release ? 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. We would still be the bottleneck, IMO anyone from the company should have the rights to do it, we are just here to make sure it works well 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. Why don't we start with a small scope and expand later if there's a demand and if we think it makes sense to allow them to trigger releases? Anyway we can replace this small piece of code quite easily if we want. 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. yep I think it's fair at the moment to start with only us. We want anyone to contribute but it's very unlikely that we want unattended release 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. Ok, I was mostly pointing it out but let's not forget about this |
||
}); | ||
|
||
const { data: comments } = await octokit.rest.issues.listComments({ | ||
owner: OWNER, | ||
repo: REPO, | ||
issue_number: Number(process.env.EVENT_NUMBER), | ||
}); | ||
|
||
return comments.some( | ||
(comment) => | ||
comment.body?.toLowerCase().trim() === 'approved' && | ||
members.find((member) => member.login === comment.user?.login) | ||
); | ||
} | ||
|
||
async function processRelease(): Promise<void> { | ||
if (!process.env.GITHUB_TOKEN) { | ||
throw new Error('Environment variable `GITHUB_TOKEN` does not exist.'); | ||
|
@@ -163,16 +193,13 @@ async function processRelease(): Promise<void> { | |
throw new Error('Environment variable `EVENT_NUMBER` does not exist.'); | ||
} | ||
|
||
const issueBody = getIssueBody(); | ||
|
||
if ( | ||
!getMarkdownSection(issueBody, TEXT.approvalHeader) | ||
.split('\n') | ||
.find((line) => line.startsWith(`- [x] ${TEXT.approved}`)) | ||
) { | ||
throw new Error('The issue was not approved.'); | ||
if (!(await isAuthorizedRelease())) { | ||
throw new Error( | ||
'The issue was not approved.\nA team member must leave a comment "approved" in the release issue.' | ||
); | ||
} | ||
|
||
const issueBody = await getIssueBody(); | ||
const versionsToRelease = getVersionsToRelease(issueBody); | ||
|
||
await updateOpenApiTools(versionsToRelease); | ||
|
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.
You don't need to build a new octokit everytime, this should be a local variable
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.
What if we want to pass a different github token? I think it's easy to maintain stuff in
common
without directly accessingprocess.env.xxx
.