Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions .github/workflows/manage-issue-header.yml
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'
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

Choose a reason for hiding this comment

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

Alright @MisRob, thanks!


- 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});
69 changes: 69 additions & 0 deletions scripts/manage-issue-header.js
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}`);
}
};