-
Notifications
You must be signed in to change notification settings - Fork 6
Add shared action to manage issue headers #24
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| name: Manage issue header | ||
|
|
||
| on: | ||
| workflow_call: | ||
| secrets: | ||
| LE_BOT_APP_ID: | ||
| description: "GitHub App ID for authentication" | ||
| required: true | ||
| LE_BOT_PRIVATE_KEY: | ||
| description: "GitHub App Private Key for authentication" | ||
| required: true | ||
| jobs: | ||
| manage-issue-header: | ||
| runs-on: ubuntu-latest | ||
| if: | | ||
| (github.event.action == 'labeled' && github.event.label.name == 'help wanted') || | ||
| (github.event.action == 'unlabeled' && github.event.label.name == 'help wanted') | ||
| steps: | ||
| - name: Generate App Token | ||
| id: generate-token | ||
| uses: tibdex/github-app-token@v2 | ||
| with: | ||
| app_id: ${{ secrets.LE_BOT_APP_ID }} | ||
| private_key: ${{ secrets.LE_BOT_PRIVATE_KEY }} | ||
|
|
||
| - name: Checkout called repository | ||
| uses: actions/checkout@v3 | ||
| with: | ||
| repository: learningequality/.github | ||
| ref: main | ||
| token: ${{ steps.generate-token.outputs.token }} | ||
|
|
||
| - name: Setup Node.js | ||
| uses: actions/setup-node@v3 | ||
| with: | ||
| node-version: '16' | ||
|
|
||
| - name: Install dependencies | ||
| run: npm install | ||
|
|
||
| - name: Run script | ||
| id: run-script | ||
| uses: actions/github-script@v6 | ||
| with: | ||
| github-token: ${{ steps.generate-token.outputs.token }} | ||
| script: | | ||
| const script = require('./scripts/manage-issue-header.js'); | ||
| return await script({github, context, core}); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /** | ||
| * Updates issue contributing header according to the presence of the 'help wanted' label. | ||
| */ | ||
|
|
||
| const HELP_WANTED_LABEL = 'help wanted'; | ||
|
|
||
| const HEADER_START_MARKER = '<!---HEADER START-->'; | ||
| const HEADER_END_MARKER = '<!---HEADER END-->'; | ||
|
|
||
| const HELP_WANTED_HEADER = '<!---HEADER START-->\n\n<img height="20px" src="https://i.imgur.com/0ZZG9qx.jpeg">\n\n🙂 Looking for an issue? Welcome! This issue is open for contribution. If this is the first time you’re requesting an issue, please:\n\n- **Read the <a href="https://learningequality.org/contributing-to-our-open-code-base/" target="_blank">Contributing guidelines</a>** carefully. **Pay extra attention to the [Using generative AI](https://learningequality.org/contributing-to-our-open-code-base/#using-generative-ai)**. **Pull requests and comments that don’t follow the guidelines won’t be answered.**\n- **Confirm that you’ve read the guidelines** in your comment.\n\n<img height="20px" src="https://i.imgur.com/0ZZG9qx.jpeg">\n\n<!---HEADER END-->\n\n'; | ||
|
|
||
| const NON_HELP_WANTED_HEADER = '<!---HEADER START-->\n\n<img height="20px" src="https://i.imgur.com/c7hUeb5.jpeg">\n\n❌ **This issue is not open for contribution. Please read the <a href="https://learningequality.org/contributing-to-our-open-code-base/" target="_blank">Contributing guidelines</a>** carefully to learn about the contributing process and how to find suitable issues.\n\n<img height="20px" src="https://i.imgur.com/c7hUeb5.jpeg">\n\n<!---HEADER END-->\n\n'; | ||
|
|
||
| function clearHeader(issueBody) { | ||
| const startIndex = issueBody.indexOf(HEADER_START_MARKER); | ||
| const endIndex = issueBody.indexOf(HEADER_END_MARKER); | ||
|
|
||
| if (startIndex === -1 || endIndex === -1) { | ||
| return issueBody; | ||
| } | ||
|
|
||
| return issueBody.substring(0, startIndex) + issueBody.substring(endIndex + HEADER_END_MARKER.length).trimStart(); | ||
| } | ||
|
|
||
| module.exports = async ({ github, context, core }) => { | ||
| try { | ||
| const repoOwner = context.repo.owner; | ||
| const repoName = context.repo.repo; | ||
| const issueNumber = context.payload.issue.number; | ||
| const actionType = context.payload.action; | ||
| const labelName = context.payload.label.name; | ||
|
|
||
| const labelAdded = actionType === "labeled"; | ||
| const labelRemoved = actionType === "unlabeled"; | ||
|
|
||
| if (!labelAdded && !labelRemoved) { | ||
| return; | ||
| } | ||
|
|
||
| if (labelName !== HELP_WANTED_LABEL) { | ||
| return; | ||
| } | ||
|
|
||
| const issue = await github.rest.issues.get({ | ||
| owner: repoOwner, | ||
| repo: repoName, | ||
| issue_number: issueNumber | ||
| }); | ||
|
|
||
| const currentBody = issue.data.body || ""; | ||
|
|
||
| let newBody = clearHeader(currentBody); | ||
| if (labelAdded) { | ||
| newBody = HELP_WANTED_HEADER + newBody; | ||
| } else if (labelRemoved) { | ||
| newBody = NON_HELP_WANTED_HEADER + newBody; | ||
| } | ||
|
|
||
| await github.rest.issues.update({ | ||
| owner: repoOwner, | ||
| repo: repoName, | ||
| issue_number: issueNumber, | ||
| body: newBody | ||
| }); | ||
|
|
||
| } catch (error) { | ||
| core.setFailed(`Error: ${error.message}`); | ||
| } | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
(Just curious) Is there a reason to use node 16 in this particular repository? Do we have any restriction in our dependencies or scripts to use this node version?
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 copied this + the script structure from another action which runs well. I think it affects the way the script is called at the bottom of this file and also how the main function is exported in the script part.
Also it's the most common version we use in other community actions, however I think that may be rather random choice.
It'd be good to eventually use same versions and also programming language, I plan to open an issue for that at some point.
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.
Alright @MisRob, thanks!