Skip to content
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

Adding option to use private PAT token on GitHub Q&A sample #713

Merged
merged 53 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
88fa1d3
Adding option to use private PAT token on GitHub Q&A sample
mahomedalid Apr 28, 2023
6b696eb
Solving conflict - merge
mahomedalid Apr 28, 2023
c7fce3a
.ConfigureAwait(false) to the await task
mahomedalid Apr 28, 2023
e9f2c6d
Solving CA2254
mahomedalid Apr 28, 2023
ebc82b2
Merge branch 'main' into features/github-private-repo
shawncal Apr 28, 2023
ee1e96b
Merge branch 'main' into features/github-private-repo
lemillermicrosoft Apr 28, 2023
4fc1651
Merge
mahomedalid May 8, 2023
a06bd27
solving warning CA1727
mahomedalid May 8, 2023
ba78f8b
Merge branch 'main' into features/github-private-repo
mahomedalid May 8, 2023
c04c6e2
Merge branch 'main' into features/github-private-repo
mahomedalid May 8, 2023
e9f9ee1
Merge branch 'main' into features/github-private-repo
mahomedalid May 9, 2023
66cec61
Fixing linter
mahomedalid May 9, 2023
2ca62c4
Merge branch 'features/github-private-repo' of github.com:mahomedalid…
mahomedalid May 9, 2023
8787bc6
Merge branch 'main' into features/github-private-repo
mahomedalid May 9, 2023
f0fbd9c
Merge branch 'main' into features/github-private-repo
mahomedalid May 9, 2023
0aa0608
Merge branch 'main' into features/github-private-repo
mahomedalid May 16, 2023
ada98e9
Merge branch 'main' into features/github-private-repo
mahomedalid May 17, 2023
e80d717
Merge branch 'main' into features/github-private-repo
mahomedalid May 17, 2023
316eb15
Fixing merge
mahomedalid May 17, 2023
c30befb
Merge branch 'main' into features/github-private-repo
mahomedalid May 17, 2023
240c308
Removing unused headers
mahomedalid May 17, 2023
f0551dd
Merge branch 'features/github-private-repo' of github.com:mahomedalid…
mahomedalid May 17, 2023
be1d2ad
Merge branch 'main' into features/github-private-repo
lemillermicrosoft May 19, 2023
6c507b7
Merge branch 'main' into features/github-private-repo
lemillermicrosoft May 19, 2023
3b6960e
Merge branch 'main' into features/github-private-repo
adrianwyatt Jun 1, 2023
7aa3e1e
Merge branch 'main' into features/github-private-repo
adrianwyatt Jun 1, 2023
d5ac004
add 'daa' to word listing
adrianwyatt Jun 1, 2023
ebfa776
Merge branch 'main' into features/github-private-repo
adrianwyatt Jun 6, 2023
082a8d5
Merge branch 'main' into features/github-private-repo
lemillermicrosoft Jun 8, 2023
c7ea47a
Merge branch 'main' into features/github-private-repo
lemillermicrosoft Jun 12, 2023
5528de3
Merge branch 'main' into features/github-private-repo
mahomedalid Jun 13, 2023
9b31f37
Merge branch 'main' into features/github-private-repo
hathind-ms Jun 13, 2023
0bef874
Merge branch 'main' into features/github-private-repo
hathind-ms Jun 13, 2023
38c5d17
Merge branch 'main' into features/github-private-repo
craigomatic Jun 14, 2023
36fef41
Update samples/dotnet/github-skills/GitHubSkill.cs
adrianwyatt Jun 14, 2023
e2cd64c
Updated 'pat' to 'personal access token'
adrianwyatt Jun 14, 2023
a1c2e8b
Removed dependency of WebFileDownloadSkill from GitHub sample skill
adrianwyatt Jun 14, 2023
a0eba4b
Merge branch 'main' into features/github-private-repo
adrianwyatt Jun 14, 2023
e6258c8
Adding tooltip and link to github tokens
mahomedalid Jun 14, 2023
2c1ae60
update to use bearer and proper GH API
adrianwyatt Jun 14, 2023
53d6a52
Merge branch 'main' into features/github-private-repo
craigomatic Jun 15, 2023
31d1028
Remove unhelpful comment
adrianwyatt Jun 15, 2023
1596c27
Merge branch 'main' into features/github-private-repo
craigomatic Jun 15, 2023
1cbf977
Update Extensions.cs
mahomedalid Jun 15, 2023
d509d7c
Merge branch 'main' into features/github-private-repo
hathind-ms Jun 19, 2023
7de6df3
Merge branch 'main' into features/github-private-repo
craigomatic Jun 20, 2023
62462f3
Merge branch 'main' into features/github-private-repo
mahomedalid Jun 21, 2023
9acd748
ADditional information needed for the api
mahomedalid Jun 21, 2023
bb58c6f
Removing changes to webfiledownloadskill
mahomedalid Jun 21, 2023
550c6ab
Removing changes to webfiledownloadskill
mahomedalid Jun 21, 2023
845e479
Removing unnecesary blank line
mahomedalid Jun 21, 2023
6a30f85
Update GitHubSkill.cs
mahomedalid Jun 21, 2023
9010fb6
Merge branch 'main' into features/github-private-repo
craigomatic Jun 22, 2023
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
19 changes: 19 additions & 0 deletions dotnet/src/Skills/Skills.Web/WebFileDownloadSkill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
Expand All @@ -27,6 +28,11 @@ public static class Parameters
/// Where to save file.
/// </summary>
public const string FilePath = "filePath";

/// <summary>
/// Default headers for the download requests
/// </summary>
public const string DefaultHttpHeaders = "defaultHttpHeaders";
}

private readonly ILogger _logger;
Expand All @@ -44,6 +50,19 @@ public WebFileDownloadSkill(ILogger<WebFileDownloadSkill>? logger = null)
this._httpClient = new HttpClient(this._httpClientHandler);
}

public async Task DownloadToFileAsync(string source, IDictionary<string, string> headers, SKContext context)
{
this._httpClient.DefaultRequestHeaders.Clear();
mahomedalid marked this conversation as resolved.
Show resolved Hide resolved

foreach (var header in headers)
{
this._httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
mahomedalid marked this conversation as resolved.
Show resolved Hide resolved
}

await this.DownloadToFileAsync(source, context).ConfigureAwait(false);
this._httpClient.DefaultRequestHeaders.Clear();
}

/// <summary>
/// Downloads a file to a local file path.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions samples/apps/github-qna-webapp-react/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const App: FC = () => {
const [selectedTabValue, setSelectedTabValue] = useState<string>('completion');
const [project, setProject] = useState<string>('');
const [branch, setBranch] = useState<string>('');
const [repoToken, setRepoToken] = useState<string>('');

useEffect(() => {
changeAppState(appState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const enum DownloadState {
const GitHubProjectSelection: FC<IData> = ({ uri, keyConfig, prevProject, prevBranch, onLoadProject, onBack }) => {
const [project, setProject] = useState<string>(prevProject);
const [branch, setBranch] = useState<string>(prevBranch);
const [patToken, setToken] = useState<string>('');
const [downloadState, setDownloadState] = useState<DownloadState>(DownloadState.Setup);
const sk = useSemanticKernel(uri);

Expand All @@ -42,6 +43,7 @@ const GitHubProjectSelection: FC<IData> = ({ uri, keyConfig, prevProject, prevBr
value: project || '',
inputs: [
{ key: 'repositoryBranch', value: branch || '' },
{ key: 'patToken', value: patToken || '' },
{ key: 'searchPattern', value: '*.md' },
],
},
Expand Down Expand Up @@ -98,6 +100,20 @@ const GitHubProjectSelection: FC<IData> = ({ uri, keyConfig, prevProject, prevBr
placeholder="https://github.com/microsoft/semantic-kernel"
/>
</div>
<Label>
<strong>GitHub PAT Token (optional)</strong>
mahomedalid marked this conversation as resolved.
Show resolved Hide resolved
</Label>
<div style={{ display: 'flex', flexDirection: 'row', gap: 10 }}>
<Input
style={{ width: '100%' }}
type="password"
value={patToken}
onChange={(e) => {
setToken(e.target.value);
}}
placeholder=""
/>
</div>
<Label>
<strong>Branch Name</strong>
</Label>
Expand Down
33 changes: 29 additions & 4 deletions samples/dotnet/github-skills/GitHubSkill.cs
craigomatic marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel.SkillDefinition;
using Microsoft.SemanticKernel.Skills.Web;
using Microsoft.SemanticKernel.Text;

namespace GitHubSkills;

Expand All @@ -37,6 +36,11 @@ public static class Parameters
/// </summary>
public const string SearchPattern = "searchPattern";

/// <summary>
/// Personal access token for private repositories.
/// </summary>
public const string PatToken = "patToken";
adrianwyatt marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Document file path.
/// </summary>
Expand Down Expand Up @@ -132,9 +136,30 @@ public async Task SummarizeRepositoryAsync(string source, SKContext context)
try
{
var repositoryUri = source.Trim(new char[] { ' ', '/' });
var context1 = new SKContext(new ContextVariables(), NullMemory.Instance, null, context.Log);
context1.Variables.Set(Parameters.FilePath, filePath);
await this._downloadSkill.DownloadToFileAsync($"{repositoryUri}/archive/refs/heads/{repositoryBranch}.zip", context1);
var downloadSkillContext = new SKContext(new ContextVariables(), NullMemory.Instance, null, context.Log);
downloadSkillContext.Variables.Set(Parameters.FilePath, filePath);

var repoBundle = $"{repositoryUri}/archive/refs/heads/{repositoryBranch}.zip";

this._logger.LogDebug("Downloading {repoBundle}", repoBundle);

if (context.Variables.Get(Parameters.PatToken, out string patToken))
{
this._logger.LogDebug($"PAT detected, adding authorization headers");

// If variables would be a dictionary of object we could probably pass headers,
// we can also serialize it to string...
var headers = new Dictionary<string, string>()
{
{ "Authorization", $"token {patToken}" }
};

await this._downloadSkill.DownloadToFileAsync(repoBundle, headers, downloadSkillContext);
}
else
{
await this._downloadSkill.DownloadToFileAsync(repoBundle, downloadSkillContext);
}

ZipFile.ExtractToDirectory(filePath, directoryPath);

Expand Down