Fetch Project & Column IDs #3
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
name: Fetch Project & Column IDs | |
on: | |
workflow_dispatch: | |
jobs: | |
fetch-ids: | |
runs-on: ubuntu-latest | |
steps: | |
# ✅ Step 1: Fetch Project IDs | |
- name: Get Project IDs | |
id: get_project_ids | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const org = context.repo.owner; | |
const projectNames = ["P1", "P2"]; // Update these as needed | |
const query = `query($org: String!) { | |
organization(login: $org) { | |
projectsV2(first: 10) { | |
nodes { | |
id | |
title | |
} | |
} | |
} | |
}`; | |
const result = await github.graphql(query, { org }); | |
if (!result.organization || !result.organization.projectsV2.nodes.length) { | |
console.log("❌ No projects found."); | |
return JSON.stringify({}); | |
} | |
let projectIds = {}; | |
result.organization.projectsV2.nodes.forEach(p => { | |
if (projectNames.includes(p.title)) { | |
projectIds[p.title] = p.id; | |
} | |
}); | |
console.log("====== PROJECT IDs ======"); | |
Object.entries(projectIds).forEach(([name, id]) => { | |
console.log(`Project "${name}" = Project ID: "${id}"`); | |
}); | |
return JSON.stringify(projectIds); # ✅ Always returns valid JSON | |
# ✅ Step 2: Set project IDs as environment variable | |
- name: Store Project IDs | |
run: echo "PROJECT_IDS=${{ steps.get_project_ids.outputs.result }}" >> $GITHUB_ENV | |
# ✅ Step 3: Fetch Column IDs | |
- name: Get Column IDs | |
id: get_columns | |
uses: actions/github-script@v7 | |
env: | |
PROJECT_IDS: ${{ env.PROJECT_IDS }} # ✅ Fetch stored project IDs | |
with: | |
script: | | |
const projectIds = process.env.PROJECT_IDS ? JSON.parse(process.env.PROJECT_IDS) : {}; | |
if (Object.keys(projectIds).length === 0) { | |
console.log("❌ No valid project IDs found."); | |
return JSON.stringify({}); | |
} | |
let columnData = {}; | |
for (const [projectName, projectId] of Object.entries(projectIds)) { | |
const query = `query($projectId: ID!) { | |
node(id: $projectId) { | |
... on ProjectV2 { | |
fields(first: 10) { | |
nodes { | |
id | |
name | |
} | |
} | |
} | |
} | |
}`; | |
const result = await github.graphql(query, { projectId }); | |
if (!result.node || !result.node.fields.nodes.length) { | |
console.log(`❌ No columns found for Project ${projectName}.`); | |
continue; | |
} | |
columnData[projectName] = {}; | |
result.node.fields.nodes.forEach(field => { | |
columnData[projectName][field.name] = field.id; | |
}); | |
} | |
console.log("====== COLUMN IDs ======"); | |
Object.entries(columnData).forEach(([project, columns]) => { | |
console.log(`Project "${project}":`); | |
Object.entries(columns).forEach(([name, id]) => { | |
console.log(` Column "${name}" = ID: "${id}"`); | |
}); | |
}); | |
return JSON.stringify(columnData); |